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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
let { exports } = wasmEvalText(`(module
(func (export "i32") (param i32) (result i32)
local.get 0
)
(func (export "f32") (param f32) (result f32)
local.get 0
)
(func (export "f64") (param f64) (result f64)
local.get 0
)
(func (export "mixed_args")
(param i32) (param i32) (param i32) (param i32) (param i32) ;; 5 i32
(param $f64 f64) ;; 1 f64
(param i32)
(result f64)
local.get $f64
)
)`);
const options = getJitCompilerOptions();
const jitThreshold = options['ion.warmup.trigger'] * 2 + 2;
let coercions = {
i32(x) { return x|0; },
f32(x) { return Math.fround(x); },
f64(x) { return +x; }
}
function call(func, coercion, arg) {
let expected;
try {
expected = coercion(arg);
} catch(e) {
expected = e.message;
}
for (var i = jitThreshold; i --> 0;) {
try {
assertEq(func(arg), expected);
} catch(e) {
assertEq(e.message, expected);
}
}
}
// Test misc kinds of arguments.
(function() {
const inputs = [
42,
3.5,
-0,
-Infinity,
2**32,
true,
Symbol(),
undefined,
null,
{},
{ valueOf() { return 13.37; } },
"bonjour"
];
for (let arg of inputs) {
for (let func of ['i32', 'f32', 'f64']) {
call(exports[func], coercions[func], arg);
}
}
})();
// Test mixup of float and int arguments.
(function() {
for (let i = 0; i < 10; i++) {
assertEq(exports.mixed_args(i, i+1, i+2, i+3, i+4, i+0.5, i+5), i+0.5);
}
})();
// Test high number of arguments.
// All integers.
let {func} = wasmEvalText(`(module
(func (export "func")
${Array(32).join('(param i32)')}
(param $last i32)
(result i32)
local.get $last
)
)`).exports;
(function() {
for (let i = 0; i < 10; i++) {
assertEq(func(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9, i+10, i+11, i+12, i+13, i+14, i+15,
i+16, i+17, i+18, i+19, i+20, i+21, i+22, i+23, i+24, i+25, i+26, i+27, i+28, i+29, i+30, i+31
), i+31);
}
})();
// All floats.
func = wasmEvalText(`(module
(func (export "func")
${Array(32).join('(param f64)')}
(param $last i32)
(result i32)
local.get $last
)
)`).exports.func;
(function() {
for (let i = 0; i < 10; i++) {
assertEq(func(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9, i+10, i+11, i+12, i+13, i+14, i+15,
i+16, i+17, i+18, i+19, i+20, i+21, i+22, i+23, i+24, i+25, i+26, i+27, i+28, i+29, i+30, i+31
), i+31);
}
})();
// Mix em up! 1 i32, then 1 f32, then 1 f64, and again up to 32 args.
let params = [];
for (let i = 0; i < 32; i++) {
params.push((i % 3 == 0) ? 'i32' :
(i % 3 == 1) ? 'f32' :
'f64'
);
}
func = wasmEvalText(`(module
(func (export "func")
${Array(32).join('(param f64)')}
(param $last i32)
(result i32)
local.get $last
)
)`).exports.func;
(function() {
for (let i = 0; i < 10; i++) {
assertEq(func(i, i+1, i+2, i+3, i+4, i+5, i+6, i+7, i+8, i+9, i+10, i+11, i+12, i+13, i+14, i+15,
i+16, i+17, i+18, i+19, i+20, i+21, i+22, i+23, i+24, i+25, i+26, i+27, i+28, i+29, i+30, i+31
), i+31);
}
})();
|