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 load("test/mjsunit/wasm/wasm-module-builder.js"); | 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 var module = (function () { | 10 var module = (function () { |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 kExprGetLocal, 0, // -- | 66 kExprGetLocal, 0, // -- |
67 kExprI32Popcnt // -- | 67 kExprI32Popcnt // -- |
68 ]); | 68 ]); |
69 var main = builder.addFunction("main", kSig_i_iii) | 69 var main = builder.addFunction("main", kSig_i_iii) |
70 .addBody([ | 70 .addBody([ |
71 kExprGetLocal, 1, | 71 kExprGetLocal, 1, |
72 kExprGetLocal, 2, | 72 kExprGetLocal, 2, |
73 kExprGetLocal, 0, | 73 kExprGetLocal, 0, |
74 kExprCallIndirect, sig_i_ii | 74 kExprCallIndirect, sig_i_ii |
75 ]) | 75 ]) |
76 .exportFunc() | 76 .exportFunc(); |
77 builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); | 77 builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); |
78 | 78 |
79 return builder.instantiate({mul: function(a, b) { return a * b | 0; }}); | 79 return builder.instantiate({mul: function(a, b) { return a * b | 0; }}); |
80 })(); | 80 })(); |
81 | 81 |
82 assertEquals(-6, module.exports.main(0, -2, 3)); | 82 assertEquals(-6, module.exports.main(0, -2, 3)); |
83 assertEquals(99, module.exports.main(1, 22, 77)); | 83 assertEquals(99, module.exports.main(1, 22, 77)); |
84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); | 84 assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)"); |
85 assertTraps(kTrapFuncSigMismatch, "module.exports.main(3, 12, 33)"); | 85 assertTraps(kTrapFuncSigMismatch, "module.exports.main(3, 12, 33)"); |
86 assertTraps(kTrapFuncInvalid, "module.exports.main(4, 12, 33)"); | 86 assertTraps(kTrapFuncInvalid, "module.exports.main(4, 12, 33)"); |
87 | 87 |
88 | 88 function AddFunctions(builder) { |
89 module = (function () { | |
90 var builder = new WasmModuleBuilder(); | |
91 | |
92 var mul = builder.addFunction("mul", kSig_i_ii) | 89 var mul = builder.addFunction("mul", kSig_i_ii) |
93 .addBody([ | 90 .addBody([ |
94 kExprGetLocal, 0, // -- | 91 kExprGetLocal, 0, // -- |
95 kExprGetLocal, 1, // -- | 92 kExprGetLocal, 1, // -- |
96 kExprI32Mul // -- | 93 kExprI32Mul // -- |
97 ]); | 94 ]); |
98 var add = builder.addFunction("add", kSig_i_ii) | 95 var add = builder.addFunction("add", kSig_i_ii) |
99 .addBody([ | 96 .addBody([ |
100 kExprGetLocal, 0, // -- | 97 kExprGetLocal, 0, // -- |
101 kExprGetLocal, 1, // -- | 98 kExprGetLocal, 1, // -- |
102 kExprI32Add // -- | 99 kExprI32Add // -- |
103 ]); | 100 ]); |
104 var sub = builder.addFunction("sub", kSig_i_ii) | 101 var sub = builder.addFunction("sub", kSig_i_ii) |
105 .addBody([ | 102 .addBody([ |
106 kExprGetLocal, 0, // -- | 103 kExprGetLocal, 0, // -- |
107 kExprGetLocal, 1, // -- | 104 kExprGetLocal, 1, // -- |
108 kExprI32Sub // -- | 105 kExprI32Sub // -- |
109 ]); | 106 ]); |
| 107 return {mul: mul, add: add, sub: sub}; |
| 108 } |
| 109 |
| 110 |
| 111 module = (function () { |
| 112 var builder = new WasmModuleBuilder(); |
| 113 |
| 114 var f = AddFunctions(builder); |
110 builder.addFunction("main", kSig_i_ii) | 115 builder.addFunction("main", kSig_i_ii) |
111 .addBody([ | 116 .addBody([ |
112 kExprI32Const, 33, // -- | 117 kExprI32Const, 33, // -- |
113 kExprGetLocal, 0, // -- | 118 kExprGetLocal, 0, // -- |
114 kExprGetLocal, 1, // -- | 119 kExprGetLocal, 1, // -- |
115 kExprCallIndirect, 0]) // -- | 120 kExprCallIndirect, 0]) // -- |
116 .exportAs("main"); | 121 .exportAs("main"); |
117 | 122 |
118 builder.appendToTable([mul.index, add.index, sub.index]); | 123 builder.appendToTable([f.mul.index, f.add.index, f.sub.index]); |
119 | 124 |
120 return builder.instantiate(); | 125 return builder.instantiate(); |
121 })(); | 126 })(); |
122 | 127 |
123 assertEquals(33, module.exports.main(1, 0)); | 128 assertEquals(33, module.exports.main(1, 0)); |
124 assertEquals(66, module.exports.main(2, 0)); | 129 assertEquals(66, module.exports.main(2, 0)); |
125 assertEquals(34, module.exports.main(1, 1)); | 130 assertEquals(34, module.exports.main(1, 1)); |
126 assertEquals(35, module.exports.main(2, 1)); | 131 assertEquals(35, module.exports.main(2, 1)); |
127 assertEquals(32, module.exports.main(1, 2)); | 132 assertEquals(32, module.exports.main(1, 2)); |
128 assertEquals(31, module.exports.main(2, 2)); | 133 assertEquals(31, module.exports.main(2, 2)); |
129 assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)"); | 134 assertTraps(kTrapFuncInvalid, "module.exports.main(12, 3)"); |
130 | 135 |
131 (function ConstBaseTest() { | 136 (function ConstBaseTest() { |
132 print("ConstBaseTest..."); | 137 print("ConstBaseTest..."); |
133 function instanceWithTable(base, length) { | 138 function instanceWithTable(base, length) { |
134 var builder = new WasmModuleBuilder(); | 139 var builder = new WasmModuleBuilder(); |
135 | 140 |
136 var mul = builder.addFunction("mul", kSig_i_ii) | 141 var f = AddFunctions(builder); |
137 .addBody([ | |
138 kExprGetLocal, 0, // -- | |
139 kExprGetLocal, 1, // -- | |
140 kExprI32Mul // -- | |
141 ]); | |
142 var add = builder.addFunction("add", kSig_i_ii) | |
143 .addBody([ | |
144 kExprGetLocal, 0, // -- | |
145 kExprGetLocal, 1, // -- | |
146 kExprI32Add // -- | |
147 ]); | |
148 var sub = builder.addFunction("sub", kSig_i_ii) | |
149 .addBody([ | |
150 kExprGetLocal, 0, // -- | |
151 kExprGetLocal, 1, // -- | |
152 kExprI32Sub // -- | |
153 ]); | |
154 builder.addFunction("main", kSig_i_ii) | 142 builder.addFunction("main", kSig_i_ii) |
155 .addBody([ | 143 .addBody([ |
156 kExprI32Const, 33, // -- | 144 kExprI32Const, 33, // -- |
157 kExprGetLocal, 0, // -- | 145 kExprGetLocal, 0, // -- |
158 kExprGetLocal, 1, // -- | 146 kExprGetLocal, 1, // -- |
159 kExprCallIndirect, 0]) // -- | 147 kExprCallIndirect, 0]) // -- |
160 .exportAs("main"); | 148 .exportAs("main"); |
161 | 149 |
162 builder.setFunctionTableLength(length); | 150 builder.setFunctionTableLength(length); |
163 builder.addFunctionTableInit(base, false, [add.index, sub.index, mul.index])
; | 151 builder.addFunctionTableInit(base, false, [f.add.index, f.sub.index, f.mul.i
ndex]); |
164 | 152 |
165 return builder.instantiate(); | 153 return builder.instantiate(); |
166 } | 154 } |
167 | 155 |
168 for (var i = 0; i < 5; i++) { | 156 for (var i = 0; i < 5; i++) { |
169 print(" base = " + i); | 157 print(" base = " + i); |
170 var module = instanceWithTable(i, 10); | 158 var module = instanceWithTable(i, 10); |
171 main = module.exports.main; | 159 main = module.exports.main; |
172 for (var j = 0; j < i; j++) { | 160 for (var j = 0; j < i; j++) { |
173 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); | 161 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); |
174 } | 162 } |
175 assertEquals(34, main(1, i + 0)); | 163 assertEquals(34, main(1, i + 0)); |
176 assertEquals(35, main(2, i + 0)); | 164 assertEquals(35, main(2, i + 0)); |
177 assertEquals(32, main(1, i + 1)); | 165 assertEquals(32, main(1, i + 1)); |
178 assertEquals(31, main(2, i + 1)); | 166 assertEquals(31, main(2, i + 1)); |
179 assertEquals(33, main(1, i + 2)); | 167 assertEquals(33, main(1, i + 2)); |
180 assertEquals(66, main(2, i + 2)); | 168 assertEquals(66, main(2, i + 2)); |
181 assertTraps(kTrapFuncInvalid, "main(12, 10)"); | 169 assertTraps(kTrapFuncInvalid, "main(12, 10)"); |
182 } | 170 } |
183 })(); | 171 })(); |
184 | 172 |
185 (function GlobalBaseTest() { | 173 (function GlobalBaseTest() { |
186 print("GlobalBaseTest..."); | 174 print("GlobalBaseTest..."); |
187 | 175 |
188 var builder = new WasmModuleBuilder(); | 176 var builder = new WasmModuleBuilder(); |
189 | 177 |
190 var mul = builder.addFunction("mul", kSig_i_ii) | 178 var f = AddFunctions(builder); |
191 .addBody([ | |
192 kExprGetLocal, 0, // -- | |
193 kExprGetLocal, 1, // -- | |
194 kExprI32Mul // -- | |
195 ]); | |
196 var add = builder.addFunction("add", kSig_i_ii) | |
197 .addBody([ | |
198 kExprGetLocal, 0, // -- | |
199 kExprGetLocal, 1, // -- | |
200 kExprI32Add // -- | |
201 ]); | |
202 var sub = builder.addFunction("sub", kSig_i_ii) | |
203 .addBody([ | |
204 kExprGetLocal, 0, // -- | |
205 kExprGetLocal, 1, // -- | |
206 kExprI32Sub // -- | |
207 ]); | |
208 builder.addFunction("main", kSig_i_ii) | 179 builder.addFunction("main", kSig_i_ii) |
209 .addBody([ | 180 .addBody([ |
210 kExprI32Const, 33, // -- | 181 kExprI32Const, 33, // -- |
211 kExprGetLocal, 0, // -- | 182 kExprGetLocal, 0, // -- |
212 kExprGetLocal, 1, // -- | 183 kExprGetLocal, 1, // -- |
213 kExprCallIndirect, 0]) // -- | 184 kExprCallIndirect, 0]) // -- |
214 .exportAs("main"); | 185 .exportAs("main"); |
215 | 186 |
216 builder.setFunctionTableLength(10); | 187 builder.setFunctionTableLength(10); |
217 var g = builder.addImportedGlobal("base", undefined, kAstI32); | 188 var g = builder.addImportedGlobal("base", undefined, kAstI32); |
218 builder.addFunctionTableInit(g, true, [mul.index, add.index, sub.index]); | 189 builder.addFunctionTableInit(g, true, [f.mul.index, f.add.index, f.sub.index])
; |
219 | 190 |
220 var module = new WebAssembly.Module(builder.toBuffer()); | 191 var module = new WebAssembly.Module(builder.toBuffer()); |
221 | 192 |
222 for (var i = 0; i < 5; i++) { | 193 for (var i = 0; i < 5; i++) { |
223 print(" base = " + i); | 194 print(" base = " + i); |
224 var instance = new WebAssembly.Instance(module, {base: i}); | 195 var instance = new WebAssembly.Instance(module, {base: i}); |
225 main = instance.exports.main; | 196 main = instance.exports.main; |
226 for (var j = 0; j < i; j++) { | 197 for (var j = 0; j < i; j++) { |
227 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); | 198 assertTraps(kTrapFuncSigMismatch, "main(12, " + j + ")"); |
228 } | 199 } |
229 assertEquals(33, main(1, i + 0)); | 200 assertEquals(33, main(1, i + 0)); |
230 assertEquals(66, main(2, i + 0)); | 201 assertEquals(66, main(2, i + 0)); |
231 assertEquals(34, main(1, i + 1)); | 202 assertEquals(34, main(1, i + 1)); |
232 assertEquals(35, main(2, i + 1)); | 203 assertEquals(35, main(2, i + 1)); |
233 assertEquals(32, main(1, i + 2)); | 204 assertEquals(32, main(1, i + 2)); |
234 assertEquals(31, main(2, i + 2)); | 205 assertEquals(31, main(2, i + 2)); |
235 assertTraps(kTrapFuncInvalid, "main(12, 10)"); | 206 assertTraps(kTrapFuncInvalid, "main(12, 10)"); |
236 } | 207 } |
237 })(); | 208 })(); |
OLD | NEW |