1 // 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 7 load("test/mjsunit/wasm/wasm-constants.js"); 8 load("test/mjsunit/wasm/wasm-module-builder.js"); 9 10 function makeFFI(func) { 11 var builder = new WasmModuleBuilder(); 12 13 var sig_index = builder.addType(kSig_i_dd); 14 builder.addImport("func", sig_index); 15 builder.addFunction("main", sig_index) 16 .addBody([ 17 kExprGetLocal, 0, // -- 18 kExprGetLocal, 1, // -- 19 kExprCallImport, kArity2, 0, // -- 20 ]) 21 .exportFunc() 22 23 return builder.instantiate({func: func}).exports.main; 24 } 25 26 27 function 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 39 function 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 112 runTest(makeReentrantFFI); 113 runTest(makeFFI); 114