| 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 // Used for encoding f32 and double constants to bits. | 5 // Used for encoding f32 and double constants to bits. |
| 6 let __buffer = new ArrayBuffer(8); | 6 let __buffer = new ArrayBuffer(8); |
| 7 let byte_view = new Int8Array(__buffer); | 7 let byte_view = new Int8Array(__buffer); |
| 8 let f32_view = new Float32Array(__buffer); | 8 let f32_view = new Float32Array(__buffer); |
| 9 let f64_view = new Float64Array(__buffer); | 9 let f64_view = new Float64Array(__buffer); |
| 10 | 10 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 // Copy the temporary buffer. | 76 // Copy the temporary buffer. |
| 77 this.push(...section); | 77 this.push(...section); |
| 78 } | 78 } |
| 79 } | 79 } |
| 80 | 80 |
| 81 class WasmFunctionBuilder { | 81 class WasmFunctionBuilder { |
| 82 constructor(module, name, type_index) { | 82 constructor(module, name, type_index) { |
| 83 this.module = module; | 83 this.module = module; |
| 84 this.name = name; | 84 this.name = name; |
| 85 this.type_index = type_index; | 85 this.type_index = type_index; |
| 86 this.body = []; |
| 86 } | 87 } |
| 87 | 88 |
| 88 exportAs(name) { | 89 exportAs(name) { |
| 89 this.module.exports.push({name: name, kind: kExternalFunction, index: this.i
ndex}); | 90 this.module.exports.push({name: name, kind: kExternalFunction, index: this.i
ndex}); |
| 90 return this; | 91 return this; |
| 91 } | 92 } |
| 92 | 93 |
| 93 exportFunc() { | 94 exportFunc() { |
| 94 this.exportAs(this.name); | 95 this.exportAs(this.name); |
| 95 return this; | 96 return this; |
| 96 } | 97 } |
| 97 | 98 |
| 98 addBody(body) { | 99 addBody(body) { |
| 99 this.body = body; | 100 this.body = body; |
| 100 return this; | 101 return this; |
| 101 } | 102 } |
| 102 | 103 |
| 103 addLocals(locals) { | 104 addLocals(locals) { |
| 104 this.locals = locals; | 105 this.locals = locals; |
| 105 return this; | 106 return this; |
| 106 } | 107 } |
| 108 |
| 109 end() { |
| 110 return this.module; |
| 111 } |
| 107 } | 112 } |
| 108 | 113 |
| 109 class WasmGlobalBuilder { | 114 class WasmGlobalBuilder { |
| 110 constructor(module, type, mutable) { | 115 constructor(module, type, mutable) { |
| 111 this.module = module; | 116 this.module = module; |
| 112 this.type = type; | 117 this.type = type; |
| 113 this.mutable = mutable; | 118 this.mutable = mutable; |
| 114 this.init = 0; | 119 this.init = 0; |
| 115 } | 120 } |
| 116 | 121 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 131 this.segments = []; | 136 this.segments = []; |
| 132 this.explicit = []; | 137 this.explicit = []; |
| 133 this.pad = null; | 138 this.pad = null; |
| 134 this.num_imported_funcs = 0; | 139 this.num_imported_funcs = 0; |
| 135 this.num_imported_globals = 0; | 140 this.num_imported_globals = 0; |
| 136 return this; | 141 return this; |
| 137 } | 142 } |
| 138 | 143 |
| 139 addStart(start_index) { | 144 addStart(start_index) { |
| 140 this.start_index = start_index; | 145 this.start_index = start_index; |
| 146 return this; |
| 141 } | 147 } |
| 142 | 148 |
| 143 addMemory(min, max, exp) { | 149 addMemory(min, max, exp) { |
| 144 this.memory = {min: min, max: max, exp: exp}; | 150 this.memory = {min: min, max: max, exp: exp}; |
| 145 return this; | 151 return this; |
| 146 } | 152 } |
| 147 | 153 |
| 148 addPadFunctionTable(size) { | 154 addPadFunctionTable(size) { |
| 149 this.pad = size; | 155 this.pad = size; |
| 150 return this; | 156 return this; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 addImportedGlobal(module, name, type) { | 196 addImportedGlobal(module, name, type) { |
| 191 let o = {module: module, name: name, kind: kExternalGlobal, type: type, | 197 let o = {module: module, name: name, kind: kExternalGlobal, type: type, |
| 192 mutable: false} | 198 mutable: false} |
| 193 this.imports.push(o); | 199 this.imports.push(o); |
| 194 return this.num_imported_globals++; | 200 return this.num_imported_globals++; |
| 195 } | 201 } |
| 196 | 202 |
| 197 addImportedMemory(module, name, initial = 0, maximum) { | 203 addImportedMemory(module, name, initial = 0, maximum) { |
| 198 let o = {module: module, name: name, kind: kExternalMemory, initial: initial
, maximum: maximum}; | 204 let o = {module: module, name: name, kind: kExternalMemory, initial: initial
, maximum: maximum}; |
| 199 this.imports.push(o); | 205 this.imports.push(o); |
| 206 return this; |
| 200 } | 207 } |
| 201 | 208 |
| 202 addDataSegment(addr, data, is_global = false) { | 209 addDataSegment(addr, data, is_global = false) { |
| 203 this.segments.push({addr: addr, data: data, is_global: is_global}); | 210 this.segments.push({addr: addr, data: data, is_global: is_global}); |
| 204 return this.segments.length - 1; | 211 return this.segments.length - 1; |
| 205 } | 212 } |
| 206 | 213 |
| 207 exportMemoryAs(name) { | 214 exportMemoryAs(name) { |
| 208 this.exports.push({name: name, kind: kExternalMemory, index: 0}); | 215 this.exports.push({name: name, kind: kExternalMemory, index: 0}); |
| 209 } | 216 } |
| 210 | 217 |
| 211 appendToTable(array) { | 218 appendToTable(array) { |
| 212 this.table.push(...array); | 219 this.table.push(...array); |
| 213 return this; | 220 return this; |
| 214 } | 221 } |
| 215 | 222 |
| 216 toArray(debug) { | 223 toArray(debug = false) { |
| 217 let binary = new Binary; | 224 let binary = new Binary; |
| 218 let wasm = this; | 225 let wasm = this; |
| 219 | 226 |
| 220 // Add header | 227 // Add header |
| 221 binary.emit_header(); | 228 binary.emit_header(); |
| 222 | 229 |
| 223 // Add type section | 230 // Add type section |
| 224 if (wasm.types.length > 0) { | 231 if (wasm.types.length > 0) { |
| 225 if (debug) print("emitting types @ " + binary.length); | 232 if (debug) print("emitting types @ " + binary.length); |
| 226 binary.emit_section(kTypeSectionCode, section => { | 233 binary.emit_section(kTypeSectionCode, section => { |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 var name = func.name == undefined ? "" : func.name; | 483 var name = func.name == undefined ? "" : func.name; |
| 477 section.emit_string(name); | 484 section.emit_string(name); |
| 478 section.emit_u8(0); // local names count == 0 | 485 section.emit_u8(0); // local names count == 0 |
| 479 } | 486 } |
| 480 }); | 487 }); |
| 481 } | 488 } |
| 482 | 489 |
| 483 return binary; | 490 return binary; |
| 484 } | 491 } |
| 485 | 492 |
| 486 toBuffer(debug) { | 493 toBuffer(debug = false) { |
| 487 let bytes = this.toArray(debug); | 494 let bytes = this.toArray(debug); |
| 488 let buffer = new ArrayBuffer(bytes.length); | 495 let buffer = new ArrayBuffer(bytes.length); |
| 489 let view = new Uint8Array(buffer); | 496 let view = new Uint8Array(buffer); |
| 490 for (let i = 0; i < bytes.length; i++) { | 497 for (let i = 0; i < bytes.length; i++) { |
| 491 let val = bytes[i]; | 498 let val = bytes[i]; |
| 492 if ((typeof val) == "string") val = val.charCodeAt(0); | 499 if ((typeof val) == "string") val = val.charCodeAt(0); |
| 493 view[i] = val | 0; | 500 view[i] = val | 0; |
| 494 } | 501 } |
| 495 return buffer; | 502 return buffer; |
| 496 } | 503 } |
| 497 | 504 |
| 498 instantiate(...args) { | 505 instantiate(...args) { |
| 499 let module = new WebAssembly.Module(this.toBuffer()); | 506 let module = new WebAssembly.Module(this.toBuffer()); |
| 500 let instance = new WebAssembly.Instance(module, ...args); | 507 let instance = new WebAssembly.Instance(module, ...args); |
| 501 return instance; | 508 return instance; |
| 502 } | 509 } |
| 503 } | 510 } |
| OLD | NEW |