Skip to content

Commit 1a8150c

Browse files
authored
Add readBinaryWithFeatures to JS API (#8541)
1 parent fafc285 commit 1a8150c

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/js/binaryen.js-post.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,14 @@ Module['readBinary'] = function(data) {
33133313
return wrapModule(ptr);
33143314
};
33153315

3316+
Module['readBinaryWithFeatures'] = function(data, features) {
3317+
const buffer = _malloc(data.length);
3318+
HEAP8.set(data, buffer);
3319+
const ptr = handleFatalError(() => Module['_BinaryenModuleReadWithFeatures'](buffer, data.length, features));
3320+
_free(buffer);
3321+
return wrapModule(ptr);
3322+
};
3323+
33163324
// Parses text format to a module
33173325
Module['parseText'] = function(text) {
33183326
const buffer = _malloc(text.length + 1);

test/binaryen.js/kitchen-sink.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,55 @@ function test_binaries() {
10201020
module.dispose();
10211021
}
10221022

1023+
function test_binaries_with_features() {
1024+
var builder = new binaryen.TypeBuilder(1);
1025+
builder.setStructType(0, [
1026+
{ type: binaryen.i32, packedType: binaryen.notPacked, mutable: true },
1027+
{ type: binaryen.f64, packedType: binaryen.notPacked, mutable: true }
1028+
]);
1029+
var [structHeapType] = builder.buildAndDispose();
1030+
var structType = binaryen.getTypeFromHeapType(structHeapType, true);
1031+
1032+
var features = binaryen.Features.ReferenceTypes | binaryen.Features.GC;
1033+
module = new binaryen.Module();
1034+
module.setFeatures(features);
1035+
1036+
module.addGlobal("struct-global",
1037+
structType,
1038+
true,
1039+
module.struct.new(
1040+
[module.i32.const(42), module.f64.const(3.14)],
1041+
binaryen.getHeapType(structType)
1042+
)
1043+
);
1044+
1045+
module.addFunction("get-field", binaryen.none, binaryen.i32, [],
1046+
module.struct.get(
1047+
0,
1048+
module.global.get("struct-global", structType),
1049+
binaryen.i32,
1050+
false
1051+
)
1052+
);
1053+
1054+
assert(module.validate());
1055+
binaryen.setDebugInfo(true);
1056+
var buffer = module.emitBinary();
1057+
binaryen.setDebugInfo(false);
1058+
module.dispose();
1059+
1060+
module = binaryen.readBinaryWithFeatures(buffer, features);
1061+
1062+
assert(module.validate());
1063+
console.log("module loaded from binary with features:");
1064+
console.log(module.emitText());
1065+
module.dispose();
1066+
1067+
module = binaryen.readBinaryWithFeatures(buffer, binaryen.Features.MVP);
1068+
assert(!module.validate());
1069+
module.dispose();
1070+
}
1071+
10231072
function test_interpret() {
10241073
// create a simple module with a start method that prints a number, and interpret it, printing that number.
10251074
module = new binaryen.Module();
@@ -1230,6 +1279,7 @@ test_ids();
12301279
test_core();
12311280
test_relooper();
12321281
test_binaries();
1282+
test_binaries_with_features();
12331283
test_interpret();
12341284
test_nonvalid();
12351285
test_parsing();

test/binaryen.js/kitchen-sink.js.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,35 @@ module loaded from binary form:
27512751
)
27522752
)
27532753

2754+
module loaded from binary with features:
2755+
(module
2756+
(type $0 (struct (field (mut i32)) (field (mut f64))))
2757+
(type $1 (func (result i32)))
2758+
(global $struct-global (mut (ref null $0)) (struct.new $0
2759+
(i32.const 42)
2760+
(f64.const 3.14)
2761+
))
2762+
(func $get-field (type $1) (result i32)
2763+
(struct.get $0 0
2764+
(global.get $struct-global)
2765+
)
2766+
)
2767+
)
2768+
2769+
[wasm-validator error in function get-field] unexpected false: struct.get requires gc [--enable-gc], on
2770+
(struct.get $struct.0 0
2771+
(global.get $struct-global)
2772+
)
2773+
[wasm-validator error in module] unexpected false: all used types should be allowed, on
2774+
struct-global
2775+
[wasm-validator error in module] unexpected false: struct.new requires gc [--enable-gc], on
2776+
(struct.new $struct.0
2777+
(i32.const 42)
2778+
(f64.const 3.14)
2779+
)
2780+
[wasm-validator error in module] unexpected false: , on
2781+
struct-global
2782+
global type requires additional features [--enable-reference-types --enable-gc]
27542783
(module
27552784
(type $0 (func (param i32)))
27562785
(type $1 (func))

0 commit comments

Comments
 (0)