Skip to content

Commit e1bbe79

Browse files
committed
Add more tests
1 parent 1937e00 commit e1bbe79

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

Diff for: test/js-api/js-string/constants.tentative.any.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

Diff for: test/js-api/js-string/imports.tentative.any.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// META: global=window,dedicatedworker,jsshell,shadowrealm
2+
// META: script=/wasm/jsapi/wasm-module-builder.js
3+
4+
test(() => {
5+
let builder = new WasmModuleBuilder();
6+
7+
// Import a string constant
8+
builder.addImportedGlobal("constants", "constant", kWasmExternRef, false);
9+
10+
// Import a builtin function
11+
builder.addImport(
12+
"wasm:js-string",
13+
"test",
14+
{params: [kWasmExternRef], results: [kWasmI32]});
15+
16+
let buffer = builder.toBuffer();
17+
let module = new WebAssembly.Module(buffer, {
18+
builtins: ["js-string"],
19+
importedStringConstants: "constants"
20+
});
21+
let imports = WebAssembly.Module.imports(module);
22+
23+
// All imports that refer to a builtin module are suppressed from import
24+
// reflection.
25+
assert_equals(imports.length, 0);
26+
});

0 commit comments

Comments
 (0)