externref-conversions.js (2206B)
1 // Output type is non-nullable if the input type is non-nullable 2 wasmValidateText(`(module 3 (func (param externref) (result anyref) 4 local.get 0 5 any.convert_extern 6 ) 7 (func (param anyref) (result externref) 8 local.get 0 9 extern.convert_any 10 ) 11 (func (param (ref extern)) (result (ref any)) 12 local.get 0 13 any.convert_extern 14 ) 15 (func (param (ref any)) (result (ref extern)) 16 local.get 0 17 extern.convert_any 18 ) 19 (func (result (ref any)) 20 unreachable 21 any.convert_extern 22 ) 23 (func (result (ref extern)) 24 unreachable 25 extern.convert_any 26 ) 27 )`); 28 29 // Output type is nullable if the input type is nullable 30 wasmFailValidateText(`(module 31 (func (param externref) (result (ref any)) 32 local.get 0 33 any.convert_extern 34 ) 35 )`, /expected/); 36 wasmFailValidateText(`(module 37 (func (param anyref) (result (ref extern)) 38 local.get 0 39 extern.convert_any 40 ) 41 )`, /expected/); 42 43 // Can round trip an externref through anyref and get the same thing back 44 let {roundtripThroughAny} = wasmEvalText(`(module 45 (func (export "roundtripThroughAny") (param externref) (result externref) 46 local.get 0 47 any.convert_extern 48 extern.convert_any 49 ) 50 )`).exports; 51 for (let value of WasmExternrefValues) { 52 assertEq(value, roundtripThroughAny(value)); 53 } 54 55 assertEq(MaxI31refValue*100, roundtripThroughAny(MaxI31refValue*100)); 56 assertEq(MinI31refValue*100, roundtripThroughAny(MinI31refValue*100)); 57 58 // Can round trip GC objects through externref and get the same thing back 59 let {testStruct, testArray} = wasmEvalText(`(module 60 (type $struct (struct)) 61 (type $array (array i32)) 62 63 (func (export "testStruct") (result i32) 64 (local (ref $struct)) 65 (local.set 0 struct.new $struct) 66 local.get 0 67 (ref.eq 68 (ref.cast (ref null $struct) 69 (any.convert_extern 70 (extern.convert_any 71 local.get 0 72 ) 73 ) 74 ) 75 ) 76 ) 77 78 (func (export "testArray") (result i32) 79 (local (ref $array)) 80 (local.set 0 (array.new $array i32.const 0 i32.const 0)) 81 local.get 0 82 (ref.eq 83 (ref.cast (ref null $array) 84 (any.convert_extern 85 (extern.convert_any 86 local.get 0 87 ) 88 ) 89 ) 90 ) 91 ) 92 )`).exports; 93 94 assertEq(testStruct(), 1); 95 assertEq(testArray(), 1);