| 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 function WasmFunctionBuilder(name, sig_index) { | 5 function WasmFunctionBuilder(name, sig_index) { |
| 6 this.name = name; | 6 this.name = name; |
| 7 this.sig_index = sig_index; | 7 this.sig_index = sig_index; |
| 8 this.exports = []; | 8 this.exports = []; |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 WasmModuleBuilder.prototype.addMemory = function(min, max, exp) { | 46 WasmModuleBuilder.prototype.addMemory = function(min, max, exp) { |
| 47 this.memory = {min: min, max: max, exp: exp}; | 47 this.memory = {min: min, max: max, exp: exp}; |
| 48 return this; | 48 return this; |
| 49 } | 49 } |
| 50 | 50 |
| 51 WasmModuleBuilder.prototype.addExplicitSection = function(bytes) { | 51 WasmModuleBuilder.prototype.addExplicitSection = function(bytes) { |
| 52 this.explicit.push(bytes); | 52 this.explicit.push(bytes); |
| 53 return this; | 53 return this; |
| 54 } | 54 } |
| 55 | 55 |
| 56 // Add a signature; format is [rettype, param0, param1, ...] | 56 // Add a signature; format is [param_count, param0, param1, ..., retcount, ret0] |
| 57 WasmModuleBuilder.prototype.addSignature = function(sig) { | 57 WasmModuleBuilder.prototype.addSignature = function(sig) { |
| 58 // TODO: canonicalize signatures? | 58 // TODO: canonicalize signatures? |
| 59 this.signatures.push(sig); | 59 this.signatures.push(sig); |
| 60 return this.signatures.length - 1; | 60 return this.signatures.length - 1; |
| 61 } | 61 } |
| 62 | 62 |
| 63 WasmModuleBuilder.prototype.addFunction = function(name, sig) { | 63 WasmModuleBuilder.prototype.addFunction = function(name, sig) { |
| 64 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); | 64 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); |
| 65 var func = new WasmFunctionBuilder(name, sig_index); | 65 var func = new WasmFunctionBuilder(name, sig_index); |
| 66 func.index = this.functions.length; | 66 func.index = this.functions.length; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 | 127 |
| 128 function emit_bytes(bytes, data) { | 128 function emit_bytes(bytes, data) { |
| 129 for (var i = 0; i < data.length; i++) { | 129 for (var i = 0; i < data.length; i++) { |
| 130 bytes.push(data[i] & 0xff); | 130 bytes.push(data[i] & 0xff); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 function emit_section(bytes, section_code, content_generator) { | 134 function emit_section(bytes, section_code, content_generator) { |
| 135 // Start the section in a temporary buffer: its full length isn't know yet. | 135 // Emit section name. |
| 136 emit_string(bytes, section_names[section_code]); |
| 137 // Emit the section to a temporary buffer: its full length isn't know yet. |
| 136 var tmp_bytes = []; | 138 var tmp_bytes = []; |
| 137 emit_string(tmp_bytes, section_names[section_code]); | |
| 138 content_generator(tmp_bytes); | 139 content_generator(tmp_bytes); |
| 139 // Now that we know the section length, emit it and copy the section. | 140 // Emit section length. |
| 140 emit_varint(bytes, tmp_bytes.length); | 141 emit_varint(bytes, tmp_bytes.length); |
| 142 // Copy the temporary buffer. |
| 141 Array.prototype.push.apply(bytes, tmp_bytes); | 143 Array.prototype.push.apply(bytes, tmp_bytes); |
| 142 } | 144 } |
| 143 | 145 |
| 144 WasmModuleBuilder.prototype.toArray = function(debug) { | 146 WasmModuleBuilder.prototype.toArray = function(debug) { |
| 145 // Add header bytes | 147 // Add header bytes |
| 146 var bytes = []; | 148 var bytes = []; |
| 147 bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3, | 149 bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3, |
| 148 kWasmV0, kWasmV1, kWasmV2, kWasmV3]); | 150 kWasmV0, kWasmV1, kWasmV2, kWasmV3]); |
| 149 | 151 |
| 150 var wasm = this; | 152 var wasm = this; |
| 151 | 153 |
| 152 // Add signatures section | 154 // Add signatures section |
| 153 if (wasm.signatures.length > 0) { | 155 if (wasm.signatures.length > 0) { |
| 154 if (debug) print("emitting signatures @ " + bytes.length); | 156 if (debug) print("emitting signatures @ " + bytes.length); |
| 155 emit_section(bytes, kDeclSignatures, function(bytes) { | 157 emit_section(bytes, kDeclSignatures, function(bytes) { |
| 156 emit_varint(bytes, wasm.signatures.length); | 158 emit_varint(bytes, wasm.signatures.length); |
| 157 for (sig of wasm.signatures) { | 159 for (sig of wasm.signatures) { |
| 158 var params = sig.length - 1; | 160 emit_u8(bytes, kWasmFunctionTypeForm); |
| 159 emit_varint(bytes, params); | |
| 160 for (var j = 0; j < sig.length; j++) { | 161 for (var j = 0; j < sig.length; j++) { |
| 161 emit_u8(bytes, sig[j]); | 162 emit_u8(bytes, sig[j]); |
| 162 } | 163 } |
| 163 } | 164 } |
| 164 }); | 165 }); |
| 165 } | 166 } |
| 166 | 167 |
| 167 // Add imports section | 168 // Add imports section |
| 168 if (wasm.imports.length > 0) { | 169 if (wasm.imports.length > 0) { |
| 169 if (debug) print("emitting imports @ " + bytes.length); | 170 if (debug) print("emitting imports @ " + bytes.length); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 } | 335 } |
| 335 | 336 |
| 336 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 337 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
| 337 var buffer = this.toBuffer(); | 338 var buffer = this.toBuffer(); |
| 338 if (memory != undefined) { | 339 if (memory != undefined) { |
| 339 return Wasm.instantiateModule(buffer, ffi, memory); | 340 return Wasm.instantiateModule(buffer, ffi, memory); |
| 340 } else { | 341 } else { |
| 341 return Wasm.instantiateModule(buffer, ffi); | 342 return Wasm.instantiateModule(buffer, ffi); |
| 342 } | 343 } |
| 343 } | 344 } |
| OLD | NEW |