11cb0ef41Sopenharmony_ci/** 21cb0ef41Sopenharmony_ci * `t` should be a function that takes at least three arguments: 31cb0ef41Sopenharmony_ci * 41cb0ef41Sopenharmony_ci * - the name of the test; 51cb0ef41Sopenharmony_ci * - the expected error (to be passed to `assert_throws_js`); 61cb0ef41Sopenharmony_ci * - a function that takes a `WasmModuleBuilder` and initializes it; 71cb0ef41Sopenharmony_ci * - (optionally) an options object. 81cb0ef41Sopenharmony_ci * 91cb0ef41Sopenharmony_ci * The function is expected to create a test that checks if instantiating a 101cb0ef41Sopenharmony_ci * module with the result of the `WasmModuleBuilder` and the options object 111cb0ef41Sopenharmony_ci * (if any) yields the correct error. 121cb0ef41Sopenharmony_ci */ 131cb0ef41Sopenharmony_cifunction test_bad_imports(t) { 141cb0ef41Sopenharmony_ci function value_type(type) { 151cb0ef41Sopenharmony_ci switch (type) { 161cb0ef41Sopenharmony_ci case "i32": return kWasmI32; 171cb0ef41Sopenharmony_ci case "i64": return kWasmI64; 181cb0ef41Sopenharmony_ci case "f32": return kWasmF32; 191cb0ef41Sopenharmony_ci case "f64": return kWasmF64; 201cb0ef41Sopenharmony_ci default: throw new TypeError(`Unexpected type ${type}`); 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci for (const value of [null, true, "", Symbol(), 1, 0.1, NaN]) { 251cb0ef41Sopenharmony_ci t(`Non-object imports argument: ${format_value(value)}`, 261cb0ef41Sopenharmony_ci TypeError, 271cb0ef41Sopenharmony_ci builder => {}, 281cb0ef41Sopenharmony_ci value); 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci for (const value of [undefined, null, true, "", Symbol(), 1, 0.1, NaN]) { 321cb0ef41Sopenharmony_ci const imports = { 331cb0ef41Sopenharmony_ci "module": value, 341cb0ef41Sopenharmony_ci }; 351cb0ef41Sopenharmony_ci t(`Non-object module: ${format_value(value)}`, 361cb0ef41Sopenharmony_ci TypeError, 371cb0ef41Sopenharmony_ci builder => { 381cb0ef41Sopenharmony_ci builder.addImport("module", "fn", kSig_v_v); 391cb0ef41Sopenharmony_ci }, 401cb0ef41Sopenharmony_ci imports); 411cb0ef41Sopenharmony_ci } 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci t(`Missing imports argument`, 441cb0ef41Sopenharmony_ci TypeError, 451cb0ef41Sopenharmony_ci builder => { 461cb0ef41Sopenharmony_ci builder.addImport("module", "fn", kSig_v_v); 471cb0ef41Sopenharmony_ci }); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci for (const [value, name] of [[undefined, "undefined"], [{}, "empty object"], [{ "module\0": null }, "wrong property"]]) { 501cb0ef41Sopenharmony_ci t(`Imports argument with missing property: ${name}`, 511cb0ef41Sopenharmony_ci TypeError, 521cb0ef41Sopenharmony_ci builder => { 531cb0ef41Sopenharmony_ci builder.addImport("module", "fn", kSig_v_v); 541cb0ef41Sopenharmony_ci }, 551cb0ef41Sopenharmony_ci value); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci for (const value of [undefined, null, true, "", Symbol(), 1, 0.1, NaN, {}]) { 591cb0ef41Sopenharmony_ci t(`Importing a function with an incorrectly-typed value: ${format_value(value)}`, 601cb0ef41Sopenharmony_ci WebAssembly.LinkError, 611cb0ef41Sopenharmony_ci builder => { 621cb0ef41Sopenharmony_ci builder.addImport("module", "fn", kSig_v_v); 631cb0ef41Sopenharmony_ci }, 641cb0ef41Sopenharmony_ci { 651cb0ef41Sopenharmony_ci "module": { 661cb0ef41Sopenharmony_ci "fn": value, 671cb0ef41Sopenharmony_ci }, 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci const nonGlobals = [ 721cb0ef41Sopenharmony_ci [undefined], 731cb0ef41Sopenharmony_ci [null], 741cb0ef41Sopenharmony_ci [true], 751cb0ef41Sopenharmony_ci [""], 761cb0ef41Sopenharmony_ci [Symbol()], 771cb0ef41Sopenharmony_ci [{}, "plain object"], 781cb0ef41Sopenharmony_ci [WebAssembly.Global, "WebAssembly.Global"], 791cb0ef41Sopenharmony_ci [WebAssembly.Global.prototype, "WebAssembly.Global.prototype"], 801cb0ef41Sopenharmony_ci [Object.create(WebAssembly.Global.prototype), "Object.create(WebAssembly.Global.prototype)"], 811cb0ef41Sopenharmony_ci ]; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci for (const type of ["i32", "i64", "f32", "f64"]) { 841cb0ef41Sopenharmony_ci const extendedNonGlobals = nonGlobals.concat([ 851cb0ef41Sopenharmony_ci type === "i64" ? [0, "Number"] : [0n, "BigInt"], 861cb0ef41Sopenharmony_ci [new WebAssembly.Global({value: type === "f32" ? "f64" : "f32"}), "WebAssembly.Global object (wrong value type)"], 871cb0ef41Sopenharmony_ci ]); 881cb0ef41Sopenharmony_ci for (const [value, name = format_value(value)] of extendedNonGlobals) { 891cb0ef41Sopenharmony_ci t(`Importing an ${type} global with an incorrectly-typed value: ${name}`, 901cb0ef41Sopenharmony_ci WebAssembly.LinkError, 911cb0ef41Sopenharmony_ci builder => { 921cb0ef41Sopenharmony_ci builder.addImportedGlobal("module", "global", value_type(type)); 931cb0ef41Sopenharmony_ci }, 941cb0ef41Sopenharmony_ci { 951cb0ef41Sopenharmony_ci "module": { 961cb0ef41Sopenharmony_ci "global": value, 971cb0ef41Sopenharmony_ci }, 981cb0ef41Sopenharmony_ci }); 991cb0ef41Sopenharmony_ci } 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci for (const type of ["i32", "i64", "f32", "f64"]) { 1031cb0ef41Sopenharmony_ci const value = type === "i64" ? 0n : 0; 1041cb0ef41Sopenharmony_ci t(`Importing an ${type} mutable global with a primitive value`, 1051cb0ef41Sopenharmony_ci WebAssembly.LinkError, 1061cb0ef41Sopenharmony_ci builder => { 1071cb0ef41Sopenharmony_ci builder.addImportedGlobal("module", "global", value_type(type), true); 1081cb0ef41Sopenharmony_ci }, 1091cb0ef41Sopenharmony_ci { 1101cb0ef41Sopenharmony_ci "module": { 1111cb0ef41Sopenharmony_ci "global": value, 1121cb0ef41Sopenharmony_ci }, 1131cb0ef41Sopenharmony_ci }); 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci const global = new WebAssembly.Global({ "value": type }, value); 1161cb0ef41Sopenharmony_ci t(`Importing an ${type} mutable global with an immutable Global object`, 1171cb0ef41Sopenharmony_ci WebAssembly.LinkError, 1181cb0ef41Sopenharmony_ci builder => { 1191cb0ef41Sopenharmony_ci builder.addImportedGlobal("module", "global", value_type(type), true); 1201cb0ef41Sopenharmony_ci }, 1211cb0ef41Sopenharmony_ci { 1221cb0ef41Sopenharmony_ci "module": { 1231cb0ef41Sopenharmony_ci "global": global, 1241cb0ef41Sopenharmony_ci }, 1251cb0ef41Sopenharmony_ci }); 1261cb0ef41Sopenharmony_ci } 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ci const nonMemories = [ 1291cb0ef41Sopenharmony_ci [undefined], 1301cb0ef41Sopenharmony_ci [null], 1311cb0ef41Sopenharmony_ci [true], 1321cb0ef41Sopenharmony_ci [""], 1331cb0ef41Sopenharmony_ci [Symbol()], 1341cb0ef41Sopenharmony_ci [1], 1351cb0ef41Sopenharmony_ci [0.1], 1361cb0ef41Sopenharmony_ci [NaN], 1371cb0ef41Sopenharmony_ci [{}, "plain object"], 1381cb0ef41Sopenharmony_ci [WebAssembly.Memory, "WebAssembly.Memory"], 1391cb0ef41Sopenharmony_ci [WebAssembly.Memory.prototype, "WebAssembly.Memory.prototype"], 1401cb0ef41Sopenharmony_ci [Object.create(WebAssembly.Memory.prototype), "Object.create(WebAssembly.Memory.prototype)"], 1411cb0ef41Sopenharmony_ci [new WebAssembly.Memory({"initial": 256}), "WebAssembly.Memory object (too large)"], 1421cb0ef41Sopenharmony_ci ]; 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci for (const [value, name = format_value(value)] of nonMemories) { 1451cb0ef41Sopenharmony_ci t(`Importing memory with an incorrectly-typed value: ${name}`, 1461cb0ef41Sopenharmony_ci WebAssembly.LinkError, 1471cb0ef41Sopenharmony_ci builder => { 1481cb0ef41Sopenharmony_ci builder.addImportedMemory("module", "memory", 0, 128); 1491cb0ef41Sopenharmony_ci }, 1501cb0ef41Sopenharmony_ci { 1511cb0ef41Sopenharmony_ci "module": { 1521cb0ef41Sopenharmony_ci "memory": value, 1531cb0ef41Sopenharmony_ci }, 1541cb0ef41Sopenharmony_ci }); 1551cb0ef41Sopenharmony_ci } 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci const nonTables = [ 1581cb0ef41Sopenharmony_ci [undefined], 1591cb0ef41Sopenharmony_ci [null], 1601cb0ef41Sopenharmony_ci [true], 1611cb0ef41Sopenharmony_ci [""], 1621cb0ef41Sopenharmony_ci [Symbol()], 1631cb0ef41Sopenharmony_ci [1], 1641cb0ef41Sopenharmony_ci [0.1], 1651cb0ef41Sopenharmony_ci [NaN], 1661cb0ef41Sopenharmony_ci [{}, "plain object"], 1671cb0ef41Sopenharmony_ci [WebAssembly.Table, "WebAssembly.Table"], 1681cb0ef41Sopenharmony_ci [WebAssembly.Table.prototype, "WebAssembly.Table.prototype"], 1691cb0ef41Sopenharmony_ci [Object.create(WebAssembly.Table.prototype), "Object.create(WebAssembly.Table.prototype)"], 1701cb0ef41Sopenharmony_ci [new WebAssembly.Table({"element": "anyfunc", "initial": 256}), "WebAssembly.Table object (too large)"], 1711cb0ef41Sopenharmony_ci ]; 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci for (const [value, name = format_value(value)] of nonTables) { 1741cb0ef41Sopenharmony_ci t(`Importing table with an incorrectly-typed value: ${name}`, 1751cb0ef41Sopenharmony_ci WebAssembly.LinkError, 1761cb0ef41Sopenharmony_ci builder => { 1771cb0ef41Sopenharmony_ci builder.addImportedTable("module", "table", 0, 128); 1781cb0ef41Sopenharmony_ci }, 1791cb0ef41Sopenharmony_ci { 1801cb0ef41Sopenharmony_ci "module": { 1811cb0ef41Sopenharmony_ci "table": value, 1821cb0ef41Sopenharmony_ci }, 1831cb0ef41Sopenharmony_ci }); 1841cb0ef41Sopenharmony_ci } 1851cb0ef41Sopenharmony_ci} 186