-
Notifications
You must be signed in to change notification settings - Fork 456
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,17 +107,23 @@ 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. | ||
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; | ||
} | ||
|
||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also here, the spec version looks more reasonable than the v8 version. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
} | ||
|
||
|
@@ -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)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.