This commit is contained in:
Kuitos 2022-10-05 15:50:57 +08:00 committed by GitHub
parent edd19b11b9
commit e5a175caa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 28 deletions

View File

@ -1,11 +1,20 @@
import { writeFileSync } from 'fs';
import { join } from 'path';
import { version } from './package.json';
import globals from 'globals';
// generate version.ts
const versionFilePath = join(__dirname, './src/version.ts');
writeFileSync(versionFilePath, `export const version = '${version}';`);
// generate globals.ts
const globalsFilePath = join(__dirname, './src/sandbox/globals.ts');
writeFileSync(
globalsFilePath,
`// generated from https://github.com/sindresorhus/globals/blob/main/globals.json builtin part
export const globals = ${JSON.stringify(Object.keys(globals.builtin), null, 2)};`,
);
export default {
target: 'browser',
esm: 'babel',

View File

@ -115,6 +115,7 @@
"cross-env": "^7.0.2",
"dumi": "^1.1.0-beta.24",
"father-build": "^1.7.0",
"globals": "^13.17.0",
"husky": "^2.3.0",
"jest": "^25.2.2",
"levenary": "^1.1.1",

View File

@ -4,6 +4,8 @@
*/
import { isBoundedFunction, isCallable, isConstructable } from '../utils';
import { globals } from './globals';
import { without } from 'lodash';
type AppInstance = { name: string; window: WindowProxy };
let currentRunningApp: AppInstance | null = null;
@ -20,6 +22,10 @@ export function setCurrentRunningApp(appInstance: { name: string; window: Window
currentRunningApp = appInstance;
}
const scopedGlobals = ['window', 'self', 'globalThis', 'top', 'parent', 'hasOwnProperty', 'document', 'eval'];
export const unscopedGlobals = [...without(globals, ...scopedGlobals), 'requestAnimationFrame'];
export const lexicalGlobals = [...unscopedGlobals, ...scopedGlobals];
const functionBoundedValueMap = new WeakMap<CallableFunction, CallableFunction>();
export function getTargetValue(target: any, value: any): any {
@ -27,7 +33,6 @@ export function getTargetValue(target: any, value: any): any {
isCallable && !isBoundedFunction && !isConstructable window.consolewindow.atob Illegal invocation
prototype
@warning edge case lodash.isFunction iframe top window
@warning configurable及writable都为false的readonly属性proxy必须返回原值
*/
if (isCallable(value) && !isBoundedFunction(value) && !isConstructable(value)) {
const cachedBoundFunction = functionBoundedValueMap.get(value);
@ -79,30 +84,3 @@ export function getTargetValue(target: any, value: any): any {
return value;
}
export const unscopedGlobals = [
'undefined',
'Array',
'Object',
'String',
'Boolean',
'Math',
'Number',
'Symbol',
'parseFloat',
'Float32Array',
'isNaN',
'Infinity',
'Reflect',
'Float64Array',
'Function',
'Map',
'NaN',
'Promise',
'Proxy',
'Set',
'parseInt',
'requestAnimationFrame',
];
export const lexicalGlobals = [...unscopedGlobals, 'globalThis', 'window', 'self'];

69
src/sandbox/globals.ts Normal file
View File

@ -0,0 +1,69 @@
// generated from https://github.com/sindresorhus/globals/blob/main/globals.json builtin part
export const globals = [
'AggregateError',
'Array',
'ArrayBuffer',
'Atomics',
'BigInt',
'BigInt64Array',
'BigUint64Array',
'Boolean',
'constructor',
'DataView',
'Date',
'decodeURI',
'decodeURIComponent',
'encodeURI',
'encodeURIComponent',
'Error',
'escape',
'eval',
'EvalError',
'FinalizationRegistry',
'Float32Array',
'Float64Array',
'Function',
'globalThis',
'hasOwnProperty',
'Infinity',
'Int16Array',
'Int32Array',
'Int8Array',
'isFinite',
'isNaN',
'isPrototypeOf',
'JSON',
'Map',
'Math',
'NaN',
'Number',
'Object',
'parseFloat',
'parseInt',
'Promise',
'propertyIsEnumerable',
'Proxy',
'RangeError',
'ReferenceError',
'Reflect',
'RegExp',
'Set',
'SharedArrayBuffer',
'String',
'Symbol',
'SyntaxError',
'toLocaleString',
'toString',
'TypeError',
'Uint16Array',
'Uint32Array',
'Uint8Array',
'Uint8ClampedArray',
'undefined',
'unescape',
'URIError',
'valueOf',
'WeakMap',
'WeakRef',
'WeakSet',
];