Commit c9576a81 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Several fixes in wasm-module-builder.js

This CL cleans up a few things as noted by binji in
https://github.com/WebAssembly/spec/pull/979, plus a few more I found
along the way.
In particular:
1) Remove the unused and incorrect {bytesWithHeader} method.
2) Introduce kMaxVarInt32Size and kMaxVarInt64Size constants.
3) Remove redundant {ensure_space} calls (irrelevant for performance).
4) Use {toModule} method instead of duplicating code.
5) Merge two identical leb encoding implementations.

R=titzer@chromium.org
CC=​binji@chromium.org

Change-Id: Idec74e2e46a71766107c182a4176c516d883adad
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1511273
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60124}
parent a72db1d2
...@@ -48,12 +48,8 @@ var kWasmV3 = 0; ...@@ -48,12 +48,8 @@ var kWasmV3 = 0;
var kHeaderSize = 8; var kHeaderSize = 8;
var kPageSize = 65536; var kPageSize = 65536;
var kSpecMaxPages = 65535; var kSpecMaxPages = 65535;
var kMaxVarInt32Size = 5;
function bytesWithHeader(...input) { var kMaxVarInt64Size = 10;
const header =
[kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3];
return bytes(header + input);
}
let kDeclNoLocals = 0; let kDeclNoLocals = 0;
...@@ -534,30 +530,26 @@ class Binary { ...@@ -534,30 +530,26 @@ class Binary {
this.buffer[this.length++] = val >> 24; this.buffer[this.length++] = val >> 24;
} }
emit_u32v(val) { emit_leb(val, max_len) {
this.ensure_space(5); this.ensure_space(max_len);
while (true) { for (let i = 0; i < max_len; ++i) {
let v = val & 0xff; let v = val & 0xff;
val = val >>> 7; val = val >>> 7;
if (val == 0) { if (val == 0) {
this.buffer[this.length++] = v; this.buffer[this.length++] = v;
break; return;
} }
this.buffer[this.length++] = v | 0x80; this.buffer[this.length++] = v | 0x80;
} }
throw new Error("Leb value exceeds maximum length of " + max_len);
}
emit_u32v(val) {
this.emit_leb(val, kMaxVarInt32Size);
} }
emit_u64v(val) { emit_u64v(val) {
this.ensure_space(10); this.emit_leb(val, kMaxVarInt64Size);
while (true) {
let v = val & 0xff;
val = val >>> 7;
if (val == 0) {
this.buffer[this.length++] = v;
break;
}
this.buffer[this.length++] = v | 0x80;
}
} }
emit_bytes(data) { emit_bytes(data) {
...@@ -749,7 +741,6 @@ class WasmModuleBuilder { ...@@ -749,7 +741,6 @@ class WasmModuleBuilder {
addCustomSection(name, bytes) { addCustomSection(name, bytes) {
name = this.stringToBytes(name); name = this.stringToBytes(name);
var section = new Binary(); var section = new Binary();
section.ensure_space(1 + 5 + 5 + name.length + bytes.length);
section.emit_u8(0); section.emit_u8(0);
section.emit_u32v(name.length + bytes.length); section.emit_u32v(name.length + bytes.length);
section.emit_bytes(name); section.emit_bytes(name);
...@@ -1200,7 +1191,6 @@ class WasmModuleBuilder { ...@@ -1200,7 +1191,6 @@ class WasmModuleBuilder {
header.emit_u8(decl.type); header.emit_u8(decl.type);
} }
section.ensure_space(5 + header.length + func.body.length);
section.emit_u32v(header.length + func.body.length); section.emit_u32v(header.length + func.body.length);
section.emit_bytes(header.trunc_buffer()); section.emit_bytes(header.trunc_buffer());
section.emit_bytes(func.body); section.emit_bytes(func.body);
...@@ -1301,7 +1291,7 @@ class WasmModuleBuilder { ...@@ -1301,7 +1291,7 @@ class WasmModuleBuilder {
} }
instantiate(ffi) { instantiate(ffi) {
let module = new WebAssembly.Module(this.toBuffer()); let module = this.toModule();
let instance = new WebAssembly.Instance(module, ffi); let instance = new WebAssembly.Instance(module, ffi);
return instance; return instance;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment