From 0ff6d5c2eba51f24deebbe4502061498f3f30409 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 16 May 2020 23:18:14 +0300 Subject: [PATCH 1/9] modernize loader --- lib/loader/index.js | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 0fd7770b91..607f7a58d8 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -47,14 +47,14 @@ function getStringImpl(buffer, ptr) { let length = U32[(ptr + SIZE_OFFSET) >>> 2] >>> 1; let offset = ptr >>> 1; if (length <= CHUNKSIZE) return String.fromCharCode.apply(String, U16.subarray(offset, offset + length)); - const parts = []; + let parts = ''; do { const last = U16[offset + CHUNKSIZE - 1]; const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE; - parts.push(String.fromCharCode.apply(String, U16.subarray(offset, offset += size))); + parts += String.fromCharCode.apply(String, U16.subarray(offset, offset += size)); length -= size; } while (length > CHUNKSIZE); - return parts.join("") + String.fromCharCode.apply(String, U16.subarray(offset, offset + length)); + return parts + String.fromCharCode.apply(String, U16.subarray(offset, offset + length)); } /** Prepares the base module prior to instantiation. */ @@ -70,11 +70,11 @@ function preInstantiate(imports) { const env = (imports.env = imports.env || {}); env.abort = env.abort || function abort(msg, file, line, colm) { const memory = extendedExports.memory || env.memory; // prefer exported, otherwise try imported - throw Error("abort: " + getString(memory, msg) + " at " + getString(memory, file) + ":" + line + ":" + colm); + throw Error(`abort: ${getString(memory, msg)} at ${getString(memory, file)}:${line}:${colm}`); }; env.trace = env.trace || function trace(msg, n) { const memory = extendedExports.memory || env.memory; - console.log("trace: " + getString(memory, msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", ")); + console.log(`trace: ${getString(memory, msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", ")}`); }; env.seed = env.seed || function seed() { return Date.now(); @@ -98,7 +98,7 @@ function postInstantiate(extendedExports, instance) { function getInfo(id) { const U32 = new Uint32Array(memory.buffer); const count = U32[rttiBase >>> 2]; - if ((id >>>= 0) >= count) throw Error("invalid id: " + id); + if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); return U32[(rttiBase + 4 >>> 2) + id * 2]; } @@ -106,7 +106,7 @@ function postInstantiate(extendedExports, instance) { function getBase(id) { const U32 = new Uint32Array(memory.buffer); const count = U32[rttiBase >>> 2]; - if ((id >>>= 0) >= count) throw Error("invalid id: " + id); + if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); return U32[(rttiBase + 4 >>> 2) + id * 2 + 1]; } @@ -135,7 +135,7 @@ function postInstantiate(extendedExports, instance) { function __getString(ptr) { const buffer = memory.buffer; const id = new Uint32Array(buffer)[ptr + ID_OFFSET >>> 2]; - if (id !== STRING_ID) throw Error("not a string: " + ptr); + if (id !== STRING_ID) throw Error(`not a string: ${ptr}`); return getStringImpl(buffer, ptr); } @@ -157,13 +157,13 @@ function postInstantiate(extendedExports, instance) { case 3: return new (signed ? BigInt64Array : BigUint64Array)(buffer); } } - throw Error("unsupported align: " + alignLog2); + throw Error(`unsupported align: ${alignLog2}`); } /** Allocates a new array in the module's memory and returns its retained pointer. */ function __allocArray(id, values) { const info = getInfo(id); - if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error("not an array: " + id + ", flags= " + info); + if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); const align = getValueAlign(info); const length = values.length; const buf = alloc(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); @@ -195,7 +195,7 @@ function postInstantiate(extendedExports, instance) { const U32 = new Uint32Array(memory.buffer); const id = U32[arr + ID_OFFSET >>> 2]; const info = getInfo(id); - if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error("not an array: " + id + ", flags=" + info); + if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); const align = getValueAlign(info); let buf = info & STATICARRAY ? arr @@ -244,8 +244,8 @@ function postInstantiate(extendedExports, instance) { /** Attach a set of get TypedArray and View functions to the exports. */ function attachTypedArrayFunctions(ctor, name, align) { - extendedExports["__get" + name] = getTypedArray.bind(null, ctor, align); - extendedExports["__get" + name + "View"] = getTypedArrayView.bind(null, ctor, align); + extendedExports[`__get${name}`] = getTypedArray.bind(null, ctor, align); + extendedExports[`__get${name}View`] = getTypedArrayView.bind(null, ctor, align); } [ @@ -273,7 +273,7 @@ function postInstantiate(extendedExports, instance) { const U32 = new Uint32Array(memory.buffer); let id = U32[(ptr + ID_OFFSET) >>> 2]; if (id <= U32[rttiBase >>> 2]) { - do if (id == baseId) return true; + do if (id === baseId) return true; while (id = getBase(id)); } return false; @@ -364,9 +364,7 @@ function demangle(exports, extendedExports = {}) { return ctor.wrap(ctor.prototype.constructor(0, ...args)); }; ctor.prototype = { - valueOf: function valueOf() { - return this[THIS]; - } + valueOf() { return this[THIS]; } }; ctor.wrap = function(thisValue) { return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } }); @@ -383,8 +381,8 @@ function demangle(exports, extendedExports = {}) { let getter = exports[internalName.replace("set:", "get:")]; let setter = exports[internalName.replace("get:", "set:")]; Object.defineProperty(curr, name, { - get: function() { return getter(this[THIS]); }, - set: function(value) { setter(this[THIS], value); }, + get() { return getter(this[THIS]); }, + set(value) { setter(this[THIS], value); }, enumerable: true }); } From 2076a1e6bf1d0a44c9d1baf5bd750f6db3ad9049 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 16 May 2020 23:24:11 +0300 Subject: [PATCH 2/9] revert some changes --- lib/loader/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 607f7a58d8..e7620b4e9a 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -364,7 +364,7 @@ function demangle(exports, extendedExports = {}) { return ctor.wrap(ctor.prototype.constructor(0, ...args)); }; ctor.prototype = { - valueOf() { return this[THIS]; } + valueOf: function valueOf() { return this[THIS]; } }; ctor.wrap = function(thisValue) { return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } }); @@ -381,8 +381,8 @@ function demangle(exports, extendedExports = {}) { let getter = exports[internalName.replace("set:", "get:")]; let setter = exports[internalName.replace("get:", "set:")]; Object.defineProperty(curr, name, { - get() { return getter(this[THIS]); }, - set(value) { setter(this[THIS], value); }, + get: function() { return getter(this[THIS]); }, + set: function(value) { setter(this[THIS], value); }, enumerable: true }); } From 32aec8b272ec21b376108d25997a24d1c3ec2ab2 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 16 May 2020 23:37:42 +0300 Subject: [PATCH 3/9] fix --- lib/loader/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index e7620b4e9a..2b81d969ad 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -273,7 +273,7 @@ function postInstantiate(extendedExports, instance) { const U32 = new Uint32Array(memory.buffer); let id = U32[(ptr + ID_OFFSET) >>> 2]; if (id <= U32[rttiBase >>> 2]) { - do if (id === baseId) return true; + do if (id == baseId) return true; while (id = getBase(id)); } return false; @@ -364,7 +364,7 @@ function demangle(exports, extendedExports = {}) { return ctor.wrap(ctor.prototype.constructor(0, ...args)); }; ctor.prototype = { - valueOf: function valueOf() { return this[THIS]; } + valueOf() { return this[THIS]; } }; ctor.wrap = function(thisValue) { return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } }); @@ -381,8 +381,8 @@ function demangle(exports, extendedExports = {}) { let getter = exports[internalName.replace("set:", "get:")]; let setter = exports[internalName.replace("get:", "set:")]; Object.defineProperty(curr, name, { - get: function() { return getter(this[THIS]); }, - set: function(value) { setter(this[THIS], value); }, + get() { return getter(this[THIS]); }, + set(value) { setter(this[THIS], value); }, enumerable: true }); } From 2ef43dd8fb8abecfc465722b4d9e5626cf71ea15 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 18 May 2020 01:13:06 +0300 Subject: [PATCH 4/9] refactor preInstantiate a little --- lib/loader/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 2b81d969ad..1e7dc730e0 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -74,11 +74,10 @@ function preInstantiate(imports) { }; env.trace = env.trace || function trace(msg, n) { const memory = extendedExports.memory || env.memory; - console.log(`trace: ${getString(memory, msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", ")}`); - }; - env.seed = env.seed || function seed() { - return Date.now(); + const params = Array.prototype.slice.call(arguments, 2, 2 + n); + console.log(`trace: ${getString(memory, msg) + (n ? " " : "") + params.join(", ")}`); }; + env.seed = env.seed || Date.now; imports.Math = imports.Math || Math; imports.Date = imports.Date || Date; From 9429298c26224452232ac4e4367e252bdba2a81d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 18 May 2020 01:19:16 +0300 Subject: [PATCH 5/9] more --- lib/loader/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 1e7dc730e0..1009df2d7f 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -72,10 +72,9 @@ function preInstantiate(imports) { const memory = extendedExports.memory || env.memory; // prefer exported, otherwise try imported throw Error(`abort: ${getString(memory, msg)} at ${getString(memory, file)}:${line}:${colm}`); }; - env.trace = env.trace || function trace(msg, n) { + env.trace = env.trace || function trace(msg, n, ...args) { const memory = extendedExports.memory || env.memory; - const params = Array.prototype.slice.call(arguments, 2, 2 + n); - console.log(`trace: ${getString(memory, msg) + (n ? " " : "") + params.join(", ")}`); + console.log(`trace: ${getString(memory, msg) + (n ? " " : "") + args.slice(0, n).join(", ")}`); }; env.seed = env.seed || Date.now; imports.Math = imports.Math || Math; From bd8f35c61b4c181296262490529a575c9cf43165 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 18 May 2020 01:23:46 +0300 Subject: [PATCH 6/9] remove unnecessary parentesies --- lib/loader/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 1009df2d7f..7b2351b3ac 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -44,7 +44,7 @@ const CHUNKSIZE = 1024; function getStringImpl(buffer, ptr) { const U32 = new Uint32Array(buffer); const U16 = new Uint16Array(buffer); - let length = U32[(ptr + SIZE_OFFSET) >>> 2] >>> 1; + let length = U32[ptr + SIZE_OFFSET >>> 2] >>> 1; let offset = ptr >>> 1; if (length <= CHUNKSIZE) return String.fromCharCode.apply(String, U16.subarray(offset, offset + length)); let parts = ''; @@ -269,7 +269,7 @@ function postInstantiate(extendedExports, instance) { /** Tests whether an object is an instance of the class represented by the specified base id. */ function __instanceof(ptr, baseId) { const U32 = new Uint32Array(memory.buffer); - let id = U32[(ptr + ID_OFFSET) >>> 2]; + let id = U32[ptr + ID_OFFSET >>> 2]; if (id <= U32[rttiBase >>> 2]) { do if (id == baseId) return true; while (id = getBase(id)); From 17a91010c8631f66c88f9b64b1f83d56851c6295 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 18 May 2020 02:11:06 +0300 Subject: [PATCH 7/9] replace str concat to interpolations --- lib/loader/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 7b2351b3ac..baf10a8cd3 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -74,7 +74,7 @@ function preInstantiate(imports) { }; env.trace = env.trace || function trace(msg, n, ...args) { const memory = extendedExports.memory || env.memory; - console.log(`trace: ${getString(memory, msg) + (n ? " " : "") + args.slice(0, n).join(", ")}`); + console.log(`trace: ${getString(memory, msg)}${n ? " " : ""}${args.slice(0, n).join(", ")}`); }; env.seed = env.seed || Date.now; imports.Math = imports.Math || Math; From 80d6f93557c54d3b45094803f07fb81905c38bcd Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 18 May 2020 02:18:27 +0300 Subject: [PATCH 8/9] make validateArrayInfo helper --- lib/loader/index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index baf10a8cd3..412d586ca3 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -100,6 +100,13 @@ function postInstantiate(extendedExports, instance) { return U32[(rttiBase + 4 >>> 2) + id * 2]; } + /** Gets and validate runtime type info for the given id for array like objects */ + function validateArrayInfo(id) { + const info = getInfo(id); + if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); + return info; + } + /** Gets the runtime base id for the given id. */ function getBase(id) { const U32 = new Uint32Array(memory.buffer); @@ -160,8 +167,7 @@ function postInstantiate(extendedExports, instance) { /** Allocates a new array in the module's memory and returns its retained pointer. */ function __allocArray(id, values) { - const info = getInfo(id); - if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); + const info = validateArrayInfo(id); const align = getValueAlign(info); const length = values.length; const buf = alloc(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); @@ -192,8 +198,7 @@ function postInstantiate(extendedExports, instance) { function __getArrayView(arr) { const U32 = new Uint32Array(memory.buffer); const id = U32[arr + ID_OFFSET >>> 2]; - const info = getInfo(id); - if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); + const info = validateArrayInfo(id); const align = getValueAlign(info); let buf = info & STATICARRAY ? arr From a7b1287fef4b4193187c682164e9bc38af33f9a6 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 2 Jun 2020 12:22:49 +0300 Subject: [PATCH 9/9] rename validateArrayInfo to getArrayInfo --- lib/loader/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 412d586ca3..6a358ccf42 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -101,7 +101,7 @@ function postInstantiate(extendedExports, instance) { } /** Gets and validate runtime type info for the given id for array like objects */ - function validateArrayInfo(id) { + function getArrayInfo(id) { const info = getInfo(id); if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); return info; @@ -167,7 +167,7 @@ function postInstantiate(extendedExports, instance) { /** Allocates a new array in the module's memory and returns its retained pointer. */ function __allocArray(id, values) { - const info = validateArrayInfo(id); + const info = getArrayInfo(id); const align = getValueAlign(info); const length = values.length; const buf = alloc(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); @@ -198,7 +198,7 @@ function postInstantiate(extendedExports, instance) { function __getArrayView(arr) { const U32 = new Uint32Array(memory.buffer); const id = U32[arr + ID_OFFSET >>> 2]; - const info = validateArrayInfo(id); + const info = getArrayInfo(id); const align = getValueAlign(info); let buf = info & STATICARRAY ? arr