From 1e71fb7c737c654850153033d7688a375db12d3c Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 15:50:56 +0300 Subject: [PATCH 01/20] loader: add ability create arrays with capacity --- lib/loader/index.js | 21 ++++++++++++--------- lib/loader/tests/index.js | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index f0bfca90fa..08e2f0e6d9 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -193,10 +193,11 @@ function postInstantiate(extendedExports, instance) { } /** Allocates a new array in the module's memory and returns its pointer. */ - function __newArray(id, values) { + function __newArray(id, valuesOrLen) { const info = getArrayInfo(id); const align = getValueAlign(info); - const length = values.length; + const hasValues = Array.isArray(valuesOrLen); + const length = hasValues ? valuesOrLen.length : +valuesOrLen; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { @@ -212,14 +213,16 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); - if (info & VAL_MANAGED) { - for (let i = 0; i < length; ++i) { - const value = values[i]; - view[(buf >>> align) + i] = value; + if (hasValues) { + const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); + if (info & VAL_MANAGED) { + for (let i = 0; i < length; ++i) { + const value = valuesOrLen[i]; + view[(buf >>> align) + i] = value; + } + } else { + view.set(valuesOrLen, buf >>> align); } - } else { - view.set(values, buf >>> align); } return result; } diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index 1f59445aa6..de293582f2 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -186,6 +186,20 @@ function test(file) { assert.deepStrictEqual(exports.__getArray(ref), arr); } + // should be able to create empty arrays + { + let ref = exports.__newArray(exports.ARRAYI32_ID); + assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); + assert.deepStrictEqual(exports.__getArray(ref), []); + } + + // should be able to create arrays with capacity + { + let ref = exports.__newArray(exports.ARRAYI32_ID, 32); + assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); + assert.deepStrictEqual(exports.__getArray(ref).length, 32); + } + // should be able to work with normal arrays { let arr = [1, 2, 3, 4, 5]; From 28b96d9cb9d7d9d6f4145f8c3a13531d6f17d0c9 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 15:52:36 +0300 Subject: [PATCH 02/20] refactor --- lib/loader/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 08e2f0e6d9..07b271a178 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -193,11 +193,12 @@ function postInstantiate(extendedExports, instance) { } /** Allocates a new array in the module's memory and returns its pointer. */ - function __newArray(id, valuesOrLen) { + function __newArray(id, valuesOrCapacity) { + const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const hasValues = Array.isArray(valuesOrLen); - const length = hasValues ? valuesOrLen.length : +valuesOrLen; + const hasValues = Array.isArray(input); + const length = hasValues ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { @@ -217,11 +218,11 @@ function postInstantiate(extendedExports, instance) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { - const value = valuesOrLen[i]; + const value = input[i]; view[(buf >>> align) + i] = value; } } else { - view.set(valuesOrLen, buf >>> align); + view.set(input, buf >>> align); } } return result; From 73f0e5de4e52959a657b3e0fbe6bccf2ff1a36b8 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:04:09 +0300 Subject: [PATCH 03/20] fix --- 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 07b271a178..ad06418db6 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,8 +197,8 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const hasValues = Array.isArray(input); - const length = hasValues ? input.length : +input; + const arrayLike = input != null && typeof input.length != 'undefined'; + const length = arrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { @@ -214,7 +214,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (hasValues) { + if (arrayLike) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { From 5d49e273e69305e31298078ae885766ac7473ff6 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:12:43 +0300 Subject: [PATCH 04/20] better --- 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 ad06418db6..5b9c82fc12 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,7 +197,7 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const arrayLike = input != null && typeof input.length != 'undefined'; + const arrayLike = input != null && typeof input.length === "number"; const length = arrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; From 04b7b4387252325766efd1b629e0dd61dbbb28f7 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:20:37 +0300 Subject: [PATCH 05/20] update definisions --- lib/loader/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loader/index.d.ts b/lib/loader/index.d.ts index ce2dc2ef3a..1a87720bed 100644 --- a/lib/loader/index.d.ts +++ b/lib/loader/index.d.ts @@ -89,7 +89,7 @@ export interface ASUtil { /** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */ __newArrayBuffer(buf: ArrayBuffer): number; /** Allocates a new array in the module's memory and returns a reference (pointer) to it. */ - __newArray(id: number, values: ArrayLike): number; + __newArray(id: number, valuesOrCapacity: ArrayLike | number): number; /** Allocates an instance of the class represented by the specified id. */ __new(size: number, id: number): number; From 68adb8ae2430aa6a902ad29f8aa91caef7b1e059 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:21:17 +0300 Subject: [PATCH 06/20] make it optional --- lib/loader/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loader/index.d.ts b/lib/loader/index.d.ts index 1a87720bed..2a765eab0d 100644 --- a/lib/loader/index.d.ts +++ b/lib/loader/index.d.ts @@ -89,7 +89,7 @@ export interface ASUtil { /** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */ __newArrayBuffer(buf: ArrayBuffer): number; /** Allocates a new array in the module's memory and returns a reference (pointer) to it. */ - __newArray(id: number, valuesOrCapacity: ArrayLike | number): number; + __newArray(id: number, valuesOrCapacity?: ArrayLike | number): number; /** Allocates an instance of the class represented by the specified id. */ __new(size: number, id: number): number; From d27dc53a43135c4d1d3b40af50069c90bd01c258 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:23:54 +0300 Subject: [PATCH 07/20] deepStrictEqual ->strictEqual --- lib/loader/tests/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index de293582f2..f6a5b4a190 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -197,7 +197,7 @@ function test(file) { { let ref = exports.__newArray(exports.ARRAYI32_ID, 32); assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); - assert.deepStrictEqual(exports.__getArray(ref).length, 32); + assert.strictEqual(exports.__getArray(ref).length, 32); } // should be able to work with normal arrays From b3afc897cb1e1ae245bef6bf115713fbc0185c57 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:28:56 +0300 Subject: [PATCH 08/20] better --- 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 5b9c82fc12..8a9d84ac77 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,8 +197,8 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const arrayLike = input != null && typeof input.length === "number"; - const length = arrayLike ? input.length : +input; + const arrayLike = input != null && typeof input[Symbol.iterator] === 'function'; + const length = +(arrayLike ? input.length : input); const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { From 2713a406ab335329ff49c2bbe149c46e8c2f985b Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:29:45 +0300 Subject: [PATCH 09/20] add test --- lib/loader/tests/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index f6a5b4a190..b31f4b5f0e 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -200,6 +200,13 @@ function test(file) { assert.strictEqual(exports.__getArray(ref).length, 32); } + // should be able to create arrays with capacity from arraylike object + { + let ref = exports.__newArray(exports.ARRAYI32_ID, { length: 16 }); + assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); + assert.strictEqual(exports.__getArray(ref).length, 16); + } + // should be able to work with normal arrays { let arr = [1, 2, 3, 4, 5]; From 9cfbe01ff998bf92a5a9055a335ca38bbe80f5b3 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:38:01 +0300 Subject: [PATCH 10/20] fix --- lib/loader/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 8a9d84ac77..3206642872 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,8 +197,9 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const arrayLike = input != null && typeof input[Symbol.iterator] === 'function'; - const length = +(arrayLike ? input.length : input); + const arrayLike = input != null && typeof input.length === "number"; + const iterable = input[Symbol.iterator] === 'function'; + const length = arrayLike || iterable ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { @@ -214,7 +215,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (arrayLike) { + if (iterable) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { From 1aa60328c519840b0ee1cfc5d16be75ac6b212d5 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:46:24 +0300 Subject: [PATCH 11/20] works? --- 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 3206642872..e66fb8a6ae 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,9 +197,8 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const arrayLike = input != null && typeof input.length === "number"; - const iterable = input[Symbol.iterator] === 'function'; - const length = arrayLike || iterable ? input.length : +input; + const isCapacity = input === "number"; + const length = isCapacity ? input : !!input && input.length; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { @@ -215,7 +214,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (iterable) { + if (!isCapacity) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { From 0de2903051b8e05b5543ccf66947d9023710cdb7 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:51:46 +0300 Subject: [PATCH 12/20] revert --- lib/loader/index.js | 2 +- lib/loader/tests/index.js | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index e66fb8a6ae..1bd79b4d00 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -198,7 +198,7 @@ function postInstantiate(extendedExports, instance) { const info = getArrayInfo(id); const align = getValueAlign(info); const isCapacity = input === "number"; - const length = isCapacity ? input : !!input && input.length; + const length = +(isCapacity ? input : input != null && input.length); const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index b31f4b5f0e..f6a5b4a190 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -200,13 +200,6 @@ function test(file) { assert.strictEqual(exports.__getArray(ref).length, 32); } - // should be able to create arrays with capacity from arraylike object - { - let ref = exports.__newArray(exports.ARRAYI32_ID, { length: 16 }); - assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); - assert.strictEqual(exports.__getArray(ref).length, 16); - } - // should be able to work with normal arrays { let arr = [1, 2, 3, 4, 5]; From 3d4a77037948e125cf4ede5a7eb255ac656fb820 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 7 Aug 2021 16:55:06 +0300 Subject: [PATCH 13/20] revert --- 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 1bd79b4d00..f3944e610a 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,8 +197,8 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const isCapacity = input === "number"; - const length = +(isCapacity ? input : input != null && input.length); + const isArrayLike = input != null && typeof input.length === "number"; + const length = isArrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { @@ -214,7 +214,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (!isCapacity) { + if (isArrayLike) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { From 4c257a6c20aa47fe2c3dc5b3d4521f9491e9c513 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 10 Aug 2021 20:50:18 +0300 Subject: [PATCH 14/20] better --- lib/loader/index.js | 8 +++++--- lib/loader/tests/index.js | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index f3944e610a..966910a7a1 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,7 +197,9 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const isArrayLike = input != null && typeof input.length === "number"; + const isObject = typeof input === "object"; + const isArrayLike = isObject && typeof input.length === "number"; + const hasIterator = isObject && typeof input[Symbol.iterator] === "function"; const length = isArrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; @@ -214,7 +216,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (isArrayLike) { + if (hasIterator && isArrayLike) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { @@ -420,7 +422,7 @@ export function demangle(exports, extendedExports = {}) { }); } } else { - if (name === 'constructor') { + if (name === "constructor") { (curr[name] = (...args) => { setArgumentsLength(args.length); return elem(...args); diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index f6a5b4a190..cc2ea24d94 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -200,6 +200,13 @@ function test(file) { assert.strictEqual(exports.__getArray(ref).length, 32); } + // should be able to create arrays with capacity from array like object + { + let ref = exports.__newArray(exports.ARRAYI32_ID, { length: 4 }); + assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); + assert.strictEqual(exports.__getArray(ref).length, 4); + } + // should be able to work with normal arrays { let arr = [1, 2, 3, 4, 5]; From 3701e4644d3cfe898c973231568d04dc5fef283d Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 10 Aug 2021 20:57:49 +0300 Subject: [PATCH 15/20] better --- 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 966910a7a1..e2c6f462e5 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -199,7 +199,7 @@ function postInstantiate(extendedExports, instance) { const align = getValueAlign(info); const isObject = typeof input === "object"; const isArrayLike = isObject && typeof input.length === "number"; - const hasIterator = isObject && typeof input[Symbol.iterator] === "function"; + const hasIterator = isArrayLike && typeof input[Symbol.iterator] === "function"; const length = isArrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; @@ -216,7 +216,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (hasIterator && isArrayLike) { + if (hasIterator) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { From 47dbf2a6b762b762ff7d828527c68b9e39aeb405 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 13 Aug 2021 14:25:08 +0300 Subject: [PATCH 16/20] simplify --- lib/loader/index.d.ts | 2 +- lib/loader/index.js | 6 ++---- lib/loader/tests/index.js | 7 ------- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/loader/index.d.ts b/lib/loader/index.d.ts index 2a765eab0d..0b9a389396 100644 --- a/lib/loader/index.d.ts +++ b/lib/loader/index.d.ts @@ -89,7 +89,7 @@ export interface ASUtil { /** Allocates a new ArrayBuffer in the module's memory and returns a reference (pointer) to it. */ __newArrayBuffer(buf: ArrayBuffer): number; /** Allocates a new array in the module's memory and returns a reference (pointer) to it. */ - __newArray(id: number, valuesOrCapacity?: ArrayLike | number): number; + __newArray(id: number, valuesOrCapacity?: Array | ArrayBufferView | number): number; /** Allocates an instance of the class represented by the specified id. */ __new(size: number, id: number): number; diff --git a/lib/loader/index.js b/lib/loader/index.js index e2c6f462e5..9a580e4fb7 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,9 +197,7 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const isObject = typeof input === "object"; - const isArrayLike = isObject && typeof input.length === "number"; - const hasIterator = isArrayLike && typeof input[Symbol.iterator] === "function"; + const isArrayLike = input != null && typeof input.length === "number"; const length = isArrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; @@ -216,7 +214,7 @@ function postInstantiate(extendedExports, instance) { if (info & ARRAY) U32[arr + ARRAY_LENGTH_OFFSET >>> 2] = length; result = arr; } - if (hasIterator) { + if (isArrayLike) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index cc2ea24d94..f6a5b4a190 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -200,13 +200,6 @@ function test(file) { assert.strictEqual(exports.__getArray(ref).length, 32); } - // should be able to create arrays with capacity from array like object - { - let ref = exports.__newArray(exports.ARRAYI32_ID, { length: 4 }); - assert(exports.__instanceof(ref, exports.ARRAYI32_ID)); - assert.strictEqual(exports.__getArray(ref).length, 4); - } - // should be able to work with normal arrays { let arr = [1, 2, 3, 4, 5]; From d7d715b02f0a10da4cecdd66385a8c682239231a Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 13 Aug 2021 14:45:12 +0300 Subject: [PATCH 17/20] simplify --- 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 9a580e4fb7..5f854f1d89 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -197,7 +197,7 @@ function postInstantiate(extendedExports, instance) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); - const isArrayLike = input != null && typeof input.length === "number"; + const isArrayLike = typeof input !== "number"; const length = isArrayLike ? input.length : +input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; From 36cdfde3fb1227abf52e93ca92cfb80b5508cbe9 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 13 Aug 2021 14:49:15 +0300 Subject: [PATCH 18/20] fix --- 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 5f854f1d89..b96c58d20a 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -193,12 +193,12 @@ function postInstantiate(extendedExports, instance) { } /** Allocates a new array in the module's memory and returns its pointer. */ - function __newArray(id, valuesOrCapacity) { + function __newArray(id, valuesOrCapacity = 0) { const input = valuesOrCapacity; const info = getArrayInfo(id); const align = getValueAlign(info); const isArrayLike = typeof input !== "number"; - const length = isArrayLike ? input.length : +input; + const length = isArrayLike ? input.length : input; const buf = __new(length << align, info & STATICARRAY ? id : ARRAYBUFFER_ID); let result; if (info & STATICARRAY) { From 30357177b8d561b36a8d3ac204222fe230845126 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 13 Aug 2021 14:54:53 +0300 Subject: [PATCH 19/20] cache buf >>> align --- lib/loader/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index b96c58d20a..bbd60a3236 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -216,13 +216,14 @@ function postInstantiate(extendedExports, instance) { } if (isArrayLike) { const view = getView(align, info & VAL_SIGNED, info & VAL_FLOAT); + const start = buf >>> align; if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { const value = input[i]; - view[(buf >>> align) + i] = value; + view[start + i] = value; } } else { - view.set(input, buf >>> align); + view.set(input, start); } } return result; From 536f6150c8970886a6cf6404c4db7e019ee602be Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Fri, 13 Aug 2021 14:55:42 +0300 Subject: [PATCH 20/20] simplify --- lib/loader/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index bbd60a3236..8dbad05367 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -219,8 +219,7 @@ function postInstantiate(extendedExports, instance) { const start = buf >>> align; if (info & VAL_MANAGED) { for (let i = 0; i < length; ++i) { - const value = input[i]; - view[start + i] = value; + view[start + i] = input[i]; } } else { view.set(input, start);