From dacd2d8f5e3107df2175fc46b9fe98f2e2009f90 Mon Sep 17 00:00:00 2001 From: toonlink Date: Mon, 19 Jun 2023 17:10:56 -0400 Subject: [PATCH] Change instead patches to use .reduce to fix calling the original function in Hermes --- package.json | 4 ++-- src/hook.ts | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index c16008d..efb47f4 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "spitroast", - "version": "1.4.2", + "version": "1.4.3", "description": "A simple JavaScript function patcher.", "main": "dist/cjs.js", "module": "dist/esm/index.js", "types": "dist/types/index.d.ts", "scripts": { - "test": "mocha", + "test": "npm run prepublish && mocha", "prepublish": "ttsc && esbuild --bundle src/index.ts --outfile=dist/cjs.js --format=cjs" }, "repository": { diff --git a/src/hook.ts b/src/hook.ts index 1242053..38cc599 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -26,18 +26,16 @@ export default function ( } // Instead patches - let insteadPatchedFunc = (...args: unknown[]) => - isConstruct - ? Reflect.construct(patch.o, args, ctxt) - : patch.o.apply(ctxt, args); - - for (const callback of patch.i.values()) { - const oldPatchFunc = insteadPatchedFunc; - - insteadPatchedFunc = (...args) => callback.call(ctxt, args, oldPatchFunc); - } - - let workingRetVal = insteadPatchedFunc(...funcArgs); + let workingRetVal = [...patch.i.values()].reduce( + (prev, current) => + (...args: unknown[]) => + current.call(ctxt, args, prev), + // This calls the original function + (...args: unknown[]) => + isConstruct + ? Reflect.construct(patch.o, args, ctxt) + : patch.o.apply(ctxt, args) + )(...funcArgs); // After patches for (const hook of patch.a.values())