events.js (3245B)
1 // Tests for tag section support 2 3 load(libdir + "wasm-binary.js"); 4 5 function wasmEval(code, imports) { 6 new WebAssembly.Instance(new WebAssembly.Module(code), imports).exports; 7 } 8 9 function wasmError(code, errorType, regexp) { 10 assertErrorMessage(() => wasmEval(code, {}), errorType, regexp); 11 } 12 13 const emptyType = { args: [], ret: VoidCode }; 14 const badExnType = { args: [], ret: I32Code }; 15 16 wasmEvalText(` 17 (module 18 (type (func (param i32))) 19 (tag $exn (type 0))) 20 `); 21 22 wasmError( 23 moduleWithSections([ 24 sigSection([emptyType]), 25 memorySection(0), 26 { name: tagId, body: [] }, 27 ]), 28 WebAssembly.CompileError, 29 /expected number of tags/ 30 ); 31 32 wasmError( 33 moduleWithSections([ 34 sigSection([emptyType]), 35 memorySection(0), 36 { name: tagId, body: [1, 1] }, 37 ]), 38 WebAssembly.CompileError, 39 /illegal tag kind/ 40 ); 41 42 wasmError( 43 moduleWithSections([ 44 sigSection([emptyType]), 45 memorySection(0), 46 { name: tagId, body: [1, 0] }, 47 ]), 48 WebAssembly.CompileError, 49 /expected function index in tag/ 50 ); 51 52 wasmEval( 53 moduleWithSections([ 54 sigSection([emptyType]), 55 memorySection(0), 56 tagSection([{ type: 0 }]), 57 ]) 58 ); 59 60 wasmError( 61 moduleWithSections([ 62 sigSection([badExnType]), 63 memorySection(0), 64 tagSection([{ type: 0 }]), 65 ]), 66 WebAssembly.CompileError, 67 /tag function types must not return anything/ 68 ); 69 70 wasmError( 71 moduleWithSections([ 72 sigSection([emptyType]), 73 memorySection(0), 74 tagSection([{ type: 1 }]), 75 ]), 76 WebAssembly.CompileError, 77 /function type index in tag out of bounds/ 78 ); 79 80 wasmError( 81 moduleWithSections([ 82 sigSection([emptyType]), 83 tagSection([{ type: 0 }]), 84 memorySection(0), 85 ]), 86 WebAssembly.CompileError, 87 /expected custom section/ 88 ); 89 90 (() => { 91 const body = [1]; 92 body.push(...string("mod")); 93 body.push(...string("exn")); 94 body.push(...varU32(ExternTagCode)); 95 96 wasmError( 97 moduleWithSections([ 98 sigSection([emptyType]), 99 { name: importId, body: body }, 100 ]), 101 WebAssembly.CompileError, 102 /expected tag kind/ 103 ); 104 105 body.push(...varU32(0)); 106 wasmError( 107 moduleWithSections([ 108 sigSection([emptyType]), 109 { name: importId, body: body }, 110 ]), 111 WebAssembly.CompileError, 112 /expected function index in tag/ 113 ); 114 115 body.push(...varU32(1)); 116 wasmError( 117 moduleWithSections([ 118 sigSection([emptyType]), 119 { name: importId, body: body }, 120 ]), 121 WebAssembly.CompileError, 122 /function type index in tag out of bounds/ 123 ); 124 })(); 125 126 wasmEval( 127 moduleWithSections([ 128 sigSection([emptyType]), 129 memorySection(0), 130 tagSection([{ type: 0 }]), 131 exportSection([{ tagIndex: 0, name: "exn" }]), 132 ]) 133 ); 134 135 wasmError( 136 moduleWithSections([ 137 sigSection([emptyType]), 138 memorySection(0), 139 tagSection([{ type: 0 }]), 140 exportSection([{ tagIndex: 1, name: "exn" }]), 141 ]), 142 WebAssembly.CompileError, 143 /exported tag index out of bounds/ 144 ); 145 146 (() => { 147 const body = [1]; 148 body.push(...string("exn")); 149 body.push(...varU32(ExternTagCode)); 150 wasmError( 151 moduleWithSections([ 152 sigSection([emptyType]), 153 memorySection(0), 154 tagSection([{ type: 0 }]), 155 { name: exportId, body: body }, 156 ]), 157 WebAssembly.CompileError, 158 /expected tag index/ 159 ); 160 })();