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