From c5f22cd93dfa413f25a89d06bda49e29c62a7cdc Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 16:29:24 +0300 Subject: [PATCH 01/14] [loader] add __getFunction method --- lib/loader/index.d.ts | 3 +++ lib/loader/index.js | 8 ++++++++ lib/loader/tests/index.js | 21 ++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/loader/index.d.ts b/lib/loader/index.d.ts index ce2dc2ef3a..3ffc842373 100644 --- a/lib/loader/index.d.ts +++ b/lib/loader/index.d.ts @@ -82,6 +82,9 @@ export interface ASUtil { /** Gets a live view on a Float64Array's values in the module's memory. */ __getFloat64ArrayView(ptr: number): Float64Array; + /** Gets a function from poiner which contain table's index. */ + __getFunction(ptr: number): ((...args: unknown[]) => unknown) | null; + /** Tests whether a managed object is an instance of the class represented by the specified base id. */ __instanceof(ptr: number, baseId: number): boolean; /** Allocates a new string in the module's memory and returns a reference (pointer) to it. */ diff --git a/lib/loader/index.js b/lib/loader/index.js index f0bfca90fa..0702f30d3a 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -263,6 +263,14 @@ function postInstantiate(extendedExports, instance) { extendedExports.__getArrayBuffer = __getArrayBuffer; + /** Gets a function from poiner which contain table's index. */ + function __getFunction(ptr) { + const index = new Uint32Array(memory.buffer)[ptr >>> 2]; + return table.get(index); + } + + extendedExports.__getFunction = __getFunction; + /** Copies a typed array's values from the module's memory. */ function getTypedArray(Type, alignLog2, ptr) { return new Type(getTypedArrayView(Type, alignLog2, ptr)); diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index 1f59445aa6..aa3b65ae7b 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -261,7 +261,10 @@ function test(file) { function testInstantiate(file) { // should be able to instantiate from a buffer (async () => { - const { exports, instance, module } = await loader.instantiate(fs.readFileSync(__dirname + "/build/" + file), {}); + const { exports, instance, module } = await loader.instantiate( + fs.readFileSync(__dirname + "/build/" + file), + {} + ); assert(exports.memory); assert(instance && instance instanceof WebAssembly.Instance); assert(module && module instanceof WebAssembly.Module); @@ -269,8 +272,10 @@ function testInstantiate(file) { // should be able to instantiate from a wasm module (async () => { - const wasmModule = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/" + file)); - const { exports, instance, module } = await loader.instantiate(wasmModule, {}); + const { exports, instance, module } = await loader.instantiate( + new WebAssembly.Module(fs.readFileSync(__dirname + "/build/" + file)), + {} + ); assert(exports.memory); assert(instance && instance instanceof WebAssembly.Instance); assert(module && module instanceof WebAssembly.Module); @@ -278,7 +283,10 @@ function testInstantiate(file) { // should be able to instantiate from a promise yielding a buffer (async () => { - const { exports, instance, module } = await loader.instantiate(fs.promises.readFile(__dirname + "/build/" + file), {}); + const { exports, instance, module } = await loader.instantiate( + fs.promises.readFile(__dirname + "/build/" + file), + {} + ); assert(exports.memory); assert(instance && instance instanceof WebAssembly.Instance); assert(module && module instanceof WebAssembly.Module); @@ -286,7 +294,10 @@ function testInstantiate(file) { // should be able to mimic instantiateStreaming under node (for now) (async () => { - const { exports, instance, module } = await loader.instantiateStreaming(fs.promises.readFile(__dirname + "/build/" + file), {}); + const { exports, instance, module } = await loader.instantiateStreaming( + fs.promises.readFile(__dirname + "/build/" + file), + {} + ); assert(exports.memory); assert(instance && instance instanceof WebAssembly.Instance); assert(module && module instanceof WebAssembly.Module); From 3c1bff0040d929e92931a07466e44d66594258cf Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 20:37:32 +0300 Subject: [PATCH 02/14] add test --- lib/loader/package.json | 4 ++-- lib/loader/tests/assembly/index.ts | 4 ++++ lib/loader/tests/build/default.wasm | Bin 12073 -> 12096 bytes lib/loader/tests/build/legacy.wasm | Bin 12073 -> 12096 bytes lib/loader/tests/index.js | 6 ++++++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/loader/package.json b/lib/loader/package.json index 2092906ae5..aa5a588d52 100644 --- a/lib/loader/package.json +++ b/lib/loader/package.json @@ -30,8 +30,8 @@ }, "scripts": { "asbuild": "npm run asbuild:default && npm run asbuild:legacy", - "asbuild:default": "node ../../bin/asc tests/assembly/index.ts --binaryFile tests/build/default.wasm --exportRuntime", - "asbuild:legacy": "node ../../bin/asc tests/assembly/index.ts --disable mutable-globals --binaryFile tests/build/legacy.wasm --exportRuntime", + "asbuild:default": "node ../../bin/asc tests/assembly/index.ts --binaryFile tests/build/default.wasm --exportRuntime --exportTable", + "asbuild:legacy": "node ../../bin/asc tests/assembly/index.ts --disable mutable-globals --binaryFile tests/build/legacy.wasm --exportRuntime --exportTable", "build": "npx esm2umd loader index.js > umd/index.js", "test": "node tests && node tests/umd" }, diff --git a/lib/loader/tests/assembly/index.ts b/lib/loader/tests/assembly/index.ts index af9801e6af..9f76940b29 100644 --- a/lib/loader/tests/assembly/index.ts +++ b/lib/loader/tests/assembly/index.ts @@ -69,6 +69,10 @@ export function dotrace(num: f64): void { trace("The answer is", 1, num); } +export function getVaraddFunc(): (a: i32, b: i32) => i32 { + return varadd; +} + export const UINT8ARRAY_ID = idof(); export const INT16ARRAY_ID = idof(); export const UINT16ARRAY_ID = idof(); diff --git a/lib/loader/tests/build/default.wasm b/lib/loader/tests/build/default.wasm index cd267fa256afadbeff99e480b7ef7344540a221a..b105c1038df01f7ed7131609a0cb046f0d3d536e 100644 GIT binary patch delta 877 zcmb7AO;6NN6uqyV&R3bCB1A)sDan0!Fb-faIEl&BvXIj&H*R|AT&P+*Vtp70~qy zuJ1Cqb5uDrXv5vyVULnF!3?VT!`cp7ZA=NwD3;}u$1oz^w(G;alx21N=vT{FmK}H= zu^G&%mPHwJtvU{gd^UM7L;{83y6h3eAIhgT>dBB%kNDtfH!v-TRj>M;)_4%m5LRL; zJWj2iJW{z~EULDJ*NfRmc${r@M_N1ZMMB4B0R=x7F@dlg1O)k|SmFgCIbhmy*lEF| zjKCVh%(a1b*~{2(l4;^M*)oJF8+)F(GO(Uj;JA(T2`|8G?2<_-UWZN^*_jjq6RBu6 z`)!KxT7seb{8Qo@!n=x+%plCW@(ZGH@;{U4>u$BLrwjG)`oPD80_Gks433;xR>wd` zIv7&gl-8<=ic~RQO6I<7zR2>KzFtk2u1KI}L62mTHnqJqD2WtatnpvPMr}b&$1QFC z|2}yzp+AZhW3}ERuchuG;5$S8{KHU|e;G2+{8p)!TkKXrl`7E++7h2DHIT#Kl^zJ} z8=eu^7%t{+>SCgHE5=FwY`Ah>FuEKu!ZoRbI*=&nW_qQl>j`xdp$(}b>cnbM*BmYx zCDi0)V^B<+HcB~1E$BL27TbX~%6H1A1^?VA3Va{qcca*SOd}E+eAMU{{fp(2z=ar} Z#Q3Tl`98<^yKJ2J$0trYy=$C9%3sX(>LCCC delta 826 zcmb7=%}*0S7{=e(?FVIPSrDRIX2xuaS2V)|u1W-__6^REKmhH4%*xjkKQ%z$? z?momIyW_sc$Bp-IKIPS;kc?Y+JxKb zqccXD#Nu!#b5z^ZYg&xJVjN?iJcWyLZ8|>OO=8^g!`;0U#x`|bVsofy7&Fctyk-SN z9ZnoJQD6Y$phLLH>Mf7>JXj!poj1Tr1f03VhlYN`Vk33JulOx*no<^k6G_2IY5ml( z%LyXWVpC=}A~iIgRNdatYg1oNXxW@H=y>T+qB3(h!Ouh%mj>ieplZeSjZF17gNC6h6$4ZI7jrIH9tq@i;6xA`Cm1{i2v zOUu<1?N}Y%7yGfRXmxiuo=Blpkn0!Fb-faIEl&BvXIj&H*R|AT&P+*Vtp70~qy zuJ1Cqb5uDrXv5vyVULnF!3?VT!`cp7ZA=NwD3;}u$1oz^w(G;alx21N=vT{FmK}H= zu^G&%mPHwJtvU{gd^UM7L;{83y6h3eAIhgT>dBB%kNDtfH!v-TRj>M;)_4%m5LRL; zJWj2iJW{z~EULDJ*NfRmc${r@M_N1ZMMB4B0R=x7F@dlg1O)k|SmFgCIbhmy*lEF| zjKCVh%(a1b*~{2(l4;^M*)oJF8+)F(GO(Uj;JA(T2`|8G?2<_-UWZN^*_jjq6RBu6 z`)!KxT7seb{8Qo@!n=x+%plCW@(ZGH@;{U4>u$BLrwjG)`oPD80_Gks433;xR>wd` zIv7&gl-8<=ic~RQO6I<7zR2>KzFtk2u1KI}L62mTHnqJqD2WtatnpvPMr}b&$1QFC z|2}yzp+AZhW3}ERuchuG;5$S8{KHU|e;G2+{8p)!TkKXrl`7E++7h2DHIT#Kl^zJ} z8=eu^7%t{+>SCgHE5=FwY`Ah>FuEKu!ZoRbI*=&nW_qQl>j`xdp$(}b>cnbM*BmYx zCDi0)V^B<+HcB~1E$BL27TbX~%6H1A1^?VA3Va{qcca*SOd}E+eAMU{{fp(2z=ar} Z#Q3Tl`98<^yKJ2J$0trYy=$C9%3sX(>LCCC delta 826 zcmb7=%}*0S7{=e(?FVIPSrDRIX2xuaS2V)|u1W-__6^REKmhH4%*xjkKQ%z$? z?momIyW_sc$Bp-IKIPS;kc?Y+JxKb zqccXD#Nu!#b5z^ZYg&xJVjN?iJcWyLZ8|>OO=8^g!`;0U#x`|bVsofy7&Fctyk-SN z9ZnoJQD6Y$phLLH>Mf7>JXj!poj1Tr1f03VhlYN`Vk33JulOx*no<^k6G_2IY5ml( z%LyXWVpC=}A~iIgRNdatYg1oNXxW@H=y>T+qB3(h!Ouh%mj>ieplZeSjZF17gNC6h6$4ZI7jrIH9tq@i;6xA`Cm1{i2v zOUu<1?N}Y%7yGfRXmxiuo=Blpk Date: Mon, 16 Aug 2021 20:48:38 +0300 Subject: [PATCH 03/14] more --- lib/loader/tests/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/loader/tests/index.js b/lib/loader/tests/index.js index 669b828f35..2808a24b0e 100644 --- a/lib/loader/tests/index.js +++ b/lib/loader/tests/index.js @@ -234,7 +234,11 @@ function test(file) { // should be able to return a function { const addFunc = exports.__getFunction(exports.getVaraddFunc()); + assert(typeof addFunc === "function"); assert.strictEqual(addFunc(1, 2), 3); + + const invalidFunc = exports.__getFunction(0); + assert(invalidFunc == null); } // should be able to use trace From 7628b24da559ff9880fb9ad90845a60d3fdff1fc Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 20:54:43 +0300 Subject: [PATCH 04/14] use authors --- lib/loader/package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/loader/package.json b/lib/loader/package.json index aa5a588d52..cb02fb6537 100644 --- a/lib/loader/package.json +++ b/lib/loader/package.json @@ -10,7 +10,10 @@ "wasm" ], "version": "0.0.0", - "author": "Daniel Wirtz ", + "authors": [ + "Daniel Wirtz ", + "MaxGraey " + ], "license": "Apache-2.0", "homepage": "https://assemblyscript.org", "repository": { From 9548abe676dcd111c968d24fd66f1908d6d61fb4 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 21:02:08 +0300 Subject: [PATCH 05/14] split to autor and contributors --- lib/loader/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/loader/package.json b/lib/loader/package.json index cb02fb6537..8b9a69518f 100644 --- a/lib/loader/package.json +++ b/lib/loader/package.json @@ -10,8 +10,8 @@ "wasm" ], "version": "0.0.0", - "authors": [ - "Daniel Wirtz ", + "author": "Daniel Wirtz ", + "contributors": [ "MaxGraey " ], "license": "Apache-2.0", From d10692384a6e62cf2bd36cdebba08cd65479eeec Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 21:28:43 +0300 Subject: [PATCH 06/14] check table existing --- lib/loader/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/loader/index.js b/lib/loader/index.js index 0702f30d3a..8a5103519d 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -83,6 +83,7 @@ function preInstantiate(imports) { return extendedExports; } +const E_NOEXPORT_TABLE = "Operation requires compiling with --exportTable"; const E_NOEXPORTRUNTIME = "Operation requires compiling with --exportRuntime"; const F_NOEXPORTRUNTIME = function() { throw Error(E_NOEXPORTRUNTIME); }; @@ -265,6 +266,7 @@ function postInstantiate(extendedExports, instance) { /** Gets a function from poiner which contain table's index. */ function __getFunction(ptr) { + if (!table) throw Error(E_NOEXPORT_TABLE); const index = new Uint32Array(memory.buffer)[ptr >>> 2]; return table.get(index); } From 7a8bc439d6a4fffead3024863aa4ffb28f7999df Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 21:31:51 +0300 Subject: [PATCH 07/14] refactor --- lib/loader/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 8a5103519d..059380c5ce 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -34,6 +34,10 @@ const ARRAYBUFFERVIEW_SIZE = 12; const ARRAY_LENGTH_OFFSET = 12; const ARRAY_SIZE = 16; +const E_NO_EXPORT_TABLE = "Operation requires compiling with --exportTable"; +const E_NO_EXPORT_RUNTIME = "Operation requires compiling with --exportRuntime"; +const F_NO_EXPORT_RUNTIME = function() { throw Error(E_NO_EXPORT_RUNTIME); }; + const BIGINT = typeof BigUint64Array !== "undefined"; const THIS = Symbol(); @@ -83,23 +87,19 @@ function preInstantiate(imports) { return extendedExports; } -const E_NOEXPORT_TABLE = "Operation requires compiling with --exportTable"; -const E_NOEXPORTRUNTIME = "Operation requires compiling with --exportRuntime"; -const F_NOEXPORTRUNTIME = function() { throw Error(E_NOEXPORTRUNTIME); }; - /** Prepares the final module once instantiation is complete. */ function postInstantiate(extendedExports, instance) { const exports = instance.exports; const memory = exports.memory; const table = exports.table; - const __new = exports.__new || F_NOEXPORTRUNTIME; - const __pin = exports.__pin || F_NOEXPORTRUNTIME; - const __unpin = exports.__unpin || F_NOEXPORTRUNTIME; - const __collect = exports.__collect || F_NOEXPORTRUNTIME; + const __new = exports.__new || F_NO_EXPORT_RUNTIME; + const __pin = exports.__pin || F_NO_EXPORT_RUNTIME; + const __unpin = exports.__unpin || F_NO_EXPORT_RUNTIME; + const __collect = exports.__collect || F_NO_EXPORT_RUNTIME; const __rtti_base = exports.__rtti_base; const getRttiCount = __rtti_base ? function (arr) { return arr[__rtti_base >>> 2]; } - : F_NOEXPORTRUNTIME; + : F_NO_EXPORT_RUNTIME; extendedExports.__new = __new; extendedExports.__pin = __pin; @@ -266,7 +266,7 @@ function postInstantiate(extendedExports, instance) { /** Gets a function from poiner which contain table's index. */ function __getFunction(ptr) { - if (!table) throw Error(E_NOEXPORT_TABLE); + if (!table) throw Error(E_NO_EXPORT_TABLE); const index = new Uint32Array(memory.buffer)[ptr >>> 2]; return table.get(index); } From 33bcebf8b5ab7d54f58a466b75dd36a0d111aace Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 23:48:25 +0300 Subject: [PATCH 08/14] refactor --- lib/loader/index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 059380c5ce..05bf5b98f3 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -97,7 +97,7 @@ function postInstantiate(extendedExports, instance) { const __unpin = exports.__unpin || F_NO_EXPORT_RUNTIME; const __collect = exports.__collect || F_NO_EXPORT_RUNTIME; const __rtti_base = exports.__rtti_base; - const getRttiCount = __rtti_base + const getRttCount = __rtti_base ? function (arr) { return arr[__rtti_base >>> 2]; } : F_NO_EXPORT_RUNTIME; @@ -106,27 +106,23 @@ function postInstantiate(extendedExports, instance) { extendedExports.__unpin = __unpin; extendedExports.__collect = __collect; - /** Gets the runtime type info for the given id. */ - function getInfo(id) { + /** Gets the runtime type info for the given id and specific field. */ + function getRtt(id, field = 0) { const U32 = new Uint32Array(memory.buffer); - const count = getRttiCount(U32); - if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); - return U32[(__rtti_base + 4 >>> 2) + id * 2]; + if ((id >>>= 0) >= getRttCount(U32)) throw Error(`invalid id: ${id}`); + return U32[(__rtti_base + 4 >>> 2) + (id << 1) + field]; } /** Gets and validate runtime type info for the given id for array like objects */ function getArrayInfo(id) { - const info = getInfo(id); + const info = getRtt(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); - const count = getRttiCount(U32); - if ((id >>>= 0) >= count) throw Error(`invalid id: ${id}`); - return U32[(__rtti_base + 4 >>> 2) + id * 2 + 1]; + return getRtt(id, 1); } /** Gets the runtime alignment of a collection's values. */ @@ -316,7 +312,7 @@ function postInstantiate(extendedExports, instance) { function __instanceof(ptr, baseId) { const U32 = new Uint32Array(memory.buffer); let id = U32[ptr + ID_OFFSET >>> 2]; - if (id <= getRttiCount(U32)) { + if (id <= getRttCount(U32)) { do { if (id == baseId) return true; id = getBase(id); From 1163c0dddbe7073e28848aa5948497b3a8b60f54 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 16 Aug 2021 23:55:46 +0300 Subject: [PATCH 09/14] simplify --- lib/loader/index.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 05bf5b98f3..2f84c0d466 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -120,11 +120,6 @@ function postInstantiate(extendedExports, instance) { return info; } - /** Gets the runtime base id for the given id. */ - function getBase(id) { - return getRtt(id, 1); - } - /** Gets the runtime alignment of a collection's values. */ function getValueAlign(info) { return 31 - Math.clz32((info >>> VAL_ALIGN_OFFSET) & 31); // -1 if none @@ -315,7 +310,7 @@ function postInstantiate(extendedExports, instance) { if (id <= getRttCount(U32)) { do { if (id == baseId) return true; - id = getBase(id); + id = getRtt(id, 1); // gets runtime base } while (id); } return false; From 5417434663e1dac32ee0cb37ade59eb364ab3b65 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Tue, 17 Aug 2021 00:29:07 +0300 Subject: [PATCH 10/14] revert some changes --- lib/loader/index.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 2f84c0d466..522b433fcf 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -97,7 +97,7 @@ function postInstantiate(extendedExports, instance) { const __unpin = exports.__unpin || F_NO_EXPORT_RUNTIME; const __collect = exports.__collect || F_NO_EXPORT_RUNTIME; const __rtti_base = exports.__rtti_base; - const getRttCount = __rtti_base + const getRttiCount = __rtti_base ? function (arr) { return arr[__rtti_base >>> 2]; } : F_NO_EXPORT_RUNTIME; @@ -106,16 +106,23 @@ function postInstantiate(extendedExports, instance) { extendedExports.__unpin = __unpin; extendedExports.__collect = __collect; - /** Gets the runtime type info for the given id and specific field. */ - function getRtt(id, field = 0) { + /** Gets the runtime type info for the given id. */ + function getRttInfo(id) { const U32 = new Uint32Array(memory.buffer); - if ((id >>>= 0) >= getRttCount(U32)) throw Error(`invalid id: ${id}`); - return U32[(__rtti_base + 4 >>> 2) + (id << 1) + field]; + if ((id >>>= 0) >= getRttiCount(U32)) throw Error(`invalid id: ${id}`); + return U32[(__rtti_base + 4 >>> 2) + (id << 1)]; + } + + /** Gets the runtime base id for the given id. */ + function getRttBase(id) { + const U32 = new Uint32Array(memory.buffer); + if ((id >>>= 0) >= getRttiCount(U32)) throw Error(`invalid id: ${id}`); + return U32[(__rtti_base + 4 >>> 2) + (id << 1) + 1]; } /** Gets and validate runtime type info for the given id for array like objects */ function getArrayInfo(id) { - const info = getRtt(id); + const info = getRttInfo(id); if (!(info & (ARRAYBUFFERVIEW | ARRAY | STATICARRAY))) throw Error(`not an array: ${id}, flags=${info}`); return info; } @@ -307,10 +314,10 @@ function postInstantiate(extendedExports, instance) { function __instanceof(ptr, baseId) { const U32 = new Uint32Array(memory.buffer); let id = U32[ptr + ID_OFFSET >>> 2]; - if (id <= getRttCount(U32)) { + if (id <= getRttiCount(U32)) { do { if (id == baseId) return true; - id = getRtt(id, 1); // gets runtime base + id = getRttBase(id); } while (id); } return false; From b510c955fa1ab24a8f9e67beeac9656318b13796 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 28 Aug 2021 19:41:23 +0300 Subject: [PATCH 11/14] refactorings --- lib/loader/index.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/loader/index.js b/lib/loader/index.js index 522b433fcf..c2c3815b99 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -36,7 +36,7 @@ const ARRAY_SIZE = 16; const E_NO_EXPORT_TABLE = "Operation requires compiling with --exportTable"; const E_NO_EXPORT_RUNTIME = "Operation requires compiling with --exportRuntime"; -const F_NO_EXPORT_RUNTIME = function() { throw Error(E_NO_EXPORT_RUNTIME); }; +const F_NO_EXPORT_RUNTIME = () => { throw Error(E_NO_EXPORT_RUNTIME); }; const BIGINT = typeof BigUint64Array !== "undefined"; const THIS = Symbol(); @@ -45,6 +45,11 @@ const STRING_SMALLSIZE = 192; // break-even point in V8 const STRING_CHUNKSIZE = 1024; // mitigate stack overflow const utf16 = new TextDecoder("utf-16le", { fatal: true }); // != wtf16 +/** polyfill for Object.hasOwn */ +Object.hasOwn = Object.hasOwn || function(prop) { + return Object.prototype.hasOwnProperty.call(this, prop); +}; + /** Gets a string from memory. */ function getStringImpl(buffer, ptr) { let len = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2] >>> 1; @@ -97,9 +102,7 @@ function postInstantiate(extendedExports, instance) { const __unpin = exports.__unpin || F_NO_EXPORT_RUNTIME; const __collect = exports.__collect || F_NO_EXPORT_RUNTIME; const __rtti_base = exports.__rtti_base; - const getRttiCount = __rtti_base - ? function (arr) { return arr[__rtti_base >>> 2]; } - : F_NO_EXPORT_RUNTIME; + const getRttiCount = __rtti_base ? arr => arr[__rtti_base >>> 2] : F_NO_EXPORT_RUNTIME; extendedExports.__new = __new; extendedExports.__pin = __pin; @@ -382,13 +385,13 @@ export function demangle(exports, extendedExports = {}) { ? length => { exports["__argumentsLength"].value = length; } : exports["__setArgumentsLength"] || exports["__setargc"] || (() => { /* nop */ }); for (let internalName in exports) { - if (!Object.prototype.hasOwnProperty.call(exports, internalName)) continue; + if (!Object.hasOwn(exports, internalName)) continue; const elem = exports[internalName]; let parts = internalName.split("."); let curr = extendedExports; while (parts.length > 1) { let part = parts.shift(); - if (!Object.prototype.hasOwnProperty.call(curr, part)) curr[part] = {}; + if (!Object.hasOwn(curr, part)) curr[part] = {}; curr = curr[part]; } let name = parts[0]; @@ -397,13 +400,13 @@ export function demangle(exports, extendedExports = {}) { const className = name.substring(0, hash); const classElem = curr[className]; if (typeof classElem === "undefined" || !classElem.prototype) { - const ctor = function(...args) { + const ctor = (...args) => { return ctor.wrap(ctor.prototype.constructor(0, ...args)); }; ctor.prototype = { valueOf() { return this[THIS]; } }; - ctor.wrap = function(thisValue) { + ctor.wrap = thisValue => { return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } }); }; if (classElem) Object.getOwnPropertyNames(classElem).forEach(name => @@ -414,7 +417,7 @@ export function demangle(exports, extendedExports = {}) { name = name.substring(hash + 1); curr = curr[className].prototype; if (/^(get|set):/.test(name)) { - if (!Object.prototype.hasOwnProperty.call(curr, name = name.substring(4))) { + if (!Object.hasOwn(curr, name = name.substring(4))) { let getter = exports[internalName.replace("set:", "get:")]; let setter = exports[internalName.replace("get:", "set:")]; Object.defineProperty(curr, name, { @@ -425,7 +428,7 @@ export function demangle(exports, extendedExports = {}) { } } else { if (name === 'constructor') { - (curr[name] = (...args) => { + (curr[name] = function(...args) { setArgumentsLength(args.length); return elem(...args); }).original = elem; @@ -438,7 +441,7 @@ export function demangle(exports, extendedExports = {}) { } } else { if (/^(get|set):/.test(name)) { - if (!Object.prototype.hasOwnProperty.call(curr, name = name.substring(4))) { + if (!Object.hasOwn(curr, name = name.substring(4))) { Object.defineProperty(curr, name, { get: exports[internalName.replace("set:", "get:")], set: exports[internalName.replace("get:", "set:")], From e3d761beca18adf372013573382ac2b032c608d1 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 28 Aug 2021 19:45:18 +0300 Subject: [PATCH 12/14] 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 c2c3815b99..ccfcfec504 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -46,8 +46,8 @@ const STRING_CHUNKSIZE = 1024; // mitigate stack overflow const utf16 = new TextDecoder("utf-16le", { fatal: true }); // != wtf16 /** polyfill for Object.hasOwn */ -Object.hasOwn = Object.hasOwn || function(prop) { - return Object.prototype.hasOwnProperty.call(this, prop); +Object.hasOwn = Object.hasOwn || function(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); }; /** Gets a string from memory. */ From b3fd5ab17f5c65fa498fa4a71c732d4919b5780e Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 28 Aug 2021 19:51:47 +0300 Subject: [PATCH 13/14] 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 ccfcfec504..398b63e57e 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -400,13 +400,13 @@ export function demangle(exports, extendedExports = {}) { const className = name.substring(0, hash); const classElem = curr[className]; if (typeof classElem === "undefined" || !classElem.prototype) { - const ctor = (...args) => { + const ctor = function(...args) { return ctor.wrap(ctor.prototype.constructor(0, ...args)); }; ctor.prototype = { valueOf() { return this[THIS]; } }; - ctor.wrap = thisValue => { + ctor.wrap = function(thisValue) { return Object.create(ctor.prototype, { [THIS]: { value: thisValue, writable: false } }); }; if (classElem) Object.getOwnPropertyNames(classElem).forEach(name => From 4eb54d11c5ae461d34c4d38ee732d76de0e44d6f Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sun, 29 Aug 2021 16:45:37 +0300 Subject: [PATCH 14/14] simplify demangle's loop --- 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 398b63e57e..f61735b0f9 100644 --- a/lib/loader/index.js +++ b/lib/loader/index.js @@ -384,8 +384,7 @@ export function demangle(exports, extendedExports = {}) { const setArgumentsLength = exports["__argumentsLength"] ? length => { exports["__argumentsLength"].value = length; } : exports["__setArgumentsLength"] || exports["__setargc"] || (() => { /* nop */ }); - for (let internalName in exports) { - if (!Object.hasOwn(exports, internalName)) continue; + for (let internalName of Object.keys(exports)) { const elem = exports[internalName]; let parts = internalName.split("."); let curr = extendedExports;