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) { | |
6 this.pos = -1; | |
7 this.string = string; | |
8 } | |
9 | |
10 function DataRef(data) { | 5 function DataRef(data) { |
11 this.pos = -1; | 6 this.pos = -1; |
12 this.data = data; | 7 this.data = data; |
13 } | 8 } |
14 | 9 |
15 function WasmFunctionBuilder(name, sig_index) { | 10 function WasmFunctionBuilder(name, sig_index) { |
16 this.name = name; | 11 this.name = name; |
17 this.sig_index = sig_index; | 12 this.sig_index = sig_index; |
18 this.exports = []; | 13 this.exports = []; |
19 } | 14 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 105 } |
111 | 106 |
112 function emit_u32(bytes, val) { | 107 function emit_u32(bytes, val) { |
113 bytes.push(val & 0xff); | 108 bytes.push(val & 0xff); |
114 bytes.push((val >> 8) & 0xff); | 109 bytes.push((val >> 8) & 0xff); |
115 bytes.push((val >> 16) & 0xff); | 110 bytes.push((val >> 16) & 0xff); |
116 bytes.push((val >> 24) & 0xff); | 111 bytes.push((val >> 24) & 0xff); |
117 } | 112 } |
118 | 113 |
119 function emit_string(bytes, string) { | 114 function emit_string(bytes, string) { |
120 bytes.push(new StringRef(string)); | 115 emit_varint(bytes, string.length); |
121 bytes.push(0); | 116 for (var i = 0; i < string.length; i++) { |
122 bytes.push(0); | 117 emit_u8(bytes, string.charCodeAt(i)); |
123 bytes.push(0); | 118 } |
124 } | 119 } |
125 | 120 |
126 function emit_data_ref(bytes, string) { | 121 function emit_data_ref(bytes, string) { |
127 bytes.push(new DataRef(string)); | 122 bytes.push(new DataRef(string)); |
128 bytes.push(0); | 123 bytes.push(0); |
129 bytes.push(0); | 124 bytes.push(0); |
130 bytes.push(0); | 125 bytes.push(0); |
131 } | 126 } |
132 | 127 |
133 function emit_varint(bytes, val) { | 128 function emit_varint(bytes, val) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 167 } |
173 | 168 |
174 // Add imports section | 169 // Add imports section |
175 if (this.imports.length > 0) { | 170 if (this.imports.length > 0) { |
176 if (debug) print("emitting imports @ " + bytes.length); | 171 if (debug) print("emitting imports @ " + bytes.length); |
177 emit_u8(bytes, kDeclImportTable); | 172 emit_u8(bytes, kDeclImportTable); |
178 emit_varint(bytes, this.imports.length); | 173 emit_varint(bytes, this.imports.length); |
179 for (imp of this.imports) { | 174 for (imp of this.imports) { |
180 emit_varint(bytes, imp.sig_index); | 175 emit_varint(bytes, imp.sig_index); |
181 emit_string(bytes, imp.module); | 176 emit_string(bytes, imp.module); |
182 if (imp.name == undefined) { | 177 emit_string(bytes, imp.name || ''); |
183 emit_u32(bytes, 0); | |
184 } else { | |
185 emit_string(bytes, imp.name); | |
186 } | |
187 } | 178 } |
188 } | 179 } |
189 | 180 |
190 // Add functions section | 181 // Add functions section |
191 var names = false; | 182 var names = false; |
192 var exports = 0; | 183 var exports = 0; |
193 if (this.functions.length > 0) { | 184 if (this.functions.length > 0) { |
194 if (debug) print("emitting functions @ " + bytes.length); | 185 if (debug) print("emitting functions @ " + bytes.length); |
195 emit_u8(bytes, kDeclFunctions); | 186 emit_u8(bytes, kDeclFunctions); |
196 emit_varint(bytes, this.functions.length); | 187 emit_varint(bytes, this.functions.length); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 if (debug) print("emitting explicit @ " + bytes.length); | 281 if (debug) print("emitting explicit @ " + bytes.length); |
291 for (var i = 0; i < exp.length; i++) { | 282 for (var i = 0; i < exp.length; i++) { |
292 emit_u8(bytes, exp[i]); | 283 emit_u8(bytes, exp[i]); |
293 } | 284 } |
294 } | 285 } |
295 | 286 |
296 // End the module. | 287 // End the module. |
297 if (debug) print("emitting end @ " + bytes.length); | 288 if (debug) print("emitting end @ " + bytes.length); |
298 emit_u8(bytes, kDeclEnd); | 289 emit_u8(bytes, kDeclEnd); |
299 | 290 |
300 // Collect references and canonicalize strings. | 291 // Collect references. |
301 var strings = new Object(); | 292 var strings = new Object(); |
302 var data_segments = []; | 293 var data_segments = []; |
303 var count = 0; | 294 var count = 0; |
304 for (var i = 0; i < bytes.length; i++) { | 295 for (var i = 0; i < bytes.length; i++) { |
305 var b = bytes[i]; | 296 var b = bytes[i]; |
306 if (b instanceof StringRef) { | |
307 count++; | |
308 var prev = strings[b.string]; | |
309 if (prev) { | |
310 bytes[i] = prev; | |
311 } else { | |
312 strings[b.string] = b; | |
313 } | |
314 } | |
315 if (b instanceof DataRef) { | 297 if (b instanceof DataRef) { |
316 data_segments.push(b); | 298 data_segments.push(b); |
317 count++; | 299 count++; |
318 } | 300 } |
319 } | 301 } |
320 | 302 |
321 if (count > 0) { | 303 if (count > 0) { |
322 // Emit strings. | |
323 if (debug) print("emitting strings @ " + bytes.length); | |
324 for (str in strings) { | |
325 var ref = strings[str]; | |
326 if (!(ref instanceof StringRef)) continue; | |
327 if (debug) print(" \"" + str + "\" @ " + bytes.length); | |
328 ref.pos = bytes.length; | |
329 for (var i = 0; i < str.length; i++) { | |
330 emit_u8(bytes, str.charCodeAt(i)); | |
331 } | |
332 emit_u8(bytes, 0); // null terminator. | |
333 } | |
334 // Emit data. | 304 // Emit data. |
335 if (debug) print("emitting data @ " + bytes.length); | 305 if (debug) print("emitting data @ " + bytes.length); |
336 for (ref of data_segments) { | 306 for (ref of data_segments) { |
337 ref.pos = bytes.length; | 307 ref.pos = bytes.length; |
338 for (var i = 0; i < ref.data.length; i++) { | 308 for (var i = 0; i < ref.data.length; i++) { |
339 emit_u8(bytes, ref.data[i]); | 309 emit_u8(bytes, ref.data[i]); |
340 } | 310 } |
341 } | 311 } |
342 // Update references to strings and data. | 312 // Update references to strings and data. |
343 for (var i = 0; i < bytes.length; i++) { | 313 for (var i = 0; i < bytes.length; i++) { |
344 var b = bytes[i]; | 314 var b = bytes[i]; |
345 if (b instanceof StringRef || b instanceof DataRef) { | 315 if (b instanceof DataRef) { |
346 bytes[i] = b.pos & 0xFF; | 316 bytes[i] = b.pos & 0xFF; |
347 bytes[i + 1] = (b.pos >> 8) & 0xFF; | 317 bytes[i + 1] = (b.pos >> 8) & 0xFF; |
348 bytes[i + 2] = (b.pos >> 16) & 0xFF; | 318 bytes[i + 2] = (b.pos >> 16) & 0xFF; |
349 bytes[i + 3] = (b.pos >> 24) & 0xFF; | 319 bytes[i + 3] = (b.pos >> 24) & 0xFF; |
350 } | 320 } |
351 } | 321 } |
352 } | 322 } |
353 | 323 |
354 return bytes; | 324 return bytes; |
355 } | 325 } |
(...skipping 11 matching lines...) Expand all Loading... |
367 } | 337 } |
368 | 338 |
369 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 339 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
370 var buffer = this.toBuffer(); | 340 var buffer = this.toBuffer(); |
371 if (memory != undefined) { | 341 if (memory != undefined) { |
372 return Wasm.instantiateModule(buffer, ffi, memory); | 342 return Wasm.instantiateModule(buffer, ffi, memory); |
373 } else { | 343 } else { |
374 return Wasm.instantiateModule(buffer, ffi); | 344 return Wasm.instantiateModule(buffer, ffi); |
375 } | 345 } |
376 } | 346 } |
OLD | NEW |