|
| 1 | +// META: global=window,dedicatedworker,jsshell,shadowrealm |
| 2 | +// META: script=/wasm/jsapi/wasm-module-builder.js |
| 3 | + |
| 4 | +// Instantiate a module with an imported global and return the global. |
| 5 | +function instantiateImportedGlobal(module, name, type, mutable, importedStringConstants) { |
| 6 | + let builder = new WasmModuleBuilder(); |
| 7 | + builder.addImportedGlobal(module, name, type, mutable); |
| 8 | + builder.addExportOfKind("global", kExternalGlobal, 0); |
| 9 | + let bytes = builder.toBuffer(); |
| 10 | + let mod = new WebAssembly.Module(bytes, { importedStringConstants }); |
| 11 | + let instance = new WebAssembly.Instance(mod, {}); |
| 12 | + return instance.exports["global"]; |
| 13 | +} |
| 14 | + |
| 15 | +const badGlobalTypes = [ |
| 16 | + [kWasmAnyRef, false], |
| 17 | + [kWasmAnyRef, true], |
| 18 | + [wasmRefType(kWasmAnyRef), false], |
| 19 | + [wasmRefType(kWasmAnyRef), true], |
| 20 | + [kWasmFuncRef, false], |
| 21 | + [kWasmFuncRef, true], |
| 22 | + [wasmRefType(kWasmFuncRef), false], |
| 23 | + [wasmRefType(kWasmFuncRef), true], |
| 24 | + [kWasmExternRef, true], |
| 25 | + [wasmRefType(kWasmExternRef), true], |
| 26 | +]; |
| 27 | +for ([type, mutable] of badGlobalTypes) { |
| 28 | + test(() => { |
| 29 | + assert_throws_js(WebAssembly.CompileError, |
| 30 | + () => instantiateImportedGlobal("'", "constant", type, mutable, "'"), |
| 31 | + "type mismatch"); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +const goodGlobalTypes = [ |
| 36 | + [kWasmExternRef, false], |
| 37 | + [wasmRefType(kWasmExternRef), false], |
| 38 | +]; |
| 39 | +const constants = [ |
| 40 | + '', |
| 41 | + '\0', |
| 42 | + '0', |
| 43 | + '0'.repeat(100000), |
| 44 | + '\uD83D\uDE00', |
| 45 | +]; |
| 46 | +const namespaces = [ |
| 47 | + "", |
| 48 | + "'", |
| 49 | + "strings" |
| 50 | +]; |
| 51 | + |
| 52 | +for (let namespace of namespaces) { |
| 53 | + for (let constant of constants) { |
| 54 | + for ([type, mutable] of goodGlobalTypes) { |
| 55 | + test(() => { |
| 56 | + let result = instantiateImportedGlobal(namespace, constant, type, mutable, namespace); |
| 57 | + assert_equals(result.value, constant); |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments