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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Current Trunk
- (If new wat parser is enabled) Source map comments on `else` branches must
now be placed above the instruction inside the `else` branch rather than on
the `else` branch itself.
- Add a new `BinaryenModuleReadWithFeatures` function to the C API that allows
to configure which features to enable in the parser.

v117
----
Expand Down
11 changes: 8 additions & 3 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5851,14 +5851,15 @@ char* BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module,
return output;
}

BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
BinaryenModuleRef BinaryenModuleReadWithFeatures(char* input,
size_t inputSize,
BinaryenFeatures features) {
auto* wasm = new Module;
std::vector<char> buffer(false);
buffer.resize(inputSize);
std::copy_n(input, inputSize, buffer.begin());
try {
// TODO: allow providing features in the C API
WasmBinaryReader parser(*wasm, FeatureSet::MVP, buffer);
WasmBinaryReader parser(*wasm, features, buffer);
parser.read();
} catch (ParseException& p) {
p.dump(std::cerr);
Expand All @@ -5867,6 +5868,10 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
return wasm;
}

BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
return BinaryenModuleReadWithFeatures(input, inputSize, BinaryenFeatureMVP());
}

void BinaryenModuleInterpret(BinaryenModuleRef module) {
ShellExternalInterface interface;
ModuleRunner instance(*(Module*)module, &interface, {});
Expand Down
6 changes: 5 additions & 1 deletion src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -3175,10 +3175,14 @@ BINARYEN_API char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef module);
BINARYEN_API char*
BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module, bool optimize);

// Deserialize a module from binary form.
// Deserialize a module from binary form, assuming the MVP feature set.
BINARYEN_API BinaryenModuleRef BinaryenModuleRead(char* input,
size_t inputSize);

// Deserialize a module from binary form, enabling the given feature set.
BINARYEN_API BinaryenModuleRef BinaryenModuleReadWithFeatures(
char* input, size_t inputSize, BinaryenFeatures featureSet);

// Execute a module in the Binaryen interpreter. This will create an instance of
// the module, run it in the interpreter - which means running the start method
// - and then destroying the instance.
Expand Down
26 changes: 26 additions & 0 deletions test/example/c-api-kitchen-sink.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,32 @@ void test_features() {
printf("BinaryenFeatureAll: %d\n", BinaryenFeatureAll());
}

void test_read_with_feature() {
BinaryenModuleRef module = BinaryenModuleCreate();
// Having multiple tables makes this module inherently not MVP compatible
// and requires the externref feature enabled to parse successfully.
BinaryenAddTable(module, "tab", 0, 100, BinaryenTypeFuncref());
BinaryenAddTable(module, "tab2", 0, 100, BinaryenTypeFuncref());

BinaryenFeatures features =
BinaryenFeatureMVP() | BinaryenFeatureReferenceTypes();
BinaryenModuleSetFeatures(module, features);

size_t bufferSize = 1024;
char* buffer = malloc(bufferSize);
size_t written = BinaryenModuleWrite(module, buffer, bufferSize);
BinaryenModuleDispose(module);

// See we can read the bytes and get a valid module from there.
BinaryenModuleRef readModule =
BinaryenModuleReadWithFeatures(buffer, written, features);
int valid = BinaryenModuleValidate(readModule);
assert(valid);
BinaryenModuleDispose(readModule);

free(buffer);
}

void test_core() {

// Module creation
Expand Down