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 // Used for encoding f32 and double constants to bits. | 5 // Used for encoding f32 and double constants to bits. |
6 let __buffer = new ArrayBuffer(8); | 6 let __buffer = new ArrayBuffer(8); |
7 let byte_view = new Int8Array(__buffer); | 7 let byte_view = new Int8Array(__buffer); |
8 let f32_view = new Float32Array(__buffer); | 8 let f32_view = new Float32Array(__buffer); |
9 let f64_view = new Float64Array(__buffer); | 9 let f64_view = new Float64Array(__buffer); |
10 | 10 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 } | 125 } |
126 } | 126 } |
127 | 127 |
128 class WasmModuleBuilder { | 128 class WasmModuleBuilder { |
129 constructor() { | 129 constructor() { |
130 this.types = []; | 130 this.types = []; |
131 this.imports = []; | 131 this.imports = []; |
132 this.exports = []; | 132 this.exports = []; |
133 this.globals = []; | 133 this.globals = []; |
134 this.functions = []; | 134 this.functions = []; |
135 this.table = []; | 135 this.function_table = []; |
| 136 this.function_table_length = 0; |
| 137 this.function_table_inits = []; |
136 this.segments = []; | 138 this.segments = []; |
137 this.explicit = []; | 139 this.explicit = []; |
138 this.pad = null; | |
139 this.num_imported_funcs = 0; | 140 this.num_imported_funcs = 0; |
140 this.num_imported_globals = 0; | 141 this.num_imported_globals = 0; |
141 return this; | 142 return this; |
142 } | 143 } |
143 | 144 |
144 addStart(start_index) { | 145 addStart(start_index) { |
145 this.start_index = start_index; | 146 this.start_index = start_index; |
146 return this; | 147 return this; |
147 } | 148 } |
148 | 149 |
149 addMemory(min, max, exp) { | 150 addMemory(min, max, exp) { |
150 this.memory = {min: min, max: max, exp: exp}; | 151 this.memory = {min: min, max: max, exp: exp}; |
151 return this; | 152 return this; |
152 } | 153 } |
153 | 154 |
154 addPadFunctionTable(size) { | |
155 this.pad = size; | |
156 return this; | |
157 } | |
158 | |
159 addExplicitSection(bytes) { | 155 addExplicitSection(bytes) { |
160 this.explicit.push(bytes); | 156 this.explicit.push(bytes); |
161 return this; | 157 return this; |
162 } | 158 } |
163 | 159 |
164 addType(type) { | 160 addType(type) { |
165 // TODO: canonicalize types? | 161 // TODO: canonicalize types? |
166 this.types.push(type); | 162 this.types.push(type); |
167 return this.types.length - 1; | 163 return this.types.length - 1; |
168 } | 164 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 209 |
214 addDataSegment(addr, data, is_global = false) { | 210 addDataSegment(addr, data, is_global = false) { |
215 this.segments.push({addr: addr, data: data, is_global: is_global}); | 211 this.segments.push({addr: addr, data: data, is_global: is_global}); |
216 return this.segments.length - 1; | 212 return this.segments.length - 1; |
217 } | 213 } |
218 | 214 |
219 exportMemoryAs(name) { | 215 exportMemoryAs(name) { |
220 this.exports.push({name: name, kind: kExternalMemory, index: 0}); | 216 this.exports.push({name: name, kind: kExternalMemory, index: 0}); |
221 } | 217 } |
222 | 218 |
223 appendToTable(array) { | 219 addFunctionTableInit(base, is_global, array) { |
224 this.table.push(...array); | 220 this.function_table_inits.push({base: base, is_global: is_global, array: arr
ay}); |
| 221 if (!is_global) { |
| 222 var length = base + array.length; |
| 223 if (length > this.function_table_length) this.function_table_length = leng
th; |
| 224 } |
225 return this; | 225 return this; |
226 } | 226 } |
227 | 227 |
| 228 appendToTable(array) { |
| 229 return this.addFunctionTableInit(this.function_table.length, false, array); |
| 230 } |
| 231 |
| 232 setFunctionTableLength(length) { |
| 233 this.function_table_length = length; |
| 234 return this; |
| 235 } |
| 236 |
228 toArray(debug = false) { | 237 toArray(debug = false) { |
229 let binary = new Binary; | 238 let binary = new Binary; |
230 let wasm = this; | 239 let wasm = this; |
231 | 240 |
232 // Add header | 241 // Add header |
233 binary.emit_header(); | 242 binary.emit_header(); |
234 | 243 |
235 // Add type section | 244 // Add type section |
236 if (wasm.types.length > 0) { | 245 if (wasm.types.length > 0) { |
237 if (debug) print("emitting types @ " + binary.length); | 246 if (debug) print("emitting types @ " + binary.length); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 binary.emit_section(kFunctionSectionCode, section => { | 294 binary.emit_section(kFunctionSectionCode, section => { |
286 section.emit_u32v(wasm.functions.length); | 295 section.emit_u32v(wasm.functions.length); |
287 for (let func of wasm.functions) { | 296 for (let func of wasm.functions) { |
288 has_names = has_names || (func.name != undefined && | 297 has_names = has_names || (func.name != undefined && |
289 func.name.length > 0); | 298 func.name.length > 0); |
290 section.emit_u32v(func.type_index); | 299 section.emit_u32v(func.type_index); |
291 } | 300 } |
292 }); | 301 }); |
293 } | 302 } |
294 | 303 |
295 // Add table. | 304 // Add function_table. |
296 if (wasm.table.length > 0) { | 305 if (wasm.function_table_length > 0) { |
297 if (debug) print("emitting table @ " + binary.length); | 306 if (debug) print("emitting table @ " + binary.length); |
298 binary.emit_section(kTableSectionCode, section => { | 307 binary.emit_section(kTableSectionCode, section => { |
299 section.emit_u8(1); // one table entry | 308 section.emit_u8(1); // one table entry |
300 section.emit_u8(kWasmAnyFunctionTypeForm); | 309 section.emit_u8(kWasmAnyFunctionTypeForm); |
301 section.emit_u8(1); | 310 section.emit_u8(1); |
302 section.emit_u32v(wasm.table.length); | 311 section.emit_u32v(wasm.function_table_length); |
303 section.emit_u32v(wasm.table.length); | 312 section.emit_u32v(wasm.function_table_length); |
304 }); | 313 }); |
305 } | 314 } |
306 | 315 |
307 // Add memory section | 316 // Add memory section |
308 if (wasm.memory != undefined) { | 317 if (wasm.memory != undefined) { |
309 if (debug) print("emitting memory @ " + binary.length); | 318 if (debug) print("emitting memory @ " + binary.length); |
310 binary.emit_section(kMemorySectionCode, section => { | 319 binary.emit_section(kMemorySectionCode, section => { |
311 section.emit_u8(1); // one memory entry | 320 section.emit_u8(1); // one memory entry |
312 section.emit_u32v(kResizableMaximumFlag); | 321 section.emit_u32v(kResizableMaximumFlag); |
313 section.emit_u32v(wasm.memory.min); | 322 section.emit_u32v(wasm.memory.min); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 396 |
388 // Add start function section. | 397 // Add start function section. |
389 if (wasm.start_index != undefined) { | 398 if (wasm.start_index != undefined) { |
390 if (debug) print("emitting start function @ " + binary.length); | 399 if (debug) print("emitting start function @ " + binary.length); |
391 binary.emit_section(kStartSectionCode, section => { | 400 binary.emit_section(kStartSectionCode, section => { |
392 section.emit_u32v(wasm.start_index); | 401 section.emit_u32v(wasm.start_index); |
393 }); | 402 }); |
394 } | 403 } |
395 | 404 |
396 // Add table elements. | 405 // Add table elements. |
397 if (wasm.table.length > 0) { | 406 if (wasm.function_table_inits.length > 0) { |
398 if (debug) print("emitting table @ " + binary.length); | 407 if (debug) print("emitting table @ " + binary.length); |
399 binary.emit_section(kElementSectionCode, section => { | 408 binary.emit_section(kElementSectionCode, section => { |
400 section.emit_u8(1); | 409 var inits = wasm.function_table_inits; |
| 410 section.emit_u32v(inits.length); |
401 section.emit_u8(0); // table index | 411 section.emit_u8(0); // table index |
402 section.emit_u8(kExprI32Const); | 412 |
403 section.emit_u8(0); | 413 for (let init of inits) { |
404 section.emit_u8(kExprEnd); | 414 if (init.is_global) { |
405 section.emit_u32v(wasm.table.length); | 415 section.emit_u8(kExprGetGlobal); |
406 for (let index of wasm.table) { | 416 } else { |
407 section.emit_u32v(index); | 417 section.emit_u8(kExprI32Const); |
| 418 } |
| 419 section.emit_u32v(init.base); |
| 420 section.emit_u8(kExprEnd); |
| 421 section.emit_u32v(init.array.length); |
| 422 for (let index of init.array) { |
| 423 section.emit_u32v(index); |
| 424 } |
408 } | 425 } |
409 }); | 426 }); |
410 } | 427 } |
411 | 428 |
412 // Add function bodies. | 429 // Add function bodies. |
413 if (wasm.functions.length > 0) { | 430 if (wasm.functions.length > 0) { |
414 // emit function bodies | 431 // emit function bodies |
415 if (debug) print("emitting code @ " + binary.length); | 432 if (debug) print("emitting code @ " + binary.length); |
416 binary.emit_section(kCodeSectionCode, section => { | 433 binary.emit_section(kCodeSectionCode, section => { |
417 section.emit_u32v(wasm.functions.length); | 434 section.emit_u32v(wasm.functions.length); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 } | 523 } |
507 return buffer; | 524 return buffer; |
508 } | 525 } |
509 | 526 |
510 instantiate(...args) { | 527 instantiate(...args) { |
511 let module = new WebAssembly.Module(this.toBuffer()); | 528 let module = new WebAssembly.Module(this.toBuffer()); |
512 let instance = new WebAssembly.Instance(module, ...args); | 529 let instance = new WebAssembly.Instance(module, ...args); |
513 return instance; | 530 return instance; |
514 } | 531 } |
515 } | 532 } |
OLD | NEW |