Skip to content

Commit

Permalink
Change instead patches to use .reduce to fix calling the original fun…
Browse files Browse the repository at this point in the history
…ction in Hermes
  • Loading branch information
twnlink committed Jun 19, 2023
1 parent 642081f commit dacd2d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
22 changes: 10 additions & 12 deletions src/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit dacd2d8

Please sign in to comment.