Debian Codegraph @

js/src/jit-test/tests/wasm/bigint/stubs.js

raw
1 // This is similar to the corresponding JIT exit stub test in the
2 // wasm/import-export.js file, but it also tests BigInt cases.
3 (function testImportJitExit() {
4 let options = getJitCompilerOptions();
5 if (!options["baseline.enable"]) return;
6
7 let baselineTrigger = options["baseline.warmup.trigger"];
8
9 let valueToConvert = 0;
10 function ffi(n) {
11 if (n == 1337n) {
12 return BigInt(valueToConvert);
13 }
14 return 42n;
15 }
16
17 // This case is intended to test that the I64 to BigInt argument
18 // filling for the JIT exit stub does not interfere with the other
19 // argument registers. It's important that the BigInt argument comes
20 // first (to test that it doesn't clobber the subsequent ones).
21 function ffi2(n, x1, x2, x3, x4, x5, x6) {
22 return (
23 42n +
24 BigInt(x1) +
25 BigInt(x2) +
26 BigInt(x3) +
27 BigInt(x4) +
28 BigInt(x5) +
29 BigInt(x6)
30 );
31 }
32
33 // This case is for testing issues with potential GC.
34 function ffi3(n1, n2, n3, n4) {
35 return n3;
36 }
37
38 // Baseline compile ffis.
39 for (let i = baselineTrigger + 1; i-- > 0; ) {
40 ffi(BigInt(i));
41 ffi2(BigInt(i), i, i, i, i, i, i);
42 ffi3(BigInt(i), BigInt(i), BigInt(i), BigInt(i));
43 }
44
45 let imports = {
46 a: {
47 ffi,
48 ffi2,
49 ffi3,
50 },
51 };
52
53 ex = wasmEvalText(
54 `(module
55 (import "a" "ffi" (func $ffi (param i64) (result i64)))
56 (import "a" "ffi2" (func $ffi2
57 (param i64 i32 i32 i32 i32 i32 i32)
58 (result i64)))
59 (import "a" "ffi3" (func $ffi3 (param i64 i64 i64 i64) (result i64)))
60
61 (func (export "callffi") (param i64) (result i64)
62 local.get 0
63 call $ffi
64 )
65
66 (func (export "callffi2") (param i32 i64) (result i64)
67 local.get 1
68 local.get 0
69 local.get 0
70 local.get 0
71 local.get 0
72 local.get 0
73 local.get 0
74 call $ffi2
75 )
76
77 (func (export "callffi3") (param i64 i64 i64 i64) (result i64)
78 local.get 0
79 local.get 1
80 local.get 2
81 local.get 3
82 call $ffi3
83 )
84 )`,
85 imports
86 ).exports;
87
88 // Enable the jit exit for each JS callee.
89 assertEq(ex.callffi(0n), 42n);
90 assertEq(ex.callffi2(2, 0n), 54n);
91 assertEq(ex.callffi3(0n, 1n, 2n, 3n), 2n);
92
93 // Test the jit exit under normal conditions.
94 assertEq(ex.callffi(0n), 42n);
95 assertEq(ex.callffi(1337n), 0n);
96 assertEq(ex.callffi2(2, 0n), 54n);
97 assertEq(ex.callffi3(0n, 1n, 2n, 3n), 2n);
98
99 // Test the jit exit with GC stress in order to ensure that
100 // any trigger of GC in the stub do not cause errors.
101 if (this.gczeal) {
102 this.gczeal(2, 1); // Collect on every allocation
103 }
104 for (let i = 0; i < 1000; i++) {
105 assertEq(ex.callffi3(0n, 1n, 2n, 3n), 2n);
106 }
107 })();
108
109 // Test JIT entry stub
110 (function testJitEntry() {
111 let options = getJitCompilerOptions();
112 if (!options["baseline.enable"]) return;
113
114 let baselineTrigger = options["baseline.warmup.trigger"];
115
116 i = wasmEvalText(
117 `(module
118 (func (export "foo") (param i64) (result i64)
119 local.get 0
120 i64.const 1
121 i64.add
122 return
123 )
124 )`,
125 {}
126 ).exports;
127
128 function caller(n) {
129 return i.foo(42n);
130 }
131
132 // Baseline compile ffis.
133 for (let i = baselineTrigger + 1; i-- > 0; ) {
134 caller(i);
135 }
136
137 // Enable the jit entry.
138 assertEq(caller(0), 43n);
139
140 // Test the jit exit under normal conditions.
141 assertEq(caller(0), 43n);
142 })();