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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
var ITERATIONS = 10;
var INNER_ITERATIONS = 100;
let instance = wasmEvalText(`(module
(func (export "add") (param i32) (param i32) (result i32)
local.get 0
local.get 1
i32.add
)
(func (export "no_arg") (result i32)
i32.const 42
i32.const 58
i32.add
)
(global $g (mut i32) (i32.const 0))
(func (export "set_global_one") (param i32)
local.get 0
global.set $g
)
(func (export "set_global_two") (param i32) (param i32)
local.get 0
local.get 1
i32.add
global.set $g
)
(func (export "glob") (result i32)
global.get $g
)
)`).exports;
function run(name, func) {
for (let i = ITERATIONS; i --> 0;) {
func();
}
}
function testCallKnown() {
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(instance.add(i, i + 1), 2*i + 1);
}
}
function testCallKnownRectifying() {
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(instance.add(i + 1), i+1);
}
}
function jsAdd(x, y) {
return (x|0) + (y|0) | 0;
}
function testCallGeneric() {
var arr = [instance.add, jsAdd];
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(arr[i%2](i, i+1), 2*i + 1);
}
}
function testCallGenericRectifying() {
var arr = [instance.add, jsAdd];
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(arr[i%2](i+1), i + 1);
}
}
function testCallScriptedGetter() {
var obj = {};
Object.defineProperty(obj, 'x', {
get: instance.no_arg
});
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(obj.x, 100);
}
}
function testCallScriptedGetterRectifying() {
var obj = {};
Object.defineProperty(obj, 'x', {
// Missing two arguments.
get: instance.add
});
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(obj.x, 0);
}
}
function testCallScriptedSetter() {
var obj = {};
Object.defineProperty(obj, 'x', {
set: instance.set_global_one
});
for (let i = 0; i < INNER_ITERATIONS; i++) {
obj.x = i;
}
assertEq(instance.glob(), INNER_ITERATIONS-1);
}
function testCallScriptedSetterRectifying() {
var obj = {};
Object.defineProperty(obj, 'x', {
set: instance.set_global_two
});
for (let i = 0; i < INNER_ITERATIONS; i++) {
obj.x = i;
}
assertEq(instance.glob(), INNER_ITERATIONS-1);
}
function testFunctionApplyArray() {
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(instance.add.apply(null, [i, i + 1]), 2*i+1);
}
}
function testFunctionApplyArrayRectifying() {
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(instance.add.apply(null, [i + 1]), i+1);
}
}
function testFunctionApplyArgs() {
function wrapper() {
assertEq(instance.add.apply(null, arguments), 2*arguments[0]+1);
}
for (let i = 0; i < INNER_ITERATIONS; i++) {
wrapper(i, i + 1);
}
}
function testFunctionApplyArgsRectifying() {
function wrapper() {
assertEq(instance.add.apply(null, arguments), arguments[0]);
}
for (let i = 0; i < INNER_ITERATIONS; i++) {
wrapper(i + 1);
}
}
function testFunctionCall() {
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(instance.add.call(null, i, i + 1), 2*i+1);
}
}
function testFunctionCallRectifying() {
for (let i = 0; i < INNER_ITERATIONS; i++) {
assertEq(instance.add.call(null, i + 1), i+1);
}
}
run('call known', testCallKnown);
run('call known rectifying', testCallKnownRectifying);
run('call generic', testCallGeneric);
run('call generic rectifying', testCallGenericRectifying);
run('scripted getter', testCallScriptedGetter);
run('scripted getter rectifiying', testCallScriptedGetterRectifying);
run('scripted setter', testCallScriptedSetter);
run('scripted setter rectifiying', testCallScriptedSetterRectifying);
run('function.apply array', testFunctionApplyArray);
run('function.apply array rectifying', testFunctionApplyArrayRectifying);
run('function.apply args', testFunctionApplyArgs);
run('function.apply args rectifying', testFunctionApplyArgsRectifying);
run('function.call', testFunctionCall);
run('function.call rectifying', testFunctionCallRectifying);
|