Chromium Code Reviews| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 addLocals(locals) { | 97 addLocals(locals) { |
| 98 this.locals = locals; | 98 this.locals = locals; |
| 99 return this; | 99 return this; |
| 100 } | 100 } |
| 101 } | 101 } |
| 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.globals = []; | |
| 107 this.functions = []; | 108 this.functions = []; |
| 108 this.exports = []; | 109 this.exports = []; |
| 109 this.table = []; | 110 this.table = []; |
| 110 this.segments = []; | 111 this.segments = []; |
| 111 this.explicit = []; | 112 this.explicit = []; |
| 112 this.pad = null; | 113 this.pad = null; |
| 113 return this; | 114 return this; |
| 114 } | 115 } |
| 115 | 116 |
| 116 addStart(start_index) { | 117 addStart(start_index) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 131 this.explicit.push(bytes); | 132 this.explicit.push(bytes); |
| 132 return this; | 133 return this; |
| 133 } | 134 } |
| 134 | 135 |
| 135 addType(type) { | 136 addType(type) { |
| 136 // TODO: canonicalize types? | 137 // TODO: canonicalize types? |
| 137 this.types.push(type); | 138 this.types.push(type); |
| 138 return this.types.length - 1; | 139 return this.types.length - 1; |
| 139 } | 140 } |
| 140 | 141 |
| 142 addGlobal(local_type) { | |
| 143 this.globals.push(local_type); | |
| 144 return this.globals.length - 1; | |
| 145 } | |
| 146 | |
| 141 addFunction(name, type) { | 147 addFunction(name, type) { |
| 142 let type_index = (typeof type) == "number" ? type : this.addType(type); | 148 let type_index = (typeof type) == "number" ? type : this.addType(type); |
| 143 let func = new WasmFunctionBuilder(name, type_index); | 149 let func = new WasmFunctionBuilder(name, type_index); |
| 144 func.index = this.functions.length; | 150 func.index = this.functions.length; |
| 145 this.functions.push(func); | 151 this.functions.push(func); |
| 146 return func; | 152 return func; |
| 147 } | 153 } |
| 148 | 154 |
| 149 addImportWithModule(module, name, type) { | 155 addImportWithModule(module, name, type) { |
| 150 let type_index = (typeof type) == "number" ? type : this.addType(type); | 156 let type_index = (typeof type) == "number" ? type : this.addType(type); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 section.emit_u8(param); | 191 section.emit_u8(param); |
| 186 } | 192 } |
| 187 section.emit_varint(type.results.length); | 193 section.emit_varint(type.results.length); |
| 188 for (let result of type.results) { | 194 for (let result of type.results) { |
| 189 section.emit_u8(result); | 195 section.emit_u8(result); |
| 190 } | 196 } |
| 191 } | 197 } |
| 192 }); | 198 }); |
| 193 } | 199 } |
| 194 | 200 |
| 201 if (wasm.globals.length > 0) { | |
| 202 if (debug) print ("emitting globals @ " + binary.length); | |
| 203 binary.emit_section(kDeclGlobals, section => { | |
| 204 section.emit_varint(wasm.globals.length); | |
| 205 for (let global_type of wasm.globals) { | |
| 206 section.emit_varint(0); //length of global name | |
|
bradnelson
2016/08/30 23:36:20
Formatting on this comment is odd.
Mircea Trofin
2016/08/31 00:08:15
Done.
| |
| 207 section.emit_u8(global_type); | |
| 208 section.emit_u8(false); // we do not support exported globals | |
|
bradnelson
2016/08/30 23:36:21
These should be 2 spaces.
Mircea Trofin
2016/08/31 00:08:15
Done.
| |
| 209 } | |
| 210 }); | |
| 211 } | |
| 212 | |
| 195 // Add imports section | 213 // Add imports section |
| 196 if (wasm.imports.length > 0) { | 214 if (wasm.imports.length > 0) { |
| 197 if (debug) print("emitting imports @ " + binary.length); | 215 if (debug) print("emitting imports @ " + binary.length); |
| 198 binary.emit_section(kDeclImports, section => { | 216 binary.emit_section(kDeclImports, section => { |
| 199 section.emit_varint(wasm.imports.length); | 217 section.emit_varint(wasm.imports.length); |
| 200 for (let imp of wasm.imports) { | 218 for (let imp of wasm.imports) { |
| 201 section.emit_varint(imp.type); | 219 section.emit_varint(imp.type); |
| 202 section.emit_string(imp.module); | 220 section.emit_string(imp.module); |
| 203 section.emit_string(imp.name || ''); | 221 section.emit_string(imp.name || ''); |
| 204 } | 222 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 } | 374 } |
| 357 return buffer; | 375 return buffer; |
| 358 } | 376 } |
| 359 | 377 |
| 360 instantiate(...args) { | 378 instantiate(...args) { |
| 361 let module = new WebAssembly.Module(this.toBuffer()); | 379 let module = new WebAssembly.Module(this.toBuffer()); |
| 362 let instance = new WebAssembly.Instance(module, ...args); | 380 let instance = new WebAssembly.Instance(module, ...args); |
| 363 return instance; | 381 return instance; |
| 364 } | 382 } |
| 365 } | 383 } |
| OLD | NEW |