From 31de427fe7f306831fd4ae51a7c1722a5340ecc0 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Tue, 6 Aug 2019 19:03:06 +0800 Subject: [PATCH] :zap: cache the bounded function in sandbox --- src/sandbox.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/sandbox.ts b/src/sandbox.ts index 58b30b56c..40dc3dfd5 100644 --- a/src/sandbox.ts +++ b/src/sandbox.ts @@ -65,6 +65,7 @@ export function genSandbox(appName: string) { let renderSandboxSnapshot: Map | null = null; let inAppSandbox = true; + const boundValueSymbol = Symbol('bound value'); const sandbox = new Proxy(window, { set(target: Window, p: PropertyKey, value: any): boolean { @@ -101,9 +102,14 @@ export function genSandbox(appName: string) { @warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常) */ if (typeof value === 'function' && !isConstructable(value)) { + if (value[boundValueSymbol]) { + return value[boundValueSymbol]; + } + const boundValue = value.bind(target); // some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function. Object.keys(value).forEach(key => boundValue[key] = value[key]); + Object.defineProperty(value, boundValueSymbol, { enumerable: false, value: boundValue }); return boundValue; }