Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(contents, contents + contentsSize);
wasm->userSections.push_back(customSection);
}

//
// ========== CFG / Relooper ==========
//
Expand Down
9 changes: 9 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 ==========
//
Expand Down
5 changes: 5 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
11 changes: 11 additions & 0 deletions test/binaryen.js/custom-section.js
Original file line number Diff line number Diff line change
@@ -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());
26 changes: 26 additions & 0 deletions test/binaryen.js/custom-section.js.txt
Original file line number Diff line number Diff line change
@@ -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"
)