OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // 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 | 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 function StringRef(string) { | 5 function StringRef(string) { |
6 this.pos = -1; | 6 this.pos = -1; |
7 this.string = string; | 7 this.string = string; |
8 } | 8 } |
9 | 9 |
10 function DataRef(data) { | 10 function DataRef(data) { |
11 this.pos = -1; | 11 this.pos = -1; |
12 this.data = data; | 12 this.data = data; |
13 } | 13 } |
14 | 14 |
15 function WasmFunctionBuilder(name, sig_index) { | 15 function WasmFunctionBuilder(name, sig_index) { |
16 this.name = name; | 16 this.name = name; |
17 this.sig_index = sig_index; | 17 this.sig_index = sig_index; |
18 this.exports = []; | 18 this.exports = []; |
19 } | 19 } |
20 | 20 |
21 WasmFunctionBuilder.prototype.exportAs = function(name) { | 21 WasmFunctionBuilder.prototype.exportAs = function(name) { |
22 this.exports.push(name); | 22 this.exports.push(name); |
23 return this; | 23 return this; |
24 } | 24 } |
25 | 25 |
| 26 WasmFunctionBuilder.prototype.exportFunc = function() { |
| 27 this.exports.push(this.name); |
| 28 return this; |
| 29 } |
| 30 |
26 WasmFunctionBuilder.prototype.addBody = function(body) { | 31 WasmFunctionBuilder.prototype.addBody = function(body) { |
27 this.body = body; | 32 this.body = body; |
28 return this; | 33 return this; |
29 } | 34 } |
30 | 35 |
31 WasmFunctionBuilder.prototype.addLocals = function(locals) { | 36 WasmFunctionBuilder.prototype.addLocals = function(locals) { |
32 this.locals = locals; | 37 this.locals = locals; |
33 return this; | 38 return this; |
34 } | 39 } |
35 | 40 |
36 function WasmModuleBuilder() { | 41 function WasmModuleBuilder() { |
37 this.signatures = []; | 42 this.signatures = []; |
38 this.imports = []; | 43 this.imports = []; |
39 this.functions = []; | 44 this.functions = []; |
40 this.exports = []; | 45 this.exports = []; |
41 this.function_table = []; | 46 this.function_table = []; |
42 this.data_segments = []; | 47 this.data_segments = []; |
| 48 this.explicit = []; |
43 return this; | 49 return this; |
44 } | 50 } |
45 | 51 |
| 52 WasmModuleBuilder.prototype.addStart = function(start_index) { |
| 53 this.start_index = start_index; |
| 54 } |
| 55 |
46 WasmModuleBuilder.prototype.addMemory = function(min, max, exp) { | 56 WasmModuleBuilder.prototype.addMemory = function(min, max, exp) { |
47 this.memory = {min: min, max: max, exp: exp}; | 57 this.memory = {min: min, max: max, exp: exp}; |
48 return this; | 58 return this; |
49 } | 59 } |
50 | 60 |
| 61 WasmModuleBuilder.prototype.addExplicitSection = function(bytes) { |
| 62 this.explicit.push(bytes); |
| 63 return this; |
| 64 } |
| 65 |
51 // Add a signature; format is [rettype, param0, param1, ...] | 66 // Add a signature; format is [rettype, param0, param1, ...] |
52 WasmModuleBuilder.prototype.addSignature = function(sig) { | 67 WasmModuleBuilder.prototype.addSignature = function(sig) { |
53 // TODO: canonicalize signatures? | 68 // TODO: canonicalize signatures? |
54 this.signatures.push(sig); | 69 this.signatures.push(sig); |
55 return this.signatures.length - 1; | 70 return this.signatures.length - 1; |
56 } | 71 } |
57 | 72 |
58 WasmModuleBuilder.prototype.addFunction = function(name, sig) { | 73 WasmModuleBuilder.prototype.addFunction = function(name, sig) { |
59 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); | 74 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); |
60 var func = new WasmFunctionBuilder(name, sig_index); | 75 var func = new WasmFunctionBuilder(name, sig_index); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 } | 207 } |
193 emit_u16(bytes, func.body.length); | 208 emit_u16(bytes, func.body.length); |
194 for (var i = 0; i < func.body.length; i++) { | 209 for (var i = 0; i < func.body.length; i++) { |
195 emit_u8(bytes, func.body[i]); | 210 emit_u8(bytes, func.body[i]); |
196 } | 211 } |
197 | 212 |
198 index++; | 213 index++; |
199 } | 214 } |
200 } | 215 } |
201 | 216 |
| 217 // Add start function section. |
| 218 if (this.start_index != undefined) { |
| 219 if (debug) print("emitting start function @ " + bytes.length); |
| 220 emit_u8(bytes, kDeclStartFunction); |
| 221 emit_varint(bytes, this.start_index); |
| 222 } |
| 223 |
202 if (this.function_table.length > 0) { | 224 if (this.function_table.length > 0) { |
203 if (debug) print("emitting function table @ " + bytes.length); | 225 if (debug) print("emitting function table @ " + bytes.length); |
204 emit_u8(bytes, kDeclFunctionTable); | 226 emit_u8(bytes, kDeclFunctionTable); |
205 emit_varint(bytes, this.function_table.length); | 227 emit_varint(bytes, this.function_table.length); |
206 for (index of this.function_table) { | 228 for (index of this.function_table) { |
207 emit_u16(bytes, index); | 229 emit_u16(bytes, index); |
208 } | 230 } |
209 } | 231 } |
210 | 232 |
211 if (exports > 0) { | 233 if (exports > 0) { |
(...skipping 13 matching lines...) Expand all Loading... |
225 emit_u8(bytes, kDeclDataSegments); | 247 emit_u8(bytes, kDeclDataSegments); |
226 emit_varint(bytes, this.data_segments.length); | 248 emit_varint(bytes, this.data_segments.length); |
227 for (seg of this.data_segments) { | 249 for (seg of this.data_segments) { |
228 emit_u32(bytes, seg.addr); | 250 emit_u32(bytes, seg.addr); |
229 emit_data_ref(bytes, seg.data); | 251 emit_data_ref(bytes, seg.data); |
230 emit_u32(bytes, seg.data.length); | 252 emit_u32(bytes, seg.data.length); |
231 emit_u8(bytes, seg.init ? 1 : 0); | 253 emit_u8(bytes, seg.init ? 1 : 0); |
232 } | 254 } |
233 } | 255 } |
234 | 256 |
| 257 // Emit any explicitly added sections |
| 258 for (exp of this.explicit) { |
| 259 if (debug) print("emitting explicit @ " + bytes.length); |
| 260 for (var i = 0; i < exp.length; i++) { |
| 261 emit_u8(bytes, exp[i]); |
| 262 } |
| 263 } |
| 264 |
235 // End the module. | 265 // End the module. |
236 if (debug) print("emitting end @ " + bytes.length); | 266 if (debug) print("emitting end @ " + bytes.length); |
237 emit_u8(bytes, kDeclEnd); | 267 emit_u8(bytes, kDeclEnd); |
238 | 268 |
239 // Collect references and canonicalize strings. | 269 // Collect references and canonicalize strings. |
240 var strings = new Object(); | 270 var strings = new Object(); |
241 var data_segments = []; | 271 var data_segments = []; |
242 var count = 0; | 272 var count = 0; |
243 for (var i = 0; i < bytes.length; i++) { | 273 for (var i = 0; i < bytes.length; i++) { |
244 var b = bytes[i]; | 274 var b = bytes[i]; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 var buffer = new ArrayBuffer(bytes.length); | 328 var buffer = new ArrayBuffer(bytes.length); |
299 var view = new Uint8Array(buffer); | 329 var view = new Uint8Array(buffer); |
300 for (var i = 0; i < bytes.length; i++) { | 330 for (var i = 0; i < bytes.length; i++) { |
301 var val = bytes[i]; | 331 var val = bytes[i]; |
302 if ((typeof val) == "string") val = val.charCodeAt(0); | 332 if ((typeof val) == "string") val = val.charCodeAt(0); |
303 view[i] = val | 0; | 333 view[i] = val | 0; |
304 } | 334 } |
305 return buffer; | 335 return buffer; |
306 } | 336 } |
307 | 337 |
308 WasmModuleBuilder.prototype.instantiate = function(ffi) { | 338 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
309 var buffer = this.toBuffer(); | 339 var buffer = this.toBuffer(); |
310 return _WASMEXP_.instantiateModule(buffer, ffi); | 340 if (memory != undefined) { |
| 341 return _WASMEXP_.instantiateModule(buffer, ffi, memory); |
| 342 } else { |
| 343 return _WASMEXP_.instantiateModule(buffer, ffi); |
| 344 } |
311 } | 345 } |
OLD | NEW |