🎨 optimize current proxy setting logic (#1486)

This commit is contained in:
Kuitos 2021-07-27 21:14:42 +08:00 committed by GitHub
parent 64337f01e8
commit 761287ac3b
3 changed files with 25 additions and 7 deletions

View File

@ -123,7 +123,11 @@ test('hasOwnProperty should works well', () => {
test('descriptor of non-configurable and non-enumerable property existed in raw window should be the same after modified in sandbox', () => {
Object.defineProperty(window, 'nonConfigurableProp', { configurable: false, writable: true });
// eslint-disable-next-line getter-return
Object.defineProperty(window, 'nonConfigurablePropWithAccessor', { configurable: false, get() {}, set() {} });
Object.defineProperty(window, 'nonConfigurablePropWithAccessor', {
configurable: false,
get() {},
set() {},
});
Object.defineProperty(window, 'enumerableProp', { enumerable: true, writable: true });
Object.defineProperty(window, 'nonEnumerableProp', { enumerable: false, writable: true });
@ -297,6 +301,7 @@ test('the prototype should be kept while we create a function with prototype on
const proxy = new ProxySandbox('new-function').proxy as any;
function test() {}
proxy.fn = test;
expect(proxy.fn === test).toBeFalsy();
expect(proxy.fn.prototype).toBe(test.prototype);
@ -348,6 +353,18 @@ it('should return true while [[GetPrototypeOf]] invoked by proxy object', () =>
expect(Reflect.getPrototypeOf(proxy)).toBe(Object.getPrototypeOf(window));
});
it('should get current running sandbox proxy correctly', async () => {
const { proxy } = new ProxySandbox('running');
await Promise.resolve().then(() => {
expect(getCurrentRunningSandboxProxy()).toBeNull();
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const unused = proxy.accessing;
expect(getCurrentRunningSandboxProxy()).toBe(proxy);
});
});
it('native window function calling should always be bound with window', () => {
window.nativeWindowFunction = function nativeWindowFunction(this: any) {
if (this !== undefined && this !== window) {

View File

@ -5,7 +5,7 @@
import { isBoundedFunction, isCallable, isConstructable } from '../utils';
let currentRunningSandboxProxy: WindowProxy | null;
let currentRunningSandboxProxy: WindowProxy | null = null;
export function getCurrentRunningSandboxProxy() {
return currentRunningSandboxProxy;
}

View File

@ -215,6 +215,12 @@ export default class ProxySandbox implements SandBox {
},
get(target: FakeWindow, p: PropertyKey): any {
setCurrentRunningSandboxProxy(proxy);
// FIXME if you have any other good ideas
// remove the mark in next tick, thus we can identify whether it in micro app or not
// this approach is just a workaround, it could not cover all complex cases, such as the micro app runs in the same task context with master in some case
nextTick(() => setCurrentRunningSandboxProxy(null));
if (p === Symbol.unscopables) return unscopables;
// avoid who using window.window or window.self to escape the sandbox environment to touch the really window
@ -247,11 +253,6 @@ export default class ProxySandbox implements SandBox {
// mark the symbol to document while accessing as document.createElement could know is invoked by which sandbox for dynamic append patcher
if (p === 'document' || p === 'eval') {
setCurrentRunningSandboxProxy(proxy);
// FIXME if you have any other good ideas
// remove the mark in next tick, thus we can identify whether it in micro app or not
// this approach is just a workaround, it could not cover all complex cases, such as the micro app runs in the same task context with master in some case
nextTick(() => setCurrentRunningSandboxProxy(null));
switch (p) {
case 'document':
return document;