| 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
|
| index 7b77a8c9b105106a2231c6ac12d0f4e66d964a4d..c99ab0a64d43ed149b2ee3c0a0005a627629aa19 100644
|
| --- a/test/mjsunit/wasm/wasm-module-builder.js
|
| +++ b/test/mjsunit/wasm/wasm-module-builder.js
|
| @@ -2,6 +2,12 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| +// Used for encoding f32 and double constants to bits.
|
| +let __buffer = new ArrayBuffer(8);
|
| +let byte_view = new Int8Array(__buffer);
|
| +let f32_view = new Float32Array(__buffer);
|
| +let f64_view = new Float64Array(__buffer);
|
| +
|
| class Binary extends Array {
|
| emit_u8(val) {
|
| this.push(val);
|
| @@ -19,7 +25,7 @@ class Binary extends Array {
|
| this.push((val >> 24) & 0xff);
|
| }
|
|
|
| - emit_varint(val) {
|
| + emit_u32v(val) {
|
| while (true) {
|
| let v = val & 0xff;
|
| val = val >>> 7;
|
| @@ -40,7 +46,7 @@ class Binary extends Array {
|
| emit_string(string) {
|
| // When testing illegal names, we pass a byte array directly.
|
| if (string instanceof Array) {
|
| - this.emit_varint(string.length);
|
| + this.emit_u32v(string.length);
|
| this.emit_bytes(string);
|
| return;
|
| }
|
| @@ -48,7 +54,7 @@ class Binary extends Array {
|
| // This is the hacky way to convert a JavaScript string to a UTF8 encoded
|
| // string only containing single-byte characters.
|
| let string_utf8 = unescape(encodeURIComponent(string));
|
| - this.emit_varint(string_utf8.length);
|
| + this.emit_u32v(string_utf8.length);
|
| for (let i = 0; i < string_utf8.length; i++) {
|
| this.emit_u8(string_utf8.charCodeAt(i));
|
| }
|
| @@ -66,26 +72,26 @@ class Binary extends Array {
|
| let section = new Binary;
|
| content_generator(section);
|
| // Emit section length.
|
| - this.emit_varint(section.length);
|
| + this.emit_u32v(section.length);
|
| // Copy the temporary buffer.
|
| this.push(...section);
|
| }
|
| }
|
|
|
| class WasmFunctionBuilder {
|
| - constructor(name, type_index) {
|
| + constructor(module, name, type_index) {
|
| + this.module = module;
|
| this.name = name;
|
| this.type_index = type_index;
|
| - this.exports = [];
|
| }
|
|
|
| exportAs(name) {
|
| - this.exports.push(name);
|
| + this.module.exports.push({name: name, kind: kExternalFunction, index: this.index});
|
| return this;
|
| }
|
|
|
| exportFunc() {
|
| - this.exports.push(this.name);
|
| + this.exportAs(this.name);
|
| return this;
|
| }
|
|
|
| @@ -100,17 +106,33 @@ class WasmFunctionBuilder {
|
| }
|
| }
|
|
|
| +class WasmGlobalBuilder {
|
| + constructor(module, type, mutable) {
|
| + this.module = module;
|
| + this.type = type;
|
| + this.mutable = mutable;
|
| + this.init = 0;
|
| + }
|
| +
|
| + exportAs(name) {
|
| + this.module.exports.push({name: name, kind: kExternalGlobal, index: this.index});
|
| + return this;
|
| + }
|
| +}
|
| +
|
| class WasmModuleBuilder {
|
| constructor() {
|
| this.types = [];
|
| this.imports = [];
|
| + this.exports = [];
|
| this.globals = [];
|
| this.functions = [];
|
| - this.exports = [];
|
| this.table = [];
|
| this.segments = [];
|
| this.explicit = [];
|
| this.pad = null;
|
| + this.num_imported_funcs = 0;
|
| + this.num_imported_globals = 0;
|
| return this;
|
| }
|
|
|
| @@ -139,29 +161,39 @@ class WasmModuleBuilder {
|
| return this.types.length - 1;
|
| }
|
|
|
| - addGlobal(local_type) {
|
| - this.globals.push(local_type);
|
| - return this.globals.length - 1;
|
| + addGlobal(local_type, mutable) {
|
| + let glob = new WasmGlobalBuilder(this, local_type, mutable);
|
| + glob.index = this.globals.length + this.num_imported_globals;
|
| + this.globals.push(glob);
|
| + return glob;
|
| }
|
|
|
| addFunction(name, type) {
|
| let type_index = (typeof type) == "number" ? type : this.addType(type);
|
| - let func = new WasmFunctionBuilder(name, type_index);
|
| - func.index = this.functions.length + this.imports.length;
|
| + let func = new WasmFunctionBuilder(this, name, type_index);
|
| + func.index = this.functions.length + this.num_imported_funcs;
|
| this.functions.push(func);
|
| return func;
|
| }
|
|
|
| addImportWithModule(module, name, type) {
|
| let type_index = (typeof type) == "number" ? type : this.addType(type);
|
| - this.imports.push({module: module, name: name, type: type_index});
|
| - return this.imports.length - 1;
|
| + this.imports.push({module: module, name: name, kind: kExternalFunction,
|
| + type: type_index});
|
| + return this.num_imported_funcs++;
|
| }
|
|
|
| addImport(name, type) {
|
| return this.addImportWithModule(name, undefined, type);
|
| }
|
|
|
| + addImportedGlobal(module, name, type) {
|
| + let o = {module: module, name: name, kind: kExternalGlobal, type: type,
|
| + mutable: false}
|
| + this.imports.push(o);
|
| + return this.num_imported_globals++;
|
| + }
|
| +
|
| addDataSegment(addr, data, init) {
|
| this.segments.push({addr: addr, data: data, init: init});
|
| return this.segments.length - 1;
|
| @@ -183,14 +215,14 @@ class WasmModuleBuilder {
|
| if (wasm.types.length > 0) {
|
| if (debug) print("emitting types @ " + binary.length);
|
| binary.emit_section(kTypeSectionCode, section => {
|
| - section.emit_varint(wasm.types.length);
|
| + section.emit_u32v(wasm.types.length);
|
| for (let type of wasm.types) {
|
| section.emit_u8(kWasmFunctionTypeForm);
|
| - section.emit_varint(type.params.length);
|
| + section.emit_u32v(type.params.length);
|
| for (let param of type.params) {
|
| section.emit_u8(param);
|
| }
|
| - section.emit_varint(type.results.length);
|
| + section.emit_u32v(type.results.length);
|
| for (let result of type.results) {
|
| section.emit_u8(result);
|
| }
|
| @@ -202,12 +234,19 @@ class WasmModuleBuilder {
|
| if (wasm.imports.length > 0) {
|
| if (debug) print("emitting imports @ " + binary.length);
|
| binary.emit_section(kImportSectionCode, section => {
|
| - section.emit_varint(wasm.imports.length);
|
| + section.emit_u32v(wasm.imports.length);
|
| for (let imp of wasm.imports) {
|
| section.emit_string(imp.module);
|
| section.emit_string(imp.name || '');
|
| - section.emit_u8(kExternalFunction);
|
| - section.emit_varint(imp.type);
|
| + section.emit_u8(imp.kind);
|
| + if (imp.kind == kExternalFunction) {
|
| + section.emit_u32v(imp.type);
|
| + } else if (imp.kind == kExternalGlobal) {
|
| + section.emit_u32v(imp.type);
|
| + section.emit_u8(imp.mutable);
|
| + } else {
|
| + throw new Error("unknown/unsupported import kind " + imp.kind);
|
| + }
|
| }
|
| });
|
| }
|
| @@ -215,16 +254,14 @@ class WasmModuleBuilder {
|
| // Add functions declarations
|
| let has_names = false;
|
| let names = false;
|
| - let exports = 0;
|
| if (wasm.functions.length > 0) {
|
| if (debug) print("emitting function decls @ " + binary.length);
|
| binary.emit_section(kFunctionSectionCode, section => {
|
| - section.emit_varint(wasm.functions.length);
|
| + section.emit_u32v(wasm.functions.length);
|
| for (let func of wasm.functions) {
|
| has_names = has_names || (func.name != undefined &&
|
| func.name.length > 0);
|
| - exports += func.exports.length;
|
| - section.emit_varint(func.type_index);
|
| + section.emit_u32v(func.type_index);
|
| }
|
| });
|
| }
|
| @@ -236,8 +273,8 @@ class WasmModuleBuilder {
|
| section.emit_u8(1); // one table entry
|
| section.emit_u8(kWasmAnyFunctionTypeForm);
|
| section.emit_u8(1);
|
| - section.emit_varint(wasm.table.length);
|
| - section.emit_varint(wasm.table.length);
|
| + section.emit_u32v(wasm.table.length);
|
| + section.emit_u32v(wasm.table.length);
|
| });
|
| }
|
|
|
| @@ -246,9 +283,9 @@ class WasmModuleBuilder {
|
| if (debug) print("emitting memory @ " + binary.length);
|
| binary.emit_section(kMemorySectionCode, section => {
|
| section.emit_u8(1); // one memory entry
|
| - section.emit_varint(kResizableMaximumFlag);
|
| - section.emit_varint(wasm.memory.min);
|
| - section.emit_varint(wasm.memory.max);
|
| + section.emit_u32v(kResizableMaximumFlag);
|
| + section.emit_u32v(wasm.memory.min);
|
| + section.emit_u32v(wasm.memory.max);
|
| });
|
| }
|
|
|
| @@ -256,28 +293,46 @@ class WasmModuleBuilder {
|
| if (wasm.globals.length > 0) {
|
| if (debug) print ("emitting globals @ " + binary.length);
|
| binary.emit_section(kGlobalSectionCode, section => {
|
| - section.emit_varint(wasm.globals.length);
|
| - for (let global_type of wasm.globals) {
|
| - section.emit_u8(global_type);
|
| - section.emit_u8(true); // mutable
|
| - switch (global_type) {
|
| + section.emit_u32v(wasm.globals.length);
|
| + for (let global of wasm.globals) {
|
| + section.emit_u8(global.type);
|
| + section.emit_u8(global.mutable);
|
| + if ((typeof global.init_index) == "undefined") {
|
| + // Emit a constant initializer.
|
| + switch (global.type) {
|
| case kAstI32:
|
| section.emit_u8(kExprI32Const);
|
| - section.emit_u8(0);
|
| + section.emit_u32v(global.init);
|
| break;
|
| case kAstI64:
|
| section.emit_u8(kExprI64Const);
|
| - section.emit_u8(0);
|
| + section.emit_u8(global.init);
|
| break;
|
| case kAstF32:
|
| section.emit_u8(kExprF32Const);
|
| - section.emit_u32(0);
|
| + f32_view[0] = global.init;
|
| + section.emit_u8(byte_view[0]);
|
| + section.emit_u8(byte_view[1]);
|
| + section.emit_u8(byte_view[2]);
|
| + section.emit_u8(byte_view[3]);
|
| break;
|
| case kAstF64:
|
| - section.emit_u8(kExprI32Const);
|
| - section.emit_u32(0);
|
| - section.emit_u32(0);
|
| + section.emit_u8(kExprF64Const);
|
| + f64_view[0] = global.init;
|
| + section.emit_u8(byte_view[0]);
|
| + section.emit_u8(byte_view[1]);
|
| + section.emit_u8(byte_view[2]);
|
| + section.emit_u8(byte_view[3]);
|
| + section.emit_u8(byte_view[4]);
|
| + section.emit_u8(byte_view[5]);
|
| + section.emit_u8(byte_view[6]);
|
| + section.emit_u8(byte_view[7]);
|
| break;
|
| + }
|
| + } else {
|
| + // Emit a global-index initializer.
|
| + section.emit_u8(kExprGetGlobal);
|
| + section.emit_u32v(global.init_index);
|
| }
|
| section.emit_u8(kExprEnd); // end of init expression
|
| }
|
| @@ -286,16 +341,15 @@ class WasmModuleBuilder {
|
|
|
| // Add export table.
|
| var mem_export = (wasm.memory != undefined && wasm.memory.exp);
|
| - if (exports > 0 || mem_export) {
|
| + var exports_count = wasm.exports.length + (mem_export ? 1 : 0);
|
| + if (exports_count > 0) {
|
| if (debug) print("emitting exports @ " + binary.length);
|
| binary.emit_section(kExportSectionCode, section => {
|
| - section.emit_varint(exports + (mem_export ? 1 : 0));
|
| - for (let func of wasm.functions) {
|
| - for (let exp of func.exports) {
|
| - section.emit_string(exp);
|
| - section.emit_u8(kExternalFunction);
|
| - section.emit_varint(func.index);
|
| - }
|
| + section.emit_u32v(exports_count);
|
| + for (let exp of wasm.exports) {
|
| + section.emit_string(exp.name);
|
| + section.emit_u8(exp.kind);
|
| + section.emit_u32v(exp.index);
|
| }
|
| if (mem_export) {
|
| section.emit_string("memory");
|
| @@ -309,7 +363,7 @@ class WasmModuleBuilder {
|
| if (wasm.start_index != undefined) {
|
| if (debug) print("emitting start function @ " + binary.length);
|
| binary.emit_section(kStartSectionCode, section => {
|
| - section.emit_varint(wasm.start_index);
|
| + section.emit_u32v(wasm.start_index);
|
| });
|
| }
|
|
|
| @@ -322,9 +376,9 @@ class WasmModuleBuilder {
|
| section.emit_u8(kExprI32Const);
|
| section.emit_u8(0);
|
| section.emit_u8(kExprEnd);
|
| - section.emit_varint(wasm.table.length);
|
| + section.emit_u32v(wasm.table.length);
|
| for (let index of wasm.table) {
|
| - section.emit_varint(index);
|
| + section.emit_u32v(index);
|
| }
|
| });
|
| }
|
| @@ -334,7 +388,7 @@ class WasmModuleBuilder {
|
| // emit function bodies
|
| if (debug) print("emitting code @ " + binary.length);
|
| binary.emit_section(kCodeSectionCode, section => {
|
| - section.emit_varint(wasm.functions.length);
|
| + section.emit_u32v(wasm.functions.length);
|
| for (let func of wasm.functions) {
|
| // Function body length will be patched later.
|
| let local_decls = [];
|
| @@ -356,13 +410,13 @@ class WasmModuleBuilder {
|
| }
|
|
|
| let header = new Binary;
|
| - header.emit_varint(local_decls.length);
|
| + header.emit_u32v(local_decls.length);
|
| for (let decl of local_decls) {
|
| - header.emit_varint(decl.count);
|
| + header.emit_u32v(decl.count);
|
| header.emit_u8(decl.type);
|
| }
|
|
|
| - section.emit_varint(header.length + func.body.length);
|
| + section.emit_u32v(header.length + func.body.length);
|
| section.emit_bytes(header);
|
| section.emit_bytes(func.body);
|
| }
|
| @@ -373,13 +427,13 @@ class WasmModuleBuilder {
|
| if (wasm.segments.length > 0) {
|
| if (debug) print("emitting data segments @ " + binary.length);
|
| binary.emit_section(kDataSectionCode, section => {
|
| - section.emit_varint(wasm.segments.length);
|
| + section.emit_u32v(wasm.segments.length);
|
| for (let seg of wasm.segments) {
|
| section.emit_u8(0); // linear memory index 0
|
| section.emit_u8(kExprI32Const);
|
| - section.emit_varint(seg.addr);
|
| + section.emit_u32v(seg.addr);
|
| section.emit_u8(kExprEnd);
|
| - section.emit_varint(seg.data.length);
|
| + section.emit_u32v(seg.data.length);
|
| section.emit_bytes(seg.data);
|
| }
|
| });
|
| @@ -396,7 +450,7 @@ class WasmModuleBuilder {
|
| if (debug) print("emitting names @ " + binary.length);
|
| binary.emit_section(kUnknownSectionCode, section => {
|
| section.emit_string("name");
|
| - section.emit_varint(wasm.functions.length);
|
| + section.emit_u32v(wasm.functions.length);
|
| for (let func of wasm.functions) {
|
| var name = func.name == undefined ? "" : func.name;
|
| section.emit_string(name);
|
|
|