Skip to content

Commit

Permalink
exec context fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nyariv committed Aug 28, 2021
1 parent de9ce4c commit 3996d4b
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build/Sandbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IExecContext extends IExecutionTree {
export declare class SandboxGlobal {
constructor(globals: IGlobals);
}
export declare function createContext(executionTree: IExecutionTree): IExecContext;
export declare function createContext(context: IContext, executionTree: IExecutionTree): IExecContext;
export default class Sandbox {
context: IContext;
constructor(options?: IOptions);
Expand Down
23 changes: 12 additions & 11 deletions build/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ export class SandboxGlobal {
}
}
}
export function createContext(executionTree) {
export function createContext(context, executionTree) {
const evals = new Map();
const context = {
ctx: this.context,
const execContext = {
ctx: context,
constants: executionTree.constants,
tree: executionTree.tree,
getSubscriptions: new Set(),
setSubscriptions: new WeakMap(),
changeSubscriptions: new WeakMap(),
evals
};
const func = sandboxFunction(context);
const func = sandboxFunction(execContext);
evals.set(Function, func);
evals.set(eval, sandboxedEval(func));
evals.set(setTimeout, sandboxedSetTimeout(func));
evals.set(setInterval, sandboxedSetInterval(func));
return context;
return execContext;
}
const contextStore = new WeakMap();
export default class Sandbox {
Expand Down Expand Up @@ -186,10 +186,11 @@ export default class Sandbox {
for (let i of Object.getOwnPropertyNames(globalThis)) {
globals[i] = globalThis[i];
}
return new Sandbox({
const sandbox = new Sandbox({
globals,
audit: true,
}).executeTree(createContext(parse(code)), scopes);
});
return sandbox.executeTree(createContext(sandbox.context, parse(code)), scopes);
}
static parse(code) {
return parse(code);
Expand All @@ -205,7 +206,7 @@ export default class Sandbox {
}, context, context.tree, scopes);
}
compile(code, optimize = false) {
const context = createContext(parse(code, optimize));
const context = createContext(this.context, parse(code, optimize));
const exec = (...scopes) => {
return this.executeTree(context, scopes).result;
};
Expand All @@ -214,7 +215,7 @@ export default class Sandbox {
}
;
compileAsync(code, optimize = false) {
const context = createContext(parse(code, optimize));
const context = createContext(this.context, parse(code, optimize));
const exec = async (...scopes) => {
return (await this.executeTreeAsync(context, scopes)).result;
};
Expand All @@ -223,15 +224,15 @@ export default class Sandbox {
}
;
compileExpression(code, optimize = false) {
const context = createContext(parse(code, optimize, true));
const context = createContext(this.context, parse(code, optimize, true));
const exec = (...scopes) => {
return this.executeTree(context, scopes).result;
};
contextStore.set(exec, context);
return exec;
}
compileExpressionAsync(code, optimize = false) {
const context = createContext(parse(code, optimize, true));
const context = createContext(this.context, parse(code, optimize, true));
const exec = async (...scopes) => {
return (await this.executeTreeAsync(context, scopes)).result;
};
Expand Down
2 changes: 1 addition & 1 deletion dist/Sandbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IExecContext extends IExecutionTree {
export declare class SandboxGlobal {
constructor(globals: IGlobals);
}
export declare function createContext(executionTree: IExecutionTree): IExecContext;
export declare function createContext(context: IContext, executionTree: IExecutionTree): IExecContext;
export default class Sandbox {
context: IContext;
constructor(options?: IOptions);
Expand Down
2 changes: 1 addition & 1 deletion dist/Sandbox.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/Sandbox.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/node/Sandbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface IExecContext extends IExecutionTree {
export declare class SandboxGlobal {
constructor(globals: IGlobals);
}
export declare function createContext(executionTree: IExecutionTree): IExecContext;
export declare function createContext(context: IContext, executionTree: IExecutionTree): IExecContext;
export default class Sandbox {
context: IContext;
constructor(options?: IOptions);
Expand Down
23 changes: 12 additions & 11 deletions dist/node/Sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2887,23 +2887,23 @@ class SandboxGlobal {
}
}
}
function createContext(executionTree) {
function createContext(context, executionTree) {
const evals = new Map();
const context = {
ctx: this.context,
const execContext = {
ctx: context,
constants: executionTree.constants,
tree: executionTree.tree,
getSubscriptions: new Set(),
setSubscriptions: new WeakMap(),
changeSubscriptions: new WeakMap(),
evals
};
const func = sandboxFunction(context);
const func = sandboxFunction(execContext);
evals.set(Function, func);
evals.set(eval, sandboxedEval(func));
evals.set(setTimeout, sandboxedSetTimeout(func));
evals.set(setInterval, sandboxedSetInterval(func));
return context;
return execContext;
}
const contextStore = new WeakMap();
class Sandbox {
Expand Down Expand Up @@ -3063,10 +3063,11 @@ class Sandbox {
for (let i of Object.getOwnPropertyNames(globalThis)) {
globals[i] = globalThis[i];
}
return new Sandbox({
const sandbox = new Sandbox({
globals,
audit: true,
}).executeTree(createContext(parse(code)), scopes);
});
return sandbox.executeTree(createContext(sandbox.context, parse(code)), scopes);
}
static parse(code) {
return parse(code);
Expand All @@ -3082,7 +3083,7 @@ class Sandbox {
}, context, context.tree, scopes);
}
compile(code, optimize = false) {
const context = createContext(parse(code, optimize));
const context = createContext(this.context, parse(code, optimize));
const exec = (...scopes) => {
return this.executeTree(context, scopes).result;
};
Expand All @@ -3091,7 +3092,7 @@ class Sandbox {
}
;
compileAsync(code, optimize = false) {
const context = createContext(parse(code, optimize));
const context = createContext(this.context, parse(code, optimize));
const exec = async (...scopes) => {
return (await this.executeTreeAsync(context, scopes)).result;
};
Expand All @@ -3100,15 +3101,15 @@ class Sandbox {
}
;
compileExpression(code, optimize = false) {
const context = createContext(parse(code, optimize, true));
const context = createContext(this.context, parse(code, optimize, true));
const exec = (...scopes) => {
return this.executeTree(context, scopes).result;
};
contextStore.set(exec, context);
return exec;
}
compileExpressionAsync(code, optimize = false) {
const context = createContext(parse(code, optimize, true));
const context = createContext(this.context, parse(code, optimize, true));
const exec = async (...scopes) => {
return (await this.executeTreeAsync(context, scopes)).result;
};
Expand Down
23 changes: 12 additions & 11 deletions src/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ export class SandboxGlobal {
}
}

export function createContext(executionTree: IExecutionTree) {
export function createContext(context: IContext, executionTree: IExecutionTree) {
const evals = new Map();
const context: IExecContext = {
ctx: this.context,
const execContext: IExecContext = {
ctx: context,
constants: executionTree.constants,
tree: executionTree.tree,
getSubscriptions: new Set<(obj: object, name: string) => void>(),
setSubscriptions: new WeakMap<object, Map<string, Set<() => void>>>(),
changeSubscriptions: new WeakMap(),
evals
}
const func = sandboxFunction(context);
const func = sandboxFunction(execContext);
evals.set(Function, func);
evals.set(eval, sandboxedEval(func));
evals.set(setTimeout, sandboxedSetTimeout(func));
evals.set(setInterval, sandboxedSetInterval(func));
return context;
return execContext;
}

const contextStore: WeakMap<(...scopes: (IScope)[]) => unknown|Promise<unknown>, IExecContext> = new WeakMap()
Expand Down Expand Up @@ -266,10 +266,11 @@ export default class Sandbox {
for (let i of Object.getOwnPropertyNames(globalThis)) {
globals[i] = globalThis[i];
}
return new Sandbox({
const sandbox = new Sandbox({
globals,
audit: true,
}).executeTree(createContext(parse(code)), scopes);
});
return sandbox.executeTree(createContext(sandbox.context, parse(code)), scopes);
}

static parse(code: string) {
Expand All @@ -289,7 +290,7 @@ export default class Sandbox {
}

compile<T>(code: string, optimize = false): (...scopes: (IScope)[]) => T {
const context = createContext(parse(code, optimize));
const context = createContext(this.context, parse(code, optimize));
const exec = (...scopes: (IScope)[]) => {
return this.executeTree(context, scopes).result;
};
Expand All @@ -298,7 +299,7 @@ export default class Sandbox {
};

compileAsync<T>(code: string, optimize = false): (...scopes: (IScope)[]) => Promise<T> {
const context = createContext(parse(code, optimize));
const context = createContext(this.context, parse(code, optimize));
const exec = async (...scopes: (IScope)[]) => {
return (await this.executeTreeAsync(context, scopes)).result;
};
Expand All @@ -307,7 +308,7 @@ export default class Sandbox {
};

compileExpression<T>(code: string, optimize = false): (...scopes: (IScope)[]) => T {
const context = createContext(parse(code, optimize, true));
const context = createContext(this.context, parse(code, optimize, true));
const exec = (...scopes: (IScope)[]) => {
return this.executeTree(context, scopes).result;
};
Expand All @@ -316,7 +317,7 @@ export default class Sandbox {
}

compileExpressionAsync<T>(code: string, optimize = false): (...scopes: (IScope)[]) => Promise<T> {
const context = createContext(parse(code, optimize, true));
const context = createContext(this.context, parse(code, optimize, true));
const exec = async (...scopes: (IScope)[]) => {
return (await this.executeTreeAsync(context, scopes)).result;
};
Expand Down

0 comments on commit 3996d4b

Please sign in to comment.