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}; diff --git a/test/harness/wasm-module-builder.js b/test/harness/wasm-module-builder.js index d9948eb1a4..f73794bacc 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; @@ -101,7 +108,7 @@ 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); + 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. @@ -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); } }); @@ -543,19 +556,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 +624,8 @@ class WasmModuleBuilder { let instance = new WebAssembly.Instance(module, ffi); return instance; } + + toModule(debug = false) { + return new WebAssembly.Module(this.toBuffer(debug)); + } }