MCPcopy Create free account
hub / github.com/nodejs/node / setup

Function setup

deps/v8/test/mjsunit/wasm/inlining-multi-instance.js:17–77  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
16
17function setup() {
18 const kInitialTableSize = 1;
19 let sharedTable = new WebAssembly.Table({
20 element: "anyfunc",
21 initial: kInitialTableSize,
22 });
23
24 let builder = new WasmModuleBuilder();
25 let type_i_v = builder.addType(kSig_i_v);
26 builder.addImportedTable("env", "table", kInitialTableSize);
27 // We use this global's value to differentiate which instance we are executing.
28 let importedGlobal = builder.addImportedGlobal("env", "g", kWasmI32);
29 builder
30 .addFunction("callee", kSig_i_v)
31 .addBody([kExprGlobalGet, importedGlobal, ...wasmI32Const(5), kExprI32Add])
32 .exportFunc();
33 builder
34 .addFunction("call_indirect", type_i_v)
35 .addBody([
36 ...wasmI32Const(/* index of funcref in table */ 0),
37 kExprCallIndirect, type_i_v, kTableZero,
38 ])
39 .exportFunc();
40 builder
41 .addFunction("return_call_indirect", type_i_v)
42 .addBody([
43 ...wasmI32Const(/* index of funcref in table */ 0),
44 kExprReturnCallIndirect, type_i_v, kTableZero,
45 ])
46 .exportFunc();
47 builder
48 .addFunction("call_ref", type_i_v)
49 .addBody([
50 ...wasmI32Const(/* index of funcref in table */ 0),
51 // This is essentially the same as for call_indirect above, just desugared.
52 kExprTableGet, kTableZero,
53 kGCPrefix, kExprRefCast, type_i_v,
54 kExprCallRef, type_i_v,
55 ])
56 .exportFunc();
57 let wasmModule = builder.toModule();
58
59 // Both instances share the same table, but have different global values to
60 // distinguish them.
61 let instance1 = new WebAssembly.Instance(wasmModule, {
62 env: {
63 g: 1000,
64 table: sharedTable,
65 },
66 });
67 let instance2 = new WebAssembly.Instance(wasmModule, {
68 env: {
69 g: 2000,
70 table: sharedTable,
71 },
72 });
73
74 // And the table contains a reference to the function of the first instance.

Callers 1

Calls 9

addTypeMethod · 0.95
addImportedTableMethod · 0.95
addImportedGlobalMethod · 0.95
addFunctionMethod · 0.95
toModuleMethod · 0.95
wasmI32ConstFunction · 0.70
exportFuncMethod · 0.45
addBodyMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected