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 |
6 | 6 |
7 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
8 | 8 |
9 function testCallFFI(func, check) { | 9 function testCallImport(func, check) { |
10 var kBodySize = 6; | 10 var kBodySize = 6; |
11 var kNameFunOffset = 24 + kBodySize + 1; | 11 var kNameFunOffset = 29 + kBodySize + 1; |
12 var kNameMainOffset = kNameFunOffset + 4; | 12 var kNameMainOffset = kNameFunOffset + 4; |
13 | 13 |
14 var ffi = new Object(); | 14 var ffi = new Object(); |
15 ffi.fun = func; | 15 ffi.fun = func; |
16 | 16 |
17 var data = bytes( | 17 var data = bytes( |
18 // signatures | 18 // signatures |
19 kDeclSignatures, 1, | 19 kDeclSignatures, 1, |
20 2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int | 20 2, kAstI32, kAstF64, kAstF64, // (f64,f64) -> int |
21 // -- foreign function | |
22 kDeclFunctions, 2, | |
23 kDeclFunctionName | kDeclFunctionImport, | |
24 0, 0, | |
25 kNameFunOffset, 0, 0, 0, // name offset | |
26 // -- main function | 21 // -- main function |
| 22 kDeclFunctions, |
| 23 1, |
27 kDeclFunctionName | kDeclFunctionExport, | 24 kDeclFunctionName | kDeclFunctionExport, |
28 0, 0, | 25 0, 0, |
29 kNameMainOffset, 0, 0, 0, // name offset | 26 kNameMainOffset, 0, 0, 0, // name offset |
30 kBodySize, 0, | 27 kBodySize, 0, |
31 // main body | 28 // main body |
32 kExprCallFunction, 0, // -- | 29 kExprCallImport, 0, // -- |
33 kExprGetLocal, 0, // -- | 30 kExprGetLocal, 0, // -- |
34 kExprGetLocal, 1, // -- | 31 kExprGetLocal, 1, // -- |
| 32 // imports |
| 33 kDeclImportTable, |
| 34 1, |
| 35 0, 0, // sig index |
| 36 0, 0, 0, 0, // module name offset |
| 37 kNameFunOffset, 0, 0, 0, // function name offset |
35 // names | 38 // names |
36 kDeclEnd, | 39 kDeclEnd, |
37 'f', 'u', 'n', 0, // -- | 40 'f', 'u', 'n', 0, // -- |
38 'm', 'a', 'i', 'n', 0 // -- | 41 'm', 'a', 'i', 'n', 0 // -- |
39 ); | 42 ); |
40 | 43 |
41 var module = _WASMEXP_.instantiateModule(data, ffi); | 44 var module = _WASMEXP_.instantiateModule(data, ffi); |
42 | 45 |
43 assertEquals("function", typeof module.main); | 46 assertEquals("function", typeof module.main); |
44 | 47 |
45 for (var i = 0; i < 100000; i += 10003) { | 48 for (var i = 0; i < 100000; i += 10003) { |
46 var a = 22.5 + i, b = 10.5 + i; | 49 var a = 22.5 + i, b = 10.5 + i; |
47 var r = module.main(a, b); | 50 var r = module.main(a, b); |
48 check(r, a, b); | 51 check(r, a, b); |
(...skipping 16 matching lines...) Expand all Loading... |
65 | 68 |
66 function check_FOREIGN_SUB(r, a, b) { | 69 function check_FOREIGN_SUB(r, a, b) { |
67 assertEquals(a - b | 0, r); | 70 assertEquals(a - b | 0, r); |
68 assertTrue(was_called); | 71 assertTrue(was_called); |
69 // assertEquals(global, params[0]); // sloppy mode | 72 // assertEquals(global, params[0]); // sloppy mode |
70 assertEquals(a, params[1]); | 73 assertEquals(a, params[1]); |
71 assertEquals(b, params[2]); | 74 assertEquals(b, params[2]); |
72 was_called = false; | 75 was_called = false; |
73 } | 76 } |
74 | 77 |
75 testCallFFI(FOREIGN_SUB, check_FOREIGN_SUB); | 78 testCallImport(FOREIGN_SUB, check_FOREIGN_SUB); |
76 | 79 |
77 | 80 |
78 function FOREIGN_ABCD(a, b, c, d) { | 81 function FOREIGN_ABCD(a, b, c, d) { |
79 print("FOREIGN_ABCD(" + a + ", " + b + ", " + c + ", " + d + ")"); | 82 print("FOREIGN_ABCD(" + a + ", " + b + ", " + c + ", " + d + ")"); |
80 was_called = true; | 83 was_called = true; |
81 params[0] = this; | 84 params[0] = this; |
82 params[1] = a; | 85 params[1] = a; |
83 params[2] = b; | 86 params[2] = b; |
84 params[3] = c; | 87 params[3] = c; |
85 params[4] = d; | 88 params[4] = d; |
86 return (a * b * 6) | 0; | 89 return (a * b * 6) | 0; |
87 } | 90 } |
88 | 91 |
89 function check_FOREIGN_ABCD(r, a, b) { | 92 function check_FOREIGN_ABCD(r, a, b) { |
90 assertEquals((a * b * 6) | 0, r); | 93 assertEquals((a * b * 6) | 0, r); |
91 assertTrue(was_called); | 94 assertTrue(was_called); |
92 // assertEquals(global, params[0]); // sloppy mode. | 95 // assertEquals(global, params[0]); // sloppy mode. |
93 assertEquals(a, params[1]); | 96 assertEquals(a, params[1]); |
94 assertEquals(b, params[2]); | 97 assertEquals(b, params[2]); |
95 assertEquals(undefined, params[3]); | 98 assertEquals(undefined, params[3]); |
96 assertEquals(undefined, params[4]); | 99 assertEquals(undefined, params[4]); |
97 was_called = false; | 100 was_called = false; |
98 } | 101 } |
99 | 102 |
100 testCallFFI(FOREIGN_ABCD, check_FOREIGN_ABCD); | 103 testCallImport(FOREIGN_ABCD, check_FOREIGN_ABCD); |
101 | 104 |
102 function FOREIGN_ARGUMENTS0() { | 105 function FOREIGN_ARGUMENTS0() { |
103 print("FOREIGN_ARGUMENTS0"); | 106 print("FOREIGN_ARGUMENTS0"); |
104 was_called = true; | 107 was_called = true; |
105 length = arguments.length; | 108 length = arguments.length; |
106 for (var i = 0; i < arguments.length; i++) { | 109 for (var i = 0; i < arguments.length; i++) { |
107 params[i] = arguments[i]; | 110 params[i] = arguments[i]; |
108 } | 111 } |
109 return (arguments[0] * arguments[1] * 7) | 0; | 112 return (arguments[0] * arguments[1] * 7) | 0; |
110 } | 113 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 function check_FOREIGN_ARGUMENTS(r, a, b) { | 155 function check_FOREIGN_ARGUMENTS(r, a, b) { |
153 assertEquals((a * b * 7) | 0, r); | 156 assertEquals((a * b * 7) | 0, r); |
154 assertTrue(was_called); | 157 assertTrue(was_called); |
155 assertEquals(2, length); | 158 assertEquals(2, length); |
156 assertEquals(a, params[0]); | 159 assertEquals(a, params[0]); |
157 assertEquals(b, params[1]); | 160 assertEquals(b, params[1]); |
158 was_called = false; | 161 was_called = false; |
159 } | 162 } |
160 | 163 |
161 // Check a bunch of uses of the arguments object. | 164 // Check a bunch of uses of the arguments object. |
162 testCallFFI(FOREIGN_ARGUMENTS0, check_FOREIGN_ARGUMENTS); | 165 testCallImport(FOREIGN_ARGUMENTS0, check_FOREIGN_ARGUMENTS); |
163 testCallFFI(FOREIGN_ARGUMENTS1, check_FOREIGN_ARGUMENTS); | 166 testCallImport(FOREIGN_ARGUMENTS1, check_FOREIGN_ARGUMENTS); |
164 testCallFFI(FOREIGN_ARGUMENTS2, check_FOREIGN_ARGUMENTS); | 167 testCallImport(FOREIGN_ARGUMENTS2, check_FOREIGN_ARGUMENTS); |
165 testCallFFI(FOREIGN_ARGUMENTS3, check_FOREIGN_ARGUMENTS); | 168 testCallImport(FOREIGN_ARGUMENTS3, check_FOREIGN_ARGUMENTS); |
166 testCallFFI(FOREIGN_ARGUMENTS4, check_FOREIGN_ARGUMENTS); | 169 testCallImport(FOREIGN_ARGUMENTS4, check_FOREIGN_ARGUMENTS); |
167 | 170 |
168 function returnValue(val) { | 171 function returnValue(val) { |
169 return function(a, b) { | 172 return function(a, b) { |
170 print("RETURN_VALUE ", val); | 173 print("RETURN_VALUE ", val); |
171 return val; | 174 return val; |
172 } | 175 } |
173 } | 176 } |
174 | 177 |
175 | 178 |
176 function checkReturn(expected) { | 179 function checkReturn(expected) { |
177 return function(r, a, b) { assertEquals(expected, r); } | 180 return function(r, a, b) { assertEquals(expected, r); } |
178 } | 181 } |
179 | 182 |
180 // Check that returning weird values doesn't crash | 183 // Check that returning weird values doesn't crash |
181 testCallFFI(returnValue(undefined), checkReturn(0)); | 184 testCallImport(returnValue(undefined), checkReturn(0)); |
182 testCallFFI(returnValue(null), checkReturn(0)); | 185 testCallImport(returnValue(null), checkReturn(0)); |
183 testCallFFI(returnValue("0"), checkReturn(0)); | 186 testCallImport(returnValue("0"), checkReturn(0)); |
184 testCallFFI(returnValue("-77"), checkReturn(-77)); | 187 testCallImport(returnValue("-77"), checkReturn(-77)); |
185 | 188 |
186 var objWithValueOf = {valueOf: function() { return 198; }} | 189 var objWithValueOf = {valueOf: function() { return 198; }} |
187 | 190 |
188 testCallFFI(returnValue(objWithValueOf), checkReturn(198)); | 191 testCallImport(returnValue(objWithValueOf), checkReturn(198)); |
189 | 192 |
190 | 193 |
191 function testCallBinopVoid(type, func, check) { | 194 function testCallBinopVoid(type, func, check) { |
192 var kBodySize = 10; | 195 var kBodySize = 10; |
193 var kNameFunOffset = 28 + kBodySize + 1; | 196 var kNameFunOffset = 28 + kBodySize + 1; |
194 var kNameMainOffset = kNameFunOffset + 4; | 197 var kNameMainOffset = kNameFunOffset + 4; |
195 | 198 |
196 var ffi = new Object(); | 199 var ffi = new Object(); |
197 | 200 |
198 var passed_length = -1; | 201 var passed_length = -1; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 | 327 |
325 assertEquals("function", typeof module.main); | 328 assertEquals("function", typeof module.main); |
326 | 329 |
327 for (var i = -9; i < 900; i += 6.125) { | 330 for (var i = -9; i < 900; i += 6.125) { |
328 module.main(i); | 331 module.main(i); |
329 } | 332 } |
330 } | 333 } |
331 | 334 |
332 testCallPrint(); | 335 testCallPrint(); |
333 testCallPrint(); | 336 testCallPrint(); |
OLD | NEW |