Skip to content

Commit 66cb7b3

Browse files
dcodeIOkripken
authored andcommitted
Add BinaryenAddCustomSection API (#2381)
This adds a new BinaryenAddCustomSection API so a generator can add arbitrary custom sections to a module.
1 parent 4e80fde commit 66cb7b3

6 files changed

Lines changed: 86 additions & 0 deletions

File tree

build-js.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,10 @@ export_function "_BinaryenExportGetKind"
926926
export_function "_BinaryenExportGetName"
927927
export_function "_BinaryenExportGetValue"
928928

929+
# Custom sections
930+
931+
export_function "_BinaryenAddCustomSection"
932+
929933
# 'Relooper' operations
930934
export_function "_RelooperCreate"
931935
export_function "_RelooperAddBlock"

src/binaryen-c.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,6 +4306,37 @@ const char* BinaryenExportGetValue(BinaryenExportRef export_) {
43064306
return ((Export*)export_)->value.c_str();
43074307
}
43084308

4309+
//
4310+
// ========= Custom sections =========
4311+
//
4312+
4313+
void BinaryenAddCustomSection(BinaryenModuleRef module,
4314+
const char* name,
4315+
const char* contents,
4316+
BinaryenIndex contentsSize) {
4317+
if (tracing) {
4318+
std::cout << " {\n";
4319+
std::cout << " const char contents[] = { ";
4320+
for (BinaryenIndex i = 0; i < contentsSize; i++) {
4321+
if (i > 0) {
4322+
std::cout << ", ";
4323+
}
4324+
std::cout << int(contents[i]);
4325+
}
4326+
std::cout << " };\n";
4327+
std::cout << " BinaryenAddCustomSection(the_module, ";
4328+
traceNameOrNULL(name);
4329+
std::cout << ", contents, " << contentsSize << ");\n";
4330+
std::cout << " }\n";
4331+
}
4332+
4333+
auto* wasm = (Module*)module;
4334+
wasm::UserSection customSection;
4335+
customSection.name = name;
4336+
customSection.data = std::vector<char>(contents, contents + contentsSize);
4337+
wasm->userSections.push_back(customSection);
4338+
}
4339+
43094340
//
43104341
// ========== CFG / Relooper ==========
43114342
//

src/binaryen-c.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,15 @@ BINARYEN_API const char* BinaryenExportGetName(BinaryenExportRef export_);
14321432
// Gets the internal name of the specified export.
14331433
BINARYEN_API const char* BinaryenExportGetValue(BinaryenExportRef export_);
14341434

1435+
//
1436+
// ========= Custom sections =========
1437+
//
1438+
1439+
BINARYEN_API void BinaryenAddCustomSection(BinaryenModuleRef module,
1440+
const char* name,
1441+
const char* contents,
1442+
BinaryenIndex contentsSize);
1443+
14351444
//
14361445
// ========== CFG / Relooper ==========
14371446
//

src/js/binaryen.js-post.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,11 @@ function wrapModule(module, self) {
21052105
self['setFeatures'] = function(features) {
21062106
Module['_BinaryenModuleSetFeatures'](module, features);
21072107
};
2108+
self['addCustomSection'] = function(name, contents) {
2109+
return preserveStack(function() {
2110+
return Module['_BinaryenAddCustomSection'](module, strToStack(name), i8sToStack(contents), contents.length);
2111+
});
2112+
};
21082113
self['emitText'] = function() {
21092114
var old = out;
21102115
var ret = '';

test/binaryen.js/custom-section.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function assert(x) {
2+
if (!x) throw 'error!';
3+
}
4+
5+
Binaryen.setAPITracing(true);
6+
var module = new Binaryen.Module();
7+
8+
module.addCustomSection("hello", [119, 111, 114, 108, 100]);
9+
10+
assert(module.validate());
11+
console.log(module.emitText());
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// beginning a Binaryen API trace
2+
#include <math.h>
3+
#include <map>
4+
#include "binaryen-c.h"
5+
int main() {
6+
std::map<size_t, BinaryenFunctionTypeRef> functionTypes;
7+
std::map<size_t, BinaryenExpressionRef> expressions;
8+
std::map<size_t, BinaryenFunctionRef> functions;
9+
std::map<size_t, BinaryenGlobalRef> globals;
10+
std::map<size_t, BinaryenEventRef> events;
11+
std::map<size_t, BinaryenExportRef> exports;
12+
std::map<size_t, RelooperBlockRef> relooperBlocks;
13+
BinaryenModuleRef the_module = NULL;
14+
RelooperRef the_relooper = NULL;
15+
the_module = BinaryenModuleCreate();
16+
expressions[size_t(NULL)] = BinaryenExpressionRef(NULL);
17+
{
18+
const char contents[] = { 119, 111, 114, 108, 100 };
19+
BinaryenAddCustomSection(the_module, "hello", contents, 5);
20+
}
21+
BinaryenModuleValidate(the_module);
22+
BinaryenModulePrint(the_module);
23+
(module
24+
;; custom section "hello", size 5, contents: "world"
25+
)
26+

0 commit comments

Comments
 (0)