Debian Codegraph @

js/src/jit-test/tests/wasm/gc/ref-global.js

raw
1 // Basic private-to-module functionality. At the moment all we have is null
2 // pointers, not very exciting.
3
4 {
5 let bin = wasmTextToBinary(
6 `(module
7 (type $point (struct
8 (field $x f64)
9 (field $y f64)))
10
11 (global $g1 (mut (ref null $point)) (ref.null $point))
12 (global $g2 (mut (ref null $point)) (ref.null $point))
13 (global $g3 (ref null $point) (ref.null $point))
14
15 ;; Restriction: cannot expose Refs outside the module, not even
16 ;; as a return value. See ref-restrict.js.
17
18 (func (export "get") (result eqref)
19 (global.get $g1))
20
21 (func (export "copy")
22 (global.set $g2 (global.get $g1)))
23
24 (func (export "clear")
25 (global.set $g1 (global.get $g3))
26 (global.set $g2 (ref.null $point))))`);
27
28 let mod = new WebAssembly.Module(bin);
29 let ins = new WebAssembly.Instance(mod).exports;
30
31 assertEq(ins.get(), null);
32 ins.copy(); // Should not crash
33 ins.clear(); // Should not crash
34 }
35
36 // Global with struct type
37
38 {
39 let bin = wasmTextToBinary(
40 `(module
41 (type $point (struct
42 (field $x f64)
43 (field $y f64)))
44
45 (global $glob (mut (ref null $point)) (ref.null $point))
46
47 (func (export "init")
48 (global.set $glob (struct.new $point (f64.const 0.5) (f64.const 2.75))))
49
50 (func (export "change")
51 (global.set $glob (struct.new $point (f64.const 3.5) (f64.const 37.25))))
52
53 (func (export "clear")
54 (global.set $glob (ref.null $point)))
55
56 (func (export "x") (result f64)
57 (struct.get $point 0 (global.get $glob)))
58
59 (func (export "y") (result f64)
60 (struct.get $point 1 (global.get $glob))))`);
61
62 let mod = new WebAssembly.Module(bin);
63 let ins = new WebAssembly.Instance(mod).exports;
64
65 assertErrorMessage(() => ins.x(), WebAssembly.RuntimeError, /dereferencing null pointer/);
66
67 ins.init();
68 assertEq(ins.x(), 0.5);
69 assertEq(ins.y(), 2.75);
70
71 ins.change();
72 assertEq(ins.x(), 3.5);
73 assertEq(ins.y(), 37.25);
74
75 ins.clear();
76 assertErrorMessage(() => ins.x(), WebAssembly.RuntimeError, /dereferencing null pointer/);
77 }
78
79 // Global value of type externref for initializer from a WebAssembly.Global,
80 // just check that it works.
81 {
82 let bin = wasmTextToBinary(
83 `(module
84 (import "" "g" (global $g externref))
85 (global $glob externref (global.get $g))
86 (func (export "get") (result externref)
87 (global.get $glob)))`);
88
89 let mod = new WebAssembly.Module(bin);
90 let obj = {zappa:37};
91 let g = new WebAssembly.Global({value: "externref"}, obj);
92 let ins = new WebAssembly.Instance(mod, {"":{g}}).exports;
93 assertEq(ins.get(), obj);
94 }