Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(997)

Unified Diff: test/mjsunit/wasm/wasm-module-builder.js

Issue 1762203002: [wasm] Add a JavaScript utility to make it easier to build WASM modules from JavaScript. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/wasm/wasm-module-builder.js
diff --git a/test/mjsunit/wasm/wasm-module-builder.js b/test/mjsunit/wasm/wasm-module-builder.js
new file mode 100644
index 0000000000000000000000000000000000000000..faec8fe738612680151095d175806e6e10e5fd69
--- /dev/null
+++ b/test/mjsunit/wasm/wasm-module-builder.js
@@ -0,0 +1,311 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+function StringRef(string) {
+ this.pos = -1;
+ this.string = string;
+}
+
+function DataRef(data) {
+ this.pos = -1;
+ this.data = data;
+}
+
+function WasmFunctionBuilder(name, sig_index) {
+ this.name = name;
+ this.sig_index = sig_index;
+ this.exports = [];
+}
+
+WasmFunctionBuilder.prototype.ExportAs =
binji 2016/03/04 05:13:26 isn't JavaScript style lowerCamelCase?
+ function(name) {
+ this.exports.push(name);
+ return this;
+}
+
+WasmFunctionBuilder.prototype.AddBody = function(body) {
+ this.body = body;
+ return this;
+}
+
+WasmFunctionBuilder.prototype.AddLocals = function(locals) {
+ this.locals = locals;
+ return this;
+}
+
+function WasmModuleBuilder() {
+ this.signatures = new Array();
binji 2016/03/04 05:13:26 this.memory = null; Otherwise it just magically a
binji 2016/03/04 05:13:26 why not []?
titzer 2016/03/05 01:26:22 That's the JS way; M-O-O-N spells undefined!
+ this.imports = new Array();
+ this.functions = new Array();
+ this.exports = new Array();
+ this.function_table = new Array();
+ this.data_segments = new Array();
+ return this;
+}
+
+WasmModuleBuilder.prototype.AddMemory = function(min, max, exp) {
+ this.memory = {min: min, max: max, exp: exp};
+ return this;
+}
+
+WasmModuleBuilder.prototype.AddSignature = function(sig) {
binji 2016/03/04 05:13:26 mention the expected type/format of sig?
titzer 2016/03/05 01:26:22 Done.
+ // TODO: canonicalize signatures?
+ this.signatures.push(sig);
+ return this.signatures.length - 1;
+}
+
+WasmModuleBuilder.prototype.AddFunction = function(name, sig) {
+ var sig_index = (typeof sig) == "number" ? sig : this.AddSignature(sig);
+ var func = new WasmFunctionBuilder(name, sig_index);
+ func.index = this.functions.length;
+ this.functions.push(func);
+ return func;
+}
+
+WasmModuleBuilder.prototype.AddImport = function(name, sig) {
+ var sig_index = (typeof sig) == "number" ? sig : this.AddSignature(sig);
+ this.imports.push({name: name, sig_index: sig_index});
+ return this.imports.length - 1;
+}
+
+WasmModuleBuilder.prototype.AddDataSegment = function(addr, data, init) {
+ this.data_segments.push({addr: addr, data: data, init: init});
+ return this.data_segments.length - 1;
+}
+
+WasmModuleBuilder.prototype.AppendToFunctionTable = function(array) {
+ this.function_table = this.function_table.concat(array);
+ return this;
+}
+
+function emit_u8(bytes, val) {
+ bytes.push(val & 0xff);
+}
+
+function emit_u16(bytes, val) {
+ bytes.push(val & 0xff);
+ bytes.push((val >> 8) & 0xff);
+}
+
+function emit_u32(bytes, val) {
+ bytes.push(val & 0xff);
+ bytes.push((val >> 8) & 0xff);
+ bytes.push((val >> 16) & 0xff);
+ bytes.push((val >> 24) & 0xff);
+}
+
+function emit_string(bytes, string) {
+ bytes.push(new StringRef(string));
+ bytes.push(0);
+ bytes.push(0);
+ bytes.push(0);
+}
+
+function emit_data_ref(bytes, string) {
+ bytes.push(new DataRef(string));
binji 2016/03/04 05:13:26 Neat trick, but why not just store the Ref in a se
titzer 2016/03/05 01:26:22 I started doing that but it was getting unwieldy w
+ bytes.push(0);
+ bytes.push(0);
+ bytes.push(0);
+}
+
+function emit_varint(bytes, val) {
+ while (true) {
+ var v = val & 0xff;
+ val = val >>> 7;
+ if (val == 0) {
+ bytes.push(v);
+ break;
+ }
+ bytes.push(v | 0x80);
+ }
+}
+
+WasmModuleBuilder.prototype.ToArray = function(debug) {
+ // Add header bytes
+ var bytes = new Array();
+ bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3]);
+
+ // Add memory section
+ if (this.memory != undefined) {
+ if (debug) print("emiting memory @ " + bytes.length);
binji 2016/03/04 05:13:26 sp: emitting
titzer 2016/03/05 01:26:22 Done.
+ emit_u8(bytes, kDeclMemory);
+ emit_varint(bytes, this.memory.min);
+ emit_varint(bytes, this.memory.max);
+ emit_u8(bytes, this.memory.exp ? 1 : 0);
+ }
+
+ // Add signatures section
+ if (this.signatures.length > 0) {
+ if (debug) print("emiting signatures @ " + bytes.length);
+ emit_u8(bytes, kDeclSignatures);
+ emit_varint(bytes, this.signatures.length);
+ for (var i = 0; i < this.signatures.length; i++) {
binji 2016/03/04 05:13:26 for-of here too?
titzer 2016/03/05 01:26:22 Done.
+ var sig = this.signatures[i];
+ var params = sig.length - 1;
+ emit_u8(bytes, params);
+ for (var j = 0; j < sig.length; j++) {
+ emit_u8(bytes, sig[j]);
+ }
+ }
+ }
+
+ // Add imports section
+ if (this.imports.length > 0) {
+ if (debug) print("emiting imports @ " + bytes.length);
+ emit_u8(bytes, kDeclImportTable);
+ emit_varint(bytes, this.imports.length);
+ for (imp of this.imports) {
+ emit_u16(bytes, imp.sig_index);
+ emit_string(bytes, "");
+ emit_string(bytes, imp.name);
+ }
+ }
+
+ // Add functions section
+ var names = false;
+ var exports = 0;
+ if (this.functions.length > 0) {
+ if (debug) print("emiting functions @ " + bytes.length);
+ emit_u8(bytes, kDeclFunctions);
+ emit_varint(bytes, this.functions.length);
+ var index = 0;
+ for (func of this.functions) {
+ var flags = 0;
+ var hasName = func.name != undefined && func.name.length > 0;
+ names = names || hasName;
+ if (hasName) flags |= kDeclFunctionName;
+ if (func.locals != undefined) flags |= kDeclFunctionLocals;
+ exports += func.exports.length;
+
+ emit_u8(bytes, flags);
+ emit_u16(bytes, func.sig_index);
+
+ if (hasName) {
+ emit_string(bytes, func.name);
+ }
+ if (func.locals != undefined) {
+ emit_u16(bytes, func.locals.i32_count);
+ emit_u16(bytes, func.locals.i64_count);
+ emit_u16(bytes, func.locals.f32_count);
+ emit_u16(bytes, func.locals.f64_count);
+ }
+ emit_u16(bytes, func.body.length);
+ for (var i = 0; i < func.body.length; i++) {
+ emit_u8(bytes, func.body[i]);
+ }
+
+ index++;
+ }
+ }
+
+ if (this.function_table.length > 0) {
+ if (debug) print("emiting function table @ " + bytes.length);
+ emit_u8(bytes, kDeclFunctionTable);
+ emit_varint(bytes, this.function_table.length);
+ for (index of this.function_table) {
+ emit_u16(bytes, index);
+ }
+ }
+
+ if (exports > 0) {
+ if (debug) print("emiting exports @ " + bytes.length);
+ emit_u8(bytes, kDeclExportTable);
+ emit_varint(bytes, exports);
+ for (func of this.functions) {
+ for (exp of func.exports) {
+ emit_u16(bytes, func.index);
+ emit_string(bytes, exp);
+ }
+ }
+ }
+
+ if (this.data_segments.length > 0) {
+ if (debug) print("emiting data segments @ " + bytes.length);
+ emit_u8(bytes, kDeclDataSegments);
+ emit_varint(bytes, this.data_segments.length);
+ for (seg of this.data_segments) {
+ emit_u32(bytes, seg.addr);
+ emit_data_ref(bytes, seg.data);
+ emit_u32(bytes, seg.data.length);
+ emit_u8(bytes, seg.init ? 1 : 0);
+ }
+ }
+
+ // End the module.
+ if (debug) print("emiting end @ " + bytes.length);
+ emit_u8(bytes, kDeclEnd);
+
+ // Collect references and canonicalize strings.
+ var strings = new Object();
+ var data_segments = new Array();
+ var count = 0;
+ for (var i = 0; i < bytes.length; i++) {
+ var b = bytes[i];
+ if (b instanceof StringRef) {
+ count++;
+ var prev = strings[b.string];
+ if (prev) {
+ bytes[i] = prev;
+ } else {
+ strings[b.string] = b;
+ }
+ }
+ if (b instanceof DataRef) {
+ data_segments.push(b);
+ count++;
+ }
+ }
+
+ if (count > 0) {
+ // Emit strings.
+ if (debug) print("emiting strings @ " + bytes.length);
+ for (str in strings) {
+ var ref = strings[str];
+ if (!(ref instanceof StringRef)) continue;
+ if (debug) print(" \"" + str + "\" @ " + bytes.length);
+ ref.pos = bytes.length;
+ for (var i = 0; i < str.length; i++) {
+ emit_u8(bytes, str.charCodeAt(i));
+ }
+ emit_u8(bytes, 0); // null terminator.
+ }
+ // Emit data.
+ if (debug) print("emiting data @ " + bytes.length);
+ for (ref of data_segments) {
+ ref.pos = bytes.length;
+ for (var i = 0; i < ref.data.length; i++) {
+ emit_u8(bytes, ref.data[i]);
+ }
+ }
+ // Update references to strings and data.
+ for (var i = 0; i < bytes.length; i++) {
+ var b = bytes[i];
+ if (b instanceof StringRef || b instanceof DataRef) {
+ bytes[i] = b.pos & 0xFF;
+ bytes[i + 1] = (b.pos >> 8) & 0xFF;
+ bytes[i + 2] = (b.pos >> 16) & 0xFF;
+ bytes[i + 3] = (b.pos >> 24) & 0xFF;
+ }
+ }
+ }
+
+ return bytes;
+}
+
+WasmModuleBuilder.prototype.ToBuffer = function(debug) {
+ var bytes = this.ToArray(debug);
+ var buffer = new ArrayBuffer(bytes.length);
+ var view = new Uint8Array(buffer);
+ for (var i = 0; i < bytes.length; i++) {
+ var val = bytes[i];
+ if ((typeof val) == "string") val = val.charCodeAt(0);
+ view[i] = val | 0;
+ }
+ return buffer;
+}
+
+WasmModuleBuilder.prototype.Instantiate = function(ffi) {
+ var buffer = this.ToBuffer();
+ return _WASMEXP_.instantiateModule(buffer, ffi);
+}

Powered by Google App Engine
This is Rietveld 408576698