Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 class Binary extends Array { | |
|
Clemens Hammacher
2017/05/31 07:56:37
Please reduce this test case to also use wasm-modu
gdeepti
2017/06/01 07:01:37
Done.
| |
| 6 emit_u8(val) { | |
| 7 this.push(val); | |
| 8 } | |
| 9 emit_u32v(val) { | |
| 10 while (true) { | |
| 11 let v = val & 0xff; | |
| 12 this.push(v); | |
| 13 break; | |
| 14 } | |
| 15 } | |
| 16 emit_bytes() { | |
| 17 } | |
| 18 emit_string(string) { | |
| 19 let string_utf8 = unescape(string); | |
| 20 this.emit_u32v(string_utf8.length); | |
| 21 for (let i = 0; i < string_utf8.length; i++) { | |
| 22 this.emit_u8(string_utf8.charCodeAt(i)); | |
| 23 } | |
| 24 } | |
| 25 emit_header() { | |
| 26 this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3, | |
| 27 kWasmV0, kWasmV1, kWasmV2, kWasmV3); | |
| 28 } | |
| 29 emit_section(section_code, content_generator) { | |
| 30 this.emit_u8(section_code); | |
| 31 let section = new Binary; | |
| 32 content_generator(section); | |
| 33 this.emit_u32v(section.length); | |
| 34 this.push(...section); | |
| 35 } | |
| 36 } | |
| 37 class WasmFunctionBuilder { | |
| 38 constructor() { | |
| 39 } | |
| 40 } | |
| 41 class WasmModuleBuilder { | |
| 42 constructor() { | |
| 43 this.imports = []; | |
| 44 this.exports = []; | |
| 45 } | |
| 46 addMemory(min, max, exp) { | |
| 47 this.memory = {}; | |
| 48 } | |
| 49 addFunction() { | |
| 50 } | |
| 51 addImportedGlobal() { | |
| 52 let o = { | |
| 53 mutable: false} | |
| 54 } | |
| 55 addImportedMemory(module = "", name, maximum) { | |
| 56 let o = {module: module, name: name, kind: kExternalMemory, | |
| 57 maximum: maximum}; | |
| 58 this.imports.push(o); | |
| 59 } | |
| 60 exportMemoryAs(name) { | |
| 61 this.exports.push({name: name, kind: kExternalMemory}); | |
| 62 } | |
| 63 toArray() { | |
| 64 let binary = new Binary; | |
| 65 let wasm = this; | |
| 66 binary.emit_header(); | |
| 67 section => { | |
| 68 }; | |
| 69 binary.emit_section(kImportSectionCode, section => { | |
| 70 section.emit_u32v(wasm.imports.length); | |
| 71 for (let imp of wasm.imports) { | |
| 72 section.emit_string(imp.module); | |
| 73 section.emit_string(imp.name || ''); | |
| 74 section.emit_u8(imp.kind); | |
| 75 section.emit_u8(); // flags | |
| 76 section.emit_u32v(); // initial | |
| 77 } | |
| 78 }); | |
| 79 if (wasm.memory !== undefined) { | |
| 80 binary.emit_section(kMemorySectionCode, section => { | |
| 81 section.emit_u32v(kResizableMaximumFlag); | |
| 82 section.emit_u32v(); | |
| 83 section.emit_u32v(); | |
| 84 }); | |
| 85 section => { | |
| 86 switch (global.type) { | |
| 87 } | |
| 88 }; | |
| 89 } | |
| 90 var mem_export = (wasm.memory !== undefined && wasm.memory.exp); | |
| 91 var exports_count = wasm.exports.length + (mem_export ? 1 : 0); | |
| 92 binary.emit_section(kExportSectionCode, section => { | |
| 93 section.emit_u32v(exports_count); | |
| 94 for (let exp of wasm.exports) { | |
| 95 section.emit_string(exp.name); | |
| 96 section.emit_u8(exp.kind); | |
| 97 section.emit_u32v(); | |
| 98 } | |
| 99 }); | |
| 100 return binary; | |
| 101 } | |
| 102 toBuffer() { | |
| 103 let bytes = this.toArray(); | |
| 104 let buffer = new ArrayBuffer(bytes.length); | |
| 105 let view = new Uint8Array(buffer); | |
| 106 for (let i = 0; i < bytes.length; i++) { | |
| 107 let val = bytes[i]; | |
| 108 view[i] = val | 0; | |
| 109 } | |
| 110 return buffer; | |
| 111 } | |
| 112 instantiate(ffi) { | |
| 113 let module = new WebAssembly.Module(this.toBuffer()); | |
| 114 let instance = new WebAssembly.Instance(module, ffi); | |
| 115 return instance; | |
| 116 } | |
| 117 } | |
| 118 var kWasmH0 = 0; | |
| 119 var kWasmH1 = 0x61; | |
| 120 var kWasmH2 = 0x73; | |
| 121 var kWasmH3 = 0x6d; | |
| 122 var kWasmV0 = 0x1; | |
| 123 var kWasmV1 = 0; | |
| 124 var kWasmV2 = 0; | |
| 125 var kWasmV3 = 0; | |
| 126 let kImportSectionCode = 2; // Import declarations | |
| 127 let kMemorySectionCode = 5; // Memory attributes | |
| 128 let kExportSectionCode = 7; // Exports | |
| 129 let kResizableMaximumFlag = 1; | |
| 130 let kExternalMemory = 2; | |
| 131 function makeSig(params, results) { | |
| 132 } | |
| 133 { | |
| 134 let builder = new WasmModuleBuilder(); | |
| 135 builder.addMemory(); | |
| 136 builder.exportMemoryAs("exported_mem"); | |
| 137 i1 = builder.instantiate(); | |
| 138 } | |
| 139 { | |
| 140 let builder = new WasmModuleBuilder(); | |
| 141 builder.addImportedMemory("fil", "imported_mem"); | |
| 142 i2 = builder.instantiate({fil: {imported_mem: i1.exports.exported_mem}}); | |
| 143 } | |
| OLD | NEW |