refactor: hookNative

This commit is contained in:
Xmader 2020-11-07 21:03:26 -05:00
parent f1a6fd81eb
commit 13afe431c8
No known key found for this signature in database
GPG key ID: A20B97FB9EB730E4

View file

@ -5,20 +5,20 @@
* make hooked methods "native" * make hooked methods "native"
*/ */
export const makeNative = (() => { export const makeNative = (() => {
const l = new Set<Function>() const l = new Map<Function, Function>()
hookNative(Function.prototype, 'toString', (_toString) => { hookNative(Function.prototype, 'toString', (_toString) => {
return function () { return function () {
if (l.has(this)) { if (l.has(this)) {
// "function () {\n [native code]\n}" const _fn = l.get(this) || parseInt // "function () {\n [native code]\n}"
return _toString.call(parseInt) as string return _toString.call(_fn) as string
} }
return _toString.call(this) as string return _toString.call(this) as string
} }
}, true) }, true)
return (fn: Function) => { return (fn: Function, original: Function) => {
l.add(fn) l.set(fn, original)
} }
})() })()
@ -40,10 +40,10 @@ export function hookNative<T extends object, M extends (keyof T)> (
target[method] = hookedFn target[method] = hookedFn
if (!async) { if (!async) {
makeNative(hookedFn as any) makeNative(hookedFn as any, _fn as any)
} else { } else {
setTimeout(() => { setTimeout(() => {
makeNative(hookedFn as any) makeNative(hookedFn as any, _fn as any)
}) })
} }
} }