blob: bfc4460d91b899d4ca640502cbfd625ef35ea1b4 [file] [log] [blame]
Ben Murdochda12d292016-06-02 14:46:10 +01001// 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
3// found in the LICENSE file.
4
5function WasmFunctionBuilder(name, sig_index) {
6 this.name = name;
7 this.sig_index = sig_index;
8 this.exports = [];
9}
10
11WasmFunctionBuilder.prototype.exportAs = function(name) {
12 this.exports.push(name);
13 return this;
14}
15
16WasmFunctionBuilder.prototype.exportFunc = function() {
17 this.exports.push(this.name);
18 return this;
19}
20
21WasmFunctionBuilder.prototype.addBody = function(body) {
22 this.body = body;
23 return this;
24}
25
26WasmFunctionBuilder.prototype.addLocals = function(locals) {
27 this.locals = locals;
28 return this;
29}
30
31function WasmModuleBuilder() {
32 this.signatures = [];
33 this.imports = [];
34 this.functions = [];
35 this.exports = [];
36 this.function_table = [];
37 this.data_segments = [];
38 this.explicit = [];
39 return this;
40}
41
42WasmModuleBuilder.prototype.addStart = function(start_index) {
43 this.start_index = start_index;
44}
45
46WasmModuleBuilder.prototype.addMemory = function(min, max, exp) {
47 this.memory = {min: min, max: max, exp: exp};
48 return this;
49}
50
51WasmModuleBuilder.prototype.addExplicitSection = function(bytes) {
52 this.explicit.push(bytes);
53 return this;
54}
55
Ben Murdochc5610432016-08-08 18:44:38 +010056// Add a signature; format is [param_count, param0, param1, ..., retcount, ret0]
Ben Murdochda12d292016-06-02 14:46:10 +010057WasmModuleBuilder.prototype.addSignature = function(sig) {
58 // TODO: canonicalize signatures?
59 this.signatures.push(sig);
60 return this.signatures.length - 1;
61}
62
63WasmModuleBuilder.prototype.addFunction = function(name, sig) {
64 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
65 var func = new WasmFunctionBuilder(name, sig_index);
66 func.index = this.functions.length;
67 this.functions.push(func);
68 return func;
69}
70
71WasmModuleBuilder.prototype.addImportWithModule = function(module, name, sig) {
72 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
73 this.imports.push({module: module, name: name, sig_index: sig_index});
74 return this.imports.length - 1;
75}
76
77WasmModuleBuilder.prototype.addImport = function(name, sig) {
Ben Murdochc5610432016-08-08 18:44:38 +010078 this.addImportWithModule(name, undefined, sig);
Ben Murdochda12d292016-06-02 14:46:10 +010079}
80
81WasmModuleBuilder.prototype.addDataSegment = function(addr, data, init) {
82 this.data_segments.push({addr: addr, data: data, init: init});
83 return this.data_segments.length - 1;
84}
85
86WasmModuleBuilder.prototype.appendToFunctionTable = function(array) {
87 this.function_table = this.function_table.concat(array);
88 return this;
89}
90
91function emit_u8(bytes, val) {
92 bytes.push(val & 0xff);
93}
94
95function emit_u16(bytes, val) {
96 bytes.push(val & 0xff);
97 bytes.push((val >> 8) & 0xff);
98}
99
100function emit_u32(bytes, val) {
101 bytes.push(val & 0xff);
102 bytes.push((val >> 8) & 0xff);
103 bytes.push((val >> 16) & 0xff);
104 bytes.push((val >> 24) & 0xff);
105}
106
107function emit_string(bytes, string) {
Ben Murdochc5610432016-08-08 18:44:38 +0100108 // When testing illegal names, we pass a byte array directly.
109 if (string instanceof Array) {
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));
Ben Murdochda12d292016-06-02 14:46:10 +0100121 }
122}
123
124function emit_varint(bytes, val) {
125 while (true) {
126 var v = val & 0xff;
127 val = val >>> 7;
128 if (val == 0) {
129 bytes.push(v);
130 break;
131 }
132 bytes.push(v | 0x80);
133 }
134}
135
136function emit_bytes(bytes, data) {
137 for (var i = 0; i < data.length; i++) {
138 bytes.push(data[i] & 0xff);
139 }
140}
141
142function emit_section(bytes, section_code, content_generator) {
Ben Murdochc5610432016-08-08 18:44:38 +0100143 // Emit section name.
144 emit_string(bytes, section_names[section_code]);
145 // Emit the section to a temporary buffer: its full length isn't know yet.
Ben Murdochda12d292016-06-02 14:46:10 +0100146 var tmp_bytes = [];
Ben Murdochda12d292016-06-02 14:46:10 +0100147 content_generator(tmp_bytes);
Ben Murdochc5610432016-08-08 18:44:38 +0100148 // Emit section length.
Ben Murdochda12d292016-06-02 14:46:10 +0100149 emit_varint(bytes, tmp_bytes.length);
Ben Murdochc5610432016-08-08 18:44:38 +0100150 // Copy the temporary buffer.
Ben Murdochda12d292016-06-02 14:46:10 +0100151 Array.prototype.push.apply(bytes, tmp_bytes);
152}
153
154WasmModuleBuilder.prototype.toArray = function(debug) {
155 // Add header bytes
156 var bytes = [];
157 bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3,
158 kWasmV0, kWasmV1, kWasmV2, kWasmV3]);
159
160 var wasm = this;
161
Ben Murdochda12d292016-06-02 14:46:10 +0100162 // Add signatures section
163 if (wasm.signatures.length > 0) {
164 if (debug) print("emitting signatures @ " + bytes.length);
165 emit_section(bytes, kDeclSignatures, function(bytes) {
166 emit_varint(bytes, wasm.signatures.length);
167 for (sig of wasm.signatures) {
Ben Murdochc5610432016-08-08 18:44:38 +0100168 emit_u8(bytes, kWasmFunctionTypeForm);
Ben Murdochda12d292016-06-02 14:46:10 +0100169 for (var j = 0; j < sig.length; j++) {
170 emit_u8(bytes, sig[j]);
171 }
172 }
173 });
174 }
175
176 // Add imports section
177 if (wasm.imports.length > 0) {
178 if (debug) print("emitting imports @ " + bytes.length);
179 emit_section(bytes, kDeclImportTable, function(bytes) {
180 emit_varint(bytes, wasm.imports.length);
181 for (imp of wasm.imports) {
182 emit_varint(bytes, imp.sig_index);
183 emit_string(bytes, imp.module);
184 emit_string(bytes, imp.name || '');
185 }
186 });
187 }
188
Ben Murdochc5610432016-08-08 18:44:38 +0100189 // Add functions declarations
Ben Murdochda12d292016-06-02 14:46:10 +0100190 var names = false;
191 var exports = 0;
192 if (wasm.functions.length > 0) {
193 var has_names = false;
194
195 // emit function signatures
196 if (debug) print("emitting function sigs @ " + bytes.length);
197 emit_section(bytes, kDeclFunctionSignatures, function(bytes) {
198 emit_varint(bytes, wasm.functions.length);
199 for (func of wasm.functions) {
200 has_names = has_names || (func.name != undefined &&
201 func.name.length > 0);
202 exports += func.exports.length;
203
204 emit_varint(bytes, func.sig_index);
205 }
206 });
207
Ben Murdochc5610432016-08-08 18:44:38 +0100208 }
209
210 // Add function table.
211 if (wasm.function_table.length > 0) {
212 if (debug) print("emitting function table @ " + bytes.length);
213 emit_section(bytes, kDeclFunctionTable, function(bytes) {
214 emit_varint(bytes, wasm.function_table.length);
215 for (index of wasm.function_table) {
216 emit_varint(bytes, index);
217 }
218 });
219 }
220
221 // Add memory section
222 if (wasm.memory != undefined) {
223 if (debug) print("emitting memory @ " + bytes.length);
224 emit_section(bytes, kDeclMemory, function(bytes) {
225 emit_varint(bytes, wasm.memory.min);
226 emit_varint(bytes, wasm.memory.max);
227 emit_u8(bytes, wasm.memory.exp ? 1 : 0);
228 });
229 }
230
231
232 // Add export table.
233 if (exports > 0) {
234 if (debug) print("emitting exports @ " + bytes.length);
235 emit_section(bytes, kDeclExportTable, function(bytes) {
236 emit_varint(bytes, exports);
237 for (func of wasm.functions) {
238 for (exp of func.exports) {
239 emit_varint(bytes, func.index);
240 emit_string(bytes, exp);
241 }
242 }
243 });
244 }
245
246 // Add start function section.
247 if (wasm.start_index != undefined) {
248 if (debug) print("emitting start function @ " + bytes.length);
249 emit_section(bytes, kDeclStartFunction, function(bytes) {
250 emit_varint(bytes, wasm.start_index);
251 });
252 }
253
254 // Add function bodies.
255 if (wasm.functions.length > 0) {
Ben Murdochda12d292016-06-02 14:46:10 +0100256 // emit function bodies
257 if (debug) print("emitting function bodies @ " + bytes.length);
258 emit_section(bytes, kDeclFunctionBodies, function(bytes) {
259 emit_varint(bytes, wasm.functions.length);
260 for (func of wasm.functions) {
261 // Function body length will be patched later.
262 var local_decls = [];
263 var l = func.locals;
264 if (l != undefined) {
265 var local_decls_count = 0;
266 if (l.i32_count > 0) {
267 local_decls.push({count: l.i32_count, type: kAstI32});
268 }
269 if (l.i64_count > 0) {
270 local_decls.push({count: l.i64_count, type: kAstI64});
271 }
272 if (l.f32_count > 0) {
273 local_decls.push({count: l.f32_count, type: kAstF32});
274 }
275 if (l.f64_count > 0) {
276 local_decls.push({count: l.f64_count, type: kAstF64});
277 }
278 }
279 var header = new Array();
280
281 emit_varint(header, local_decls.length);
282 for (decl of local_decls) {
283 emit_varint(header, decl.count);
284 emit_u8(header, decl.type);
285 }
286
287 emit_varint(bytes, header.length + func.body.length);
288 emit_bytes(bytes, header);
289 emit_bytes(bytes, func.body);
290 }
291 });
292 }
293
Ben Murdochc5610432016-08-08 18:44:38 +0100294 // Add data segments.
Ben Murdochda12d292016-06-02 14:46:10 +0100295 if (wasm.data_segments.length > 0) {
296 if (debug) print("emitting data segments @ " + bytes.length);
297 emit_section(bytes, kDeclDataSegments, function(bytes) {
298 emit_varint(bytes, wasm.data_segments.length);
299 for (seg of wasm.data_segments) {
300 emit_varint(bytes, seg.addr);
301 emit_varint(bytes, seg.data.length);
302 emit_bytes(bytes, seg.data);
303 }
304 });
305 }
306
Ben Murdochc5610432016-08-08 18:44:38 +0100307 // Add any explicitly added sections
Ben Murdochda12d292016-06-02 14:46:10 +0100308 for (exp of wasm.explicit) {
309 if (debug) print("emitting explicit @ " + bytes.length);
310 emit_bytes(bytes, exp);
311 }
312
Ben Murdochc5610432016-08-08 18:44:38 +0100313 // Add function names.
314 if (has_names) {
315 if (debug) print("emitting names @ " + bytes.length);
316 emit_section(bytes, kDeclNames, function(bytes) {
317 emit_varint(bytes, wasm.functions.length);
318 for (func of wasm.functions) {
319 var name = func.name == undefined ? "" : func.name;
320 emit_string(bytes, name);
321 emit_u8(bytes, 0); // local names count == 0
322 }
323 });
324 }
325
Ben Murdochda12d292016-06-02 14:46:10 +0100326 // End the module.
327 if (debug) print("emitting end @ " + bytes.length);
328 emit_section(bytes, kDeclEnd, function(bytes) {});
329
330 return bytes;
331}
332
333WasmModuleBuilder.prototype.toBuffer = function(debug) {
334 var bytes = this.toArray(debug);
335 var buffer = new ArrayBuffer(bytes.length);
336 var view = new Uint8Array(buffer);
337 for (var i = 0; i < bytes.length; i++) {
338 var val = bytes[i];
339 if ((typeof val) == "string") val = val.charCodeAt(0);
340 view[i] = val | 0;
341 }
342 return buffer;
343}
344
345WasmModuleBuilder.prototype.instantiate = function(ffi, memory) {
346 var buffer = this.toBuffer();
347 if (memory != undefined) {
348 return Wasm.instantiateModule(buffer, ffi, memory);
349 } else {
350 return Wasm.instantiateModule(buffer, ffi);
351 }
352}