11cb0ef41Sopenharmony_ciconst instanceTestFactory = [ 21cb0ef41Sopenharmony_ci [ 31cb0ef41Sopenharmony_ci "Empty module without imports argument", 41cb0ef41Sopenharmony_ci function() { 51cb0ef41Sopenharmony_ci return { 61cb0ef41Sopenharmony_ci buffer: emptyModuleBinary, 71cb0ef41Sopenharmony_ci args: [], 81cb0ef41Sopenharmony_ci exports: {}, 91cb0ef41Sopenharmony_ci verify: () => {}, 101cb0ef41Sopenharmony_ci }; 111cb0ef41Sopenharmony_ci } 121cb0ef41Sopenharmony_ci ], 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci [ 151cb0ef41Sopenharmony_ci "Empty module with undefined imports argument", 161cb0ef41Sopenharmony_ci function() { 171cb0ef41Sopenharmony_ci return { 181cb0ef41Sopenharmony_ci buffer: emptyModuleBinary, 191cb0ef41Sopenharmony_ci args: [undefined], 201cb0ef41Sopenharmony_ci exports: {}, 211cb0ef41Sopenharmony_ci verify: () => {}, 221cb0ef41Sopenharmony_ci }; 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci ], 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci [ 271cb0ef41Sopenharmony_ci "Empty module with empty imports argument", 281cb0ef41Sopenharmony_ci function() { 291cb0ef41Sopenharmony_ci return { 301cb0ef41Sopenharmony_ci buffer: emptyModuleBinary, 311cb0ef41Sopenharmony_ci args: [{}], 321cb0ef41Sopenharmony_ci exports: {}, 331cb0ef41Sopenharmony_ci verify: () => {}, 341cb0ef41Sopenharmony_ci }; 351cb0ef41Sopenharmony_ci } 361cb0ef41Sopenharmony_ci ], 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci [ 391cb0ef41Sopenharmony_ci "getter order for imports object", 401cb0ef41Sopenharmony_ci function() { 411cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 421cb0ef41Sopenharmony_ci builder.addImportedGlobal("module", "global1", kWasmI32); 431cb0ef41Sopenharmony_ci builder.addImportedGlobal("module2", "global3", kWasmI32); 441cb0ef41Sopenharmony_ci builder.addImportedMemory("module", "memory", 0, 128); 451cb0ef41Sopenharmony_ci builder.addImportedGlobal("module", "global2", kWasmI32); 461cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 471cb0ef41Sopenharmony_ci const order = []; 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci const imports = { 501cb0ef41Sopenharmony_ci get module() { 511cb0ef41Sopenharmony_ci order.push("module getter"); 521cb0ef41Sopenharmony_ci return { 531cb0ef41Sopenharmony_ci get global1() { 541cb0ef41Sopenharmony_ci order.push("global1 getter"); 551cb0ef41Sopenharmony_ci return 0; 561cb0ef41Sopenharmony_ci }, 571cb0ef41Sopenharmony_ci get global2() { 581cb0ef41Sopenharmony_ci order.push("global2 getter"); 591cb0ef41Sopenharmony_ci return 0; 601cb0ef41Sopenharmony_ci }, 611cb0ef41Sopenharmony_ci get memory() { 621cb0ef41Sopenharmony_ci order.push("memory getter"); 631cb0ef41Sopenharmony_ci return new WebAssembly.Memory({ "initial": 64, maximum: 128 }); 641cb0ef41Sopenharmony_ci }, 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci }, 671cb0ef41Sopenharmony_ci get module2() { 681cb0ef41Sopenharmony_ci order.push("module2 getter"); 691cb0ef41Sopenharmony_ci return { 701cb0ef41Sopenharmony_ci get global3() { 711cb0ef41Sopenharmony_ci order.push("global3 getter"); 721cb0ef41Sopenharmony_ci return 0; 731cb0ef41Sopenharmony_ci }, 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci }, 761cb0ef41Sopenharmony_ci }; 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci const expected = [ 791cb0ef41Sopenharmony_ci "module getter", 801cb0ef41Sopenharmony_ci "global1 getter", 811cb0ef41Sopenharmony_ci "module2 getter", 821cb0ef41Sopenharmony_ci "global3 getter", 831cb0ef41Sopenharmony_ci "module getter", 841cb0ef41Sopenharmony_ci "memory getter", 851cb0ef41Sopenharmony_ci "module getter", 861cb0ef41Sopenharmony_ci "global2 getter", 871cb0ef41Sopenharmony_ci ]; 881cb0ef41Sopenharmony_ci return { 891cb0ef41Sopenharmony_ci buffer, 901cb0ef41Sopenharmony_ci args: [imports], 911cb0ef41Sopenharmony_ci exports: {}, 921cb0ef41Sopenharmony_ci verify: () => assert_array_equals(order, expected), 931cb0ef41Sopenharmony_ci }; 941cb0ef41Sopenharmony_ci } 951cb0ef41Sopenharmony_ci ], 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci [ 981cb0ef41Sopenharmony_ci "imports", 991cb0ef41Sopenharmony_ci function() { 1001cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci builder.addImport("module", "fn", kSig_v_v); 1031cb0ef41Sopenharmony_ci builder.addImportedGlobal("module", "global", kWasmI32); 1041cb0ef41Sopenharmony_ci builder.addImportedMemory("module", "memory", 0, 128); 1051cb0ef41Sopenharmony_ci builder.addImportedTable("module", "table", 0, 128); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 1081cb0ef41Sopenharmony_ci const imports = { 1091cb0ef41Sopenharmony_ci "module": { 1101cb0ef41Sopenharmony_ci "fn": function() {}, 1111cb0ef41Sopenharmony_ci "global": 0, 1121cb0ef41Sopenharmony_ci "memory": new WebAssembly.Memory({ "initial": 64, maximum: 128 }), 1131cb0ef41Sopenharmony_ci "table": new WebAssembly.Table({ "element": "anyfunc", "initial": 64, maximum: 128 }), 1141cb0ef41Sopenharmony_ci }, 1151cb0ef41Sopenharmony_ci get "module2"() { 1161cb0ef41Sopenharmony_ci assert_unreached("Should not get modules that are not imported"); 1171cb0ef41Sopenharmony_ci }, 1181cb0ef41Sopenharmony_ci }; 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci return { 1211cb0ef41Sopenharmony_ci buffer, 1221cb0ef41Sopenharmony_ci args: [imports], 1231cb0ef41Sopenharmony_ci exports: {}, 1241cb0ef41Sopenharmony_ci verify: () => {}, 1251cb0ef41Sopenharmony_ci }; 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci ], 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci [ 1301cb0ef41Sopenharmony_ci "imports with empty module names", 1311cb0ef41Sopenharmony_ci function() { 1321cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci builder.addImport("", "fn", kSig_v_v); 1351cb0ef41Sopenharmony_ci builder.addImportedGlobal("", "global", kWasmI32); 1361cb0ef41Sopenharmony_ci builder.addImportedMemory("", "memory", 0, 128); 1371cb0ef41Sopenharmony_ci builder.addImportedTable("", "table", 0, 128); 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 1401cb0ef41Sopenharmony_ci const imports = { 1411cb0ef41Sopenharmony_ci "": { 1421cb0ef41Sopenharmony_ci "fn": function() {}, 1431cb0ef41Sopenharmony_ci "global": 0, 1441cb0ef41Sopenharmony_ci "memory": new WebAssembly.Memory({ "initial": 64, maximum: 128 }), 1451cb0ef41Sopenharmony_ci "table": new WebAssembly.Table({ "element": "anyfunc", "initial": 64, maximum: 128 }), 1461cb0ef41Sopenharmony_ci }, 1471cb0ef41Sopenharmony_ci }; 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci return { 1501cb0ef41Sopenharmony_ci buffer, 1511cb0ef41Sopenharmony_ci args: [imports], 1521cb0ef41Sopenharmony_ci exports: {}, 1531cb0ef41Sopenharmony_ci verify: () => {}, 1541cb0ef41Sopenharmony_ci }; 1551cb0ef41Sopenharmony_ci } 1561cb0ef41Sopenharmony_ci ], 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci [ 1591cb0ef41Sopenharmony_ci "imports with empty names", 1601cb0ef41Sopenharmony_ci function() { 1611cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ci builder.addImport("a", "", kSig_v_v); 1641cb0ef41Sopenharmony_ci builder.addImportedGlobal("b", "", kWasmI32); 1651cb0ef41Sopenharmony_ci builder.addImportedMemory("c", "", 0, 128); 1661cb0ef41Sopenharmony_ci builder.addImportedTable("d", "", 0, 128); 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 1691cb0ef41Sopenharmony_ci const imports = { 1701cb0ef41Sopenharmony_ci "a": { "": function() {} }, 1711cb0ef41Sopenharmony_ci "b": { "": 0 }, 1721cb0ef41Sopenharmony_ci "c": { "": new WebAssembly.Memory({ "initial": 64, maximum: 128 }) }, 1731cb0ef41Sopenharmony_ci "d": { "": new WebAssembly.Table({ "element": "anyfunc", "initial": 64, maximum: 128 }) }, 1741cb0ef41Sopenharmony_ci }; 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci return { 1771cb0ef41Sopenharmony_ci buffer, 1781cb0ef41Sopenharmony_ci args: [imports], 1791cb0ef41Sopenharmony_ci exports: {}, 1801cb0ef41Sopenharmony_ci verify: () => {}, 1811cb0ef41Sopenharmony_ci }; 1821cb0ef41Sopenharmony_ci } 1831cb0ef41Sopenharmony_ci ], 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci [ 1861cb0ef41Sopenharmony_ci "exports with empty name: function", 1871cb0ef41Sopenharmony_ci function() { 1881cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 1891cb0ef41Sopenharmony_ci 1901cb0ef41Sopenharmony_ci builder 1911cb0ef41Sopenharmony_ci .addFunction("", kSig_v_d) 1921cb0ef41Sopenharmony_ci .addBody([]) 1931cb0ef41Sopenharmony_ci .exportFunc(); 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci const exports = { 1981cb0ef41Sopenharmony_ci "": { "kind": "function", "name": "0", "length": 1 }, 1991cb0ef41Sopenharmony_ci }; 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci return { 2021cb0ef41Sopenharmony_ci buffer, 2031cb0ef41Sopenharmony_ci args: [], 2041cb0ef41Sopenharmony_ci exports, 2051cb0ef41Sopenharmony_ci verify: () => {}, 2061cb0ef41Sopenharmony_ci }; 2071cb0ef41Sopenharmony_ci } 2081cb0ef41Sopenharmony_ci ], 2091cb0ef41Sopenharmony_ci 2101cb0ef41Sopenharmony_ci [ 2111cb0ef41Sopenharmony_ci "exports with empty name: table", 2121cb0ef41Sopenharmony_ci function() { 2131cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci builder.setTableBounds(1); 2161cb0ef41Sopenharmony_ci builder.addExportOfKind("", kExternalTable, 0); 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci const exports = { 2211cb0ef41Sopenharmony_ci "": { "kind": "table", "length": 1 }, 2221cb0ef41Sopenharmony_ci }; 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ci return { 2251cb0ef41Sopenharmony_ci buffer, 2261cb0ef41Sopenharmony_ci args: [], 2271cb0ef41Sopenharmony_ci exports, 2281cb0ef41Sopenharmony_ci verify: () => {}, 2291cb0ef41Sopenharmony_ci }; 2301cb0ef41Sopenharmony_ci } 2311cb0ef41Sopenharmony_ci ], 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ci [ 2341cb0ef41Sopenharmony_ci "exports with empty name: global", 2351cb0ef41Sopenharmony_ci function() { 2361cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_ci builder.addGlobal(kWasmI32, true) 2391cb0ef41Sopenharmony_ci .exportAs("") 2401cb0ef41Sopenharmony_ci .init = 7; 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci const exports = { 2451cb0ef41Sopenharmony_ci "": { "kind": "global", "value": 7 }, 2461cb0ef41Sopenharmony_ci }; 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci return { 2491cb0ef41Sopenharmony_ci buffer, 2501cb0ef41Sopenharmony_ci args: [], 2511cb0ef41Sopenharmony_ci exports, 2521cb0ef41Sopenharmony_ci verify: () => {}, 2531cb0ef41Sopenharmony_ci }; 2541cb0ef41Sopenharmony_ci } 2551cb0ef41Sopenharmony_ci ], 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci [ 2581cb0ef41Sopenharmony_ci "No imports", 2591cb0ef41Sopenharmony_ci function() { 2601cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ci builder 2631cb0ef41Sopenharmony_ci .addFunction("fn", kSig_v_d) 2641cb0ef41Sopenharmony_ci .addBody([]) 2651cb0ef41Sopenharmony_ci .exportFunc(); 2661cb0ef41Sopenharmony_ci builder 2671cb0ef41Sopenharmony_ci .addFunction("fn2", kSig_v_v) 2681cb0ef41Sopenharmony_ci .addBody([]) 2691cb0ef41Sopenharmony_ci .exportFunc(); 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci builder.setTableBounds(1); 2721cb0ef41Sopenharmony_ci builder.addExportOfKind("table", kExternalTable, 0); 2731cb0ef41Sopenharmony_ci 2741cb0ef41Sopenharmony_ci builder.addGlobal(kWasmI32, true) 2751cb0ef41Sopenharmony_ci .exportAs("global") 2761cb0ef41Sopenharmony_ci .init = 7; 2771cb0ef41Sopenharmony_ci builder.addGlobal(kWasmF64, true) 2781cb0ef41Sopenharmony_ci .exportAs("global2") 2791cb0ef41Sopenharmony_ci .init = 1.2; 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci builder.addMemory(4, 8, true); 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 2841cb0ef41Sopenharmony_ci 2851cb0ef41Sopenharmony_ci const exports = { 2861cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": "0", "length": 1 }, 2871cb0ef41Sopenharmony_ci "fn2": { "kind": "function", "name": "1", "length": 0 }, 2881cb0ef41Sopenharmony_ci "table": { "kind": "table", "length": 1 }, 2891cb0ef41Sopenharmony_ci "global": { "kind": "global", "value": 7 }, 2901cb0ef41Sopenharmony_ci "global2": { "kind": "global", "value": 1.2 }, 2911cb0ef41Sopenharmony_ci "memory": { "kind": "memory", "size": 4 }, 2921cb0ef41Sopenharmony_ci }; 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci return { 2951cb0ef41Sopenharmony_ci buffer, 2961cb0ef41Sopenharmony_ci args: [], 2971cb0ef41Sopenharmony_ci exports, 2981cb0ef41Sopenharmony_ci verify: () => {}, 2991cb0ef41Sopenharmony_ci }; 3001cb0ef41Sopenharmony_ci } 3011cb0ef41Sopenharmony_ci ], 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci [ 3041cb0ef41Sopenharmony_ci "exports and imports", 3051cb0ef41Sopenharmony_ci function() { 3061cb0ef41Sopenharmony_ci const value = 102; 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 3091cb0ef41Sopenharmony_ci 3101cb0ef41Sopenharmony_ci const index = builder.addImportedGlobal("module", "global", kWasmI32); 3111cb0ef41Sopenharmony_ci builder 3121cb0ef41Sopenharmony_ci .addFunction("fn", kSig_i_v) 3131cb0ef41Sopenharmony_ci .addBody([ 3141cb0ef41Sopenharmony_ci kExprGlobalGet, 3151cb0ef41Sopenharmony_ci index, 3161cb0ef41Sopenharmony_ci kExprReturn, 3171cb0ef41Sopenharmony_ci ]) 3181cb0ef41Sopenharmony_ci .exportFunc(); 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 3211cb0ef41Sopenharmony_ci 3221cb0ef41Sopenharmony_ci const imports = { 3231cb0ef41Sopenharmony_ci "module": { 3241cb0ef41Sopenharmony_ci "global": value, 3251cb0ef41Sopenharmony_ci }, 3261cb0ef41Sopenharmony_ci }; 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci const exports = { 3291cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": "0", "length": 0 }, 3301cb0ef41Sopenharmony_ci }; 3311cb0ef41Sopenharmony_ci 3321cb0ef41Sopenharmony_ci return { 3331cb0ef41Sopenharmony_ci buffer, 3341cb0ef41Sopenharmony_ci args: [imports], 3351cb0ef41Sopenharmony_ci exports, 3361cb0ef41Sopenharmony_ci verify: instance => assert_equals(instance.exports.fn(), value) 3371cb0ef41Sopenharmony_ci }; 3381cb0ef41Sopenharmony_ci } 3391cb0ef41Sopenharmony_ci ], 3401cb0ef41Sopenharmony_ci 3411cb0ef41Sopenharmony_ci [ 3421cb0ef41Sopenharmony_ci "i64 exports and imports", 3431cb0ef41Sopenharmony_ci function() { 3441cb0ef41Sopenharmony_ci const value = 102n; 3451cb0ef41Sopenharmony_ci 3461cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 3471cb0ef41Sopenharmony_ci 3481cb0ef41Sopenharmony_ci const index = builder.addImportedGlobal("module", "global", kWasmI64); 3491cb0ef41Sopenharmony_ci builder 3501cb0ef41Sopenharmony_ci .addFunction("fn", kSig_l_v) 3511cb0ef41Sopenharmony_ci .addBody([ 3521cb0ef41Sopenharmony_ci kExprGlobalGet, 3531cb0ef41Sopenharmony_ci index, 3541cb0ef41Sopenharmony_ci kExprReturn, 3551cb0ef41Sopenharmony_ci ]) 3561cb0ef41Sopenharmony_ci .exportFunc(); 3571cb0ef41Sopenharmony_ci 3581cb0ef41Sopenharmony_ci const index2 = builder.addImportedGlobal("module", "global2", kWasmI64); 3591cb0ef41Sopenharmony_ci builder.addExportOfKind("global", kExternalGlobal, index2); 3601cb0ef41Sopenharmony_ci 3611cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 3621cb0ef41Sopenharmony_ci 3631cb0ef41Sopenharmony_ci const imports = { 3641cb0ef41Sopenharmony_ci "module": { 3651cb0ef41Sopenharmony_ci "global": value, 3661cb0ef41Sopenharmony_ci "global2": 2n ** 63n, 3671cb0ef41Sopenharmony_ci }, 3681cb0ef41Sopenharmony_ci }; 3691cb0ef41Sopenharmony_ci 3701cb0ef41Sopenharmony_ci const exports = { 3711cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": "0", "length": 0 }, 3721cb0ef41Sopenharmony_ci "global": { "kind": "global", "value": -(2n ** 63n) }, 3731cb0ef41Sopenharmony_ci }; 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ci return { 3761cb0ef41Sopenharmony_ci buffer, 3771cb0ef41Sopenharmony_ci args: [imports], 3781cb0ef41Sopenharmony_ci exports, 3791cb0ef41Sopenharmony_ci verify: instance => assert_equals(instance.exports.fn(), value) 3801cb0ef41Sopenharmony_ci }; 3811cb0ef41Sopenharmony_ci } 3821cb0ef41Sopenharmony_ci ], 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci [ 3851cb0ef41Sopenharmony_ci "import with i32-returning function", 3861cb0ef41Sopenharmony_ci function() { 3871cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci const fnIndex = builder.addImport("module", "fn", kSig_i_v); 3901cb0ef41Sopenharmony_ci const fn2 = builder 3911cb0ef41Sopenharmony_ci .addFunction("fn2", kSig_v_v) 3921cb0ef41Sopenharmony_ci .addBody([ 3931cb0ef41Sopenharmony_ci kExprCallFunction, 3941cb0ef41Sopenharmony_ci fnIndex, 3951cb0ef41Sopenharmony_ci kExprReturn, 3961cb0ef41Sopenharmony_ci ]) 3971cb0ef41Sopenharmony_ci .exportFunc(); 3981cb0ef41Sopenharmony_ci 3991cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 4001cb0ef41Sopenharmony_ci 4011cb0ef41Sopenharmony_ci let called = false; 4021cb0ef41Sopenharmony_ci const imports = { 4031cb0ef41Sopenharmony_ci "module": { 4041cb0ef41Sopenharmony_ci "fn": function() { 4051cb0ef41Sopenharmony_ci called = true; 4061cb0ef41Sopenharmony_ci return 6n; 4071cb0ef41Sopenharmony_ci }, 4081cb0ef41Sopenharmony_ci }, 4091cb0ef41Sopenharmony_ci }; 4101cb0ef41Sopenharmony_ci 4111cb0ef41Sopenharmony_ci return { 4121cb0ef41Sopenharmony_ci buffer, 4131cb0ef41Sopenharmony_ci args: [imports], 4141cb0ef41Sopenharmony_ci exports: { 4151cb0ef41Sopenharmony_ci "fn2": { "kind": "function", "name": String(fn2.index), "length": 0 }, 4161cb0ef41Sopenharmony_ci }, 4171cb0ef41Sopenharmony_ci verify: instance => { 4181cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => instance.exports.fn2()); 4191cb0ef41Sopenharmony_ci assert_true(called, "Should have called into JS"); 4201cb0ef41Sopenharmony_ci } 4211cb0ef41Sopenharmony_ci }; 4221cb0ef41Sopenharmony_ci } 4231cb0ef41Sopenharmony_ci ], 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ci [ 4261cb0ef41Sopenharmony_ci "import with function that takes and returns i32", 4271cb0ef41Sopenharmony_ci function() { 4281cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_ci const fnIndex = builder.addImport("module", "fn", kSig_i_i); 4311cb0ef41Sopenharmony_ci const fn2 = builder 4321cb0ef41Sopenharmony_ci .addFunction("fn2", kSig_i_v) 4331cb0ef41Sopenharmony_ci .addBody([ 4341cb0ef41Sopenharmony_ci kExprI32Const, 0x66, 4351cb0ef41Sopenharmony_ci kExprCallFunction, 4361cb0ef41Sopenharmony_ci fnIndex, 4371cb0ef41Sopenharmony_ci kExprReturn, 4381cb0ef41Sopenharmony_ci ]) 4391cb0ef41Sopenharmony_ci .exportFunc(); 4401cb0ef41Sopenharmony_ci 4411cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci let called = false; 4441cb0ef41Sopenharmony_ci const imports = { 4451cb0ef41Sopenharmony_ci "module": { 4461cb0ef41Sopenharmony_ci "fn": function(n) { 4471cb0ef41Sopenharmony_ci called = true; 4481cb0ef41Sopenharmony_ci assert_equals(n, -26); 4491cb0ef41Sopenharmony_ci return { valueOf() { return 6; } }; 4501cb0ef41Sopenharmony_ci }, 4511cb0ef41Sopenharmony_ci }, 4521cb0ef41Sopenharmony_ci }; 4531cb0ef41Sopenharmony_ci 4541cb0ef41Sopenharmony_ci return { 4551cb0ef41Sopenharmony_ci buffer, 4561cb0ef41Sopenharmony_ci args: [imports], 4571cb0ef41Sopenharmony_ci exports: { 4581cb0ef41Sopenharmony_ci "fn2": { "kind": "function", "name": String(fn2.index), "length": 0 }, 4591cb0ef41Sopenharmony_ci }, 4601cb0ef41Sopenharmony_ci verify: instance => { 4611cb0ef41Sopenharmony_ci assert_equals(instance.exports.fn2(), 6); 4621cb0ef41Sopenharmony_ci assert_true(called, "Should have called into JS"); 4631cb0ef41Sopenharmony_ci } 4641cb0ef41Sopenharmony_ci }; 4651cb0ef41Sopenharmony_ci } 4661cb0ef41Sopenharmony_ci ], 4671cb0ef41Sopenharmony_ci 4681cb0ef41Sopenharmony_ci [ 4691cb0ef41Sopenharmony_ci "import with i64-returning function", 4701cb0ef41Sopenharmony_ci function() { 4711cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 4721cb0ef41Sopenharmony_ci 4731cb0ef41Sopenharmony_ci const fnIndex = builder.addImport("module", "fn", kSig_l_v); 4741cb0ef41Sopenharmony_ci const fn2 = builder 4751cb0ef41Sopenharmony_ci .addFunction("fn2", kSig_v_v) 4761cb0ef41Sopenharmony_ci .addBody([ 4771cb0ef41Sopenharmony_ci kExprCallFunction, 4781cb0ef41Sopenharmony_ci fnIndex, 4791cb0ef41Sopenharmony_ci kExprReturn, 4801cb0ef41Sopenharmony_ci ]) 4811cb0ef41Sopenharmony_ci .exportFunc(); 4821cb0ef41Sopenharmony_ci 4831cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ci let called = false; 4861cb0ef41Sopenharmony_ci const imports = { 4871cb0ef41Sopenharmony_ci "module": { 4881cb0ef41Sopenharmony_ci "fn": function() { 4891cb0ef41Sopenharmony_ci called = true; 4901cb0ef41Sopenharmony_ci return 6; 4911cb0ef41Sopenharmony_ci }, 4921cb0ef41Sopenharmony_ci }, 4931cb0ef41Sopenharmony_ci }; 4941cb0ef41Sopenharmony_ci 4951cb0ef41Sopenharmony_ci return { 4961cb0ef41Sopenharmony_ci buffer, 4971cb0ef41Sopenharmony_ci args: [imports], 4981cb0ef41Sopenharmony_ci exports: { 4991cb0ef41Sopenharmony_ci "fn2": { "kind": "function", "name": String(fn2.index), "length": 0 }, 5001cb0ef41Sopenharmony_ci }, 5011cb0ef41Sopenharmony_ci verify: instance => { 5021cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => instance.exports.fn2()); 5031cb0ef41Sopenharmony_ci assert_true(called, "Should have called into JS"); 5041cb0ef41Sopenharmony_ci } 5051cb0ef41Sopenharmony_ci }; 5061cb0ef41Sopenharmony_ci } 5071cb0ef41Sopenharmony_ci ], 5081cb0ef41Sopenharmony_ci 5091cb0ef41Sopenharmony_ci [ 5101cb0ef41Sopenharmony_ci "import with function that takes and returns i64", 5111cb0ef41Sopenharmony_ci function() { 5121cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 5131cb0ef41Sopenharmony_ci 5141cb0ef41Sopenharmony_ci const fnIndex = builder.addImport("module", "fn", kSig_l_l); 5151cb0ef41Sopenharmony_ci const fn2 = builder 5161cb0ef41Sopenharmony_ci .addFunction("fn2", kSig_l_v) 5171cb0ef41Sopenharmony_ci .addBody([ 5181cb0ef41Sopenharmony_ci kExprI64Const, 0x66, 5191cb0ef41Sopenharmony_ci kExprCallFunction, 5201cb0ef41Sopenharmony_ci fnIndex, 5211cb0ef41Sopenharmony_ci kExprReturn, 5221cb0ef41Sopenharmony_ci ]) 5231cb0ef41Sopenharmony_ci .exportFunc(); 5241cb0ef41Sopenharmony_ci 5251cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ci let called = false; 5281cb0ef41Sopenharmony_ci const imports = { 5291cb0ef41Sopenharmony_ci "module": { 5301cb0ef41Sopenharmony_ci "fn": function(n) { 5311cb0ef41Sopenharmony_ci called = true; 5321cb0ef41Sopenharmony_ci assert_equals(n, -26n); 5331cb0ef41Sopenharmony_ci return { valueOf() { return 6n; } }; 5341cb0ef41Sopenharmony_ci }, 5351cb0ef41Sopenharmony_ci }, 5361cb0ef41Sopenharmony_ci }; 5371cb0ef41Sopenharmony_ci 5381cb0ef41Sopenharmony_ci return { 5391cb0ef41Sopenharmony_ci buffer, 5401cb0ef41Sopenharmony_ci args: [imports], 5411cb0ef41Sopenharmony_ci exports: { 5421cb0ef41Sopenharmony_ci "fn2": { "kind": "function", "name": String(fn2.index), "length": 0 }, 5431cb0ef41Sopenharmony_ci }, 5441cb0ef41Sopenharmony_ci verify: instance => { 5451cb0ef41Sopenharmony_ci assert_equals(instance.exports.fn2(), 6n); 5461cb0ef41Sopenharmony_ci assert_true(called, "Should have called into JS"); 5471cb0ef41Sopenharmony_ci } 5481cb0ef41Sopenharmony_ci }; 5491cb0ef41Sopenharmony_ci } 5501cb0ef41Sopenharmony_ci ], 5511cb0ef41Sopenharmony_ci 5521cb0ef41Sopenharmony_ci [ 5531cb0ef41Sopenharmony_ci "import with i32-taking function", 5541cb0ef41Sopenharmony_ci function() { 5551cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ci const fn = builder 5581cb0ef41Sopenharmony_ci .addFunction("fn", kSig_v_i) 5591cb0ef41Sopenharmony_ci .addBody([ 5601cb0ef41Sopenharmony_ci kExprReturn, 5611cb0ef41Sopenharmony_ci ]) 5621cb0ef41Sopenharmony_ci .exportFunc(); 5631cb0ef41Sopenharmony_ci 5641cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 5651cb0ef41Sopenharmony_ci 5661cb0ef41Sopenharmony_ci return { 5671cb0ef41Sopenharmony_ci buffer, 5681cb0ef41Sopenharmony_ci args: [], 5691cb0ef41Sopenharmony_ci exports: { 5701cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": String(fn.index), "length": 1 }, 5711cb0ef41Sopenharmony_ci }, 5721cb0ef41Sopenharmony_ci verify: instance => assert_throws_js(TypeError, () => instance.exports.fn(6n)) 5731cb0ef41Sopenharmony_ci }; 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci ], 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ci [ 5781cb0ef41Sopenharmony_ci "import with i64-taking function", 5791cb0ef41Sopenharmony_ci function() { 5801cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 5811cb0ef41Sopenharmony_ci 5821cb0ef41Sopenharmony_ci const fn = builder 5831cb0ef41Sopenharmony_ci .addFunction("fn", kSig_v_l) 5841cb0ef41Sopenharmony_ci .addBody([ 5851cb0ef41Sopenharmony_ci kExprReturn, 5861cb0ef41Sopenharmony_ci ]) 5871cb0ef41Sopenharmony_ci .exportFunc(); 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 5901cb0ef41Sopenharmony_ci 5911cb0ef41Sopenharmony_ci return { 5921cb0ef41Sopenharmony_ci buffer, 5931cb0ef41Sopenharmony_ci args: [], 5941cb0ef41Sopenharmony_ci exports: { 5951cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": String(fn.index), "length": 1 }, 5961cb0ef41Sopenharmony_ci }, 5971cb0ef41Sopenharmony_ci verify: instance => assert_throws_js(TypeError, () => instance.exports.fn(6)) 5981cb0ef41Sopenharmony_ci }; 5991cb0ef41Sopenharmony_ci } 6001cb0ef41Sopenharmony_ci ], 6011cb0ef41Sopenharmony_ci 6021cb0ef41Sopenharmony_ci [ 6031cb0ef41Sopenharmony_ci "export i64-returning function", 6041cb0ef41Sopenharmony_ci function() { 6051cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 6061cb0ef41Sopenharmony_ci 6071cb0ef41Sopenharmony_ci const fn = builder 6081cb0ef41Sopenharmony_ci .addFunction("fn", kSig_l_v) 6091cb0ef41Sopenharmony_ci .addBody([ 6101cb0ef41Sopenharmony_ci kExprI64Const, 0x66, 6111cb0ef41Sopenharmony_ci kExprReturn, 6121cb0ef41Sopenharmony_ci ]) 6131cb0ef41Sopenharmony_ci .exportFunc(); 6141cb0ef41Sopenharmony_ci 6151cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 6161cb0ef41Sopenharmony_ci 6171cb0ef41Sopenharmony_ci return { 6181cb0ef41Sopenharmony_ci buffer, 6191cb0ef41Sopenharmony_ci args: [], 6201cb0ef41Sopenharmony_ci exports: { 6211cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": String(fn.index), "length": 0 }, 6221cb0ef41Sopenharmony_ci }, 6231cb0ef41Sopenharmony_ci verify: instance => assert_equals(instance.exports.fn(), -26n) 6241cb0ef41Sopenharmony_ci }; 6251cb0ef41Sopenharmony_ci } 6261cb0ef41Sopenharmony_ci ], 6271cb0ef41Sopenharmony_ci 6281cb0ef41Sopenharmony_ci [ 6291cb0ef41Sopenharmony_ci "i32 mutable WebAssembly.Global import", 6301cb0ef41Sopenharmony_ci function() { 6311cb0ef41Sopenharmony_ci const initial = 102; 6321cb0ef41Sopenharmony_ci const value = new WebAssembly.Global({ "value": "i32", "mutable": true }, initial); 6331cb0ef41Sopenharmony_ci 6341cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ci const index = builder.addImportedGlobal("module", "global", kWasmI32, true); 6371cb0ef41Sopenharmony_ci const fn = builder 6381cb0ef41Sopenharmony_ci .addFunction("fn", kSig_i_v) 6391cb0ef41Sopenharmony_ci .addBody([ 6401cb0ef41Sopenharmony_ci kExprGlobalGet, 6411cb0ef41Sopenharmony_ci index, 6421cb0ef41Sopenharmony_ci kExprReturn, 6431cb0ef41Sopenharmony_ci ]) 6441cb0ef41Sopenharmony_ci .exportFunc(); 6451cb0ef41Sopenharmony_ci 6461cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 6471cb0ef41Sopenharmony_ci 6481cb0ef41Sopenharmony_ci const imports = { 6491cb0ef41Sopenharmony_ci "module": { 6501cb0ef41Sopenharmony_ci "global": value, 6511cb0ef41Sopenharmony_ci }, 6521cb0ef41Sopenharmony_ci }; 6531cb0ef41Sopenharmony_ci 6541cb0ef41Sopenharmony_ci const exports = { 6551cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": String(fn.index), "length": 0 }, 6561cb0ef41Sopenharmony_ci }; 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci return { 6591cb0ef41Sopenharmony_ci buffer, 6601cb0ef41Sopenharmony_ci args: [imports], 6611cb0ef41Sopenharmony_ci exports, 6621cb0ef41Sopenharmony_ci verify: instance => { 6631cb0ef41Sopenharmony_ci assert_equals(instance.exports.fn(), initial); 6641cb0ef41Sopenharmony_ci const after = 201; 6651cb0ef41Sopenharmony_ci value.value = after; 6661cb0ef41Sopenharmony_ci assert_equals(instance.exports.fn(), after); 6671cb0ef41Sopenharmony_ci } 6681cb0ef41Sopenharmony_ci }; 6691cb0ef41Sopenharmony_ci } 6701cb0ef41Sopenharmony_ci ], 6711cb0ef41Sopenharmony_ci 6721cb0ef41Sopenharmony_ci [ 6731cb0ef41Sopenharmony_ci "i64 mutable WebAssembly.Global import", 6741cb0ef41Sopenharmony_ci function() { 6751cb0ef41Sopenharmony_ci const initial = 102n; 6761cb0ef41Sopenharmony_ci const value = new WebAssembly.Global({ "value": "i64", "mutable": true }, initial); 6771cb0ef41Sopenharmony_ci 6781cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_ci const index = builder.addImportedGlobal("module", "global", kWasmI64, true); 6811cb0ef41Sopenharmony_ci const fn = builder 6821cb0ef41Sopenharmony_ci .addFunction("fn", kSig_l_v) 6831cb0ef41Sopenharmony_ci .addBody([ 6841cb0ef41Sopenharmony_ci kExprGlobalGet, 6851cb0ef41Sopenharmony_ci index, 6861cb0ef41Sopenharmony_ci kExprReturn, 6871cb0ef41Sopenharmony_ci ]) 6881cb0ef41Sopenharmony_ci .exportFunc(); 6891cb0ef41Sopenharmony_ci 6901cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 6911cb0ef41Sopenharmony_ci 6921cb0ef41Sopenharmony_ci const imports = { 6931cb0ef41Sopenharmony_ci "module": { 6941cb0ef41Sopenharmony_ci "global": value, 6951cb0ef41Sopenharmony_ci }, 6961cb0ef41Sopenharmony_ci }; 6971cb0ef41Sopenharmony_ci 6981cb0ef41Sopenharmony_ci const exports = { 6991cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": String(fn.index), "length": 0 }, 7001cb0ef41Sopenharmony_ci }; 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci return { 7031cb0ef41Sopenharmony_ci buffer, 7041cb0ef41Sopenharmony_ci args: [imports], 7051cb0ef41Sopenharmony_ci exports, 7061cb0ef41Sopenharmony_ci verify: instance => { 7071cb0ef41Sopenharmony_ci assert_equals(instance.exports.fn(), initial); 7081cb0ef41Sopenharmony_ci const after = 201n; 7091cb0ef41Sopenharmony_ci value.value = after; 7101cb0ef41Sopenharmony_ci assert_equals(instance.exports.fn(), after); 7111cb0ef41Sopenharmony_ci } 7121cb0ef41Sopenharmony_ci }; 7131cb0ef41Sopenharmony_ci } 7141cb0ef41Sopenharmony_ci ], 7151cb0ef41Sopenharmony_ci 7161cb0ef41Sopenharmony_ci [ 7171cb0ef41Sopenharmony_ci "Multiple i64 arguments", 7181cb0ef41Sopenharmony_ci function() { 7191cb0ef41Sopenharmony_ci const builder = new WasmModuleBuilder(); 7201cb0ef41Sopenharmony_ci 7211cb0ef41Sopenharmony_ci const fn = builder 7221cb0ef41Sopenharmony_ci .addFunction("fn", kSig_l_ll) 7231cb0ef41Sopenharmony_ci .addBody([ 7241cb0ef41Sopenharmony_ci kExprLocalGet, 1, 7251cb0ef41Sopenharmony_ci ]) 7261cb0ef41Sopenharmony_ci .exportFunc(); 7271cb0ef41Sopenharmony_ci 7281cb0ef41Sopenharmony_ci const buffer = builder.toBuffer(); 7291cb0ef41Sopenharmony_ci 7301cb0ef41Sopenharmony_ci const exports = { 7311cb0ef41Sopenharmony_ci "fn": { "kind": "function", "name": String(fn.index), "length": 2 }, 7321cb0ef41Sopenharmony_ci }; 7331cb0ef41Sopenharmony_ci 7341cb0ef41Sopenharmony_ci return { 7351cb0ef41Sopenharmony_ci buffer, 7361cb0ef41Sopenharmony_ci args: [], 7371cb0ef41Sopenharmony_ci exports, 7381cb0ef41Sopenharmony_ci verify: instance => { 7391cb0ef41Sopenharmony_ci const fn = instance.exports.fn; 7401cb0ef41Sopenharmony_ci assert_equals(fn(1n, 0n), 0n); 7411cb0ef41Sopenharmony_ci assert_equals(fn(1n, 123n), 123n); 7421cb0ef41Sopenharmony_ci assert_equals(fn(1n, -123n), -123n); 7431cb0ef41Sopenharmony_ci assert_equals(fn(1n, "5"), 5n); 7441cb0ef41Sopenharmony_ci assert_throws_js(TypeError, () => fn(1n, 5)); 7451cb0ef41Sopenharmony_ci } 7461cb0ef41Sopenharmony_ci }; 7471cb0ef41Sopenharmony_ci } 7481cb0ef41Sopenharmony_ci ], 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ci [ 7511cb0ef41Sopenharmony_ci "stray argument", 7521cb0ef41Sopenharmony_ci function() { 7531cb0ef41Sopenharmony_ci return { 7541cb0ef41Sopenharmony_ci buffer: emptyModuleBinary, 7551cb0ef41Sopenharmony_ci args: [{}, {}], 7561cb0ef41Sopenharmony_ci exports: {}, 7571cb0ef41Sopenharmony_ci verify: () => {} 7581cb0ef41Sopenharmony_ci }; 7591cb0ef41Sopenharmony_ci } 7601cb0ef41Sopenharmony_ci ], 7611cb0ef41Sopenharmony_ci]; 762