diff --git a/src/sandbox/common.ts b/src/sandbox/common.ts index 65475d5..0e21915 100644 --- a/src/sandbox/common.ts +++ b/src/sandbox/common.ts @@ -16,17 +16,18 @@ export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) { const functionBoundedValueMap = new WeakMap(); export function getTargetValue(target: any, value: any): any { + const cachedBoundFunction = functionBoundedValueMap.get(value); + if (cachedBoundFunction) { + return cachedBoundFunction; + } + /* 仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类。目前没有完美的检测方式,这里通过 prototype 中是否还有可枚举的拓展方法的方式来判断 @warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常) */ if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) { - const cachedBoundValue = functionBoundedValueMap.get(value); - if (cachedBoundValue) { - return cachedBoundValue; - } - 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