Skip to content
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

Update JavaScript WasmModuleBuilder with new functionality. #530

Merged
merged 3 commits into from
Jul 27, 2017
Merged
Changes from 1 commit
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
96 changes: 72 additions & 24 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 @@ -100,17 +107,23 @@ class WasmFunctionBuilder {

addBody(body) {
for (let b of body) {
if (typeof b !== 'number' || (b & (~0xFF)) !== 0 )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that in this case, the version in the spec repo was updated to do even stricter tests. Hence we should merge this change back to v8.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I had just blindly copied over the v8 version instead of merging the two.

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.
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 All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, the spec version looks more reasonable than the v8 version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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);
});
}

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

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