From 7328396c30e53e8faad7c00b8431aea7c33db4db Mon Sep 17 00:00:00 2001 From: Kuitos Date: Wed, 23 Aug 2023 16:41:10 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9Bcompatible=20with=20vConsole=20who?= =?UTF-8?q?=20will=20wrap=20global=20variables=20with=20proxy=20(#2630)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/common.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/sandbox/common.ts b/src/sandbox/common.ts index ac450c9..83530fc 100644 --- a/src/sandbox/common.ts +++ b/src/sandbox/common.ts @@ -40,12 +40,13 @@ export function getTargetValue(target: any, value: any): any { const boundValue = Function.prototype.bind.call(value, target); - // some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function. - // use for..in rather than Object.keys.forEach for performance reason - // eslint-disable-next-line guard-for-in,no-restricted-syntax - for (const key in value) { - boundValue[key] = value[key]; - } + // some callable function has custom fields, we need to copy the own props to boundValue. such as moment function. + Object.getOwnPropertyNames(value).forEach((key) => { + // boundValue might be a proxy, we need to check the key whether exist in it + if (!boundValue.hasOwnProperty(key)) { + Object.defineProperty(boundValue, key, Object.getOwnPropertyDescriptor(value, key)!); + } + }); // copy prototype if bound function not have but target one have // as prototype is non-enumerable mostly, we need to copy it from target function manually