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 class Binary extends Array { | 5 class Binary extends Array { |
6 emit_u8(val) { | 6 emit_u8(val) { |
7 this.push(val); | 7 this.push(val); |
8 } | 8 } |
9 | 9 |
10 emit_u16(val) { | 10 emit_u16(val) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 class WasmModuleBuilder { | 103 class WasmModuleBuilder { |
104 constructor() { | 104 constructor() { |
105 this.types = []; | 105 this.types = []; |
106 this.imports = []; | 106 this.imports = []; |
107 this.functions = []; | 107 this.functions = []; |
108 this.exports = []; | 108 this.exports = []; |
109 this.table = []; | 109 this.table = []; |
110 this.segments = []; | 110 this.segments = []; |
111 this.explicit = []; | 111 this.explicit = []; |
| 112 this.pad = null; |
| 113 return this; |
112 } | 114 } |
113 | 115 |
114 addStart(start_index) { | 116 addStart(start_index) { |
115 this.start_index = start_index; | 117 this.start_index = start_index; |
116 } | 118 } |
117 | 119 |
118 addMemory(min, max, exp) { | 120 addMemory(min, max, exp) { |
119 this.memory = {min: min, max: max, exp: exp}; | 121 this.memory = {min: min, max: max, exp: exp}; |
120 return this; | 122 return this; |
121 } | 123 } |
122 | 124 |
| 125 addPadFunctionTable(size) { |
| 126 this.pad = size; |
| 127 return this; |
| 128 } |
| 129 |
123 addExplicitSection(bytes) { | 130 addExplicitSection(bytes) { |
124 this.explicit.push(bytes); | 131 this.explicit.push(bytes); |
125 return this; | 132 return this; |
126 } | 133 } |
127 | 134 |
128 addType(type) { | 135 addType(type) { |
129 // TODO: canonicalize types? | 136 // TODO: canonicalize types? |
130 this.types.push(type); | 137 this.types.push(type); |
131 return this.types.length - 1; | 138 return this.types.length - 1; |
132 } | 139 } |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 binary.emit_section(kDeclNames, section => { | 331 binary.emit_section(kDeclNames, section => { |
325 section.emit_varint(wasm.functions.length); | 332 section.emit_varint(wasm.functions.length); |
326 for (let func of wasm.functions) { | 333 for (let func of wasm.functions) { |
327 var name = func.name == undefined ? "" : func.name; | 334 var name = func.name == undefined ? "" : func.name; |
328 section.emit_string(name); | 335 section.emit_string(name); |
329 section.emit_u8(0); // local names count == 0 | 336 section.emit_u8(0); // local names count == 0 |
330 } | 337 } |
331 }); | 338 }); |
332 } | 339 } |
333 | 340 |
| 341 // Add an indirect function table pad section. |
| 342 if (wasm.pad !== null) { |
| 343 if (debug) |
| 344 print("emitting indirect function table pad @ " + binary.length); |
| 345 binary.emit_section(kDeclFunctionTablePad, section => { |
| 346 section.emit_varint(wasm.pad); |
| 347 }); |
| 348 } |
| 349 |
| 350 // End the module. |
| 351 if (debug) print("emitting end @ " + binary.length); |
| 352 binary.emit_section(kDeclEnd, section => {}); |
| 353 |
334 return binary; | 354 return binary; |
335 } | 355 } |
336 | 356 |
337 toBuffer(debug) { | 357 toBuffer(debug) { |
338 let bytes = this.toArray(debug); | 358 let bytes = this.toArray(debug); |
339 let buffer = new ArrayBuffer(bytes.length); | 359 let buffer = new ArrayBuffer(bytes.length); |
340 let view = new Uint8Array(buffer); | 360 let view = new Uint8Array(buffer); |
341 for (let i = 0; i < bytes.length; i++) { | 361 for (let i = 0; i < bytes.length; i++) { |
342 let val = bytes[i]; | 362 let val = bytes[i]; |
343 if ((typeof val) == "string") val = val.charCodeAt(0); | 363 if ((typeof val) == "string") val = val.charCodeAt(0); |
344 view[i] = val | 0; | 364 view[i] = val | 0; |
345 } | 365 } |
346 return buffer; | 366 return buffer; |
347 } | 367 } |
348 | 368 |
349 instantiate(...args) { | 369 instantiate(...args) { |
350 let module = new WebAssembly.Module(this.toBuffer()); | 370 let module = new WebAssembly.Module(this.toBuffer()); |
351 let instance = new WebAssembly.Instance(module, ...args); | 371 let instance = new WebAssembly.Instance(module, ...args); |
352 return instance; | 372 return instance; |
353 } | 373 } |
354 } | 374 } |
OLD | NEW |