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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 section.emit_varint(func.type_index); | 220 section.emit_varint(func.type_index); |
221 } | 221 } |
222 }); | 222 }); |
223 } | 223 } |
224 | 224 |
225 // Add table. | 225 // Add table. |
226 if (wasm.table.length > 0) { | 226 if (wasm.table.length > 0) { |
227 if (debug) print("emitting table @ " + binary.length); | 227 if (debug) print("emitting table @ " + binary.length); |
228 binary.emit_section(kDeclTable, section => { | 228 binary.emit_section(kDeclTable, section => { |
229 section.emit_varint(wasm.table.length); | 229 section.emit_varint(wasm.table.length); |
| 230 if (wasm.pad !== null) { |
| 231 if (debug) print("emitting table padding @ " + binary.length); |
| 232 section.emit_varint(wasm.pad); |
| 233 } |
230 for (let index of wasm.table) { | 234 for (let index of wasm.table) { |
231 section.emit_varint(index); | 235 section.emit_varint(index); |
232 } | 236 } |
233 }); | 237 }); |
234 } | 238 } |
235 | 239 |
236 // Add memory section | 240 // Add memory section |
237 if (wasm.memory != undefined) { | 241 if (wasm.memory != undefined) { |
238 if (debug) print("emitting memory @ " + binary.length); | 242 if (debug) print("emitting memory @ " + binary.length); |
239 binary.emit_section(kDeclMemory, section => { | 243 binary.emit_section(kDeclMemory, section => { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 binary.emit_section(kDeclNames, section => { | 335 binary.emit_section(kDeclNames, section => { |
332 section.emit_varint(wasm.functions.length); | 336 section.emit_varint(wasm.functions.length); |
333 for (let func of wasm.functions) { | 337 for (let func of wasm.functions) { |
334 var name = func.name == undefined ? "" : func.name; | 338 var name = func.name == undefined ? "" : func.name; |
335 section.emit_string(name); | 339 section.emit_string(name); |
336 section.emit_u8(0); // local names count == 0 | 340 section.emit_u8(0); // local names count == 0 |
337 } | 341 } |
338 }); | 342 }); |
339 } | 343 } |
340 | 344 |
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 | |
354 return binary; | 345 return binary; |
355 } | 346 } |
356 | 347 |
357 toBuffer(debug) { | 348 toBuffer(debug) { |
358 let bytes = this.toArray(debug); | 349 let bytes = this.toArray(debug); |
359 let buffer = new ArrayBuffer(bytes.length); | 350 let buffer = new ArrayBuffer(bytes.length); |
360 let view = new Uint8Array(buffer); | 351 let view = new Uint8Array(buffer); |
361 for (let i = 0; i < bytes.length; i++) { | 352 for (let i = 0; i < bytes.length; i++) { |
362 let val = bytes[i]; | 353 let val = bytes[i]; |
363 if ((typeof val) == "string") val = val.charCodeAt(0); | 354 if ((typeof val) == "string") val = val.charCodeAt(0); |
364 view[i] = val | 0; | 355 view[i] = val | 0; |
365 } | 356 } |
366 return buffer; | 357 return buffer; |
367 } | 358 } |
368 | 359 |
369 instantiate(...args) { | 360 instantiate(...args) { |
370 let module = new WebAssembly.Module(this.toBuffer()); | 361 let module = new WebAssembly.Module(this.toBuffer()); |
371 let instance = new WebAssembly.Instance(module, ...args); | 362 let instance = new WebAssembly.Instance(module, ...args); |
372 return instance; | 363 return instance; |
373 } | 364 } |
374 } | 365 } |
OLD | NEW |