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