blob: 8b8fb7e4d47aebf4e8587568c63107f928189b27 [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");
Ben Murdochda12d292016-06-02 14:46:10 +01008load("test/mjsunit/wasm/wasm-module-builder.js");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009
10function makeFFI(func) {
Ben Murdochda12d292016-06-02 14:46:10 +010011 var builder = new WasmModuleBuilder();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012
Ben Murdochda12d292016-06-02 14:46:10 +010013 var sig_index = builder.addSignature([kAstI32, kAstF64, kAstF64]);
14 builder.addImport("func", sig_index);
15 builder.addFunction("main", sig_index)
16 .addBody([
17 kExprCallImport, 0, // --
18 kExprGetLocal, 0, // --
19 kExprGetLocal, 1]) // --
20 .exportFunc()
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021
Ben Murdochda12d292016-06-02 14:46:10 +010022 return builder.instantiate({func: func}).exports.main;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023}
24
25
26function makeReentrantFFI(func) {
27 var main = makeFFI(reenter);
28
29 function reenter(a, b) {
30 print(" reenter " + a);
31 if (a > 0) main(a - 1, b);
32 else func();
33 }
34 return main;
35}
36
37
38function runTest(builder) {
39 // ---- THROWING TEST -----------------------------------------------
40
41 function throwadd(a, b) {
42 print("-- trying throw --");
43 throw a + b;
44 }
45
46 function throwa(a) {
47 print("-- trying throw --");
48 throw a;
49 }
50
51 function throwstr() {
52 print("-- trying throw --");
53 throw "string";
54 }
55
56 assertThrows(builder(throwadd));
57 assertThrows(builder(throwa));
58 assertThrows(builder(throwstr));
59
60 try {
61 builder(throwadd)(7.8, 9.9);
62 } catch(e) {
63 print(e);
64 }
65
66 try {
67 builder(throwa)(11.8, 9.3);
68 } catch(e) {
69 print(e);
70 }
71
72
73 try {
74 builder(throwstr)(3, 5);
75 } catch(e) {
76 print(e);
77 }
78
79
80 // ---- DEOPT TEST -----------------------------------------------
81
82 function deopt() {
83 print("-- trying deopt --");
84 %DeoptimizeFunction(deopter);
85 }
86
87 var deopter = builder(deopt);
88
89 deopter(5, 5);
90 for (var i = 0; i < 9; i++) {
91 deopter(6, 6);
92 }
93
94
95 // ---- GC TEST -----------------------------------------------
96 function dogc(a, b) {
97 print("-- trying gc --");
98 gc();
99 gc();
100 }
101
102
103 var gcer = builder(dogc);
104 gcer(7, 7);
105
106 for (var i = 0; i < 9; i++) {
107 gcer(8, 8);
108 }
109}
110
111runTest(makeReentrantFFI);
112runTest(makeFFI);