• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6
7load("test/mjsunit/wasm/wasm-constants.js");
8
9function testCallFFI(func, check) {
10  var kBodySize = 6;
11  var kNameFunOffset = 24 + kBodySize + 1;
12  var kNameMainOffset = kNameFunOffset + 4;
13
14  var ffi = new Object();
15  ffi.fun = func;
16
17  var data = bytes(
18    // signatures
19    kDeclSignatures, 1,
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
27    kDeclFunctionName | kDeclFunctionExport,
28    0, 0,
29    kNameMainOffset, 0, 0, 0,   // name offset
30    kBodySize, 0,
31    // main body
32    kExprCallFunction, 0,       // --
33    kExprGetLocal, 0,           // --
34    kExprGetLocal, 1,           // --
35    // names
36    kDeclEnd,
37    'f', 'u', 'n', 0,           //  --
38    'm', 'a', 'i', 'n', 0       //  --
39  );
40
41  var module = _WASMEXP_.instantiateModule(data, ffi);
42
43  assertEquals("function", typeof module.main);
44
45  for (var i = 0; i < 100000; i += 10003) {
46    var a = 22.5 + i, b = 10.5 + i;
47    var r = module.main(a, b);
48    check(r, a, b);
49  }
50}
51
52var global = (function() { return this; })();
53var params = [-99, -99, -99, -99];
54var was_called = false;
55var length = -1;
56
57function FOREIGN_SUB(a, b) {
58  print("FOREIGN_SUB(" + a + ", " + b + ")");
59  was_called = true;
60  params[0] = this;
61  params[1] = a;
62  params[2] = b;
63  return (a - b) | 0;
64}
65
66function check_FOREIGN_SUB(r, a, b) {
67    assertEquals(a - b | 0, r);
68    assertTrue(was_called);
69//    assertEquals(global, params[0]);  // sloppy mode
70    assertEquals(a, params[1]);
71    assertEquals(b, params[2]);
72    was_called = false;
73}
74
75testCallFFI(FOREIGN_SUB, check_FOREIGN_SUB);
76
77
78function FOREIGN_ABCD(a, b, c, d) {
79  print("FOREIGN_ABCD(" + a + ", " + b + ", " + c + ", " + d + ")");
80  was_called = true;
81  params[0] = this;
82  params[1] = a;
83  params[2] = b;
84  params[3] = c;
85  params[4] = d;
86  return (a * b * 6) | 0;
87}
88
89function check_FOREIGN_ABCD(r, a, b) {
90    assertEquals((a * b * 6) | 0, r);
91    assertTrue(was_called);
92//    assertEquals(global, params[0]);  // sloppy mode.
93    assertEquals(a, params[1]);
94    assertEquals(b, params[2]);
95    assertEquals(undefined, params[3]);
96    assertEquals(undefined, params[4]);
97    was_called = false;
98}
99
100testCallFFI(FOREIGN_ABCD, check_FOREIGN_ABCD);
101
102function FOREIGN_ARGUMENTS0() {
103  print("FOREIGN_ARGUMENTS0");
104  was_called = true;
105  length = arguments.length;
106  for (var i = 0; i < arguments.length; i++) {
107    params[i] = arguments[i];
108  }
109  return (arguments[0] * arguments[1] * 7) | 0;
110}
111
112function FOREIGN_ARGUMENTS1(a) {
113  print("FOREIGN_ARGUMENTS1", a);
114  was_called = true;
115  length = arguments.length;
116  for (var i = 0; i < arguments.length; i++) {
117    params[i] = arguments[i];
118  }
119  return (arguments[0] * arguments[1] * 7) | 0;
120}
121
122function FOREIGN_ARGUMENTS2(a, b) {
123  print("FOREIGN_ARGUMENTS2", a, b);
124  was_called = true;
125  length = arguments.length;
126  for (var i = 0; i < arguments.length; i++) {
127    params[i] = arguments[i];
128  }
129  return (a * b * 7) | 0;
130}
131
132function FOREIGN_ARGUMENTS3(a, b, c) {
133  print("FOREIGN_ARGUMENTS3", a, b, c);
134  was_called = true;
135  length = arguments.length;
136  for (var i = 0; i < arguments.length; i++) {
137    params[i] = arguments[i];
138  }
139  return (a * b * 7) | 0;
140}
141
142function FOREIGN_ARGUMENTS4(a, b, c, d) {
143  print("FOREIGN_ARGUMENTS4", a, b, c, d);
144  was_called = true;
145  length = arguments.length;
146  for (var i = 0; i < arguments.length; i++) {
147    params[i] = arguments[i];
148  }
149  return (a * b * 7) | 0;
150}
151
152function check_FOREIGN_ARGUMENTS(r, a, b) {
153  assertEquals((a * b * 7) | 0, r);
154  assertTrue(was_called);
155  assertEquals(2, length);
156  assertEquals(a, params[0]);
157  assertEquals(b, params[1]);
158  was_called = false;
159}
160
161// Check a bunch of uses of the arguments object.
162testCallFFI(FOREIGN_ARGUMENTS0, check_FOREIGN_ARGUMENTS);
163testCallFFI(FOREIGN_ARGUMENTS1, check_FOREIGN_ARGUMENTS);
164testCallFFI(FOREIGN_ARGUMENTS2, check_FOREIGN_ARGUMENTS);
165testCallFFI(FOREIGN_ARGUMENTS3, check_FOREIGN_ARGUMENTS);
166testCallFFI(FOREIGN_ARGUMENTS4, check_FOREIGN_ARGUMENTS);
167
168function returnValue(val) {
169  return function(a, b) {
170    print("RETURN_VALUE ", val);
171    return val;
172  }
173}
174
175
176function checkReturn(expected) {
177  return function(r, a, b) { assertEquals(expected, r); }
178}
179
180// Check that returning weird values doesn't crash
181testCallFFI(returnValue(undefined), checkReturn(0));
182testCallFFI(returnValue(null), checkReturn(0));
183testCallFFI(returnValue("0"), checkReturn(0));
184testCallFFI(returnValue("-77"), checkReturn(-77));
185
186var objWithValueOf = {valueOf: function() { return 198; }}
187
188testCallFFI(returnValue(objWithValueOf), checkReturn(198));
189
190
191function testCallBinopVoid(type, func, check) {
192  var kBodySize = 10;
193  var kNameFunOffset = 28 + kBodySize + 1;
194  var kNameMainOffset = kNameFunOffset + 4;
195
196  var ffi = new Object();
197
198  var passed_length = -1;
199  var passed_a = -1;
200  var passed_b = -1;
201  var args_a = -1;
202  var args_b = -1;
203
204  ffi.fun = function(a, b) {
205    passed_length = arguments.length;
206    passed_a = a;
207    passed_b = b;
208    args_a = arguments[0];
209    args_b = arguments[1];
210  }
211
212  var data = bytes(
213    // -- signatures
214    kDeclSignatures, 2,
215    2, kAstStmt, type, type,    // (type,type)->void
216    2, kAstI32, type, type,     // (type,type)->int
217    // -- foreign function
218    kDeclFunctions, 2,
219    kDeclFunctionName | kDeclFunctionImport,
220    0, 0,                       // signature index
221    kNameFunOffset, 0, 0, 0,    // name offset
222    // -- main function
223    kDeclFunctionName | kDeclFunctionExport,
224    1, 0,                       // signature index
225    kNameMainOffset, 0, 0, 0,   // name offset
226    kBodySize, 0,               // body size
227    // main body
228    kExprBlock, 2,              // --
229    kExprCallFunction, 0,       // --
230    kExprGetLocal, 0,           // --
231    kExprGetLocal, 1,           // --
232    kExprI8Const, 99,           // --
233    // names
234    kDeclEnd,
235    'f', 'u', 'n', 0,           // --
236    'm', 'a', 'i', 'n', 0       // --
237  );
238
239  var module = _WASMEXP_.instantiateModule(data, ffi);
240
241  assertEquals("function", typeof module.main);
242
243  print("testCallBinopVoid", type);
244
245  for (var i = 0; i < 100000; i += 10003.1) {
246    var a = 22.5 + i, b = 10.5 + i;
247    var r = module.main(a, b);
248    assertEquals(99, r);
249    assertEquals(2, passed_length);
250    var expected_a, expected_b;
251    switch (type) {
252      case kAstI32: {
253        expected_a = a | 0;
254        expected_b = b | 0;
255        break;
256      }
257      case kAstF32: {
258        expected_a = Math.fround(a);
259        expected_b = Math.fround(b);
260        break;
261      }
262      case kAstF64: {
263        expected_a = a;
264        expected_b = b;
265        break;
266      }
267    }
268
269    assertEquals(expected_a, args_a);
270    assertEquals(expected_b, args_b);
271    assertEquals(expected_a, passed_a);
272    assertEquals(expected_b, passed_b);
273  }
274}
275
276
277testCallBinopVoid(kAstI32);
278// TODO testCallBinopVoid(kAstI64);
279testCallBinopVoid(kAstF32);
280testCallBinopVoid(kAstF64);
281
282
283
284function testCallPrint() {
285  var kBodySize = 10;
286  var kNamePrintOffset = 10 + 7 + 7 + 9 + kBodySize + 1;
287  var kNameMainOffset = kNamePrintOffset + 6;
288
289  var ffi = new Object();
290  ffi.print = print;
291
292  var data = bytes(
293    // -- signatures
294    kDeclSignatures, 2,
295    1, kAstStmt, kAstI32,       // i32->void
296    1, kAstStmt, kAstF64,       // f64->int
297    kDeclFunctions, 3,
298    // -- import print i32
299    kDeclFunctionName | kDeclFunctionImport,
300    0, 0,                       // signature index
301    kNamePrintOffset, 0, 0, 0,  // name offset
302    // -- import print f64
303    kDeclFunctionName | kDeclFunctionImport,
304    1, 0,                       // signature index
305    kNamePrintOffset, 0, 0, 0,  // name offset
306    // -- decl main
307    kDeclFunctionName | kDeclFunctionExport,
308    1, 0,                       // signature index
309    kNameMainOffset, 0, 0, 0,   // name offset
310    kBodySize, 0,               // body size
311    // main body
312    kExprBlock, 2,              // --
313    kExprCallFunction, 0,       // --
314    kExprI8Const, 97,           // --
315    kExprCallFunction, 1,       // --
316    kExprGetLocal, 0,           // --
317    // names
318    kDeclEnd,
319    'p', 'r', 'i', 'n', 't', 0, // --
320    'm', 'a', 'i', 'n', 0       // --
321  );
322
323  var module = _WASMEXP_.instantiateModule(data, ffi);
324
325  assertEquals("function", typeof module.main);
326
327  for (var i = -9; i < 900; i += 6.125) {
328      module.main(i);
329  }
330}
331
332testCallPrint();
333testCallPrint();
334