From fc826f634312a6cac69ec1f44aecdfa681bb4fcf Mon Sep 17 00:00:00 2001 From: "Ben L. Titzer" Date: Wed, 26 Jul 2017 11:26:21 +0200 Subject: [PATCH 1/3] Update JavaScript WasmModuleBuilder with new functionality. --- test/harness/wasm-module-builder.js | 96 +++++++++++++++++++++-------- 1 file changed, 72 insertions(+), 24 deletions(-) diff --git a/test/harness/wasm-module-builder.js b/test/harness/wasm-module-builder.js index d9948eb1a4..a26384e545 100644 --- a/test/harness/wasm-module-builder.js +++ b/test/harness/wasm-module-builder.js @@ -74,9 +74,7 @@ class Binary extends Array { // Emit section length. this.emit_u32v(section.length); // Copy the temporary buffer. - for (const sectionByte of section) { - this.push(sectionByte); - } + this.push(...section); } } @@ -88,6 +86,15 @@ class WasmFunctionBuilder { this.body = []; } + numLocalNames() { + if (this.local_names === undefined) return 0; + let num_local_names = 0; + for (let loc_name of this.local_names) { + if (loc_name !== undefined) ++num_local_names; + } + return num_local_names; + } + exportAs(name) { this.module.addExport(name, this.index); return this; @@ -100,8 +107,8 @@ class WasmFunctionBuilder { addBody(body) { for (let b of body) { - if (typeof b !== 'number' || (b & (~0xFF)) !== 0 ) - throw new Error('invalid body (entries have to be 8 bit numbers): ' + body); + if (typeof b != 'number') + throw new Error('invalid body (entries have to be numbers): ' + body); } this.body = body.slice(); // Automatically add the end for the function block to the body. @@ -109,8 +116,14 @@ class WasmFunctionBuilder { return this; } - addLocals(locals) { + addBodyWithEnd(body) { + this.body = body; + return this; + } + + addLocals(locals, names) { this.locals = locals; + this.local_names = names; return this; } @@ -274,6 +287,11 @@ class WasmModuleBuilder { return this; } + setName(name) { + this.name = name; + return this; + } + toArray(debug = false) { let binary = new Binary; let wasm = this; @@ -333,16 +351,11 @@ class WasmModuleBuilder { } // Add functions declarations - let num_function_names = 0; - let names = false; if (wasm.functions.length > 0) { if (debug) print("emitting function decls @ " + binary.length); binary.emit_section(kFunctionSectionCode, section => { section.emit_u32v(wasm.functions.length); for (let func of wasm.functions) { - if (func.name !== undefined) { - ++num_function_names; - } section.emit_u32v(func.type_index); } }); @@ -365,10 +378,9 @@ class WasmModuleBuilder { if (debug) print("emitting memory @ " + binary.length); binary.emit_section(kMemorySectionCode, section => { section.emit_u8(1); // one memory entry - const has_max = wasm.memory.max !== undefined; - section.emit_u32v(has_max ? kResizableMaximumFlag : 0); + section.emit_u32v(kResizableMaximumFlag); section.emit_u32v(wasm.memory.min); - if (has_max) section.emit_u32v(wasm.memory.max); + section.emit_u32v(wasm.memory.max); }); } @@ -543,19 +555,51 @@ class WasmModuleBuilder { binary.emit_bytes(exp); } - // Add function names. - if (num_function_names > 0) { + // Add names. + let num_function_names = 0; + let num_functions_with_local_names = 0; + for (let func of wasm.functions) { + if (func.name !== undefined) ++num_function_names; + if (func.numLocalNames() > 0) ++num_functions_with_local_names; + } + if (num_function_names > 0 || num_functions_with_local_names > 0 || + wasm.name !== undefined) { if (debug) print('emitting names @ ' + binary.length); binary.emit_section(kUnknownSectionCode, section => { section.emit_string('name'); - section.emit_section(kFunctionNamesCode, name_section => { - name_section.emit_u32v(num_function_names); - for (let func of wasm.functions) { - if (func.name === undefined) continue; - name_section.emit_u32v(func.index); - name_section.emit_string(func.name); - } - }); + // Emit module name. + if (wasm.name !== undefined) { + section.emit_section(kModuleNameCode, name_section => { + name_section.emit_string(wasm.name); + }); + } + // Emit function names. + if (num_function_names > 0) { + section.emit_section(kFunctionNamesCode, name_section => { + name_section.emit_u32v(num_function_names); + for (let func of wasm.functions) { + if (func.name === undefined) continue; + name_section.emit_u32v(func.index); + name_section.emit_string(func.name); + } + }); + } + // Emit local names. + if (num_functions_with_local_names > 0) { + section.emit_section(kLocalNamesCode, name_section => { + name_section.emit_u32v(num_functions_with_local_names); + for (let func of wasm.functions) { + if (func.numLocalNames() == 0) continue; + name_section.emit_u32v(func.index); + name_section.emit_u32v(func.numLocalNames()); + for (let i = 0; i < func.local_names.length; ++i) { + if (func.local_names[i] === undefined) continue; + name_section.emit_u32v(i); + name_section.emit_string(func.local_names[i]); + } + } + }); + } }); } @@ -579,4 +623,8 @@ class WasmModuleBuilder { let instance = new WebAssembly.Instance(module, ffi); return instance; } + + toModule(debug = false) { + return new WebAssembly.Module(this.toBuffer(debug)); + } } From 68761ef0069bc664f87bae03db2e1f0b1832ac00 Mon Sep 17 00:00:00 2001 From: "Ben L. Titzer" Date: Wed, 26 Jul 2017 11:45:30 +0200 Subject: [PATCH 2/3] Remove references to non-standard S128 type in wasm-constants.js --- test/harness/wasm-constants.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/harness/wasm-constants.js b/test/harness/wasm-constants.js index 68c36ef1fd..9e06985990 100644 --- a/test/harness/wasm-constants.js +++ b/test/harness/wasm-constants.js @@ -87,7 +87,6 @@ let kWasmI32 = 0x7f; let kWasmI64 = 0x7e; let kWasmF32 = 0x7d; let kWasmF64 = 0x7c; -let kWasmS128 = 0x7b; let kExternalFunction = 0; let kExternalTable = 1; @@ -118,7 +117,6 @@ let kSig_v_l = makeSig([kWasmI64], []); let kSig_v_d = makeSig([kWasmF64], []); let kSig_v_dd = makeSig([kWasmF64, kWasmF64], []); let kSig_v_ddi = makeSig([kWasmF64, kWasmF64, kWasmI32], []); -let kSig_s_v = makeSig([], [kWasmS128]); function makeSig(params, results) { return {params: params, results: results}; From db0779dd5c2b99f523f7ed5d6ed1f3f1371f397f Mon Sep 17 00:00:00 2001 From: "Ben L. Titzer" Date: Wed, 26 Jul 2017 12:54:58 +0200 Subject: [PATCH 3/3] Add back enhanced functionality. --- test/harness/wasm-module-builder.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/harness/wasm-module-builder.js b/test/harness/wasm-module-builder.js index a26384e545..f73794bacc 100644 --- a/test/harness/wasm-module-builder.js +++ b/test/harness/wasm-module-builder.js @@ -107,8 +107,8 @@ class WasmFunctionBuilder { addBody(body) { for (let b of body) { - if (typeof b != 'number') - throw new Error('invalid body (entries have to be numbers): ' + body); + if (typeof b !== 'number' || (b & (~0xFF)) !== 0 ) + throw new Error('invalid body (entries must be 8 bit numbers): ' + body); } this.body = body.slice(); // Automatically add the end for the function block to the body. @@ -378,9 +378,10 @@ class WasmModuleBuilder { if (debug) print("emitting memory @ " + binary.length); binary.emit_section(kMemorySectionCode, section => { section.emit_u8(1); // one memory entry - section.emit_u32v(kResizableMaximumFlag); + const has_max = wasm.memory.max !== undefined; + section.emit_u32v(has_max ? kResizableMaximumFlag : 0); section.emit_u32v(wasm.memory.min); - section.emit_u32v(wasm.memory.max); + if (has_max) section.emit_u32v(wasm.memory.max); }); }