blob: 4008efa5635badbdc4e42c43bb49cfc66269efb3 [file] [log] [blame]
Ben Murdoch097c5b22016-05-18 11:27:45 +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
6
7load("test/mjsunit/wasm/wasm-constants.js");
8
9function instantiate(sig, body) {
10 var module = new Array();
11 module = module.concat([
12 // -- signatures
13 kDeclSignatures, 1,
14 ]);
15 module = module.concat(sig);
16 module = module.concat([
17 // -- functions
18 kDeclFunctions, 1,
19 0, // decl flags
20 0, 0, // signature
21 body.length, 0, // body size
22 ]);
23 module = module.concat(body);
24 module = module.concat([
25 // -- declare start function
26 kDeclStartFunction,
27 0
28 ]);
29
30 var data = bytes.apply(this, module);
31 print(module);
32 print(data instanceof ArrayBuffer);
33 print(data.byteLength);
34 return _WASMEXP_.instantiateModule(data);
35}
36
37function assertFails(sig, body) {
38 try {
39 var module = instantiate(sig, body);
40 print("expected failure, but passes");
41 assertFalse(true);
42 } catch (expected) {
43 print("ok: " + expected);
44 }
45}
46
47function assertVerifies(sig, body) {
48 var module = instantiate(sig, body);
49 assertFalse(module === undefined);
50 assertFalse(module === null);
51 assertFalse(module === 0);
52 assertEquals("object", typeof module);
53 return module;
54}
55
56assertVerifies([0, kAstStmt], [kExprNop]);
57assertVerifies([0, kAstI32], [kExprI8Const, 0]);
58
59// Arguments aren't allow to start functions.
60assertFails([1, kAstI32, kAstI32], [kExprGetLocal, 0]);
61assertFails([2, kAstI32, kAstI32, kAstF32], [kExprGetLocal, 0]);
62assertFails([3, kAstI32, kAstI32, kAstF32, kAstF64], [kExprGetLocal, 0]);
63
64(function testInvalidIndex() {
65 var kBodySize = 1;
66 var data = bytes(
67 // -- signatures
68 kDeclSignatures, 1,
69 0, kAstStmt,
70 // -- functions
71 kDeclFunctions, 1,
72 0, // decl flags
73 0, 0, // signature
74 kBodySize, 0, // body size
75 kExprNop, // body
76 // -- declare start function
77 kDeclStartFunction,
78 1
79 );
80
81 assertThrows(function() { _WASMEXP_.instantiateModule(data); });
82})();
83
84
85(function testTwoStartFuncs() {
86 var kBodySize = 1;
87 var data = bytes(
88 // -- signatures
89 kDeclSignatures, 1,
90 0, kAstStmt,
91 // -- functions
92 kDeclFunctions, 1,
93 0, // decl flags
94 0, 0, // signature
95 kBodySize, 0, // body size
96 kExprNop, // body
97 // -- declare start function
98 kDeclStartFunction,
99 0,
100 // -- declare start function
101 kDeclStartFunction,
102 0
103 );
104
105 assertThrows(function() { _WASMEXP_.instantiateModule(data); });
106})();
107
108
109(function testRun() {
110 var kBodySize = 6;
111
112 var data = bytes(
113 kDeclMemory,
114 12, 12, 1, // memory
115 // -- signatures
116 kDeclSignatures, 1,
117 0, kAstStmt,
118 // -- start function
119 kDeclFunctions, 1,
120 0, // decl flags
121 0, 0, // signature
122 kBodySize, 0, // code size
123 // -- start body
124 kExprI32StoreMem, 0, kExprI8Const, 0, kExprI8Const, 77,
125 // -- declare start function
126 kDeclStartFunction,
127 0
128 );
129
130 var module = _WASMEXP_.instantiateModule(data);
131 var memory = module.memory;
132 var view = new Int8Array(memory);
133 assertEquals(77, view[0]);
134})();
135
136(function testStartFFI() {
137 var kBodySize = 2;
138 var kNameOffset = 4 + 9 + 7 + 3;
139
140 var data = bytes(
141 // -- signatures
142 kDeclSignatures, 1,
143 0, kAstStmt,
144 // -- imported function
145 kDeclFunctions, 2,
146 kDeclFunctionImport | kDeclFunctionName, // decl flags
147 0, 0, // signature
148 kNameOffset, 0, 0, 0,
149 // -- start function
150 0, // decl flags
151 0, 0, // signature
152 kBodySize, 0, // code size
153 // -- start body
154 kExprCallFunction, 0,
155 // -- declare start function
156 kDeclStartFunction,
157 1,
158 kDeclEnd,
159 'f', 'o', 'o', 0
160 );
161
162 var ranned = false;
163 var ffi = new Object();
164 ffi.foo = function() {
165 print("we ranned at stert!");
166 ranned = true;
167 }
168 var module = _WASMEXP_.instantiateModule(data, ffi);
169 var memory = module.memory;
170 var view = new Int8Array(memory);
171 assertTrue(ranned);
172})();