@nyariv/sandboxjs 0.8.23 → 0.8.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +6 -0
- package/.eslintrc.js +22 -0
- package/.prettierrc +4 -0
- package/.vscode/settings.json +4 -0
- package/README.md +1 -1
- package/build/Sandbox.d.ts +11 -79
- package/build/Sandbox.js +21 -216
- package/build/SandboxExec.d.ts +25 -0
- package/build/SandboxExec.js +169 -0
- package/build/eval.d.ts +18 -0
- package/build/eval.js +43 -0
- package/build/executor.d.ts +29 -90
- package/build/executor.js +249 -328
- package/build/parser.d.ts +8 -239
- package/build/parser.js +345 -444
- package/build/unraw.js +13 -16
- package/build/utils.d.ts +242 -0
- package/build/utils.js +276 -0
- package/dist/Sandbox.d.ts +11 -79
- package/dist/Sandbox.js +106 -1
- package/dist/Sandbox.js.map +1 -1
- package/dist/Sandbox.min.js +2 -0
- package/dist/Sandbox.min.js.map +1 -0
- package/dist/SandboxExec.d.ts +25 -0
- package/dist/SandboxExec.js +173 -0
- package/dist/SandboxExec.js.map +1 -0
- package/dist/SandboxExec.min.js +2 -0
- package/dist/SandboxExec.min.js.map +1 -0
- package/dist/eval.d.ts +18 -0
- package/dist/executor.d.ts +29 -90
- package/dist/executor.js +1270 -0
- package/dist/executor.js.map +1 -0
- package/dist/node/Sandbox.d.ts +11 -79
- package/dist/node/Sandbox.js +36 -3150
- package/dist/node/SandboxExec.d.ts +25 -0
- package/dist/node/SandboxExec.js +176 -0
- package/dist/node/eval.d.ts +18 -0
- package/dist/node/executor.d.ts +29 -90
- package/dist/node/executor.js +1289 -0
- package/dist/node/parser.d.ts +8 -239
- package/dist/node/parser.js +1527 -0
- package/dist/node/utils.d.ts +242 -0
- package/dist/node/utils.js +290 -0
- package/dist/parser.d.ts +8 -239
- package/dist/parser.js +1513 -0
- package/dist/parser.js.map +1 -0
- package/dist/utils.d.ts +242 -0
- package/dist/utils.js +279 -0
- package/dist/utils.js.map +1 -0
- package/jest.config.js +200 -0
- package/package.json +16 -5
- package/tsconfig.jest.json +16 -0
- package/.github/workflows/npm-publish.yml +0 -34
- package/rollup.config.mjs +0 -33
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { IEvalContext } from './eval';
|
|
2
|
+
import { Change, Unknown } from './executor';
|
|
3
|
+
import { IConstants, IExecutionTree, Lisp, LispItem } from './parser';
|
|
4
|
+
import SandboxExec from './SandboxExec';
|
|
5
|
+
export type replacementCallback = (obj: any, isStaticAccess: boolean) => any;
|
|
6
|
+
export interface IOptionParams {
|
|
7
|
+
audit?: boolean;
|
|
8
|
+
forbidFunctionCalls?: boolean;
|
|
9
|
+
forbidFunctionCreation?: boolean;
|
|
10
|
+
prototypeReplacements?: Map<new () => any, replacementCallback>;
|
|
11
|
+
prototypeWhitelist?: Map<any, Set<string>>;
|
|
12
|
+
globals: IGlobals;
|
|
13
|
+
executionQuota?: bigint;
|
|
14
|
+
onExecutionQuotaReached?: (ticks: Ticks, scope: Scope, context: IExecutionTree, tree: LispItem) => boolean | void;
|
|
15
|
+
}
|
|
16
|
+
export interface IOptions {
|
|
17
|
+
audit: boolean;
|
|
18
|
+
forbidFunctionCalls: boolean;
|
|
19
|
+
forbidFunctionCreation: boolean;
|
|
20
|
+
prototypeReplacements: Map<new () => any, replacementCallback>;
|
|
21
|
+
prototypeWhitelist: Map<any, Set<string>>;
|
|
22
|
+
globals: IGlobals;
|
|
23
|
+
executionQuota?: bigint;
|
|
24
|
+
onExecutionQuotaReached?: (ticks: Ticks, scope: Scope, context: IExecutionTree, tree: LispItem) => boolean | void;
|
|
25
|
+
}
|
|
26
|
+
export interface IContext {
|
|
27
|
+
sandbox: SandboxExec;
|
|
28
|
+
globalScope: Scope;
|
|
29
|
+
sandboxGlobal: ISandboxGlobal;
|
|
30
|
+
globalsWhitelist: Set<any>;
|
|
31
|
+
prototypeWhitelist: Map<any, Set<string>>;
|
|
32
|
+
options: IOptions;
|
|
33
|
+
auditReport?: IAuditReport;
|
|
34
|
+
}
|
|
35
|
+
export interface IAuditReport {
|
|
36
|
+
globalsAccess: Set<unknown>;
|
|
37
|
+
prototypeAccess: {
|
|
38
|
+
[name: string]: Set<string>;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface Ticks {
|
|
42
|
+
ticks: bigint;
|
|
43
|
+
}
|
|
44
|
+
export type SubscriptionSubject = object;
|
|
45
|
+
export interface IExecContext extends IExecutionTree {
|
|
46
|
+
ctx: IContext;
|
|
47
|
+
getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
|
|
48
|
+
setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
49
|
+
changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
50
|
+
setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
51
|
+
changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
52
|
+
registerSandboxFunction: (fn: (...args: any[]) => any) => void;
|
|
53
|
+
evals: Map<any, any>;
|
|
54
|
+
allowJit: boolean;
|
|
55
|
+
evalContext?: IEvalContext;
|
|
56
|
+
}
|
|
57
|
+
export interface ISandboxGlobal {
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
}
|
|
60
|
+
interface SandboxGlobalConstructor {
|
|
61
|
+
new (globals: IGlobals): ISandboxGlobal;
|
|
62
|
+
}
|
|
63
|
+
export declare const SandboxGlobal: SandboxGlobalConstructor;
|
|
64
|
+
export type IGlobals = ISandboxGlobal;
|
|
65
|
+
export declare class ExecContext implements IExecContext {
|
|
66
|
+
ctx: IContext;
|
|
67
|
+
constants: IConstants;
|
|
68
|
+
tree: Lisp[];
|
|
69
|
+
getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
|
|
70
|
+
setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
71
|
+
changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
72
|
+
setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
73
|
+
changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
74
|
+
evals: Map<any, any>;
|
|
75
|
+
registerSandboxFunction: (fn: (...args: any[]) => any) => void;
|
|
76
|
+
allowJit: boolean;
|
|
77
|
+
evalContext?: IEvalContext | undefined;
|
|
78
|
+
constructor(ctx: IContext, constants: IConstants, tree: Lisp[], getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>, setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>, changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>, setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>, changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>, evals: Map<any, any>, registerSandboxFunction: (fn: (...args: any[]) => any) => void, allowJit: boolean, evalContext?: IEvalContext | undefined);
|
|
79
|
+
}
|
|
80
|
+
export declare function createContext(sandbox: SandboxExec, options: IOptions): IContext;
|
|
81
|
+
export declare function createExecContext(sandbox: {
|
|
82
|
+
setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
|
|
83
|
+
changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
|
|
84
|
+
sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
|
|
85
|
+
context: IContext;
|
|
86
|
+
}, executionTree: IExecutionTree, evalContext?: IEvalContext): IExecContext;
|
|
87
|
+
export declare class CodeString {
|
|
88
|
+
start: number;
|
|
89
|
+
end: number;
|
|
90
|
+
ref: {
|
|
91
|
+
str: string;
|
|
92
|
+
};
|
|
93
|
+
constructor(str: string | CodeString);
|
|
94
|
+
substring(start: number, end?: number): CodeString;
|
|
95
|
+
get length(): number;
|
|
96
|
+
char(i: number): string | undefined;
|
|
97
|
+
toString(): string;
|
|
98
|
+
trimStart(): CodeString;
|
|
99
|
+
slice(start: number, end?: number): CodeString;
|
|
100
|
+
trim(): CodeString;
|
|
101
|
+
valueOf(): string;
|
|
102
|
+
}
|
|
103
|
+
export declare const enum VarType {
|
|
104
|
+
let = "let",
|
|
105
|
+
const = "const",
|
|
106
|
+
var = "var"
|
|
107
|
+
}
|
|
108
|
+
export declare class Scope {
|
|
109
|
+
parent: Scope | null;
|
|
110
|
+
const: {
|
|
111
|
+
[key: string]: true;
|
|
112
|
+
};
|
|
113
|
+
let: {
|
|
114
|
+
[key: string]: true;
|
|
115
|
+
};
|
|
116
|
+
var: {
|
|
117
|
+
[key: string]: true;
|
|
118
|
+
};
|
|
119
|
+
globals: {
|
|
120
|
+
[key: string]: true;
|
|
121
|
+
};
|
|
122
|
+
allVars: {
|
|
123
|
+
[key: string]: unknown;
|
|
124
|
+
} & Object;
|
|
125
|
+
functionThis?: Unknown;
|
|
126
|
+
constructor(parent: Scope | null, vars?: {}, functionThis?: Unknown);
|
|
127
|
+
get(key: string, functionScope?: boolean): Prop;
|
|
128
|
+
set(key: string, val: unknown): Prop;
|
|
129
|
+
declare(key: string, type: VarType, value?: unknown, isGlobal?: boolean): Prop;
|
|
130
|
+
}
|
|
131
|
+
export interface IScope {
|
|
132
|
+
[key: string]: any;
|
|
133
|
+
}
|
|
134
|
+
export declare class FunctionScope implements IScope {
|
|
135
|
+
}
|
|
136
|
+
export declare class LocalScope implements IScope {
|
|
137
|
+
}
|
|
138
|
+
export declare class SandboxError extends Error {
|
|
139
|
+
}
|
|
140
|
+
export declare function isLisp<Type extends Lisp = Lisp>(item: LispItem | LispItem): item is Type;
|
|
141
|
+
export declare const enum LispType {
|
|
142
|
+
None = 0,
|
|
143
|
+
Prop = 1,
|
|
144
|
+
StringIndex = 2,
|
|
145
|
+
Let = 3,
|
|
146
|
+
Const = 4,
|
|
147
|
+
Call = 5,
|
|
148
|
+
KeyVal = 6,
|
|
149
|
+
Number = 7,
|
|
150
|
+
Return = 8,
|
|
151
|
+
Assign = 9,
|
|
152
|
+
InlineFunction = 10,
|
|
153
|
+
ArrowFunction = 11,
|
|
154
|
+
CreateArray = 12,
|
|
155
|
+
If = 13,
|
|
156
|
+
IfCase = 14,
|
|
157
|
+
InlineIf = 15,
|
|
158
|
+
InlineIfCase = 16,
|
|
159
|
+
SpreadObject = 17,
|
|
160
|
+
SpreadArray = 18,
|
|
161
|
+
ArrayProp = 19,
|
|
162
|
+
PropOptional = 20,
|
|
163
|
+
CallOptional = 21,
|
|
164
|
+
CreateObject = 22,
|
|
165
|
+
Group = 23,
|
|
166
|
+
Not = 24,
|
|
167
|
+
IncrementBefore = 25,
|
|
168
|
+
IncrementAfter = 26,
|
|
169
|
+
DecrementBefore = 27,
|
|
170
|
+
DecrementAfter = 28,
|
|
171
|
+
And = 29,
|
|
172
|
+
Or = 30,
|
|
173
|
+
StrictNotEqual = 31,
|
|
174
|
+
StrictEqual = 32,
|
|
175
|
+
Plus = 33,
|
|
176
|
+
Var = 34,
|
|
177
|
+
GlobalSymbol = 35,
|
|
178
|
+
Literal = 36,
|
|
179
|
+
Function = 37,
|
|
180
|
+
Loop = 38,
|
|
181
|
+
Try = 39,
|
|
182
|
+
Switch = 40,
|
|
183
|
+
SwitchCase = 41,
|
|
184
|
+
Block = 42,
|
|
185
|
+
Expression = 43,
|
|
186
|
+
Await = 44,
|
|
187
|
+
New = 45,
|
|
188
|
+
Throw = 46,
|
|
189
|
+
Minus = 47,
|
|
190
|
+
Divide = 48,
|
|
191
|
+
Power = 49,
|
|
192
|
+
Multiply = 50,
|
|
193
|
+
Modulus = 51,
|
|
194
|
+
Equal = 52,
|
|
195
|
+
NotEqual = 53,
|
|
196
|
+
SmallerEqualThan = 54,
|
|
197
|
+
LargerEqualThan = 55,
|
|
198
|
+
SmallerThan = 56,
|
|
199
|
+
LargerThan = 57,
|
|
200
|
+
Negative = 58,
|
|
201
|
+
Positive = 59,
|
|
202
|
+
Typeof = 60,
|
|
203
|
+
Delete = 61,
|
|
204
|
+
Instanceof = 62,
|
|
205
|
+
In = 63,
|
|
206
|
+
Inverse = 64,
|
|
207
|
+
SubractEquals = 65,
|
|
208
|
+
AddEquals = 66,
|
|
209
|
+
DivideEquals = 67,
|
|
210
|
+
PowerEquals = 68,
|
|
211
|
+
MultiplyEquals = 69,
|
|
212
|
+
ModulusEquals = 70,
|
|
213
|
+
BitNegateEquals = 71,
|
|
214
|
+
BitAndEquals = 72,
|
|
215
|
+
BitOrEquals = 73,
|
|
216
|
+
UnsignedShiftRightEquals = 74,
|
|
217
|
+
ShiftRightEquals = 75,
|
|
218
|
+
ShiftLeftEquals = 76,
|
|
219
|
+
BitAnd = 77,
|
|
220
|
+
BitOr = 78,
|
|
221
|
+
BitNegate = 79,
|
|
222
|
+
BitShiftLeft = 80,
|
|
223
|
+
BitShiftRight = 81,
|
|
224
|
+
BitUnsignedShiftRight = 82,
|
|
225
|
+
BigInt = 83,
|
|
226
|
+
LiteralIndex = 84,
|
|
227
|
+
RegexIndex = 85,
|
|
228
|
+
LoopAction = 86,
|
|
229
|
+
Void = 87,
|
|
230
|
+
True = 88,
|
|
231
|
+
LispEnumSize = 89
|
|
232
|
+
}
|
|
233
|
+
export declare class Prop {
|
|
234
|
+
context: Unknown;
|
|
235
|
+
prop: string;
|
|
236
|
+
isConst: boolean;
|
|
237
|
+
isGlobal: boolean;
|
|
238
|
+
isVariable: boolean;
|
|
239
|
+
constructor(context: Unknown, prop: string, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean);
|
|
240
|
+
get<T = unknown>(context: IExecContext): T;
|
|
241
|
+
}
|
|
242
|
+
export {};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const SandboxGlobal = function SandboxGlobal(globals) {
|
|
4
|
+
if (globals === globalThis)
|
|
5
|
+
return globalThis;
|
|
6
|
+
for (const i in globals) {
|
|
7
|
+
this[i] = globals[i];
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
class ExecContext {
|
|
11
|
+
constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction, allowJit, evalContext) {
|
|
12
|
+
this.ctx = ctx;
|
|
13
|
+
this.constants = constants;
|
|
14
|
+
this.tree = tree;
|
|
15
|
+
this.getSubscriptions = getSubscriptions;
|
|
16
|
+
this.setSubscriptions = setSubscriptions;
|
|
17
|
+
this.changeSubscriptions = changeSubscriptions;
|
|
18
|
+
this.setSubscriptionsGlobal = setSubscriptionsGlobal;
|
|
19
|
+
this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
|
|
20
|
+
this.evals = evals;
|
|
21
|
+
this.registerSandboxFunction = registerSandboxFunction;
|
|
22
|
+
this.allowJit = allowJit;
|
|
23
|
+
this.evalContext = evalContext;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function createContext(sandbox, options) {
|
|
27
|
+
const sandboxGlobal = new SandboxGlobal(options.globals);
|
|
28
|
+
const context = {
|
|
29
|
+
sandbox: sandbox,
|
|
30
|
+
globalsWhitelist: new Set(Object.values(options.globals)),
|
|
31
|
+
prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
|
|
32
|
+
options,
|
|
33
|
+
globalScope: new Scope(null, options.globals, sandboxGlobal),
|
|
34
|
+
sandboxGlobal,
|
|
35
|
+
};
|
|
36
|
+
context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), new Set());
|
|
37
|
+
return context;
|
|
38
|
+
}
|
|
39
|
+
function createExecContext(sandbox, executionTree, evalContext) {
|
|
40
|
+
const evals = new Map();
|
|
41
|
+
const execContext = new ExecContext(sandbox.context, executionTree.constants, executionTree.tree, new Set(), new WeakMap(), new WeakMap(), sandbox.setSubscriptions, sandbox.changeSubscriptions, evals, (fn) => sandbox.sandboxFunctions.set(fn, execContext), !!evalContext, evalContext);
|
|
42
|
+
if (evalContext) {
|
|
43
|
+
const func = evalContext.sandboxFunction(execContext);
|
|
44
|
+
evals.set(Function, func);
|
|
45
|
+
evals.set(eval, evalContext.sandboxedEval(func));
|
|
46
|
+
evals.set(setTimeout, evalContext.sandboxedSetTimeout(func));
|
|
47
|
+
evals.set(setInterval, evalContext.sandboxedSetInterval(func));
|
|
48
|
+
}
|
|
49
|
+
return execContext;
|
|
50
|
+
}
|
|
51
|
+
class CodeString {
|
|
52
|
+
constructor(str) {
|
|
53
|
+
this.ref = { str: '' };
|
|
54
|
+
if (str instanceof CodeString) {
|
|
55
|
+
this.ref = str.ref;
|
|
56
|
+
this.start = str.start;
|
|
57
|
+
this.end = str.end;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.ref.str = str;
|
|
61
|
+
this.start = 0;
|
|
62
|
+
this.end = str.length;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
substring(start, end) {
|
|
66
|
+
if (!this.length)
|
|
67
|
+
return this;
|
|
68
|
+
start = this.start + start;
|
|
69
|
+
if (start < 0) {
|
|
70
|
+
start = 0;
|
|
71
|
+
}
|
|
72
|
+
if (start > this.end) {
|
|
73
|
+
start = this.end;
|
|
74
|
+
}
|
|
75
|
+
end = end === undefined ? this.end : this.start + end;
|
|
76
|
+
if (end < 0) {
|
|
77
|
+
end = 0;
|
|
78
|
+
}
|
|
79
|
+
if (end > this.end) {
|
|
80
|
+
end = this.end;
|
|
81
|
+
}
|
|
82
|
+
const code = new CodeString(this);
|
|
83
|
+
code.start = start;
|
|
84
|
+
code.end = end;
|
|
85
|
+
return code;
|
|
86
|
+
}
|
|
87
|
+
get length() {
|
|
88
|
+
const len = this.end - this.start;
|
|
89
|
+
return len < 0 ? 0 : len;
|
|
90
|
+
}
|
|
91
|
+
char(i) {
|
|
92
|
+
if (this.start === this.end)
|
|
93
|
+
return undefined;
|
|
94
|
+
return this.ref.str[this.start + i];
|
|
95
|
+
}
|
|
96
|
+
toString() {
|
|
97
|
+
return this.ref.str.substring(this.start, this.end);
|
|
98
|
+
}
|
|
99
|
+
trimStart() {
|
|
100
|
+
const found = /^\s+/.exec(this.toString());
|
|
101
|
+
const code = new CodeString(this);
|
|
102
|
+
if (found) {
|
|
103
|
+
code.start += found[0].length;
|
|
104
|
+
}
|
|
105
|
+
return code;
|
|
106
|
+
}
|
|
107
|
+
slice(start, end) {
|
|
108
|
+
if (start < 0) {
|
|
109
|
+
start = this.end - this.start + start;
|
|
110
|
+
}
|
|
111
|
+
if (start < 0) {
|
|
112
|
+
start = 0;
|
|
113
|
+
}
|
|
114
|
+
if (end === undefined) {
|
|
115
|
+
end = this.end - this.start;
|
|
116
|
+
}
|
|
117
|
+
if (end < 0) {
|
|
118
|
+
end = this.end - this.start + end;
|
|
119
|
+
}
|
|
120
|
+
if (end < 0) {
|
|
121
|
+
end = 0;
|
|
122
|
+
}
|
|
123
|
+
return this.substring(start, end);
|
|
124
|
+
}
|
|
125
|
+
trim() {
|
|
126
|
+
const code = this.trimStart();
|
|
127
|
+
const found = /\s+$/.exec(code.toString());
|
|
128
|
+
if (found) {
|
|
129
|
+
code.end -= found[0].length;
|
|
130
|
+
}
|
|
131
|
+
return code;
|
|
132
|
+
}
|
|
133
|
+
valueOf() {
|
|
134
|
+
return this.toString();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function keysOnly(obj) {
|
|
138
|
+
const ret = Object.assign({}, obj);
|
|
139
|
+
for (const key in ret) {
|
|
140
|
+
ret[key] = true;
|
|
141
|
+
}
|
|
142
|
+
return ret;
|
|
143
|
+
}
|
|
144
|
+
const reservedWords = new Set([
|
|
145
|
+
'instanceof',
|
|
146
|
+
'typeof',
|
|
147
|
+
'return',
|
|
148
|
+
'throw',
|
|
149
|
+
'try',
|
|
150
|
+
'catch',
|
|
151
|
+
'if',
|
|
152
|
+
'finally',
|
|
153
|
+
'else',
|
|
154
|
+
'in',
|
|
155
|
+
'of',
|
|
156
|
+
'var',
|
|
157
|
+
'let',
|
|
158
|
+
'const',
|
|
159
|
+
'for',
|
|
160
|
+
'delete',
|
|
161
|
+
'false',
|
|
162
|
+
'true',
|
|
163
|
+
'while',
|
|
164
|
+
'do',
|
|
165
|
+
'break',
|
|
166
|
+
'continue',
|
|
167
|
+
'new',
|
|
168
|
+
'function',
|
|
169
|
+
'async',
|
|
170
|
+
'await',
|
|
171
|
+
'switch',
|
|
172
|
+
'case',
|
|
173
|
+
]);
|
|
174
|
+
class Scope {
|
|
175
|
+
constructor(parent, vars = {}, functionThis) {
|
|
176
|
+
this.const = {};
|
|
177
|
+
this.let = {};
|
|
178
|
+
this.var = {};
|
|
179
|
+
const isFuncScope = functionThis !== undefined || parent === null;
|
|
180
|
+
this.parent = parent;
|
|
181
|
+
this.allVars = vars;
|
|
182
|
+
this.let = isFuncScope ? this.let : keysOnly(vars);
|
|
183
|
+
this.var = isFuncScope ? keysOnly(vars) : this.var;
|
|
184
|
+
this.globals = parent === null ? keysOnly(vars) : {};
|
|
185
|
+
this.functionThis = functionThis;
|
|
186
|
+
}
|
|
187
|
+
get(key, functionScope = false) {
|
|
188
|
+
const functionThis = this.functionThis;
|
|
189
|
+
if (key === 'this' && functionThis !== undefined) {
|
|
190
|
+
return new Prop({ this: functionThis }, key, true, false, true);
|
|
191
|
+
}
|
|
192
|
+
if (reservedWords.has(key))
|
|
193
|
+
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
194
|
+
if (this.parent === null || !functionScope || functionThis !== undefined) {
|
|
195
|
+
if (this.globals.hasOwnProperty(key)) {
|
|
196
|
+
return new Prop(functionThis, key, false, true, true);
|
|
197
|
+
}
|
|
198
|
+
if (key in this.allVars && (!(key in {}) || this.allVars.hasOwnProperty(key))) {
|
|
199
|
+
return new Prop(this.allVars, key, this.const.hasOwnProperty(key), this.globals.hasOwnProperty(key), true);
|
|
200
|
+
}
|
|
201
|
+
if (this.parent === null) {
|
|
202
|
+
return new Prop(undefined, key);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return this.parent.get(key, functionScope);
|
|
206
|
+
}
|
|
207
|
+
set(key, val) {
|
|
208
|
+
if (key === 'this')
|
|
209
|
+
throw new SyntaxError('"this" cannot be assigned');
|
|
210
|
+
if (reservedWords.has(key))
|
|
211
|
+
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
212
|
+
const prop = this.get(key);
|
|
213
|
+
if (prop.context === undefined) {
|
|
214
|
+
throw new ReferenceError(`Variable '${key}' was not declared.`);
|
|
215
|
+
}
|
|
216
|
+
if (prop.isConst) {
|
|
217
|
+
throw new TypeError(`Cannot assign to const variable '${key}'`);
|
|
218
|
+
}
|
|
219
|
+
if (prop.isGlobal) {
|
|
220
|
+
throw new SandboxError(`Cannot override global variable '${key}'`);
|
|
221
|
+
}
|
|
222
|
+
if (!(prop.context instanceof Object))
|
|
223
|
+
throw new SandboxError('Scope is not an object');
|
|
224
|
+
prop.context[prop.prop] = val;
|
|
225
|
+
return prop;
|
|
226
|
+
}
|
|
227
|
+
declare(key, type, value = undefined, isGlobal = false) {
|
|
228
|
+
if (key === 'this')
|
|
229
|
+
throw new SyntaxError('"this" cannot be declared');
|
|
230
|
+
if (reservedWords.has(key))
|
|
231
|
+
throw new SyntaxError("Unexepected token '" + key + "'");
|
|
232
|
+
if (type === 'var' && this.functionThis === undefined && this.parent !== null) {
|
|
233
|
+
return this.parent.declare(key, type, value, isGlobal);
|
|
234
|
+
}
|
|
235
|
+
else if ((this[type].hasOwnProperty(key) && type !== 'const' && !this.globals.hasOwnProperty(key)) ||
|
|
236
|
+
!(key in this.allVars)) {
|
|
237
|
+
if (isGlobal) {
|
|
238
|
+
this.globals[key] = true;
|
|
239
|
+
}
|
|
240
|
+
this[type][key] = true;
|
|
241
|
+
this.allVars[key] = value;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
throw new SandboxError(`Identifier '${key}' has already been declared`);
|
|
245
|
+
}
|
|
246
|
+
return new Prop(this.allVars, key, this.const.hasOwnProperty(key), isGlobal);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
class FunctionScope {
|
|
250
|
+
}
|
|
251
|
+
class LocalScope {
|
|
252
|
+
}
|
|
253
|
+
class SandboxError extends Error {
|
|
254
|
+
}
|
|
255
|
+
function isLisp(item) {
|
|
256
|
+
return (Array.isArray(item) &&
|
|
257
|
+
typeof item[0] === 'number' &&
|
|
258
|
+
item[0] !== 0 /* LispType.None */ &&
|
|
259
|
+
item[0] !== 88 /* LispType.True */);
|
|
260
|
+
}
|
|
261
|
+
class Prop {
|
|
262
|
+
constructor(context, prop, isConst = false, isGlobal = false, isVariable = false) {
|
|
263
|
+
this.context = context;
|
|
264
|
+
this.prop = prop;
|
|
265
|
+
this.isConst = isConst;
|
|
266
|
+
this.isGlobal = isGlobal;
|
|
267
|
+
this.isVariable = isVariable;
|
|
268
|
+
}
|
|
269
|
+
get(context) {
|
|
270
|
+
const ctx = this.context;
|
|
271
|
+
if (ctx === undefined)
|
|
272
|
+
throw new ReferenceError(`${this.prop} is not defined`);
|
|
273
|
+
if (ctx === null)
|
|
274
|
+
throw new TypeError(`Cannot read properties of null, (reading '${this.prop}')`);
|
|
275
|
+
context.getSubscriptions.forEach((cb) => cb(ctx, this.prop));
|
|
276
|
+
return ctx[this.prop];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
exports.CodeString = CodeString;
|
|
281
|
+
exports.ExecContext = ExecContext;
|
|
282
|
+
exports.FunctionScope = FunctionScope;
|
|
283
|
+
exports.LocalScope = LocalScope;
|
|
284
|
+
exports.Prop = Prop;
|
|
285
|
+
exports.SandboxError = SandboxError;
|
|
286
|
+
exports.SandboxGlobal = SandboxGlobal;
|
|
287
|
+
exports.Scope = Scope;
|
|
288
|
+
exports.createContext = createContext;
|
|
289
|
+
exports.createExecContext = createExecContext;
|
|
290
|
+
exports.isLisp = isLisp;
|