summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/gc/externref-conversions.js
blob: edfcbe9e4fa281cfa1761f770b3fc51c85e3571f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// |jit-test| skip-if: !wasmGcEnabled()

// Output type is non-nullable if the input type is non-nullable
wasmValidateText(`(module
	(func (param externref) (result anyref)
		local.get 0
		any.convert_extern
	)
	(func (param anyref) (result externref)
		local.get 0
		extern.convert_any
	)
	(func (param (ref extern)) (result (ref any))
		local.get 0
		any.convert_extern
	)
	(func (param (ref any)) (result (ref extern))
		local.get 0
		extern.convert_any
	)
	(func (result (ref any))
	    unreachable
	    any.convert_extern
	)
	(func (result (ref extern))
	    unreachable
	    extern.convert_any
	)
)`);

// Output type is nullable if the input type is nullable
wasmFailValidateText(`(module
	(func (param externref) (result (ref any))
		local.get 0
		any.convert_extern
	)
)`, /expected/);
wasmFailValidateText(`(module
	(func (param anyref) (result (ref extern))
		local.get 0
		extern.convert_any
	)
)`, /expected/);

// Can round trip an externref through anyref and get the same thing back
let {roundtripThroughAny} = wasmEvalText(`(module
	(func (export "roundtripThroughAny") (param externref) (result externref)
	  local.get 0
	  any.convert_extern
	  extern.convert_any
	)
)`).exports;
for (let value of WasmExternrefValues) {
	assertEq(value, roundtripThroughAny(value));
}

// Can round trip GC objects through externref and get the same thing back
let {testStruct, testArray} = wasmEvalText(`(module
	(type $struct (struct))
	(type $array (array i32))

	(func (export "testStruct") (result i32)
	  (local (ref $struct))
	  (local.set 0 struct.new $struct)
      local.get 0
	  (ref.eq
	  	(ref.cast (ref null $struct)
	  	  (any.convert_extern
	  	    (extern.convert_any
	  	      local.get 0
	  	    )
	  	  )
	  	)
	  )
	)

	(func (export "testArray") (result i32)
	  (local (ref $array))
	  (local.set 0 (array.new $array i32.const 0 i32.const 0))
      local.get 0
	  (ref.eq
	  	(ref.cast (ref null $array)
	  	  (any.convert_extern
	  	    (extern.convert_any
	  	      local.get 0
	  	    )
	  	  )
	  	)
	  )
	)
)`).exports;

assertEq(testStruct(), 1);
assertEq(testArray(), 1);