OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --expose-wasm | 5 // Flags: --expose-wasm --allow-natives-syntax |
6 | 6 |
7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 function testCallFFI(func, check) { | 10 function testCallFFI(func, check) { |
11 var builder = new WasmModuleBuilder(); | 11 var builder = new WasmModuleBuilder(); |
12 | 12 |
13 var sig_index = builder.addType(kSig_i_dd); | 13 var sig_index = builder.addType(kSig_i_dd); |
14 builder.addImport("func", sig_index); | 14 builder.addImport("func", sig_index); |
15 builder.addFunction("main", sig_index) | 15 builder.addFunction("main", sig_index) |
16 .addBody([ | 16 .addBody([ |
17 kExprGetLocal, 0, // -- | 17 kExprGetLocal, 0, // -- |
18 kExprGetLocal, 1, // -- | 18 kExprGetLocal, 1, // -- |
19 kExprCallImport, kArity2, 0 // -- | 19 kExprCallImport, kArity2, 0 // -- |
20 ]) // -- | 20 ]) // -- |
21 .exportFunc(); | 21 .exportFunc(); |
22 | 22 |
23 var main = builder.instantiate({func: func}).exports.main; | 23 var main = builder.instantiate({func: func}).exports.main; |
24 | 24 |
25 for (var i = 0; i < 100000; i += 10003) { | 25 for (var i = 0; i < 100000; i += 10003) { |
26 var a = 22.5 + i, b = 10.5 + i; | 26 var a = 22.5 + i, b = 10.5 + i; |
27 var r = main(a, b); | 27 var r = main(a, b); |
28 check(r, a, b); | 28 if (check) { |
| 29 check(r, a, b); |
| 30 } |
29 } | 31 } |
30 } | 32 } |
31 | 33 |
32 var global = (function() { return this; })(); | 34 var global = (function() { return this; })(); |
33 var params = [-99, -99, -99, -99, -99]; | 35 var params = [-99, -99, -99, -99, -99]; |
34 var was_called = false; | 36 var was_called = false; |
35 var length = -1; | 37 var length = -1; |
36 | 38 |
37 function FOREIGN_SUB(a, b) { | 39 function FOREIGN_SUB(a, b) { |
38 print("FOREIGN_SUB(" + a + ", " + b + ")"); | 40 print("FOREIGN_SUB(" + a + ", " + b + ")"); |
39 was_called = true; | 41 was_called = true; |
40 params[0] = this; | 42 params[0] = this; |
41 params[1] = a; | 43 params[1] = a; |
42 params[2] = b; | 44 params[2] = b; |
43 return (a - b) | 0; | 45 return (a - b) | 0; |
44 } | 46 } |
45 | 47 |
46 function check_FOREIGN_SUB(r, a, b) { | 48 function check_FOREIGN_SUB(r, a, b) { |
47 assertEquals(a - b | 0, r); | 49 assertEquals(a - b | 0, r); |
48 assertTrue(was_called); | 50 assertTrue(was_called); |
49 // assertEquals(global, params[0]); // sloppy mode | 51 // assertEquals(global, params[0]); // sloppy mode |
50 assertEquals(a, params[1]); | 52 assertEquals(a, params[1]); |
51 assertEquals(b, params[2]); | 53 assertEquals(b, params[2]); |
52 was_called = false; | 54 was_called = false; |
53 } | 55 } |
54 | 56 |
| 57 // Test calling a normal JSFunction. |
| 58 print("JSFunction"); |
55 testCallFFI(FOREIGN_SUB, check_FOREIGN_SUB); | 59 testCallFFI(FOREIGN_SUB, check_FOREIGN_SUB); |
56 | 60 |
| 61 // Test calling a proxy. |
| 62 print("Proxy"); |
| 63 var proxy_sub = new Proxy(FOREIGN_SUB, {}); |
| 64 testCallFFI(proxy_sub, check_FOREIGN_SUB); |
| 65 |
| 66 // Test calling a bind function. |
| 67 print("Bind function"); |
| 68 var bind_sub = FOREIGN_SUB.bind(); |
| 69 testCallFFI(bind_sub, check_FOREIGN_SUB); |
| 70 |
| 71 var main_for_constructor_test; |
| 72 print("Constructor"); |
| 73 (function testCallConstructor() { |
| 74 class C {} |
| 75 var builder = new WasmModuleBuilder(); |
| 76 |
| 77 var sig_index = builder.addType(kSig_i_dd); |
| 78 builder.addImport("func", sig_index); |
| 79 builder.addFunction("main", sig_index) |
| 80 .addBody([ |
| 81 kExprGetLocal, 0, // -- |
| 82 kExprGetLocal, 1, // -- |
| 83 kExprCallImport, kArity2, 0 // -- |
| 84 ]) // -- |
| 85 .exportFunc(); |
| 86 |
| 87 main_for_constructor_test = builder.instantiate({func: C}).exports.main; |
| 88 |
| 89 assertThrows("main_for_constructor_test(12, 43)", TypeError); |
| 90 }) (); |
| 91 |
| 92 print("Native function"); |
| 93 (function test_ffi_call_to_native() { |
| 94 |
| 95 var builder = new WasmModuleBuilder(); |
| 96 |
| 97 var sig_index = builder.addType(kSig_d); |
| 98 builder.addImport("func", sig_index); |
| 99 builder.addFunction("main", sig_index) |
| 100 .addBody([ |
| 101 kExprCallImport, kArity0, 0 // -- |
| 102 ]) // -- |
| 103 .exportFunc(); |
| 104 |
| 105 var main = builder.instantiate({func: Object.prototype.toString}).exports.main
; |
| 106 // The result of the call to Object.prototype.toString should be |
| 107 // [object Undefined]. However, we cannot test for this result because wasm |
| 108 // cannot return objects but converts them to float64 in this test. |
| 109 assertEquals(NaN, main()); |
| 110 })(); |
| 111 |
| 112 print("Callable JSObject"); |
| 113 testCallFFI(%GetCallable(), function check(r, a, b) {assertEquals(a - b, r);}); |
57 | 114 |
58 function FOREIGN_ABCD(a, b, c, d) { | 115 function FOREIGN_ABCD(a, b, c, d) { |
59 print("FOREIGN_ABCD(" + a + ", " + b + ", " + c + ", " + d + ")"); | 116 print("FOREIGN_ABCD(" + a + ", " + b + ", " + c + ", " + d + ")"); |
60 was_called = true; | 117 was_called = true; |
61 params[0] = this; | 118 params[0] = this; |
62 params[1] = a; | 119 params[1] = a; |
63 params[2] = b; | 120 params[2] = b; |
64 params[3] = c; | 121 params[3] = c; |
65 params[4] = d; | 122 params[4] = d; |
66 return (a * b * 6) | 0; | 123 return (a * b * 6) | 0; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 kExprCallImport, kArity1, 1 // -- | 308 kExprCallImport, kArity1, 1 // -- |
252 ]) // -- | 309 ]) // -- |
253 .exportFunc() | 310 .exportFunc() |
254 | 311 |
255 var main = builder.instantiate({print: print}).exports.main; | 312 var main = builder.instantiate({print: print}).exports.main; |
256 for (var i = -9; i < 900; i += 6.125) main(i); | 313 for (var i = -9; i < 900; i += 6.125) main(i); |
257 } | 314 } |
258 | 315 |
259 testCallPrint(); | 316 testCallPrint(); |
260 testCallPrint(); | 317 testCallPrint(); |
OLD | NEW |