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
|
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --expose-wasm --expose-gc --allow-natives-syntax
load("test/mjsunit/wasm/wasm-constants.js");
function makeFFI(func) {
var kBodySize = 6;
var kNameFunOffset = 24 + kBodySize + 1;
var kNameMainOffset = kNameFunOffset + 4;
var ffi = new Object();
ffi.fun = func;
var data = bytes(
// signatures
kDeclSignatures, 1,
2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int
// -- foreign function
kDeclFunctions, 2,
kDeclFunctionName | kDeclFunctionImport,
0, 0,
kNameFunOffset, 0, 0, 0, // name offset
// -- main function
kDeclFunctionName | kDeclFunctionExport,
0, 0,
kNameMainOffset, 0, 0, 0, // name offset
kBodySize, 0,
// main body
kExprCallFunction, 0, // --
kExprGetLocal, 0, // --
kExprGetLocal, 1, // --
// names
kDeclEnd,
'f', 'u', 'n', 0, // --
'm', 'a', 'i', 'n', 0 // --
);
var module = _WASMEXP_.instantiateModule(data, ffi);
assertEquals("function", typeof module.main);
return module.main;
}
function makeReentrantFFI(func) {
var main = makeFFI(reenter);
function reenter(a, b) {
print(" reenter " + a);
if (a > 0) main(a - 1, b);
else func();
}
return main;
}
function runTest(builder) {
// ---- THROWING TEST -----------------------------------------------
function throwadd(a, b) {
print("-- trying throw --");
throw a + b;
}
function throwa(a) {
print("-- trying throw --");
throw a;
}
function throwstr() {
print("-- trying throw --");
throw "string";
}
assertThrows(builder(throwadd));
assertThrows(builder(throwa));
assertThrows(builder(throwstr));
try {
builder(throwadd)(7.8, 9.9);
} catch(e) {
print(e);
}
try {
builder(throwa)(11.8, 9.3);
} catch(e) {
print(e);
}
try {
builder(throwstr)(3, 5);
} catch(e) {
print(e);
}
// ---- DEOPT TEST -----------------------------------------------
function deopt() {
print("-- trying deopt --");
%DeoptimizeFunction(deopter);
}
var deopter = builder(deopt);
deopter(5, 5);
for (var i = 0; i < 9; i++) {
deopter(6, 6);
}
// ---- GC TEST -----------------------------------------------
function dogc(a, b) {
print("-- trying gc --");
gc();
gc();
}
var gcer = builder(dogc);
gcer(7, 7);
for (var i = 0; i < 9; i++) {
gcer(8, 8);
}
}
runTest(makeReentrantFFI);
runTest(makeFFI);
|