From 74db4e3ce843a5223d07a09a678d9685cb9121b5 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Mon, 9 Jun 2025 22:46:17 +0700 Subject: [PATCH] fix(lib/patcher): clean up unused references for instead hooks --- lib/patcher/src/_internal.ts | 9 ++++++--- lib/patcher/src/hooks/instead.ts | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/patcher/src/_internal.ts b/lib/patcher/src/_internal.ts index d86165e..5692448 100644 --- a/lib/patcher/src/_internal.ts +++ b/lib/patcher/src/_internal.ts @@ -39,7 +39,7 @@ export interface HookNode { export interface InsteadHookNode extends HookNode>, FunctionProxyState { - readonly hook: InsteadHook + hook: InsteadHook | undefined prev: InsteadHookNode | undefined next: InsteadHookNode | undefined } @@ -180,6 +180,9 @@ export function createPatchedFunctionProxy< } export function unproxy(state: PatchedFunctionProxyState) { - const { parent, key } = state - if (parent[key] === state.proxy) parent[key] = state.target + const { parent, key, proxy } = state + if (parent[key] === proxy) { + parent[key] = state.target + patchedFunctionProxyStates.delete(proxy) + } } diff --git a/lib/patcher/src/hooks/instead.ts b/lib/patcher/src/hooks/instead.ts index 71b4ee5..417be7f 100644 --- a/lib/patcher/src/hooks/instead.ts +++ b/lib/patcher/src/hooks/instead.ts @@ -24,8 +24,8 @@ const insteadHookProxyHandler = { receiver: ThisParameterType, args: Parameters, ) { - const { next } = hookNode - return Reflect.apply(hookNode.hook, receiver, [ + const { next, hook } = hookNode + return Reflect.apply(hook!, receiver, [ args, next === undefined ? hookNode.target : next.proxy, ]) @@ -35,9 +35,9 @@ const insteadHookProxyHandler = { args: ConstructorParameters, ctor: AbstractNewable, ) { - const { next } = hookNode + const { next, hook } = hookNode return Reflect.construct( - hookNode.hook, + hook!, [args, next === undefined ? hookNode.target : next.proxy], ctor, ) @@ -51,6 +51,7 @@ function unpatchInstead( if (hookNode.unpatched) return hookNode.unpatched = true + hookNode.hook = undefined const { prev, next } = hookNode if (prev === undefined) {