diff options
author | Daniel Wirtz <dcode@dcode.io> | 2019-10-12 00:41:32 +0200 |
---|---|---|
committer | Alon Zakai <azakai@google.com> | 2019-10-11 15:41:32 -0700 |
commit | 66cb7b3f80f6ce32f426c3cb52eaf19430289ac2 (patch) | |
tree | c93ad30df8f2e0959d914afe42470ceeb3996e7d | |
parent | 4e80fde446764102fe5685496f351f0539377ff6 (diff) | |
download | binaryen-66cb7b3f80f6ce32f426c3cb52eaf19430289ac2.tar.gz binaryen-66cb7b3f80f6ce32f426c3cb52eaf19430289ac2.tar.bz2 binaryen-66cb7b3f80f6ce32f426c3cb52eaf19430289ac2.zip |
Add BinaryenAddCustomSection API (#2381)
This adds a new BinaryenAddCustomSection API so a generator can add arbitrary
custom sections to a module.
-rwxr-xr-x | build-js.sh | 4 | ||||
-rw-r--r-- | src/binaryen-c.cpp | 31 | ||||
-rw-r--r-- | src/binaryen-c.h | 9 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 5 | ||||
-rw-r--r-- | test/binaryen.js/custom-section.js | 11 | ||||
-rw-r--r-- | test/binaryen.js/custom-section.js.txt | 26 |
6 files changed, 86 insertions, 0 deletions
diff --git a/build-js.sh b/build-js.sh index 606fe6c50..37bdc00ee 100755 --- a/build-js.sh +++ b/build-js.sh @@ -926,6 +926,10 @@ export_function "_BinaryenExportGetKind" export_function "_BinaryenExportGetName" export_function "_BinaryenExportGetValue" +# Custom sections + +export_function "_BinaryenAddCustomSection" + # 'Relooper' operations export_function "_RelooperCreate" export_function "_RelooperAddBlock" diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 2307de3e6..1cb0963a2 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -4307,6 +4307,37 @@ const char* BinaryenExportGetValue(BinaryenExportRef export_) { } // +// ========= Custom sections ========= +// + +void BinaryenAddCustomSection(BinaryenModuleRef module, + const char* name, + const char* contents, + BinaryenIndex contentsSize) { + if (tracing) { + std::cout << " {\n"; + std::cout << " const char contents[] = { "; + for (BinaryenIndex i = 0; i < contentsSize; i++) { + if (i > 0) { + std::cout << ", "; + } + std::cout << int(contents[i]); + } + std::cout << " };\n"; + std::cout << " BinaryenAddCustomSection(the_module, "; + traceNameOrNULL(name); + std::cout << ", contents, " << contentsSize << ");\n"; + std::cout << " }\n"; + } + + auto* wasm = (Module*)module; + wasm::UserSection customSection; + customSection.name = name; + customSection.data = std::vector<char>(contents, contents + contentsSize); + wasm->userSections.push_back(customSection); +} + +// // ========== CFG / Relooper ========== // diff --git a/src/binaryen-c.h b/src/binaryen-c.h index ab159695a..ae0a06835 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -1433,6 +1433,15 @@ BINARYEN_API const char* BinaryenExportGetName(BinaryenExportRef export_); BINARYEN_API const char* BinaryenExportGetValue(BinaryenExportRef export_); // +// ========= Custom sections ========= +// + +BINARYEN_API void BinaryenAddCustomSection(BinaryenModuleRef module, + const char* name, + const char* contents, + BinaryenIndex contentsSize); + +// // ========== CFG / Relooper ========== // // General usage is (1) create a relooper, (2) create blocks, (3) add diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 03e57d3d5..7c99ded15 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -2105,6 +2105,11 @@ function wrapModule(module, self) { self['setFeatures'] = function(features) { Module['_BinaryenModuleSetFeatures'](module, features); }; + self['addCustomSection'] = function(name, contents) { + return preserveStack(function() { + return Module['_BinaryenAddCustomSection'](module, strToStack(name), i8sToStack(contents), contents.length); + }); + }; self['emitText'] = function() { var old = out; var ret = ''; diff --git a/test/binaryen.js/custom-section.js b/test/binaryen.js/custom-section.js new file mode 100644 index 000000000..f61af0096 --- /dev/null +++ b/test/binaryen.js/custom-section.js @@ -0,0 +1,11 @@ +function assert(x) { + if (!x) throw 'error!'; +} + +Binaryen.setAPITracing(true); +var module = new Binaryen.Module(); + +module.addCustomSection("hello", [119, 111, 114, 108, 100]); + +assert(module.validate()); +console.log(module.emitText()); diff --git a/test/binaryen.js/custom-section.js.txt b/test/binaryen.js/custom-section.js.txt new file mode 100644 index 000000000..7ac835fdf --- /dev/null +++ b/test/binaryen.js/custom-section.js.txt @@ -0,0 +1,26 @@ +// beginning a Binaryen API trace +#include <math.h> +#include <map> +#include "binaryen-c.h" +int main() { + std::map<size_t, BinaryenFunctionTypeRef> functionTypes; + std::map<size_t, BinaryenExpressionRef> expressions; + std::map<size_t, BinaryenFunctionRef> functions; + std::map<size_t, BinaryenGlobalRef> globals; + std::map<size_t, BinaryenEventRef> events; + std::map<size_t, BinaryenExportRef> exports; + std::map<size_t, RelooperBlockRef> relooperBlocks; + BinaryenModuleRef the_module = NULL; + RelooperRef the_relooper = NULL; + the_module = BinaryenModuleCreate(); + expressions[size_t(NULL)] = BinaryenExpressionRef(NULL); + { + const char contents[] = { 119, 111, 114, 108, 100 }; + BinaryenAddCustomSection(the_module, "hello", contents, 5); + } + BinaryenModuleValidate(the_module); + BinaryenModulePrint(the_module); +(module + ;; custom section "hello", size 5, contents: "world" +) + |