Commit 8808e038 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Tidy up wasm-module-builder.js

Fix some JS smells as suggested in
https://github.com/WebAssembly/spec/issues/897.

R=ahaas@chromium.org

Bug: v8:8238
Change-Id: Idc4f738da849f28477563df628dcae2805b1b47e
Reviewed-on: https://chromium-review.googlesource.com/c/1301476Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57029}
parent 4fc90a25
...@@ -84,10 +84,11 @@ class WasmFunctionBuilder { ...@@ -84,10 +84,11 @@ class WasmFunctionBuilder {
this.name = name; this.name = name;
this.type_index = type_index; this.type_index = type_index;
this.body = []; this.body = [];
this.locals = [];
this.local_names = [];
} }
numLocalNames() { numLocalNames() {
if (this.local_names === undefined) return 0;
let num_local_names = 0; let num_local_names = 0;
for (let loc_name of this.local_names) { for (let loc_name of this.local_names) {
if (loc_name !== undefined) ++num_local_names; if (loc_name !== undefined) ++num_local_names;
...@@ -123,7 +124,7 @@ class WasmFunctionBuilder { ...@@ -123,7 +124,7 @@ class WasmFunctionBuilder {
getNumLocals() { getNumLocals() {
let total_locals = 0; let total_locals = 0;
for (let l of this.locals || []) { for (let l of this.locals) {
for (let type of ["i32", "i64", "f32", "f64", "s128"]) { for (let type of ["i32", "i64", "f32", "f64", "s128"]) {
total_locals += l[type + "_count"] || 0; total_locals += l[type + "_count"] || 0;
} }
...@@ -133,10 +134,8 @@ class WasmFunctionBuilder { ...@@ -133,10 +134,8 @@ class WasmFunctionBuilder {
addLocals(locals, names) { addLocals(locals, names) {
const old_num_locals = this.getNumLocals(); const old_num_locals = this.getNumLocals();
if (!this.locals) this.locals = []
this.locals.push(locals); this.locals.push(locals);
if (names) { if (names) {
if (!this.local_names) this.local_names = [];
const missing_names = old_num_locals - this.local_names.length; const missing_names = old_num_locals - this.local_names.length;
this.local_names.push(...new Array(missing_names), ...names); this.local_names.push(...new Array(missing_names), ...names);
} }
...@@ -242,7 +241,7 @@ class WasmModuleBuilder { ...@@ -242,7 +241,7 @@ class WasmModuleBuilder {
return func; return func;
} }
addImport(module = "", name, type) { addImport(module, name, type) {
if (this.functions.length != 0) { if (this.functions.length != 0) {
throw new Error('Imported functions must be declared before local ones'); throw new Error('Imported functions must be declared before local ones');
} }
...@@ -252,7 +251,7 @@ class WasmModuleBuilder { ...@@ -252,7 +251,7 @@ class WasmModuleBuilder {
return this.num_imported_funcs++; return this.num_imported_funcs++;
} }
addImportedGlobal(module = "", name, type, mutable = false) { addImportedGlobal(module, name, type, mutable = false) {
if (this.globals.length != 0) { if (this.globals.length != 0) {
throw new Error('Imported globals must be declared before local ones'); throw new Error('Imported globals must be declared before local ones');
} }
...@@ -262,20 +261,20 @@ class WasmModuleBuilder { ...@@ -262,20 +261,20 @@ class WasmModuleBuilder {
return this.num_imported_globals++; return this.num_imported_globals++;
} }
addImportedMemory(module = "", name, initial = 0, maximum, shared) { addImportedMemory(module, name, initial = 0, maximum, shared) {
let o = {module: module, name: name, kind: kExternalMemory, let o = {module: module, name: name, kind: kExternalMemory,
initial: initial, maximum: maximum, shared: shared}; initial: initial, maximum: maximum, shared: shared};
this.imports.push(o); this.imports.push(o);
return this; return this;
} }
addImportedTable(module = "", name, initial, maximum) { addImportedTable(module, name, initial, maximum) {
let o = {module: module, name: name, kind: kExternalTable, initial: initial, let o = {module: module, name: name, kind: kExternalTable, initial: initial,
maximum: maximum}; maximum: maximum};
this.imports.push(o); this.imports.push(o);
} }
addImportedException(module = "", name, type) { addImportedException(module, name, type) {
if (this.exceptions.length != 0) { if (this.exceptions.length != 0) {
throw new Error('Imported exceptions must be declared before local ones'); throw new Error('Imported exceptions must be declared before local ones');
} }
......
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