Debian Codegraph @

js/src/jit-test/tests/wasm/builtin-modules/js-string/constants.js

raw
1 // |jit-test| skip-if: !wasmJSStringBuiltinsEnabled();
2
3 function wasmEvalWithConstants(text, namespace) {
4 return wasmEvalText(text, {}, { importedStringConstants: namespace }).exports;
5 }
6
7 // Type is not anyref
8 assertErrorMessage(() => wasmEvalWithConstants(`(module
9 (global (import "'" "") anyref)
10 )`, "'"), WebAssembly.CompileError, /type mismatch/);
11
12 // Type must be immutable (ref extern) or subtypes
13 assertErrorMessage(() => wasmEvalWithConstants(`(module
14 (global (import "'" "") (mut externref))
15 )`, "'"), WebAssembly.CompileError, /type mismatch/);
16 assertErrorMessage(() => wasmEvalWithConstants(`(module
17 (global (import "'" "") (mut (ref extern)))
18 )`, "'"), WebAssembly.CompileError, /type mismatch/);
19
20 function testString(type, literal, namespace) {
21 return wasmEvalWithConstants(`(module
22 (global (import "${namespace}" "${literal}") ${type})
23 (export "constant" (global 0))
24 )`, namespace).constant.value;
25 }
26
27 let tests = [
28 '',
29 ['\\00', '\0'],
30 '0',
31 '0'.repeat(100000),
32 '\uD83D\uDE00',
33 ];
34 let namespaces = [
35 "",
36 "'",
37 "strings"
38 ];
39
40 for (let namespace of namespaces) {
41 for (let type of ['externref', '(ref extern)']) {
42 for (let test of tests) {
43 let input;
44 let expected;
45 if (Array.isArray(test)) {
46 input = test[0];
47 expected = test[1];
48 } else {
49 input = test;
50 expected = test;
51 }
52 assertEq(testString(type, input, namespace), expected);
53 }
54 }
55 }