| 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 StringRef(string) { | 5 function StringRef(string) { |
| 6 this.pos = -1; | 6 this.pos = -1; |
| 7 this.string = string; | 7 this.string = string; |
| 8 } | 8 } |
| 9 | 9 |
| 10 function DataRef(data) { | 10 function DataRef(data) { |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 if (this.functions.length > 0) { | 183 if (this.functions.length > 0) { |
| 184 if (debug) print("emitting functions @ " + bytes.length); | 184 if (debug) print("emitting functions @ " + bytes.length); |
| 185 emit_u8(bytes, kDeclFunctions); | 185 emit_u8(bytes, kDeclFunctions); |
| 186 emit_varint(bytes, this.functions.length); | 186 emit_varint(bytes, this.functions.length); |
| 187 var index = 0; | 187 var index = 0; |
| 188 for (func of this.functions) { | 188 for (func of this.functions) { |
| 189 var flags = 0; | 189 var flags = 0; |
| 190 var hasName = func.name != undefined && func.name.length > 0; | 190 var hasName = func.name != undefined && func.name.length > 0; |
| 191 names = names || hasName; | 191 names = names || hasName; |
| 192 if (hasName) flags |= kDeclFunctionName; | 192 if (hasName) flags |= kDeclFunctionName; |
| 193 if (func.locals != undefined) flags |= kDeclFunctionLocals; | |
| 194 exports += func.exports.length; | 193 exports += func.exports.length; |
| 195 | 194 |
| 196 emit_u8(bytes, flags); | 195 emit_u8(bytes, flags); |
| 197 emit_u16(bytes, func.sig_index); | 196 emit_u16(bytes, func.sig_index); |
| 198 | 197 |
| 199 if (hasName) { | 198 if (hasName) emit_string(bytes, func.name); |
| 200 emit_string(bytes, func.name); | 199 |
| 200 // Function body length will be patched later. |
| 201 var length_pos = bytes.length; |
| 202 emit_u16(bytes, 0); |
| 203 |
| 204 var local_decls = []; |
| 205 var l = func.locals; |
| 206 if (l != undefined) { |
| 207 var local_decls_count = 0; |
| 208 if (l.i32_count > 0) { |
| 209 local_decls.push({count: l.i32_count, type: kAstI32}); |
| 210 } |
| 211 if (l.i64_count > 0) { |
| 212 local_decls.push({count: l.i64_count, type: kAstI64}); |
| 213 } |
| 214 if (l.f32_count > 0) { |
| 215 local_decls.push({count: l.f32_count, type: kAstF32}); |
| 216 } |
| 217 if (l.f64_count > 0) { |
| 218 local_decls.push({count: l.f64_count, type: kAstF64}); |
| 219 } |
| 201 } | 220 } |
| 202 if (func.locals != undefined) { | 221 emit_u8(bytes, local_decls.length); |
| 203 emit_u16(bytes, func.locals.i32_count); | 222 for (decl of local_decls) { |
| 204 emit_u16(bytes, func.locals.i64_count); | 223 emit_varint(bytes, decl.count); |
| 205 emit_u16(bytes, func.locals.f32_count); | 224 emit_u8(bytes, decl.type); |
| 206 emit_u16(bytes, func.locals.f64_count); | |
| 207 } | 225 } |
| 208 emit_u16(bytes, func.body.length); | 226 |
| 209 for (var i = 0; i < func.body.length; i++) { | 227 for (var i = 0; i < func.body.length; i++) { |
| 210 emit_u8(bytes, func.body[i]); | 228 emit_u8(bytes, func.body[i]); |
| 211 } | 229 } |
| 230 var length = bytes.length - length_pos - 2; |
| 231 bytes[length_pos] = length & 0xff; |
| 232 bytes[length_pos + 1] = (length >> 8) & 0xff; |
| 212 | 233 |
| 213 index++; | 234 index++; |
| 214 } | 235 } |
| 215 } | 236 } |
| 216 | 237 |
| 217 // Add start function section. | 238 // Add start function section. |
| 218 if (this.start_index != undefined) { | 239 if (this.start_index != undefined) { |
| 219 if (debug) print("emitting start function @ " + bytes.length); | 240 if (debug) print("emitting start function @ " + bytes.length); |
| 220 emit_u8(bytes, kDeclStartFunction); | 241 emit_u8(bytes, kDeclStartFunction); |
| 221 emit_varint(bytes, this.start_index); | 242 emit_varint(bytes, this.start_index); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 } | 357 } |
| 337 | 358 |
| 338 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 359 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
| 339 var buffer = this.toBuffer(); | 360 var buffer = this.toBuffer(); |
| 340 if (memory != undefined) { | 361 if (memory != undefined) { |
| 341 return _WASMEXP_.instantiateModule(buffer, ffi, memory); | 362 return _WASMEXP_.instantiateModule(buffer, ffi, memory); |
| 342 } else { | 363 } else { |
| 343 return _WASMEXP_.instantiateModule(buffer, ffi); | 364 return _WASMEXP_.instantiateModule(buffer, ffi); |
| 344 } | 365 } |
| 345 } | 366 } |
| OLD | NEW |