| 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 c99ab0a64d43ed149b2ee3c0a0005a627629aa19..7b77a8c9b105106a2231c6ac12d0f4e66d964a4d 100644
|
| --- a/test/mjsunit/wasm/wasm-module-builder.js
|
| +++ b/test/mjsunit/wasm/wasm-module-builder.js
|
| @@ -1,12 +1,6 @@
|
| // 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.
|
| -
|
| -// 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) {
|
| @@ -25,7 +19,7 @@
|
| this.push((val >> 24) & 0xff);
|
| }
|
|
|
| - emit_u32v(val) {
|
| + emit_varint(val) {
|
| while (true) {
|
| let v = val & 0xff;
|
| val = val >>> 7;
|
| @@ -46,7 +40,7 @@
|
| emit_string(string) {
|
| // When testing illegal names, we pass a byte array directly.
|
| if (string instanceof Array) {
|
| - this.emit_u32v(string.length);
|
| + this.emit_varint(string.length);
|
| this.emit_bytes(string);
|
| return;
|
| }
|
| @@ -54,7 +48,7 @@
|
| // 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_u32v(string_utf8.length);
|
| + this.emit_varint(string_utf8.length);
|
| for (let i = 0; i < string_utf8.length; i++) {
|
| this.emit_u8(string_utf8.charCodeAt(i));
|
| }
|
| @@ -72,26 +66,26 @@
|
| let section = new Binary;
|
| content_generator(section);
|
| // Emit section length.
|
| - this.emit_u32v(section.length);
|
| + this.emit_varint(section.length);
|
| // Copy the temporary buffer.
|
| this.push(...section);
|
| }
|
| }
|
|
|
| class WasmFunctionBuilder {
|
| - constructor(module, name, type_index) {
|
| - this.module = module;
|
| + constructor(name, type_index) {
|
| this.name = name;
|
| this.type_index = type_index;
|
| + this.exports = [];
|
| }
|
|
|
| exportAs(name) {
|
| - this.module.exports.push({name: name, kind: kExternalFunction, index: this.index});
|
| + this.exports.push(name);
|
| return this;
|
| }
|
|
|
| exportFunc() {
|
| - this.exportAs(this.name);
|
| + this.exports.push(this.name);
|
| return this;
|
| }
|
|
|
| @@ -102,20 +96,6 @@
|
|
|
| addLocals(locals) {
|
| this.locals = locals;
|
| - return this;
|
| - }
|
| -}
|
| -
|
| -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;
|
| }
|
| }
|
| @@ -124,15 +104,13 @@
|
| 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;
|
| }
|
|
|
| @@ -161,37 +139,27 @@
|
| return this.types.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;
|
| + addGlobal(local_type) {
|
| + this.globals.push(local_type);
|
| + return this.globals.length - 1;
|
| }
|
|
|
| addFunction(name, type) {
|
| let type_index = (typeof type) == "number" ? type : this.addType(type);
|
| - let func = new WasmFunctionBuilder(this, name, type_index);
|
| - func.index = this.functions.length + this.num_imported_funcs;
|
| + let func = new WasmFunctionBuilder(name, type_index);
|
| + func.index = this.functions.length + this.imports.length;
|
| 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, kind: kExternalFunction,
|
| - type: type_index});
|
| - return this.num_imported_funcs++;
|
| + this.imports.push({module: module, name: name, type: type_index});
|
| + return this.imports.length - 1;
|
| }
|
|
|
| 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) {
|
| @@ -215,14 +183,14 @@
|
| if (wasm.types.length > 0) {
|
| if (debug) print("emitting types @ " + binary.length);
|
| binary.emit_section(kTypeSectionCode, section => {
|
| - section.emit_u32v(wasm.types.length);
|
| + section.emit_varint(wasm.types.length);
|
| for (let type of wasm.types) {
|
| section.emit_u8(kWasmFunctionTypeForm);
|
| - section.emit_u32v(type.params.length);
|
| + section.emit_varint(type.params.length);
|
| for (let param of type.params) {
|
| section.emit_u8(param);
|
| }
|
| - section.emit_u32v(type.results.length);
|
| + section.emit_varint(type.results.length);
|
| for (let result of type.results) {
|
| section.emit_u8(result);
|
| }
|
| @@ -234,19 +202,12 @@
|
| if (wasm.imports.length > 0) {
|
| if (debug) print("emitting imports @ " + binary.length);
|
| binary.emit_section(kImportSectionCode, section => {
|
| - section.emit_u32v(wasm.imports.length);
|
| + section.emit_varint(wasm.imports.length);
|
| for (let imp of wasm.imports) {
|
| section.emit_string(imp.module);
|
| section.emit_string(imp.name || '');
|
| - 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);
|
| - }
|
| + section.emit_u8(kExternalFunction);
|
| + section.emit_varint(imp.type);
|
| }
|
| });
|
| }
|
| @@ -254,14 +215,16 @@
|
| // 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_u32v(wasm.functions.length);
|
| + section.emit_varint(wasm.functions.length);
|
| for (let func of wasm.functions) {
|
| has_names = has_names || (func.name != undefined &&
|
| func.name.length > 0);
|
| - section.emit_u32v(func.type_index);
|
| + exports += func.exports.length;
|
| + section.emit_varint(func.type_index);
|
| }
|
| });
|
| }
|
| @@ -273,8 +236,8 @@
|
| section.emit_u8(1); // one table entry
|
| section.emit_u8(kWasmAnyFunctionTypeForm);
|
| section.emit_u8(1);
|
| - section.emit_u32v(wasm.table.length);
|
| - section.emit_u32v(wasm.table.length);
|
| + section.emit_varint(wasm.table.length);
|
| + section.emit_varint(wasm.table.length);
|
| });
|
| }
|
|
|
| @@ -283,9 +246,9 @@
|
| if (debug) print("emitting memory @ " + binary.length);
|
| binary.emit_section(kMemorySectionCode, section => {
|
| section.emit_u8(1); // one memory entry
|
| - section.emit_u32v(kResizableMaximumFlag);
|
| - section.emit_u32v(wasm.memory.min);
|
| - section.emit_u32v(wasm.memory.max);
|
| + section.emit_varint(kResizableMaximumFlag);
|
| + section.emit_varint(wasm.memory.min);
|
| + section.emit_varint(wasm.memory.max);
|
| });
|
| }
|
|
|
| @@ -293,46 +256,28 @@
|
| if (wasm.globals.length > 0) {
|
| if (debug) print ("emitting globals @ " + binary.length);
|
| binary.emit_section(kGlobalSectionCode, section => {
|
| - 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) {
|
| + 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) {
|
| case kAstI32:
|
| section.emit_u8(kExprI32Const);
|
| - section.emit_u32v(global.init);
|
| + section.emit_u8(0);
|
| break;
|
| case kAstI64:
|
| section.emit_u8(kExprI64Const);
|
| - section.emit_u8(global.init);
|
| + section.emit_u8(0);
|
| break;
|
| case kAstF32:
|
| section.emit_u8(kExprF32Const);
|
| - 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]);
|
| + section.emit_u32(0);
|
| break;
|
| case kAstF64:
|
| - 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]);
|
| + section.emit_u8(kExprI32Const);
|
| + section.emit_u32(0);
|
| + section.emit_u32(0);
|
| 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
|
| }
|
| @@ -341,15 +286,16 @@
|
|
|
| // Add export table.
|
| var mem_export = (wasm.memory != undefined && wasm.memory.exp);
|
| - var exports_count = wasm.exports.length + (mem_export ? 1 : 0);
|
| - if (exports_count > 0) {
|
| + if (exports > 0 || mem_export) {
|
| if (debug) print("emitting exports @ " + binary.length);
|
| binary.emit_section(kExportSectionCode, section => {
|
| - 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);
|
| + 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);
|
| + }
|
| }
|
| if (mem_export) {
|
| section.emit_string("memory");
|
| @@ -363,7 +309,7 @@
|
| if (wasm.start_index != undefined) {
|
| if (debug) print("emitting start function @ " + binary.length);
|
| binary.emit_section(kStartSectionCode, section => {
|
| - section.emit_u32v(wasm.start_index);
|
| + section.emit_varint(wasm.start_index);
|
| });
|
| }
|
|
|
| @@ -376,9 +322,9 @@
|
| section.emit_u8(kExprI32Const);
|
| section.emit_u8(0);
|
| section.emit_u8(kExprEnd);
|
| - section.emit_u32v(wasm.table.length);
|
| + section.emit_varint(wasm.table.length);
|
| for (let index of wasm.table) {
|
| - section.emit_u32v(index);
|
| + section.emit_varint(index);
|
| }
|
| });
|
| }
|
| @@ -388,7 +334,7 @@
|
| // emit function bodies
|
| if (debug) print("emitting code @ " + binary.length);
|
| binary.emit_section(kCodeSectionCode, section => {
|
| - section.emit_u32v(wasm.functions.length);
|
| + section.emit_varint(wasm.functions.length);
|
| for (let func of wasm.functions) {
|
| // Function body length will be patched later.
|
| let local_decls = [];
|
| @@ -410,13 +356,13 @@
|
| }
|
|
|
| let header = new Binary;
|
| - header.emit_u32v(local_decls.length);
|
| + header.emit_varint(local_decls.length);
|
| for (let decl of local_decls) {
|
| - header.emit_u32v(decl.count);
|
| + header.emit_varint(decl.count);
|
| header.emit_u8(decl.type);
|
| }
|
|
|
| - section.emit_u32v(header.length + func.body.length);
|
| + section.emit_varint(header.length + func.body.length);
|
| section.emit_bytes(header);
|
| section.emit_bytes(func.body);
|
| }
|
| @@ -427,13 +373,13 @@
|
| if (wasm.segments.length > 0) {
|
| if (debug) print("emitting data segments @ " + binary.length);
|
| binary.emit_section(kDataSectionCode, section => {
|
| - section.emit_u32v(wasm.segments.length);
|
| + section.emit_varint(wasm.segments.length);
|
| for (let seg of wasm.segments) {
|
| section.emit_u8(0); // linear memory index 0
|
| section.emit_u8(kExprI32Const);
|
| - section.emit_u32v(seg.addr);
|
| + section.emit_varint(seg.addr);
|
| section.emit_u8(kExprEnd);
|
| - section.emit_u32v(seg.data.length);
|
| + section.emit_varint(seg.data.length);
|
| section.emit_bytes(seg.data);
|
| }
|
| });
|
| @@ -450,7 +396,7 @@
|
| if (debug) print("emitting names @ " + binary.length);
|
| binary.emit_section(kUnknownSectionCode, section => {
|
| section.emit_string("name");
|
| - section.emit_u32v(wasm.functions.length);
|
| + section.emit_varint(wasm.functions.length);
|
| for (let func of wasm.functions) {
|
| var name = func.name == undefined ? "" : func.name;
|
| section.emit_string(name);
|
|
|