binary.js (2087B)
1 load(libdir + "wasm-binary.js"); 2 3 const v2vSig = {args:[], ret:VoidCode}; 4 const v2vSigSection = sigSection([v2vSig]); 5 6 function checkInvalid(binary, errorMessage) { 7 assertErrorMessage(() => new WebAssembly.Module(binary), 8 WebAssembly.CompileError, 9 errorMessage); 10 } 11 12 // The immediate of ref.null is a heap type, not a general reference type 13 14 const invalidRefNullHeapBody = moduleWithSections([ 15 v2vSigSection, 16 declSection([0]), 17 bodySection([ 18 funcBody({locals:[], body:[ 19 RefNullCode, 20 OptRefCode, 21 FuncRefCode, 22 DropCode, 23 ]}) 24 ]) 25 ]); 26 checkInvalid(invalidRefNullHeapBody, /invalid heap type/); 27 28 const invalidRefNullHeapElem = moduleWithSections([ 29 generalElemSection([ 30 { 31 flag: PassiveElemExpr, 32 typeCode: FuncRefCode, 33 elems: [ 34 [RefNullCode, OptRefCode, FuncRefCode, EndCode] 35 ] 36 } 37 ]) 38 ]); 39 checkInvalid(invalidRefNullHeapElem, /invalid heap type/); 40 41 const invalidRefNullHeapGlobal = moduleWithSections([ 42 globalSection([ 43 { 44 valType: FuncRefCode, 45 flag: 0, 46 initExpr: [RefNullCode, OptRefCode, FuncRefCode, EndCode] 47 } 48 ]) 49 ]); 50 checkInvalid(invalidRefNullHeapGlobal, /invalid heap type/); 51 52 // Test the encoding edge case where an init expression is provided for an 53 // imported table using the same byte pattern as the table section. 54 const invalidImportedTableInit = moduleWithSections([ 55 importSection([ 56 { 57 module: "", item: "", 58 // Hand-encode a table type with an init expression, which should 59 // only appear in the table section proper: 60 // https://wasm-dsl.github.io/spectec/core/binary/modules.html#table-section 61 tableType: [ 62 0x40, 0x00, ...tableType(FuncRefCode, limits({ min: 0 })), 63 RefFuncCode, ...varS32(123), EndCode, 64 ], 65 }, 66 ]), 67 ]); 68 checkInvalid(invalidImportedTableInit, /imported tables cannot have initializer expressions/);