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 WasmFunctionBuilder(name, sig_index) { | 5 function WasmFunctionBuilder(name, sig_index) { |
6 this.name = name; | 6 this.name = name; |
7 this.sig_index = sig_index; | 7 this.sig_index = sig_index; |
8 this.exports = []; | 8 this.exports = []; |
9 } | 9 } |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 } | 29 } |
30 | 30 |
31 function WasmModuleBuilder() { | 31 function WasmModuleBuilder() { |
32 this.signatures = []; | 32 this.signatures = []; |
33 this.imports = []; | 33 this.imports = []; |
34 this.functions = []; | 34 this.functions = []; |
35 this.exports = []; | 35 this.exports = []; |
36 this.function_table = []; | 36 this.function_table = []; |
37 this.data_segments = []; | 37 this.data_segments = []; |
38 this.explicit = []; | 38 this.explicit = []; |
| 39 this.pad = null; |
39 return this; | 40 return this; |
40 } | 41 } |
41 | 42 |
42 WasmModuleBuilder.prototype.addStart = function(start_index) { | 43 WasmModuleBuilder.prototype.addStart = function(start_index) { |
43 this.start_index = start_index; | 44 this.start_index = start_index; |
44 } | 45 } |
45 | 46 |
46 WasmModuleBuilder.prototype.addMemory = function(min, max, exp) { | 47 WasmModuleBuilder.prototype.addMemory = function(min, max, exp) { |
47 this.memory = {min: min, max: max, exp: exp}; | 48 this.memory = {min: min, max: max, exp: exp}; |
48 return this; | 49 return this; |
49 } | 50 } |
50 | 51 |
| 52 WasmModuleBuilder.prototype.addPadFunctionTable = function(size) { |
| 53 this.pad = size; |
| 54 return this; |
| 55 } |
| 56 |
51 WasmModuleBuilder.prototype.addExplicitSection = function(bytes) { | 57 WasmModuleBuilder.prototype.addExplicitSection = function(bytes) { |
52 this.explicit.push(bytes); | 58 this.explicit.push(bytes); |
53 return this; | 59 return this; |
54 } | 60 } |
55 | 61 |
56 // Add a signature; format is [param_count, param0, param1, ..., retcount, ret0] | 62 // Add a signature; format is [param_count, param0, param1, ..., retcount, ret0] |
57 WasmModuleBuilder.prototype.addSignature = function(sig) { | 63 WasmModuleBuilder.prototype.addSignature = function(sig) { |
58 // TODO: canonicalize signatures? | 64 // TODO: canonicalize signatures? |
59 this.signatures.push(sig); | 65 this.signatures.push(sig); |
60 return this.signatures.length - 1; | 66 return this.signatures.length - 1; |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 emit_section(bytes, kDeclNames, function(bytes) { | 322 emit_section(bytes, kDeclNames, function(bytes) { |
317 emit_varint(bytes, wasm.functions.length); | 323 emit_varint(bytes, wasm.functions.length); |
318 for (func of wasm.functions) { | 324 for (func of wasm.functions) { |
319 var name = func.name == undefined ? "" : func.name; | 325 var name = func.name == undefined ? "" : func.name; |
320 emit_string(bytes, name); | 326 emit_string(bytes, name); |
321 emit_u8(bytes, 0); // local names count == 0 | 327 emit_u8(bytes, 0); // local names count == 0 |
322 } | 328 } |
323 }); | 329 }); |
324 } | 330 } |
325 | 331 |
| 332 // Add an indirect function table pad section. |
| 333 if (wasm.pad !== null) { |
| 334 if (debug) |
| 335 print("emitting indirect function table pad @ " + bytes.length); |
| 336 emit_section(bytes, kDeclFunctionTablePad, function(bytes) { |
| 337 emit_varint(bytes, wasm.pad); |
| 338 }); |
| 339 } |
| 340 |
326 // End the module. | 341 // End the module. |
327 if (debug) print("emitting end @ " + bytes.length); | 342 if (debug) print("emitting end @ " + bytes.length); |
328 emit_section(bytes, kDeclEnd, function(bytes) {}); | 343 emit_section(bytes, kDeclEnd, function(bytes) {}); |
329 | 344 |
330 return bytes; | 345 return bytes; |
331 } | 346 } |
332 | 347 |
333 WasmModuleBuilder.prototype.toBuffer = function(debug) { | 348 WasmModuleBuilder.prototype.toBuffer = function(debug) { |
334 var bytes = this.toArray(debug); | 349 var bytes = this.toArray(debug); |
335 var buffer = new ArrayBuffer(bytes.length); | 350 var buffer = new ArrayBuffer(bytes.length); |
336 var view = new Uint8Array(buffer); | 351 var view = new Uint8Array(buffer); |
337 for (var i = 0; i < bytes.length; i++) { | 352 for (var i = 0; i < bytes.length; i++) { |
338 var val = bytes[i]; | 353 var val = bytes[i]; |
339 if ((typeof val) == "string") val = val.charCodeAt(0); | 354 if ((typeof val) == "string") val = val.charCodeAt(0); |
340 view[i] = val | 0; | 355 view[i] = val | 0; |
341 } | 356 } |
342 return buffer; | 357 return buffer; |
343 } | 358 } |
344 | 359 |
345 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 360 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
346 var buffer = this.toBuffer(); | 361 var buffer = this.toBuffer(); |
347 if (memory != undefined) { | 362 if (memory != undefined) { |
348 return Wasm.instantiateModule(buffer, ffi, memory); | 363 return Wasm.instantiateModule(buffer, ffi, memory); |
349 } else { | 364 } else { |
350 return Wasm.instantiateModule(buffer, ffi); | 365 return Wasm.instantiateModule(buffer, ffi); |
351 } | 366 } |
352 } | 367 } |
OLD | NEW |