blob: e863b07b4f666b10281425fc34b4757cd70507bb [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --expose-wasm --expose-gc --allow-natives-syntax
6
7load("test/mjsunit/wasm/wasm-constants.js");
8
9function makeFFI(func) {
10 var kBodySize = 6;
11 var kNameFunOffset = 24 + kBodySize + 1;
12 var kNameMainOffset = kNameFunOffset + 4;
13
14 var ffi = new Object();
15 ffi.fun = func;
16
17 var data = bytes(
18 // signatures
19 kDeclSignatures, 1,
20 2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int
21 // -- foreign function
22 kDeclFunctions, 2,
23 kDeclFunctionName | kDeclFunctionImport,
24 0, 0,
25 kNameFunOffset, 0, 0, 0, // name offset
26 // -- main function
27 kDeclFunctionName | kDeclFunctionExport,
28 0, 0,
29 kNameMainOffset, 0, 0, 0, // name offset
30 kBodySize, 0,
31 // main body
32 kExprCallFunction, 0, // --
33 kExprGetLocal, 0, // --
34 kExprGetLocal, 1, // --
35 // names
36 kDeclEnd,
37 'f', 'u', 'n', 0, // --
38 'm', 'a', 'i', 'n', 0 // --
39 );
40
41 var module = _WASMEXP_.instantiateModule(data, ffi);
42
43 assertEquals("function", typeof module.main);
44
45 return module.main;
46}
47
48
49function makeReentrantFFI(func) {
50 var main = makeFFI(reenter);
51
52 function reenter(a, b) {
53 print(" reenter " + a);
54 if (a > 0) main(a - 1, b);
55 else func();
56 }
57 return main;
58}
59
60
61function runTest(builder) {
62 // ---- THROWING TEST -----------------------------------------------
63
64 function throwadd(a, b) {
65 print("-- trying throw --");
66 throw a + b;
67 }
68
69 function throwa(a) {
70 print("-- trying throw --");
71 throw a;
72 }
73
74 function throwstr() {
75 print("-- trying throw --");
76 throw "string";
77 }
78
79 assertThrows(builder(throwadd));
80 assertThrows(builder(throwa));
81 assertThrows(builder(throwstr));
82
83 try {
84 builder(throwadd)(7.8, 9.9);
85 } catch(e) {
86 print(e);
87 }
88
89 try {
90 builder(throwa)(11.8, 9.3);
91 } catch(e) {
92 print(e);
93 }
94
95
96 try {
97 builder(throwstr)(3, 5);
98 } catch(e) {
99 print(e);
100 }
101
102
103 // ---- DEOPT TEST -----------------------------------------------
104
105 function deopt() {
106 print("-- trying deopt --");
107 %DeoptimizeFunction(deopter);
108 }
109
110 var deopter = builder(deopt);
111
112 deopter(5, 5);
113 for (var i = 0; i < 9; i++) {
114 deopter(6, 6);
115 }
116
117
118 // ---- GC TEST -----------------------------------------------
119 function dogc(a, b) {
120 print("-- trying gc --");
121 gc();
122 gc();
123 }
124
125
126 var gcer = builder(dogc);
127 gcer(7, 7);
128
129 for (var i = 0; i < 9; i++) {
130 gcer(8, 8);
131 }
132}
133
134runTest(makeReentrantFFI);
135runTest(makeFFI);