From b20f66d8502200d17a1112dd21bdbcb45d9dab51 Mon Sep 17 00:00:00 2001 From: dcode Date: Fri, 11 Oct 2019 16:53:46 +0200 Subject: [PATCH 1/2] Add BinaryenAddCustomSection API --- build-js.sh | 4 ++++ src/binaryen-c.cpp | 31 ++++++++++++++++++++++++++ src/binaryen-c.h | 9 ++++++++ src/js/binaryen.js-post.js | 5 +++++ test/binaryen.js/custom-section.js | 11 +++++++++ test/binaryen.js/custom-section.js.txt | 26 +++++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 test/binaryen.js/custom-section.js create mode 100644 test/binaryen.js/custom-section.js.txt diff --git a/build-js.sh b/build-js.sh index 606fe6c505d..37bdc00ee9f 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 7b4ee2bc5dd..bebac81415e 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -4305,6 +4305,37 @@ const char* BinaryenExportGetValue(BinaryenExportRef export_) { return ((Export*)export_)->value.c_str(); } +// +// ========= 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(contents, contents + contentsSize); + wasm->userSections.push_back(customSection); +} + // // ========== CFG / Relooper ========== // diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 3d15a7efeb3..64119f02e46 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -1431,6 +1431,15 @@ BINARYEN_API const char* BinaryenExportGetName(BinaryenExportRef export_); // Gets the internal name of the specified 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 ========== // diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 8f49b105cc9..82d602b6ed6 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -2104,6 +2104,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 00000000000..f61af009658 --- /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 00000000000..7ac835fdf29 --- /dev/null +++ b/test/binaryen.js/custom-section.js.txt @@ -0,0 +1,26 @@ +// beginning a Binaryen API trace +#include +#include +#include "binaryen-c.h" +int main() { + std::map functionTypes; + std::map expressions; + std::map functions; + std::map globals; + std::map events; + std::map exports; + std::map 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" +) + From eb04d8770de55af196fc5ab02e85a233046d54be Mon Sep 17 00:00:00 2001 From: dcode Date: Fri, 11 Oct 2019 17:45:20 +0200 Subject: [PATCH 2/2] fix formatting --- src/binaryen-c.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index bebac81415e..2cf86a7b0ef 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -4316,12 +4316,12 @@ void BinaryenAddCustomSection(BinaryenModuleRef module, 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]); + 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);