Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions packages/sandbox/src/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

export const globalsInES2015 = window.Proxy
? [
'Array',
'ArrayBuffer',
'Boolean',
'constructor',
'DataView',
'Date',
'decodeURI',
'decodeURIComponent',
'encodeURI',
'encodeURIComponent',
'Error',
'escape',
'eval',
'EvalError',
'Float32Array',
'Float64Array',
'Function',
'hasOwnProperty',
'Infinity',
'Int16Array',
'Int32Array',
'Int8Array',
'isFinite',
'isNaN',
'isPrototypeOf',
'JSON',
'Map',
'Math',
'NaN',
'Number',
'Object',
'parseFloat',
'parseInt',
'Promise',
'propertyIsEnumerable',
'Proxy',
'RangeError',
'ReferenceError',
'Reflect',
'RegExp',
'Set',
'String',
'Symbol',
'SyntaxError',
'toLocaleString',
'toString',
'TypeError',
'Uint16Array',
'Uint32Array',
'Uint8Array',
'Uint8ClampedArray',
'undefined',
'unescape',
'URIError',
'valueOf',
'WeakMap',
'WeakSet',
].filter((p) => /* just keep the available properties in current window context */ p in window)
: [];
const overwrittenGlobals = ['window', 'self', 'hasOwnProperty']
export const cachedGlobals = Array.from(
new Set(
globalsInES2015.concat(overwrittenGlobals).concat('requestAnimationFrame')
),
);
6 changes: 5 additions & 1 deletion packages/sandbox/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cachedGlobals } from './constant';
export interface SandboxProps {
multiMode?: boolean;
}
Expand Down Expand Up @@ -99,7 +100,7 @@ export default class Sandbox {
if (!originalWindow.hasOwnProperty(p)) {
// record value added in sandbox
propertyAdded[p] = value;
// eslint-disable-next-line no-prototype-builtins
// eslint-disable-next-line no-prototype-builtins
} else if (!originalValues.hasOwnProperty(p)) {
// if it is already been setted in original window, record it's original value
originalValues[p] = originalWindow[p];
Expand Down Expand Up @@ -191,6 +192,9 @@ export default class Sandbox {
this.createProxySandbox();
}
try {
// Concatenate scopedGlobalVariables into variable declarations to cache global variables, avoiding proxy traversal on every use.
const scopedGlobalVariableDefinition = cachedGlobals.length ? `const {${cachedGlobals.join(',')}}=sandbox;` : '';
script =scopedGlobalVariableDefinition +script
const execScript = `with (sandbox) {;${script}\n}`;
// eslint-disable-next-line no-new-func
const code = new Function('sandbox', execScript).bind(this.sandbox);
Expand Down