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 DataRef(data) { | |
6 this.pos = -1; | |
7 this.data = data; | |
8 } | |
9 | |
10 function WasmFunctionBuilder(name, sig_index) { | 5 function WasmFunctionBuilder(name, sig_index) { |
11 this.name = name; | 6 this.name = name; |
12 this.sig_index = sig_index; | 7 this.sig_index = sig_index; |
13 this.exports = []; | 8 this.exports = []; |
14 } | 9 } |
15 | 10 |
16 WasmFunctionBuilder.prototype.exportAs = function(name) { | 11 WasmFunctionBuilder.prototype.exportAs = function(name) { |
17 this.exports.push(name); | 12 this.exports.push(name); |
18 return this; | 13 return this; |
19 } | 14 } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 bytes.push((val >> 24) & 0xff); | 106 bytes.push((val >> 24) & 0xff); |
112 } | 107 } |
113 | 108 |
114 function emit_string(bytes, string) { | 109 function emit_string(bytes, string) { |
115 emit_varint(bytes, string.length); | 110 emit_varint(bytes, string.length); |
116 for (var i = 0; i < string.length; i++) { | 111 for (var i = 0; i < string.length; i++) { |
117 emit_u8(bytes, string.charCodeAt(i)); | 112 emit_u8(bytes, string.charCodeAt(i)); |
118 } | 113 } |
119 } | 114 } |
120 | 115 |
121 function emit_data_ref(bytes, string) { | |
122 bytes.push(new DataRef(string)); | |
123 bytes.push(0); | |
124 bytes.push(0); | |
125 bytes.push(0); | |
126 } | |
127 | |
128 function emit_varint(bytes, val) { | 116 function emit_varint(bytes, val) { |
129 while (true) { | 117 while (true) { |
130 var v = val & 0xff; | 118 var v = val & 0xff; |
131 val = val >>> 7; | 119 val = val >>> 7; |
132 if (val == 0) { | 120 if (val == 0) { |
133 bytes.push(v); | 121 bytes.push(v); |
134 break; | 122 break; |
135 } | 123 } |
136 bytes.push(v | 0x80); | 124 bytes.push(v | 0x80); |
137 } | 125 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 emit_string(bytes, exp); | 250 emit_string(bytes, exp); |
263 } | 251 } |
264 } | 252 } |
265 } | 253 } |
266 | 254 |
267 if (this.data_segments.length > 0) { | 255 if (this.data_segments.length > 0) { |
268 if (debug) print("emitting data segments @ " + bytes.length); | 256 if (debug) print("emitting data segments @ " + bytes.length); |
269 emit_u8(bytes, kDeclDataSegments); | 257 emit_u8(bytes, kDeclDataSegments); |
270 emit_varint(bytes, this.data_segments.length); | 258 emit_varint(bytes, this.data_segments.length); |
271 for (seg of this.data_segments) { | 259 for (seg of this.data_segments) { |
272 emit_u32(bytes, seg.addr); | 260 emit_varint(bytes, seg.addr); |
273 emit_data_ref(bytes, seg.data); | 261 emit_varint(bytes, seg.data.length); |
274 emit_u32(bytes, seg.data.length); | 262 for (var i = 0; i < seg.data.length; i++) { |
275 emit_u8(bytes, seg.init ? 1 : 0); | 263 emit_u8(bytes, seg.data[i]); |
| 264 } |
276 } | 265 } |
277 } | 266 } |
278 | 267 |
279 // Emit any explicitly added sections | 268 // Emit any explicitly added sections |
280 for (exp of this.explicit) { | 269 for (exp of this.explicit) { |
281 if (debug) print("emitting explicit @ " + bytes.length); | 270 if (debug) print("emitting explicit @ " + bytes.length); |
282 for (var i = 0; i < exp.length; i++) { | 271 for (var i = 0; i < exp.length; i++) { |
283 emit_u8(bytes, exp[i]); | 272 emit_u8(bytes, exp[i]); |
284 } | 273 } |
285 } | 274 } |
286 | 275 |
287 // End the module. | 276 // End the module. |
288 if (debug) print("emitting end @ " + bytes.length); | 277 if (debug) print("emitting end @ " + bytes.length); |
289 emit_u8(bytes, kDeclEnd); | 278 emit_u8(bytes, kDeclEnd); |
290 | 279 |
291 // Collect references. | |
292 var strings = new Object(); | |
293 var data_segments = []; | |
294 var count = 0; | |
295 for (var i = 0; i < bytes.length; i++) { | |
296 var b = bytes[i]; | |
297 if (b instanceof DataRef) { | |
298 data_segments.push(b); | |
299 count++; | |
300 } | |
301 } | |
302 | |
303 if (count > 0) { | |
304 // Emit data. | |
305 if (debug) print("emitting data @ " + bytes.length); | |
306 for (ref of data_segments) { | |
307 ref.pos = bytes.length; | |
308 for (var i = 0; i < ref.data.length; i++) { | |
309 emit_u8(bytes, ref.data[i]); | |
310 } | |
311 } | |
312 // Update references to strings and data. | |
313 for (var i = 0; i < bytes.length; i++) { | |
314 var b = bytes[i]; | |
315 if (b instanceof DataRef) { | |
316 bytes[i] = b.pos & 0xFF; | |
317 bytes[i + 1] = (b.pos >> 8) & 0xFF; | |
318 bytes[i + 2] = (b.pos >> 16) & 0xFF; | |
319 bytes[i + 3] = (b.pos >> 24) & 0xFF; | |
320 } | |
321 } | |
322 } | |
323 | |
324 return bytes; | 280 return bytes; |
325 } | 281 } |
326 | 282 |
327 WasmModuleBuilder.prototype.toBuffer = function(debug) { | 283 WasmModuleBuilder.prototype.toBuffer = function(debug) { |
328 var bytes = this.toArray(debug); | 284 var bytes = this.toArray(debug); |
329 var buffer = new ArrayBuffer(bytes.length); | 285 var buffer = new ArrayBuffer(bytes.length); |
330 var view = new Uint8Array(buffer); | 286 var view = new Uint8Array(buffer); |
331 for (var i = 0; i < bytes.length; i++) { | 287 for (var i = 0; i < bytes.length; i++) { |
332 var val = bytes[i]; | 288 var val = bytes[i]; |
333 if ((typeof val) == "string") val = val.charCodeAt(0); | 289 if ((typeof val) == "string") val = val.charCodeAt(0); |
334 view[i] = val | 0; | 290 view[i] = val | 0; |
335 } | 291 } |
336 return buffer; | 292 return buffer; |
337 } | 293 } |
338 | 294 |
339 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 295 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
340 var buffer = this.toBuffer(); | 296 var buffer = this.toBuffer(); |
341 if (memory != undefined) { | 297 if (memory != undefined) { |
342 return Wasm.instantiateModule(buffer, ffi, memory); | 298 return Wasm.instantiateModule(buffer, ffi, memory); |
343 } else { | 299 } else { |
344 return Wasm.instantiateModule(buffer, ffi); | 300 return Wasm.instantiateModule(buffer, ffi); |
345 } | 301 } |
346 } | 302 } |
OLD | NEW |