🐛 function should return the same reference while it always bindable (#1612)
This commit is contained in:
parent
761287ac3b
commit
1301e5b0af
|
|
@ -39,6 +39,8 @@ describe('getTargetValue', () => {
|
||||||
expect(result2).toStrictEqual({ field: '456' });
|
expect(result2).toStrictEqual({ field: '456' });
|
||||||
// window.field not be affected
|
// window.field not be affected
|
||||||
expect(window.field).toEqual('123');
|
expect(window.field).toEqual('123');
|
||||||
|
// reference should be stable after first running
|
||||||
|
expect(constructableFunction).toBe(getTargetValue(window, prototypeAddedAfterFirstInvocation));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work well while value have a readonly prototype on its prototype chain', () => {
|
it('should work well while value have a readonly prototype on its prototype chain', () => {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) {
|
||||||
currentRunningSandboxProxy = proxy;
|
currentRunningSandboxProxy = proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const functionBoundedValueMap = new WeakMap<CallableFunction, CallableFunction>();
|
||||||
export function getTargetValue(target: any, value: any): any {
|
export function getTargetValue(target: any, value: any): any {
|
||||||
/*
|
/*
|
||||||
仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类,不然微应用中调用时会抛出 Illegal invocation 异常
|
仅绑定 isCallable && !isBoundedFunction && !isConstructable 的函数对象,如 window.console、window.atob 这类,不然微应用中调用时会抛出 Illegal invocation 异常
|
||||||
|
|
@ -21,6 +22,11 @@ export function getTargetValue(target: any, value: any): any {
|
||||||
@warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常)
|
@warning 这里不要随意替换成别的判断方式,因为可能触发一些 edge case(比如在 lodash.isFunction 在 iframe 上下文中可能由于调用了 top window 对象触发的安全异常)
|
||||||
*/
|
*/
|
||||||
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
|
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
|
||||||
|
const cachedBoundFunction = functionBoundedValueMap.get(value);
|
||||||
|
if (cachedBoundFunction) {
|
||||||
|
return cachedBoundFunction;
|
||||||
|
}
|
||||||
|
|
||||||
const boundValue = Function.prototype.bind.call(value, target);
|
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.
|
// some callable function has custom fields, we need to copy the enumerable props to boundValue. such as moment function.
|
||||||
|
|
@ -40,6 +46,7 @@ export function getTargetValue(target: any, value: any): any {
|
||||||
Object.defineProperty(boundValue, 'prototype', { value: value.prototype, enumerable: false, writable: true });
|
Object.defineProperty(boundValue, 'prototype', { value: value.prototype, enumerable: false, writable: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
functionBoundedValueMap.set(value, boundValue);
|
||||||
return boundValue;
|
return boundValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user