blob: 23c5658dcdcbee5ea66e57681cfcc91852dff826 [file] [log] [blame]
Ben Murdochc5610432016-08-08 18:44:38 +01001// Copyright 2016 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 --wasm-num-compilation-tasks=10
6
7load("test/mjsunit/wasm/wasm-constants.js");
8load("test/mjsunit/wasm/wasm-module-builder.js");
9
10function assertModule(module, memsize) {
11 // Check the module exists.
12 assertFalse(module === undefined);
13 assertFalse(module === null);
14 assertFalse(module === 0);
15 assertEquals("object", typeof module);
16
17 // Check the memory is an ArrayBuffer.
18 var mem = module.exports.memory;
19 assertFalse(mem === undefined);
20 assertFalse(mem === null);
21 assertFalse(mem === 0);
22 assertEquals("object", typeof mem);
23 assertTrue(mem instanceof ArrayBuffer);
24 for (var i = 0; i < 4; i++) {
25 module.exports.memory = 0; // should be ignored
26 assertEquals(mem, module.exports.memory);
27 }
28
29 assertEquals(memsize, module.exports.memory.byteLength);
30}
31
32function assertFunction(module, func) {
33 assertEquals("object", typeof module.exports);
34
35 var exp = module.exports[func];
36 assertFalse(exp === undefined);
37 assertFalse(exp === null);
38 assertFalse(exp === 0);
39 assertEquals("function", typeof exp);
40 return exp;
41}
42
43(function CompileFunctionsTest() {
44
45 var builder = new WasmModuleBuilder();
46
47 builder.addMemory(1, 1, true);
48 for (i = 0; i < 1000; i++) {
49 builder.addFunction("sub" + i, kSig_i_i)
50 .addBody([ // --
51 kExprGetLocal, 0, // --
52 kExprI32Const, i % 61, // --
53 kExprI32Sub]) // --
54 .exportFunc()
55 }
56
57 var module = builder.instantiate();
58 assertModule(module, kPageSize);
59
60 // Check the properties of the functions.
61 for (i = 0; i < 1000; i++) {
62 var sub = assertFunction(module, "sub" + i);
63 assertEquals(33 - (i % 61), sub(33));
64 }
65})();
66
67(function CallFunctionsTest() {
68
69 var builder = new WasmModuleBuilder();
70
71 var f = []
72
73 f[0] = builder.addFunction("add0", kSig_i_ii)
74 .addBody([
75 kExprGetLocal, 0, // --
76 kExprGetLocal, 1, // --
77 kExprI32Add, // --
78 ])
79 .exportFunc()
80
81 builder.addMemory(1, 1, true);
82 for (i = 1; i < 256; i++) {
83 f[i] = builder.addFunction("add" + i, kSig_i_ii)
84 .addBody([ // --
85 kExprGetLocal, 0, // --
86 kExprGetLocal, 1, // --
87 kExprCallFunction, kArity2, f[i >>> 1].index]) // --
88 .exportFunc()
89 }
90 var module = builder.instantiate();
91 assertModule(module, kPageSize);
92
93 // Check the properties of the functions.
94 for (i = 0; i < 256; i++) {
95 var add = assertFunction(module, "add" + i);
96 assertEquals(88, add(33, 55));
97 assertEquals(88888, add(33333, 55555));
98 assertEquals(8888888, add(3333333, 5555555));
99 }
100})();