Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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"); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 8 | load("test/mjsunit/wasm/wasm-module-builder.js"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 9 | |
| 10 | function makeFFI(func) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 11 | var builder = new WasmModuleBuilder(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 13 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 21 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 22 | return builder.instantiate({func: func}).exports.main; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | |
| 26 | function 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 | |
| 38 | function 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 | |
| 111 | runTest(makeReentrantFFI); |
| 112 | runTest(makeFFI); |