defaultable.js (1493B)
1 let defaultableTypes = [ 2 ['i32', 'i32.const 0 i32.eq'], 3 ['i64', 'i64.const 0 i64.eq'], 4 ['f32', 'f32.const 0 f32.eq'], 5 ['f64', 'f64.const 0 f64.eq'], 6 ['anyref', 'ref.is_null'], 7 ['funcref', 'ref.is_null'], 8 ['externref', 'ref.is_null'], 9 ['(ref null $type)', 'ref.is_null'], 10 ]; 11 if (wasmSimdEnabled()) { 12 defaultableTypes.push(['v128', 'v128.const i64x2 0 0 i8x16.eq v128.any_true']); 13 } 14 15 for (let [type, isDefault] of defaultableTypes) { 16 let {testStruct, testArray} = wasmEvalText(`(module 17 (type $type (struct)) 18 (type $struct (struct (field ${type}))) 19 (type $array (array ${type})) 20 21 (func (export "testStruct") (result i32) 22 (struct.get $struct 0 23 struct.new_default $struct) 24 ${isDefault} 25 ) 26 (func (export "testArray") (result i32) 27 (array.get $array 28 (array.new_default $array i32.const 1) 29 i32.const 0 30 ) 31 ${isDefault} 32 ) 33 )`).exports; 34 35 assertEq(testStruct(), 1); 36 assertEq(testArray(), 1); 37 } 38 39 let nonDefaultableTypes = ['(ref any)', '(ref func)', '(ref extern)', '(ref $type)']; 40 for (let type of nonDefaultableTypes) { 41 wasmFailValidateText(`(module 42 (type $type (struct)) 43 (type $struct (struct (field ${type}))) 44 (func 45 struct.new_default $struct 46 drop 47 ) 48 )`, /defaultable/); 49 wasmFailValidateText(`(module 50 (type $type (struct)) 51 (type $array (array ${type})) 52 (func 53 i32.const 1 54 array.new_default $array 55 drop 56 ) 57 )`, /defaultable/); 58 }