Skip to content

Update JavaScript WasmModuleBuilder with new functionality. #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions test/harness/wasm-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down
89 changes: 69 additions & 20 deletions test/harness/wasm-module-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -101,16 +108,22 @@ 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.
this.body.push(kExprEnd);
return this;
}

addLocals(locals) {
addBodyWithEnd(body) {
this.body = body;
return this;
}

addLocals(locals, names) {
this.locals = locals;
this.local_names = names;
return this;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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]);
}
}
});
}
});
}

Expand All @@ -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));
}
}