| 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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 return func; | 68 return func; |
| 69 } | 69 } |
| 70 | 70 |
| 71 WasmModuleBuilder.prototype.addImportWithModule = function(module, name, sig) { | 71 WasmModuleBuilder.prototype.addImportWithModule = function(module, name, sig) { |
| 72 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); | 72 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); |
| 73 this.imports.push({module: module, name: name, sig_index: sig_index}); | 73 this.imports.push({module: module, name: name, sig_index: sig_index}); |
| 74 return this.imports.length - 1; | 74 return this.imports.length - 1; |
| 75 } | 75 } |
| 76 | 76 |
| 77 WasmModuleBuilder.prototype.addImport = function(name, sig) { | 77 WasmModuleBuilder.prototype.addImport = function(name, sig) { |
| 78 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig); | 78 this.addImportWithModule(name, undefined, sig); |
| 79 this.imports.push({module: name, name: undefined, sig_index: sig_index}); | |
| 80 return this.imports.length - 1; | |
| 81 } | 79 } |
| 82 | 80 |
| 83 WasmModuleBuilder.prototype.addDataSegment = function(addr, data, init) { | 81 WasmModuleBuilder.prototype.addDataSegment = function(addr, data, init) { |
| 84 this.data_segments.push({addr: addr, data: data, init: init}); | 82 this.data_segments.push({addr: addr, data: data, init: init}); |
| 85 return this.data_segments.length - 1; | 83 return this.data_segments.length - 1; |
| 86 } | 84 } |
| 87 | 85 |
| 88 WasmModuleBuilder.prototype.appendToFunctionTable = function(array) { | 86 WasmModuleBuilder.prototype.appendToFunctionTable = function(array) { |
| 89 this.function_table = this.function_table.concat(array); | 87 this.function_table = this.function_table.concat(array); |
| 90 return this; | 88 return this; |
| 91 } | 89 } |
| 92 | 90 |
| 93 function emit_u8(bytes, val) { | 91 function emit_u8(bytes, val) { |
| 94 bytes.push(val & 0xff); | 92 bytes.push(val & 0xff); |
| 95 } | 93 } |
| 96 | 94 |
| 97 function emit_u16(bytes, val) { | 95 function emit_u16(bytes, val) { |
| 98 bytes.push(val & 0xff); | 96 bytes.push(val & 0xff); |
| 99 bytes.push((val >> 8) & 0xff); | 97 bytes.push((val >> 8) & 0xff); |
| 100 } | 98 } |
| 101 | 99 |
| 102 function emit_u32(bytes, val) { | 100 function emit_u32(bytes, val) { |
| 103 bytes.push(val & 0xff); | 101 bytes.push(val & 0xff); |
| 104 bytes.push((val >> 8) & 0xff); | 102 bytes.push((val >> 8) & 0xff); |
| 105 bytes.push((val >> 16) & 0xff); | 103 bytes.push((val >> 16) & 0xff); |
| 106 bytes.push((val >> 24) & 0xff); | 104 bytes.push((val >> 24) & 0xff); |
| 107 } | 105 } |
| 108 | 106 |
| 109 function emit_string(bytes, string) { | 107 function emit_string(bytes, string) { |
| 110 emit_varint(bytes, string.length); | 108 // When testing illegal names, we pass a byte array directly. |
| 111 for (var i = 0; i < string.length; i++) { | 109 if (string instanceof Array) { |
| 112 emit_u8(bytes, string.charCodeAt(i)); | 110 emit_varint(bytes, string.length); |
| 111 emit_bytes(bytes, string); |
| 112 return; |
| 113 } |
| 114 |
| 115 // This is the hacky way to convert a JavaScript scring to a UTF8 encoded |
| 116 // string only containing single-byte characters. |
| 117 var string_utf8 = unescape(encodeURIComponent(string)); |
| 118 emit_varint(bytes, string_utf8.length); |
| 119 for (var i = 0; i < string_utf8.length; i++) { |
| 120 emit_u8(bytes, string_utf8.charCodeAt(i)); |
| 113 } | 121 } |
| 114 } | 122 } |
| 115 | 123 |
| 116 function emit_varint(bytes, val) { | 124 function emit_varint(bytes, val) { |
| 117 while (true) { | 125 while (true) { |
| 118 var v = val & 0xff; | 126 var v = val & 0xff; |
| 119 val = val >>> 7; | 127 val = val >>> 7; |
| 120 if (val == 0) { | 128 if (val == 0) { |
| 121 bytes.push(v); | 129 bytes.push(v); |
| 122 break; | 130 break; |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 emit_bytes(bytes, exp); | 310 emit_bytes(bytes, exp); |
| 303 } | 311 } |
| 304 | 312 |
| 305 // Add function names. | 313 // Add function names. |
| 306 if (has_names) { | 314 if (has_names) { |
| 307 if (debug) print("emitting names @ " + bytes.length); | 315 if (debug) print("emitting names @ " + bytes.length); |
| 308 emit_section(bytes, kDeclNames, function(bytes) { | 316 emit_section(bytes, kDeclNames, function(bytes) { |
| 309 emit_varint(bytes, wasm.functions.length); | 317 emit_varint(bytes, wasm.functions.length); |
| 310 for (func of wasm.functions) { | 318 for (func of wasm.functions) { |
| 311 var name = func.name == undefined ? "" : func.name; | 319 var name = func.name == undefined ? "" : func.name; |
| 312 emit_string(bytes, name); | 320 emit_string(bytes, name); |
| 313 emit_u8(bytes, 0); // local names count == 0 | 321 emit_u8(bytes, 0); // local names count == 0 |
| 314 } | 322 } |
| 315 }); | 323 }); |
| 316 } | 324 } |
| 317 | 325 |
| 318 // End the module. | 326 // End the module. |
| 319 if (debug) print("emitting end @ " + bytes.length); | 327 if (debug) print("emitting end @ " + bytes.length); |
| 320 emit_section(bytes, kDeclEnd, function(bytes) {}); | 328 emit_section(bytes, kDeclEnd, function(bytes) {}); |
| 321 | 329 |
| 322 return bytes; | 330 return bytes; |
| 323 } | 331 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 335 } | 343 } |
| 336 | 344 |
| 337 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 345 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
| 338 var buffer = this.toBuffer(); | 346 var buffer = this.toBuffer(); |
| 339 if (memory != undefined) { | 347 if (memory != undefined) { |
| 340 return Wasm.instantiateModule(buffer, ffi, memory); | 348 return Wasm.instantiateModule(buffer, ffi, memory); |
| 341 } else { | 349 } else { |
| 342 return Wasm.instantiateModule(buffer, ffi); | 350 return Wasm.instantiateModule(buffer, ffi); |
| 343 } | 351 } |
| 344 } | 352 } |
| OLD | NEW |