@cuxt/sandboxjs 0.1.1 → 0.1.3

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.
Files changed (226) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +198 -185
  3. package/{build → dist/cjs}/Sandbox.d.ts +15 -6
  4. package/dist/cjs/Sandbox.js +126 -0
  5. package/dist/{SandboxExec.d.ts → cjs/SandboxExec.d.ts} +8 -17
  6. package/dist/cjs/SandboxExec.js +227 -0
  7. package/{build/eval.d.ts → dist/cjs/eval/index.d.ts} +10 -2
  8. package/dist/cjs/eval/index.js +233 -0
  9. package/dist/cjs/executor/executorUtils.d.ts +161 -0
  10. package/dist/cjs/executor/executorUtils.js +930 -0
  11. package/dist/cjs/executor/index.d.ts +1 -0
  12. package/dist/cjs/executor/index.js +2 -0
  13. package/dist/cjs/executor/ops/assignment.d.ts +1 -0
  14. package/dist/cjs/executor/ops/assignment.js +88 -0
  15. package/dist/cjs/executor/ops/call.d.ts +1 -0
  16. package/dist/cjs/executor/ops/call.js +160 -0
  17. package/dist/cjs/executor/ops/comparison.d.ts +1 -0
  18. package/dist/cjs/executor/ops/comparison.js +36 -0
  19. package/dist/cjs/executor/ops/control.d.ts +1 -0
  20. package/dist/cjs/executor/ops/control.js +203 -0
  21. package/dist/cjs/executor/ops/functions.d.ts +1 -0
  22. package/dist/cjs/executor/ops/functions.js +55 -0
  23. package/dist/cjs/executor/ops/index.d.ts +0 -0
  24. package/dist/cjs/executor/ops/index.js +11 -0
  25. package/dist/cjs/executor/ops/literals.d.ts +1 -0
  26. package/dist/cjs/executor/ops/literals.js +45 -0
  27. package/dist/cjs/executor/ops/misc.d.ts +1 -0
  28. package/dist/cjs/executor/ops/misc.js +25 -0
  29. package/dist/cjs/executor/ops/object.d.ts +1 -0
  30. package/dist/cjs/executor/ops/object.js +51 -0
  31. package/dist/cjs/executor/ops/prop.d.ts +1 -0
  32. package/dist/cjs/executor/ops/prop.js +70 -0
  33. package/dist/cjs/executor/ops/unary.d.ts +1 -0
  34. package/dist/cjs/executor/ops/unary.js +48 -0
  35. package/dist/cjs/executor/ops/variables.d.ts +1 -0
  36. package/dist/cjs/executor/ops/variables.js +16 -0
  37. package/dist/cjs/executor/opsRegistry.d.ts +33 -0
  38. package/dist/cjs/executor/opsRegistry.js +8 -0
  39. package/dist/cjs/package.json +3 -0
  40. package/dist/cjs/parser/index.d.ts +3 -0
  41. package/dist/cjs/parser/index.js +1 -0
  42. package/dist/{parser.d.ts → cjs/parser/lisp.d.ts} +29 -42
  43. package/dist/cjs/parser/lispTypes/conditionals.d.ts +2 -0
  44. package/dist/cjs/parser/lispTypes/conditionals.js +135 -0
  45. package/dist/cjs/parser/lispTypes/control.d.ts +2 -0
  46. package/dist/cjs/parser/lispTypes/control.js +204 -0
  47. package/dist/cjs/parser/lispTypes/declarations.d.ts +2 -0
  48. package/dist/cjs/parser/lispTypes/declarations.js +99 -0
  49. package/dist/cjs/parser/lispTypes/index.d.ts +3 -0
  50. package/dist/cjs/parser/lispTypes/index.js +17 -0
  51. package/dist/cjs/parser/lispTypes/operators.d.ts +2 -0
  52. package/dist/cjs/parser/lispTypes/operators.js +252 -0
  53. package/dist/cjs/parser/lispTypes/shared.d.ts +38 -0
  54. package/dist/cjs/parser/lispTypes/structures.d.ts +2 -0
  55. package/dist/cjs/parser/lispTypes/structures.js +188 -0
  56. package/dist/cjs/parser/lispTypes/values.d.ts +2 -0
  57. package/dist/cjs/parser/lispTypes/values.js +89 -0
  58. package/dist/cjs/parser/parserUtils.d.ts +34 -0
  59. package/dist/cjs/parser/parserUtils.js +968 -0
  60. package/dist/cjs/utils/CodeString.d.ts +16 -0
  61. package/dist/cjs/utils/CodeString.js +64 -0
  62. package/dist/cjs/utils/ExecContext.d.ts +34 -0
  63. package/dist/cjs/utils/ExecContext.js +171 -0
  64. package/dist/cjs/utils/Prop.d.ts +16 -0
  65. package/dist/cjs/utils/Prop.js +81 -0
  66. package/dist/cjs/utils/Scope.d.ts +47 -0
  67. package/dist/cjs/utils/Scope.js +127 -0
  68. package/dist/cjs/utils/errors.d.ts +10 -0
  69. package/dist/cjs/utils/errors.js +12 -0
  70. package/dist/cjs/utils/functionReplacements.d.ts +11 -0
  71. package/dist/cjs/utils/functionReplacements.js +362 -0
  72. package/dist/cjs/utils/index.d.ts +7 -0
  73. package/dist/cjs/utils/index.js +7 -0
  74. package/dist/cjs/utils/types.d.ts +221 -0
  75. package/dist/cjs/utils/types.js +164 -0
  76. package/dist/cjs/utils/unraw.js +145 -0
  77. package/dist/{node → esm}/Sandbox.d.ts +15 -6
  78. package/dist/esm/Sandbox.js +115 -0
  79. package/dist/esm/Sandbox.js.map +1 -0
  80. package/{build → dist/esm}/SandboxExec.d.ts +8 -17
  81. package/dist/esm/SandboxExec.js +224 -0
  82. package/dist/esm/SandboxExec.js.map +1 -0
  83. package/dist/{eval.d.ts → esm/eval/index.d.ts} +10 -2
  84. package/dist/esm/eval/index.js +235 -0
  85. package/dist/esm/eval/index.js.map +1 -0
  86. package/dist/esm/executor/executorUtils.d.ts +161 -0
  87. package/dist/esm/executor/executorUtils.js +898 -0
  88. package/dist/esm/executor/executorUtils.js.map +1 -0
  89. package/dist/esm/executor/index.d.ts +1 -0
  90. package/dist/esm/executor/index.js +2 -0
  91. package/dist/esm/executor/ops/assignment.d.ts +1 -0
  92. package/dist/esm/executor/ops/assignment.js +90 -0
  93. package/dist/esm/executor/ops/assignment.js.map +1 -0
  94. package/dist/esm/executor/ops/call.d.ts +1 -0
  95. package/dist/esm/executor/ops/call.js +162 -0
  96. package/dist/esm/executor/ops/call.js.map +1 -0
  97. package/dist/esm/executor/ops/comparison.d.ts +1 -0
  98. package/dist/esm/executor/ops/comparison.js +38 -0
  99. package/dist/esm/executor/ops/comparison.js.map +1 -0
  100. package/dist/esm/executor/ops/control.d.ts +1 -0
  101. package/dist/esm/executor/ops/control.js +205 -0
  102. package/dist/esm/executor/ops/control.js.map +1 -0
  103. package/dist/esm/executor/ops/functions.d.ts +1 -0
  104. package/dist/esm/executor/ops/functions.js +57 -0
  105. package/dist/esm/executor/ops/functions.js.map +1 -0
  106. package/dist/esm/executor/ops/index.d.ts +0 -0
  107. package/dist/esm/executor/ops/index.js +11 -0
  108. package/dist/esm/executor/ops/literals.d.ts +1 -0
  109. package/dist/esm/executor/ops/literals.js +47 -0
  110. package/dist/esm/executor/ops/literals.js.map +1 -0
  111. package/dist/esm/executor/ops/misc.d.ts +1 -0
  112. package/dist/esm/executor/ops/misc.js +27 -0
  113. package/dist/esm/executor/ops/misc.js.map +1 -0
  114. package/dist/esm/executor/ops/object.d.ts +1 -0
  115. package/dist/esm/executor/ops/object.js +53 -0
  116. package/dist/esm/executor/ops/object.js.map +1 -0
  117. package/dist/esm/executor/ops/prop.d.ts +1 -0
  118. package/dist/esm/executor/ops/prop.js +72 -0
  119. package/dist/esm/executor/ops/prop.js.map +1 -0
  120. package/dist/esm/executor/ops/unary.d.ts +1 -0
  121. package/dist/esm/executor/ops/unary.js +50 -0
  122. package/dist/esm/executor/ops/unary.js.map +1 -0
  123. package/dist/esm/executor/ops/variables.d.ts +1 -0
  124. package/dist/esm/executor/ops/variables.js +18 -0
  125. package/dist/esm/executor/ops/variables.js.map +1 -0
  126. package/dist/esm/executor/opsRegistry.d.ts +33 -0
  127. package/dist/esm/executor/opsRegistry.js +9 -0
  128. package/dist/esm/executor/opsRegistry.js.map +1 -0
  129. package/dist/esm/package.json +3 -0
  130. package/dist/esm/parser/index.d.ts +3 -0
  131. package/dist/esm/parser/index.js +1 -0
  132. package/{build/parser.d.ts → dist/esm/parser/lisp.d.ts} +29 -42
  133. package/dist/esm/parser/lispTypes/conditionals.d.ts +2 -0
  134. package/dist/esm/parser/lispTypes/conditionals.js +137 -0
  135. package/dist/esm/parser/lispTypes/conditionals.js.map +1 -0
  136. package/dist/esm/parser/lispTypes/control.d.ts +2 -0
  137. package/dist/esm/parser/lispTypes/control.js +206 -0
  138. package/dist/esm/parser/lispTypes/control.js.map +1 -0
  139. package/dist/esm/parser/lispTypes/declarations.d.ts +2 -0
  140. package/dist/esm/parser/lispTypes/declarations.js +101 -0
  141. package/dist/esm/parser/lispTypes/declarations.js.map +1 -0
  142. package/dist/esm/parser/lispTypes/index.d.ts +3 -0
  143. package/dist/esm/parser/lispTypes/index.js +19 -0
  144. package/dist/esm/parser/lispTypes/index.js.map +1 -0
  145. package/dist/esm/parser/lispTypes/operators.d.ts +2 -0
  146. package/dist/esm/parser/lispTypes/operators.js +254 -0
  147. package/dist/esm/parser/lispTypes/operators.js.map +1 -0
  148. package/dist/esm/parser/lispTypes/shared.d.ts +38 -0
  149. package/dist/esm/parser/lispTypes/structures.d.ts +2 -0
  150. package/dist/esm/parser/lispTypes/structures.js +190 -0
  151. package/dist/esm/parser/lispTypes/structures.js.map +1 -0
  152. package/dist/esm/parser/lispTypes/values.d.ts +2 -0
  153. package/dist/esm/parser/lispTypes/values.js +91 -0
  154. package/dist/esm/parser/lispTypes/values.js.map +1 -0
  155. package/dist/esm/parser/parserUtils.d.ts +34 -0
  156. package/dist/esm/parser/parserUtils.js +959 -0
  157. package/dist/esm/parser/parserUtils.js.map +1 -0
  158. package/dist/esm/utils/CodeString.d.ts +16 -0
  159. package/dist/esm/utils/CodeString.js +66 -0
  160. package/dist/esm/utils/CodeString.js.map +1 -0
  161. package/dist/esm/utils/ExecContext.d.ts +34 -0
  162. package/dist/esm/utils/ExecContext.js +168 -0
  163. package/dist/esm/utils/ExecContext.js.map +1 -0
  164. package/dist/esm/utils/Prop.d.ts +16 -0
  165. package/dist/esm/utils/Prop.js +80 -0
  166. package/dist/esm/utils/Prop.js.map +1 -0
  167. package/dist/esm/utils/Scope.d.ts +47 -0
  168. package/dist/esm/utils/Scope.js +122 -0
  169. package/dist/esm/utils/Scope.js.map +1 -0
  170. package/dist/esm/utils/errors.d.ts +10 -0
  171. package/dist/esm/utils/errors.js +10 -0
  172. package/dist/esm/utils/errors.js.map +1 -0
  173. package/dist/esm/utils/functionReplacements.d.ts +11 -0
  174. package/dist/esm/utils/functionReplacements.js +361 -0
  175. package/dist/esm/utils/functionReplacements.js.map +1 -0
  176. package/dist/esm/utils/index.d.ts +7 -0
  177. package/dist/esm/utils/index.js +7 -0
  178. package/dist/esm/utils/types.d.ts +221 -0
  179. package/dist/esm/utils/types.js +160 -0
  180. package/dist/esm/utils/types.js.map +1 -0
  181. package/dist/esm/utils/unraw.js +147 -0
  182. package/dist/esm/utils/unraw.js.map +1 -0
  183. package/dist/umd/Sandbox.min.js +2 -0
  184. package/dist/umd/Sandbox.min.js.map +1 -0
  185. package/dist/umd/SandboxExec.min.js +2 -0
  186. package/dist/umd/SandboxExec.min.js.map +1 -0
  187. package/package.json +11 -9
  188. package/build/Sandbox.js +0 -62
  189. package/build/SandboxExec.js +0 -214
  190. package/build/eval.js +0 -205
  191. package/build/executor.d.ts +0 -124
  192. package/build/executor.js +0 -1554
  193. package/build/parser.js +0 -1527
  194. package/build/unraw.js +0 -168
  195. package/build/utils.d.ts +0 -264
  196. package/build/utils.js +0 -362
  197. package/dist/Sandbox.d.ts +0 -25
  198. package/dist/Sandbox.js +0 -270
  199. package/dist/Sandbox.js.map +0 -1
  200. package/dist/Sandbox.min.js +0 -2
  201. package/dist/Sandbox.min.js.map +0 -1
  202. package/dist/SandboxExec.js +0 -218
  203. package/dist/SandboxExec.js.map +0 -1
  204. package/dist/SandboxExec.min.js +0 -2
  205. package/dist/SandboxExec.min.js.map +0 -1
  206. package/dist/executor.d.ts +0 -124
  207. package/dist/executor.js +0 -1558
  208. package/dist/executor.js.map +0 -1
  209. package/dist/node/Sandbox.js +0 -277
  210. package/dist/node/SandboxExec.d.ts +0 -66
  211. package/dist/node/SandboxExec.js +0 -225
  212. package/dist/node/eval.d.ts +0 -27
  213. package/dist/node/executor.d.ts +0 -124
  214. package/dist/node/executor.js +0 -1575
  215. package/dist/node/parser.d.ts +0 -154
  216. package/dist/node/parser.js +0 -1704
  217. package/dist/node/utils.d.ts +0 -264
  218. package/dist/node/utils.js +0 -385
  219. package/dist/parser.js +0 -1690
  220. package/dist/parser.js.map +0 -1
  221. package/dist/unraw.d.ts +0 -11
  222. package/dist/utils.d.ts +0 -264
  223. package/dist/utils.js +0 -365
  224. package/dist/utils.js.map +0 -1
  225. /package/{build → dist/cjs/utils}/unraw.d.ts +0 -0
  226. /package/dist/{node → esm/utils}/unraw.d.ts +0 -0
package/dist/executor.js DELETED
@@ -1,1558 +0,0 @@
1
- import { SandboxAccessError, hasOwnProperty, SandboxCapabilityError, isLisp, Prop, SandboxExecutionTreeError, SandboxError, SandboxExecutionQuotaExceededError, Scope, CodeString, LocalScope } from './utils.js';
2
-
3
- class ExecReturn {
4
- constructor(auditReport, result, returned, breakLoop = false, continueLoop = false) {
5
- this.auditReport = auditReport;
6
- this.result = result;
7
- this.returned = returned;
8
- this.breakLoop = breakLoop;
9
- this.continueLoop = continueLoop;
10
- }
11
- }
12
- const optional = {};
13
- function generateArgs(argNames, args) {
14
- const vars = {};
15
- argNames.forEach((arg, i) => {
16
- if (arg.startsWith('...')) {
17
- vars[arg.substring(3)] = args.slice(i);
18
- }
19
- else {
20
- vars[arg] = args[i];
21
- }
22
- });
23
- return vars;
24
- }
25
- function createFunction(argNames, parsed, ticks, context, scope, name) {
26
- if (context.ctx.options.forbidFunctionCreation) {
27
- throw new SandboxCapabilityError('Function creation is forbidden');
28
- }
29
- let func;
30
- if (name === undefined) {
31
- func = (...args) => {
32
- const vars = generateArgs(argNames, args);
33
- const res = executeTree(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars)]);
34
- return res.result;
35
- };
36
- }
37
- else {
38
- func = function sandboxedObject(...args) {
39
- const vars = generateArgs(argNames, args);
40
- const res = executeTree(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars, this)]);
41
- return res.result;
42
- };
43
- }
44
- context.registerSandboxFunction(func);
45
- context.ctx.sandboxedFunctions.add(func);
46
- return func;
47
- }
48
- function createFunctionAsync(argNames, parsed, ticks, context, scope, name) {
49
- if (context.ctx.options.forbidFunctionCreation) {
50
- throw new SandboxCapabilityError('Function creation is forbidden');
51
- }
52
- if (!context.ctx.prototypeWhitelist?.has(Promise.prototype)) {
53
- throw new SandboxCapabilityError('Async/await not permitted');
54
- }
55
- let func;
56
- if (name === undefined) {
57
- func = async (...args) => {
58
- const vars = generateArgs(argNames, args);
59
- const res = await executeTreeAsync(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars)]);
60
- return res.result;
61
- };
62
- }
63
- else {
64
- func = async function sandboxedObject(...args) {
65
- const vars = generateArgs(argNames, args);
66
- const res = await executeTreeAsync(ticks, context, parsed, scope === undefined ? [] : [new Scope(scope, vars, this)]);
67
- return res.result;
68
- };
69
- }
70
- context.registerSandboxFunction(func);
71
- context.ctx.sandboxedFunctions.add(func);
72
- return func;
73
- }
74
- function assignCheck(obj, context, op = 'assign') {
75
- if (obj.context === undefined) {
76
- throw new ReferenceError(`Cannot ${op} value to undefined.`);
77
- }
78
- if (obj.isConst) {
79
- throw new TypeError(`Assignment to constant variable.`);
80
- }
81
- if (obj.isGlobal) {
82
- throw new SandboxAccessError(`Cannot ${op} property '${obj.prop.toString()}' of a global object`);
83
- }
84
- if (obj.context === null) {
85
- throw new TypeError('Cannot set properties of null');
86
- }
87
- if (typeof obj.context[obj.prop] === 'function' &&
88
- !hasOwnProperty(obj.context, obj.prop)) {
89
- throw new SandboxAccessError(`Override prototype property '${obj.prop.toString()}' not allowed`);
90
- }
91
- if (op === 'delete') {
92
- if (hasOwnProperty(obj.context, obj.prop)) {
93
- context.changeSubscriptions
94
- .get(obj.context)
95
- ?.forEach((cb) => cb({ type: 'delete', prop: obj.prop.toString() }));
96
- context.changeSubscriptionsGlobal
97
- .get(obj.context)
98
- ?.forEach((cb) => cb({ type: 'delete', prop: obj.prop.toString() }));
99
- }
100
- }
101
- else if (hasOwnProperty(obj.context, obj.prop)) {
102
- context.setSubscriptions
103
- .get(obj.context)
104
- ?.get(obj.prop.toString())
105
- ?.forEach((cb) => cb({
106
- type: 'replace',
107
- }));
108
- context.setSubscriptionsGlobal
109
- .get(obj.context)
110
- ?.get(obj.prop.toString())
111
- ?.forEach((cb) => cb({
112
- type: 'replace',
113
- }));
114
- }
115
- else {
116
- context.changeSubscriptions
117
- .get(obj.context)
118
- ?.forEach((cb) => cb({ type: 'create', prop: obj.prop.toString() }));
119
- context.changeSubscriptionsGlobal
120
- .get(obj.context)
121
- ?.forEach((cb) => cb({ type: 'create', prop: obj.prop.toString() }));
122
- }
123
- }
124
- const arrayChange = new Set([
125
- [].push,
126
- [].pop,
127
- [].shift,
128
- [].unshift,
129
- [].splice,
130
- [].reverse,
131
- [].sort,
132
- [].copyWithin,
133
- ]);
134
- class KeyVal {
135
- constructor(key, val) {
136
- this.key = key;
137
- this.val = val;
138
- }
139
- }
140
- class SpreadObject {
141
- constructor(item) {
142
- this.item = item;
143
- }
144
- }
145
- class SpreadArray {
146
- constructor(item) {
147
- this.item = item;
148
- }
149
- }
150
- class If {
151
- constructor(t, f) {
152
- this.t = t;
153
- this.f = f;
154
- }
155
- }
156
- const literalRegex = /(\$\$)*(\$)?\${(\d+)}/g;
157
- const ops = new Map();
158
- function addOps(type, cb) {
159
- ops.set(type, cb);
160
- }
161
- const prorptyKeyTypes = ['string', 'number', 'symbol'];
162
- function isPropertyKey(val) {
163
- return prorptyKeyTypes.includes(typeof val);
164
- }
165
- function hasPossibleProperties(val) {
166
- return val !== null && val !== undefined;
167
- }
168
- addOps(1 /* LispType.Prop */, ({ done, a, b, obj, context, scope }) => {
169
- if (a === null) {
170
- throw new TypeError(`Cannot read properties of null (reading '${b?.toString()}')`);
171
- }
172
- if (!isPropertyKey(b)) {
173
- b = `${b}`;
174
- }
175
- if (a === undefined && obj === undefined && typeof b === 'string') {
176
- // is variable access
177
- const prop = scope.get(b);
178
- if (prop.context === context.ctx.sandboxGlobal) {
179
- if (context.ctx.options.audit) {
180
- context.ctx.auditReport?.globalsAccess.add(b);
181
- }
182
- }
183
- const val = prop.context ? prop.context[prop.prop] : undefined;
184
- const p = getGlobalProp(val, context, prop) || prop;
185
- done(undefined, p);
186
- return;
187
- }
188
- else if (a === undefined) {
189
- throw new TypeError(`Cannot read properties of undefined (reading '${b.toString()}')`);
190
- }
191
- if (!hasPossibleProperties(a)) {
192
- done(undefined, new Prop(undefined, b));
193
- return;
194
- }
195
- const prototypeAccess = typeof a === 'function' || !hasOwnProperty(a, b);
196
- if (context.ctx.options.audit && prototypeAccess) {
197
- let prot = Object.getPrototypeOf(a);
198
- do {
199
- if (hasOwnProperty(prot, b)) {
200
- if (context.ctx.auditReport &&
201
- !context.ctx.auditReport.prototypeAccess[prot.constructor.name]) {
202
- context.ctx.auditReport.prototypeAccess[prot.constructor.name] = new Set();
203
- }
204
- context.ctx.auditReport?.prototypeAccess[prot.constructor.name].add(b);
205
- }
206
- } while ((prot = Object.getPrototypeOf(prot)));
207
- }
208
- if (prototypeAccess) {
209
- if (typeof a === 'function') {
210
- if (hasOwnProperty(a, b)) {
211
- const whitelist = context.ctx.prototypeWhitelist.get(a.prototype);
212
- const replace = context.ctx.options.prototypeReplacements.get(a);
213
- if (replace) {
214
- done(undefined, new Prop(replace(a, true), b));
215
- return;
216
- }
217
- if (!(whitelist && (!whitelist.size || whitelist.has(b))) &&
218
- !context.ctx.sandboxedFunctions.has(a)) {
219
- throw new SandboxAccessError(`Static method or property access not permitted: ${a.name}.${b.toString()}`);
220
- }
221
- }
222
- }
223
- // Fast path: check own properties of `a` first (handles static methods like Body.json)
224
- if (hasOwnProperty(a, b)) {
225
- const replace = context.ctx.options.prototypeReplacements.get(a);
226
- if (replace) {
227
- done(undefined, new Prop(replace(a, true), b));
228
- return;
229
- }
230
- if (context.ctx.sandboxedFunctions.has(a)) {
231
- done(undefined, new Prop(a, b, false, false));
232
- return;
233
- }
234
- // Check if constructor or any ancestor is whitelisted
235
- let ctorProt = a.prototype;
236
- while (ctorProt !== null) {
237
- const whitelist = context.ctx.prototypeWhitelist.get(ctorProt);
238
- if (whitelist && (!whitelist.size || whitelist.has(b))) {
239
- done(undefined, new Prop(a, b, false, false));
240
- return;
241
- }
242
- ctorProt = Object.getPrototypeOf(ctorProt);
243
- }
244
- if (typeof a === 'function') {
245
- // Constructor not whitelisted, throw
246
- throw new SandboxAccessError(`Method or property access not permitted: ${a.name}.${b.toString()}`);
247
- }
248
- }
249
- let prot = a;
250
- while ((prot = Object.getPrototypeOf(prot))) {
251
- if (hasOwnProperty(prot, b) || b === '__proto__') {
252
- const replace = context.ctx.options.prototypeReplacements.get(prot.constructor);
253
- if (replace) {
254
- done(undefined, new Prop(replace(a, false), b));
255
- return;
256
- }
257
- if (context.ctx.sandboxedFunctions.has(prot.constructor)) {
258
- break;
259
- }
260
- // Check if this constructor or any ancestor in its prototype chain is whitelisted
261
- let ctorProt = prot.constructor;
262
- let whitelisted = false;
263
- while (ctorProt !== null) {
264
- const whitelist = context.ctx.prototypeWhitelist.get(ctorProt);
265
- if (whitelist && (!whitelist.size || whitelist.has(b))) {
266
- whitelisted = true;
267
- break;
268
- }
269
- ctorProt = Object.getPrototypeOf(ctorProt);
270
- }
271
- if (whitelisted) {
272
- break;
273
- }
274
- if (b === '__proto__') {
275
- throw new SandboxAccessError(`Access to prototype of global object is not permitted`);
276
- }
277
- throw new SandboxAccessError(`Method or property access not permitted: ${prot.constructor.name}.${b.toString()}`);
278
- }
279
- }
280
- }
281
- const val = a[b];
282
- if (typeof a === 'function') {
283
- if (b === 'prototype' && !context.ctx.sandboxedFunctions.has(a)) {
284
- throw new SandboxAccessError(`Access to prototype of global object is not permitted`);
285
- }
286
- }
287
- if (b === '__proto__' && !context.ctx.sandboxedFunctions.has(val?.constructor)) {
288
- throw new SandboxAccessError(`Access to prototype of global object is not permitted`);
289
- }
290
- const p = getGlobalProp(val, context, new Prop(a, b, false, false));
291
- if (p) {
292
- done(undefined, p);
293
- return;
294
- }
295
- const g = (obj instanceof Prop && obj.isGlobal) ||
296
- (typeof a === 'function' && !context.ctx.sandboxedFunctions.has(a)) ||
297
- context.ctx.globalsWhitelist.has(a);
298
- done(undefined, new Prop(a, b, false, g, false));
299
- });
300
- function getGlobalProp(val, context, prop) {
301
- if (!val)
302
- return;
303
- const isFunc = typeof val === 'function';
304
- if (val instanceof Prop) {
305
- if (!prop) {
306
- prop = val;
307
- }
308
- val = val.get(context);
309
- }
310
- const p = prop?.prop || 'prop';
311
- if (val === globalThis) {
312
- return new Prop({
313
- [p]: context.ctx.sandboxGlobal,
314
- }, p, prop?.isConst || false, false, prop?.isVariable || false);
315
- }
316
- const evl = isFunc && context.evals.get(val);
317
- if (evl) {
318
- return new Prop({
319
- [p]: evl,
320
- }, p, prop?.isConst || false, true, prop?.isVariable || false);
321
- }
322
- }
323
- function sanitizeArray(val, context, cache = new WeakSet()) {
324
- if (!Array.isArray(val))
325
- return val;
326
- if (cache.has(val))
327
- return val;
328
- cache.add(val);
329
- for (let i = 0; i < val.length; i++) {
330
- const item = val[i];
331
- if (item === globalThis) {
332
- val[i] = context.ctx.sandboxGlobal;
333
- }
334
- else if (typeof item === 'function') {
335
- const replacement = context.evals.get(item);
336
- if (replacement) {
337
- val[i] = replacement;
338
- }
339
- }
340
- else {
341
- sanitizeArray(item, context, cache);
342
- }
343
- }
344
- return val;
345
- }
346
- addOps(5 /* LispType.Call */, ({ done, a, b, obj, context }) => {
347
- if (context.ctx.options.forbidFunctionCalls)
348
- throw new SandboxCapabilityError('Function invocations are not allowed');
349
- if (typeof a !== 'function') {
350
- throw new TypeError(`${typeof obj?.prop === 'symbol' ? 'Symbol' : obj?.prop} is not a function`);
351
- }
352
- const vals = b
353
- .map((item) => {
354
- if (item instanceof SpreadArray) {
355
- return [...item.item];
356
- }
357
- else {
358
- return [item];
359
- }
360
- })
361
- .flat()
362
- .map((item) => valueOrProp(item, context));
363
- if (typeof obj === 'function') {
364
- const evl = context.evals.get(obj);
365
- let ret = evl ? evl(obj, ...vals) : obj(...vals);
366
- ret = getGlobalProp(ret, context) || ret;
367
- sanitizeArray(ret, context);
368
- // If the function returns a Promise, await it before signaling completion.
369
- // Without this, `done(undefined, promise)` resolves the outer await immediately,
370
- // but the Promise itself is still pending — causing async ops to be skipped.
371
- if (ret != null && typeof ret.then === 'function') {
372
- ret.then((resolved) => { done(undefined, resolved); }, (err) => { done(err); });
373
- }
374
- else {
375
- done(undefined, ret);
376
- }
377
- return;
378
- }
379
- if (obj.context[obj.prop] === JSON.stringify && context.getSubscriptions.size) {
380
- const cache = new Set();
381
- const recurse = (x) => {
382
- if (!x || !(typeof x === 'object') || cache.has(x))
383
- return;
384
- cache.add(x);
385
- for (const y of Object.keys(x)) {
386
- context.getSubscriptions.forEach((cb) => cb(x, y));
387
- recurse(x[y]);
388
- }
389
- };
390
- recurse(vals[0]);
391
- }
392
- if (obj.context instanceof Array &&
393
- arrayChange.has(obj.context[obj.prop]) &&
394
- (context.changeSubscriptions.get(obj.context) ||
395
- context.changeSubscriptionsGlobal.get(obj.context))) {
396
- let change;
397
- let changed = false;
398
- if (obj.prop === 'push') {
399
- change = {
400
- type: 'push',
401
- added: vals,
402
- };
403
- changed = !!vals.length;
404
- }
405
- else if (obj.prop === 'pop') {
406
- change = {
407
- type: 'pop',
408
- removed: obj.context.slice(-1),
409
- };
410
- changed = !!change.removed.length;
411
- }
412
- else if (obj.prop === 'shift') {
413
- change = {
414
- type: 'shift',
415
- removed: obj.context.slice(0, 1),
416
- };
417
- changed = !!change.removed.length;
418
- }
419
- else if (obj.prop === 'unshift') {
420
- change = {
421
- type: 'unshift',
422
- added: vals,
423
- };
424
- changed = !!vals.length;
425
- }
426
- else if (obj.prop === 'splice') {
427
- change = {
428
- type: 'splice',
429
- startIndex: vals[0],
430
- deleteCount: vals[1] === undefined ? obj.context.length : vals[1],
431
- added: vals.slice(2),
432
- removed: obj.context.slice(vals[0], vals[1] === undefined ? undefined : vals[0] + vals[1]),
433
- };
434
- changed = !!change.added.length || !!change.removed.length;
435
- }
436
- else if (obj.prop === 'reverse' || obj.prop === 'sort') {
437
- change = { type: obj.prop };
438
- changed = !!obj.context.length;
439
- }
440
- else if (obj.prop === 'copyWithin') {
441
- const len = vals[2] === undefined
442
- ? obj.context.length - vals[1]
443
- : Math.min(obj.context.length, vals[2] - vals[1]);
444
- change = {
445
- type: 'copyWithin',
446
- startIndex: vals[0],
447
- endIndex: vals[0] + len,
448
- added: obj.context.slice(vals[1], vals[1] + len),
449
- removed: obj.context.slice(vals[0], vals[0] + len),
450
- };
451
- changed = !!change.added.length || !!change.removed.length;
452
- }
453
- if (changed) {
454
- context.changeSubscriptions.get(obj.context)?.forEach((cb) => cb(change));
455
- context.changeSubscriptionsGlobal.get(obj.context)?.forEach((cb) => cb(change));
456
- }
457
- }
458
- obj.get(context);
459
- const evl = context.evals.get(obj.context[obj.prop]);
460
- let ret = evl ? evl(obj.context[obj.prop], ...vals) : obj.context[obj.prop](...vals);
461
- ret = getGlobalProp(ret, context) || ret;
462
- sanitizeArray(ret, context);
463
- done(undefined, ret);
464
- });
465
- addOps(22 /* LispType.CreateObject */, ({ done, b }) => {
466
- let res = {};
467
- for (const item of b) {
468
- if (item.key instanceof SpreadObject) {
469
- res = { ...res, ...item.key.item };
470
- }
471
- else {
472
- res[item.key] = item.val;
473
- }
474
- }
475
- done(undefined, res);
476
- });
477
- addOps(6 /* LispType.KeyVal */, ({ done, a, b }) => done(undefined, new KeyVal(a, b)));
478
- addOps(12 /* LispType.CreateArray */, ({ done, b, context }) => {
479
- const items = b
480
- .map((item) => {
481
- if (item instanceof SpreadArray) {
482
- return [...item.item];
483
- }
484
- else {
485
- return [item];
486
- }
487
- })
488
- .flat()
489
- .map((item) => valueOrProp(item, context));
490
- done(undefined, items);
491
- });
492
- addOps(23 /* LispType.Group */, ({ done, b }) => done(undefined, b));
493
- addOps(35 /* LispType.GlobalSymbol */, ({ done, b }) => {
494
- switch (b) {
495
- case 'true':
496
- return done(undefined, true);
497
- case 'false':
498
- return done(undefined, false);
499
- case 'null':
500
- return done(undefined, null);
501
- case 'undefined':
502
- return done(undefined, undefined);
503
- case 'NaN':
504
- return done(undefined, NaN);
505
- case 'Infinity':
506
- return done(undefined, Infinity);
507
- }
508
- done(new Error('Unknown symbol: ' + b));
509
- });
510
- addOps(7 /* LispType.Number */, ({ done, b }) => done(undefined, Number(b.replace(/_/g, ''))));
511
- addOps(83 /* LispType.BigInt */, ({ done, b }) => done(undefined, BigInt(b.replace(/_/g, ''))));
512
- addOps(2 /* LispType.StringIndex */, ({ done, b, context }) => done(undefined, context.constants.strings[parseInt(b)]));
513
- addOps(85 /* LispType.RegexIndex */, ({ done, b, context }) => {
514
- const reg = context.constants.regexes[parseInt(b)];
515
- if (!context.ctx.globalsWhitelist.has(RegExp)) {
516
- throw new SandboxCapabilityError('Regex not permitted');
517
- }
518
- else {
519
- done(undefined, new RegExp(reg.regex, reg.flags));
520
- }
521
- });
522
- addOps(84 /* LispType.LiteralIndex */, ({ exec, done, ticks, b, context, scope }) => {
523
- const item = context.constants.literals[parseInt(b)];
524
- const [, name, js] = item;
525
- const found = [];
526
- let f;
527
- const resnums = [];
528
- while ((f = literalRegex.exec(name))) {
529
- if (!f[2]) {
530
- found.push(js[parseInt(f[3], 10)]);
531
- resnums.push(f[3]);
532
- }
533
- }
534
- exec(ticks, found, scope, context, (...args) => {
535
- const reses = {};
536
- if (args.length === 1) {
537
- done(args[0]);
538
- return;
539
- }
540
- const processed = args[1];
541
- for (const i of Object.keys(processed)) {
542
- const num = resnums[i];
543
- reses[num] = processed[i];
544
- }
545
- done(undefined, name.replace(/(\\\\)*(\\)?\${(\d+)}/g, (match, $$, $, num) => {
546
- if ($)
547
- return match;
548
- const res = reses[num];
549
- return ($$ ? $$ : '') + `${valueOrProp(res, context)}`;
550
- }));
551
- });
552
- });
553
- addOps(18 /* LispType.SpreadArray */, ({ done, b }) => {
554
- done(undefined, new SpreadArray(b));
555
- });
556
- addOps(17 /* LispType.SpreadObject */, ({ done, b }) => {
557
- done(undefined, new SpreadObject(b));
558
- });
559
- addOps(24 /* LispType.Not */, ({ done, b }) => done(undefined, !b));
560
- addOps(64 /* LispType.Inverse */, ({ done, b }) => done(undefined, ~b));
561
- addOps(25 /* LispType.IncrementBefore */, ({ done, obj, context }) => {
562
- assignCheck(obj, context);
563
- done(undefined, ++obj.context[obj.prop]);
564
- });
565
- addOps(26 /* LispType.IncrementAfter */, ({ done, obj, context }) => {
566
- assignCheck(obj, context);
567
- done(undefined, obj.context[obj.prop]++);
568
- });
569
- addOps(27 /* LispType.DecrementBefore */, ({ done, obj, context }) => {
570
- assignCheck(obj, context);
571
- done(undefined, --obj.context[obj.prop]);
572
- });
573
- addOps(28 /* LispType.DecrementAfter */, ({ done, obj, context }) => {
574
- assignCheck(obj, context);
575
- done(undefined, obj.context[obj.prop]--);
576
- });
577
- addOps(9 /* LispType.Assign */, ({ done, b, obj, context, scope, bobj }) => {
578
- assignCheck(obj, context);
579
- obj.isGlobal = bobj?.isGlobal || false;
580
- if (obj.isVariable) {
581
- const s = scope.getWhereValScope(obj.prop, obj.prop === 'this');
582
- if (s === null) {
583
- throw new ReferenceError(`Cannot assign to undeclared variable '${obj.prop.toString()}'`);
584
- }
585
- s.set(obj.prop, b);
586
- if (obj.isGlobal) {
587
- s.globals[obj.prop.toString()] = true;
588
- }
589
- else {
590
- delete s.globals[obj.prop.toString()];
591
- }
592
- done(undefined, b);
593
- return;
594
- }
595
- done(undefined, (obj.context[obj.prop] = b));
596
- });
597
- addOps(66 /* LispType.AddEquals */, ({ done, b, obj, context }) => {
598
- assignCheck(obj, context);
599
- done(undefined, (obj.context[obj.prop] += b));
600
- });
601
- addOps(65 /* LispType.SubractEquals */, ({ done, b, obj, context }) => {
602
- assignCheck(obj, context);
603
- done(undefined, (obj.context[obj.prop] -= b));
604
- });
605
- addOps(67 /* LispType.DivideEquals */, ({ done, b, obj, context }) => {
606
- assignCheck(obj, context);
607
- done(undefined, (obj.context[obj.prop] /= b));
608
- });
609
- addOps(69 /* LispType.MultiplyEquals */, ({ done, b, obj, context }) => {
610
- assignCheck(obj, context);
611
- done(undefined, (obj.context[obj.prop] *= b));
612
- });
613
- addOps(68 /* LispType.PowerEquals */, ({ done, b, obj, context }) => {
614
- assignCheck(obj, context);
615
- done(undefined, (obj.context[obj.prop] **= b));
616
- });
617
- addOps(70 /* LispType.ModulusEquals */, ({ done, b, obj, context }) => {
618
- assignCheck(obj, context);
619
- done(undefined, (obj.context[obj.prop] %= b));
620
- });
621
- addOps(71 /* LispType.BitNegateEquals */, ({ done, b, obj, context }) => {
622
- assignCheck(obj, context);
623
- done(undefined, (obj.context[obj.prop] ^= b));
624
- });
625
- addOps(72 /* LispType.BitAndEquals */, ({ done, b, obj, context }) => {
626
- assignCheck(obj, context);
627
- done(undefined, (obj.context[obj.prop] &= b));
628
- });
629
- addOps(73 /* LispType.BitOrEquals */, ({ done, b, obj, context }) => {
630
- assignCheck(obj, context);
631
- done(undefined, (obj.context[obj.prop] |= b));
632
- });
633
- addOps(76 /* LispType.ShiftLeftEquals */, ({ done, b, obj, context }) => {
634
- assignCheck(obj, context);
635
- done(undefined, (obj.context[obj.prop] <<= b));
636
- });
637
- addOps(75 /* LispType.ShiftRightEquals */, ({ done, b, obj, context }) => {
638
- assignCheck(obj, context);
639
- done(undefined, (obj.context[obj.prop] >>= b));
640
- });
641
- addOps(74 /* LispType.UnsignedShiftRightEquals */, ({ done, b, obj, context }) => {
642
- assignCheck(obj, context);
643
- done(undefined, (obj.context[obj.prop] >>>= b));
644
- });
645
- addOps(90 /* LispType.AndEquals */, ({ done, b, obj, context }) => {
646
- var _a, _b;
647
- assignCheck(obj, context);
648
- done(undefined, ((_a = obj.context)[_b = obj.prop] && (_a[_b] = b)));
649
- });
650
- addOps(91 /* LispType.OrEquals */, ({ done, b, obj, context }) => {
651
- var _a, _b;
652
- assignCheck(obj, context);
653
- done(undefined, ((_a = obj.context)[_b = obj.prop] || (_a[_b] = b)));
654
- });
655
- addOps(92 /* LispType.NullishCoalescingEquals */, ({ done, b, obj, context }) => {
656
- var _a, _b;
657
- assignCheck(obj, context);
658
- done(undefined, ((_a = obj.context)[_b = obj.prop] ?? (_a[_b] = b)));
659
- });
660
- addOps(57 /* LispType.LargerThan */, ({ done, a, b }) => done(undefined, a > b));
661
- addOps(56 /* LispType.SmallerThan */, ({ done, a, b }) => done(undefined, a < b));
662
- addOps(55 /* LispType.LargerEqualThan */, ({ done, a, b }) => done(undefined, a >= b));
663
- addOps(54 /* LispType.SmallerEqualThan */, ({ done, a, b }) => done(undefined, a <= b));
664
- addOps(52 /* LispType.Equal */, ({ done, a, b }) => done(undefined, a == b));
665
- addOps(32 /* LispType.StrictEqual */, ({ done, a, b }) => done(undefined, a === b));
666
- addOps(53 /* LispType.NotEqual */, ({ done, a, b }) => done(undefined, a != b));
667
- addOps(31 /* LispType.StrictNotEqual */, ({ done, a, b }) => done(undefined, a !== b));
668
- addOps(29 /* LispType.And */, ({ done, a, b }) => done(undefined, a && b));
669
- addOps(30 /* LispType.Or */, ({ done, a, b }) => done(undefined, a || b));
670
- addOps(89 /* LispType.NullishCoalescing */, ({ done, a, b }) => done(undefined, a ?? b));
671
- addOps(77 /* LispType.BitAnd */, ({ done, a, b }) => done(undefined, a & b));
672
- addOps(78 /* LispType.BitOr */, ({ done, a, b }) => done(undefined, a | b));
673
- addOps(33 /* LispType.Plus */, ({ done, a, b }) => done(undefined, a + b));
674
- addOps(47 /* LispType.Minus */, ({ done, a, b }) => done(undefined, a - b));
675
- addOps(59 /* LispType.Positive */, ({ done, b }) => done(undefined, +b));
676
- addOps(58 /* LispType.Negative */, ({ done, b }) => done(undefined, -b));
677
- addOps(48 /* LispType.Divide */, ({ done, a, b }) => done(undefined, a / b));
678
- addOps(49 /* LispType.Power */, ({ done, a, b }) => done(undefined, a ** b));
679
- addOps(79 /* LispType.BitNegate */, ({ done, a, b }) => done(undefined, a ^ b));
680
- addOps(50 /* LispType.Multiply */, ({ done, a, b }) => done(undefined, a * b));
681
- addOps(51 /* LispType.Modulus */, ({ done, a, b }) => done(undefined, a % b));
682
- addOps(80 /* LispType.BitShiftLeft */, ({ done, a, b }) => done(undefined, a << b));
683
- addOps(81 /* LispType.BitShiftRight */, ({ done, a, b }) => done(undefined, a >> b));
684
- addOps(82 /* LispType.BitUnsignedShiftRight */, ({ done, a, b }) => done(undefined, a >>> b));
685
- addOps(60 /* LispType.Typeof */, ({ exec, done, ticks, b, context, scope }) => {
686
- exec(ticks, b, scope, context, (e, prop) => {
687
- done(undefined, typeof valueOrProp(prop, context));
688
- });
689
- });
690
- addOps(62 /* LispType.Instanceof */, ({ done, a, b }) => done(undefined, a instanceof b));
691
- addOps(63 /* LispType.In */, ({ done, a, b }) => done(undefined, a in b));
692
- addOps(61 /* LispType.Delete */, ({ done, context, bobj }) => {
693
- if (!(bobj instanceof Prop)) {
694
- done(undefined, true);
695
- return;
696
- }
697
- assignCheck(bobj, context, 'delete');
698
- if (bobj.isVariable) {
699
- done(undefined, false);
700
- return;
701
- }
702
- done(undefined, delete bobj.context?.[bobj.prop]);
703
- });
704
- addOps(8 /* LispType.Return */, ({ done, b }) => done(undefined, b));
705
- addOps(34 /* LispType.Var */, ({ done, a, b, scope, bobj }) => {
706
- done(undefined, scope.declare(a, "var" /* VarType.var */, b, bobj?.isGlobal || false));
707
- });
708
- addOps(3 /* LispType.Let */, ({ done, a, b, scope, bobj }) => {
709
- done(undefined, scope.declare(a, "let" /* VarType.let */, b, bobj?.isGlobal || false));
710
- });
711
- addOps(4 /* LispType.Const */, ({ done, a, b, scope, bobj }) => {
712
- done(undefined, scope.declare(a, "const" /* VarType.const */, b, bobj?.isGlobal || false));
713
- });
714
- addOps(11 /* LispType.ArrowFunction */, ({ done, ticks, a, b, obj, context, scope }) => {
715
- a = [...a];
716
- if (typeof obj[2] === 'string' || obj[2] instanceof CodeString) {
717
- if (context.allowJit && context.evalContext) {
718
- obj[2] = b = context.evalContext.lispifyFunction(new CodeString(obj[2]), context.constants);
719
- }
720
- else {
721
- throw new SandboxCapabilityError('Unevaluated code detected, JIT not allowed');
722
- }
723
- }
724
- if (a.shift()) {
725
- done(undefined, createFunctionAsync(a, b, ticks, context, scope));
726
- }
727
- else {
728
- done(undefined, createFunction(a, b, ticks, context, scope));
729
- }
730
- });
731
- addOps(37 /* LispType.Function */, ({ done, ticks, a, b, obj, context, scope }) => {
732
- if (typeof obj[2] === 'string' || obj[2] instanceof CodeString) {
733
- if (context.allowJit && context.evalContext) {
734
- obj[2] = b = context.evalContext.lispifyFunction(new CodeString(obj[2]), context.constants);
735
- }
736
- else {
737
- throw new SandboxCapabilityError('Unevaluated code detected, JIT not allowed');
738
- }
739
- }
740
- const isAsync = a.shift();
741
- const name = a.shift();
742
- let func;
743
- if (isAsync === 88 /* LispType.True */) {
744
- func = createFunctionAsync(a, b, ticks, context, scope, name);
745
- }
746
- else {
747
- func = createFunction(a, b, ticks, context, scope, name);
748
- }
749
- if (name) {
750
- scope.declare(name, "var" /* VarType.var */, func);
751
- }
752
- done(undefined, func);
753
- });
754
- addOps(10 /* LispType.InlineFunction */, ({ done, ticks, a, b, obj, context, scope }) => {
755
- if (typeof obj[2] === 'string' || obj[2] instanceof CodeString) {
756
- if (context.allowJit && context.evalContext) {
757
- obj[2] = b = context.evalContext.lispifyFunction(new CodeString(obj[2]), context.constants);
758
- }
759
- else {
760
- throw new SandboxCapabilityError('Unevaluated code detected, JIT not allowed');
761
- }
762
- }
763
- const isAsync = a.shift();
764
- const name = a.shift();
765
- if (name) {
766
- scope = new Scope(scope, {});
767
- }
768
- let func;
769
- if (isAsync === 88 /* LispType.True */) {
770
- func = createFunctionAsync(a, b, ticks, context, scope, name);
771
- }
772
- else {
773
- func = createFunction(a, b, ticks, context, scope, name);
774
- }
775
- if (name) {
776
- scope.declare(name, "let" /* VarType.let */, func);
777
- }
778
- done(undefined, func);
779
- });
780
- addOps(38 /* LispType.Loop */, ({ exec, done, ticks, a, b, context, scope }) => {
781
- const [checkFirst, startInternal, getIterator, startStep, step, condition, beforeStep] = a;
782
- let loop = true;
783
- const loopScope = new Scope(scope, {});
784
- const internalVars = {
785
- $$obj: undefined,
786
- };
787
- const interalScope = new Scope(loopScope, internalVars);
788
- if (exec === execAsync) {
789
- (async () => {
790
- let ad;
791
- ad = asyncDone((d) => exec(ticks, startStep, loopScope, context, d));
792
- internalVars['$$obj'] =
793
- (ad = asyncDone((d) => exec(ticks, getIterator, loopScope, context, d))).isInstant === true
794
- ? ad.instant
795
- : (await ad.p).result;
796
- ad = asyncDone((d) => exec(ticks, startInternal, interalScope, context, d));
797
- if (checkFirst)
798
- loop =
799
- (ad = asyncDone((d) => exec(ticks, condition, interalScope, context, d))).isInstant ===
800
- true
801
- ? ad.instant
802
- : (await ad.p).result;
803
- while (loop) {
804
- const innerLoopVars = {};
805
- ad = asyncDone((d) => exec(ticks, beforeStep, new Scope(interalScope, innerLoopVars), context, d));
806
- ad.isInstant === true ? ad.instant : (await ad.p).result;
807
- const res = await executeTreeAsync(ticks, context, b, [new Scope(loopScope, innerLoopVars)], 'loop');
808
- if (res instanceof ExecReturn && res.returned) {
809
- done(undefined, res);
810
- return;
811
- }
812
- if (res instanceof ExecReturn && res.breakLoop) {
813
- break;
814
- }
815
- ad = asyncDone((d) => exec(ticks, step, interalScope, context, d));
816
- loop =
817
- (ad = asyncDone((d) => exec(ticks, condition, interalScope, context, d))).isInstant ===
818
- true
819
- ? ad.instant
820
- : (await ad.p).result;
821
- }
822
- done();
823
- })().catch(done);
824
- }
825
- else {
826
- syncDone((d) => exec(ticks, startStep, loopScope, context, d));
827
- internalVars['$$obj'] = syncDone((d) => exec(ticks, getIterator, loopScope, context, d)).result;
828
- syncDone((d) => exec(ticks, startInternal, interalScope, context, d));
829
- if (checkFirst)
830
- loop = syncDone((d) => exec(ticks, condition, interalScope, context, d)).result;
831
- while (loop) {
832
- const innerLoopVars = {};
833
- syncDone((d) => exec(ticks, beforeStep, new Scope(interalScope, innerLoopVars), context, d));
834
- const res = executeTree(ticks, context, b, [new Scope(loopScope, innerLoopVars)], 'loop');
835
- if (res instanceof ExecReturn && res.returned) {
836
- done(undefined, res);
837
- return;
838
- }
839
- if (res instanceof ExecReturn && res.breakLoop) {
840
- break;
841
- }
842
- syncDone((d) => exec(ticks, step, interalScope, context, d));
843
- loop = syncDone((d) => exec(ticks, condition, interalScope, context, d)).result;
844
- }
845
- done();
846
- }
847
- });
848
- addOps(86 /* LispType.LoopAction */, ({ done, a, context, inLoopOrSwitch }) => {
849
- if ((inLoopOrSwitch === 'switch' && a === 'continue') || !inLoopOrSwitch) {
850
- throw new TypeError('Illegal ' + a + ' statement');
851
- }
852
- done(undefined, new ExecReturn(context.ctx.auditReport, undefined, false, a === 'break', a === 'continue'));
853
- });
854
- addOps(13 /* LispType.If */, ({ exec, done, ticks, a, b, context, scope, inLoopOrSwitch }) => {
855
- exec(ticks, valueOrProp(a, context) ? b.t : b.f, scope, context, done, inLoopOrSwitch);
856
- });
857
- addOps(15 /* LispType.InlineIf */, ({ exec, done, ticks, a, b, context, scope }) => {
858
- exec(ticks, valueOrProp(a, context) ? b.t : b.f, scope, context, done, undefined);
859
- });
860
- addOps(16 /* LispType.InlineIfCase */, ({ done, a, b }) => done(undefined, new If(a, b)));
861
- addOps(14 /* LispType.IfCase */, ({ done, a, b }) => done(undefined, new If(a, b)));
862
- addOps(40 /* LispType.Switch */, ({ exec, done, ticks, a, b, context, scope }) => {
863
- exec(ticks, a, scope, context, (...args) => {
864
- if (args.length === 1) {
865
- done(args[0]);
866
- return;
867
- }
868
- let toTest = args[1];
869
- toTest = valueOrProp(toTest, context);
870
- if (exec === execSync) {
871
- let res;
872
- let isTrue = false;
873
- for (const caseItem of b) {
874
- if (isTrue ||
875
- (isTrue =
876
- !caseItem[1] ||
877
- toTest ===
878
- valueOrProp(syncDone((d) => exec(ticks, caseItem[1], scope, context, d)).result, context))) {
879
- if (!caseItem[2])
880
- continue;
881
- res = executeTree(ticks, context, caseItem[2], [scope], 'switch');
882
- if (res.breakLoop)
883
- break;
884
- if (res.returned) {
885
- done(undefined, res);
886
- return;
887
- }
888
- if (!caseItem[1]) {
889
- // default case
890
- break;
891
- }
892
- }
893
- }
894
- done();
895
- }
896
- else {
897
- (async () => {
898
- let res;
899
- let isTrue = false;
900
- for (const caseItem of b) {
901
- let ad;
902
- if (isTrue ||
903
- (isTrue =
904
- !caseItem[1] ||
905
- toTest ===
906
- valueOrProp((ad = asyncDone((d) => exec(ticks, caseItem[1], scope, context, d))).isInstant ===
907
- true
908
- ? ad.instant
909
- : (await ad.p).result, context))) {
910
- if (!caseItem[2])
911
- continue;
912
- res = await executeTreeAsync(ticks, context, caseItem[2], [scope], 'switch');
913
- if (res.breakLoop)
914
- break;
915
- if (res.returned) {
916
- done(undefined, res);
917
- return;
918
- }
919
- if (!caseItem[1]) {
920
- // default case
921
- break;
922
- }
923
- }
924
- }
925
- done();
926
- })().catch(done);
927
- }
928
- });
929
- });
930
- addOps(39 /* LispType.Try */, ({ exec, done, ticks, a, b, context, scope, inLoopOrSwitch }) => {
931
- const [exception, catchBody, finallyBody] = b;
932
- // Execute try block
933
- executeTreeWithDone(exec, (...tryArgs) => {
934
- const tryHadError = tryArgs.length === 1;
935
- const tryError = tryHadError ? tryArgs[0] : undefined;
936
- const tryResult = !tryHadError && tryArgs.length > 1 ? tryArgs[1] : undefined;
937
- // Handler to execute finally and complete
938
- const executeFinallyAndComplete = (hadError, errorOrResult) => {
939
- if (finallyBody && finallyBody.length > 0) {
940
- // Execute finally block
941
- executeTreeWithDone(exec, (...finallyArgs) => {
942
- const finallyHadError = finallyArgs.length === 1;
943
- const finallyResult = !finallyHadError && finallyArgs.length > 1 ? finallyArgs[1] : undefined;
944
- // If finally throws an error, it overrides everything
945
- if (finallyHadError) {
946
- done(finallyArgs[0]);
947
- return;
948
- }
949
- // If finally has a control flow statement (return/break/continue), it overrides everything
950
- if (finallyResult instanceof ExecReturn &&
951
- (finallyResult.returned || finallyResult.breakLoop || finallyResult.continueLoop)) {
952
- done(undefined, finallyResult);
953
- return;
954
- }
955
- // Otherwise, return the original try/catch result/error
956
- if (hadError) {
957
- done(errorOrResult);
958
- }
959
- else if (errorOrResult instanceof ExecReturn) {
960
- // If try/catch returned or has some other control flow, pass that through
961
- if (errorOrResult.returned ||
962
- errorOrResult.breakLoop ||
963
- errorOrResult.continueLoop) {
964
- done(undefined, errorOrResult);
965
- }
966
- else {
967
- // Normal completion - don't return a value
968
- done();
969
- }
970
- }
971
- else {
972
- // Try/catch completed normally, just signal completion with no return value
973
- done();
974
- }
975
- }, ticks, context, finallyBody, [new Scope(scope, {})], inLoopOrSwitch);
976
- }
977
- else {
978
- // No finally block, just return result/error
979
- if (hadError) {
980
- done(errorOrResult);
981
- }
982
- else if (errorOrResult instanceof ExecReturn) {
983
- // If try/catch returned or has some other control flow, pass that through
984
- if (errorOrResult.returned || errorOrResult.breakLoop || errorOrResult.continueLoop) {
985
- done(undefined, errorOrResult);
986
- }
987
- else {
988
- // Normal completion - don't return a value
989
- done();
990
- }
991
- }
992
- else {
993
- done();
994
- }
995
- }
996
- };
997
- // If try had an error and there's a catch block, execute catch
998
- if (tryHadError && catchBody && catchBody.length > 0) {
999
- const sc = {};
1000
- if (exception)
1001
- sc[exception] = tryError;
1002
- executeTreeWithDone(exec, (...catchArgs) => {
1003
- const catchHadError = catchArgs.length === 1;
1004
- const catchErrorOrResult = catchHadError
1005
- ? catchArgs[0]
1006
- : catchArgs.length > 1
1007
- ? catchArgs[1]
1008
- : undefined;
1009
- // Execute finally with catch result
1010
- executeFinallyAndComplete(catchHadError, catchErrorOrResult);
1011
- }, ticks, context, catchBody, [new Scope(scope, sc)], inLoopOrSwitch);
1012
- }
1013
- else {
1014
- // No catch or no error, execute finally with try result
1015
- executeFinallyAndComplete(tryHadError, tryHadError ? tryError : tryResult);
1016
- }
1017
- }, ticks, context, a, [new Scope(scope)], inLoopOrSwitch);
1018
- });
1019
- addOps(87 /* LispType.Void */, ({ done }) => {
1020
- done();
1021
- });
1022
- addOps(45 /* LispType.New */, ({ done, a, b, context }) => {
1023
- if (!context.ctx.globalsWhitelist.has(a) && !context.ctx.sandboxedFunctions.has(a)) {
1024
- throw new SandboxAccessError(`Object construction not allowed: ${a.constructor.name}`);
1025
- }
1026
- done(undefined, new a(...b));
1027
- });
1028
- addOps(46 /* LispType.Throw */, ({ done, b }) => {
1029
- done(b);
1030
- });
1031
- addOps(43 /* LispType.Expression */, ({ done, a }) => done(undefined, a.pop()));
1032
- addOps(0 /* LispType.None */, ({ done }) => done());
1033
- function valueOrProp(a, context) {
1034
- if (a instanceof Prop)
1035
- return a.get(context);
1036
- if (a === optional)
1037
- return undefined;
1038
- return a;
1039
- }
1040
- function execMany(ticks, exec, tree, done, scope, context, inLoopOrSwitch) {
1041
- if (exec === execSync) {
1042
- _execManySync(ticks, tree, done, scope, context, inLoopOrSwitch);
1043
- }
1044
- else {
1045
- _execManyAsync(ticks, tree, done, scope, context, inLoopOrSwitch).catch(done);
1046
- }
1047
- }
1048
- function _execManySync(ticks, tree, done, scope, context, inLoopOrSwitch) {
1049
- const ret = [];
1050
- for (let i = 0; i < tree.length; i++) {
1051
- let res = syncDone((d) => execSync(ticks, tree[i], scope, context, d, inLoopOrSwitch)).result;
1052
- if (res instanceof ExecReturn && (res.returned || res.breakLoop || res.continueLoop)) {
1053
- done(undefined, res);
1054
- return;
1055
- }
1056
- if (isLisp(tree[i]) && tree[i][0] === 8 /* LispType.Return */) {
1057
- done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
1058
- return;
1059
- }
1060
- ret.push(res);
1061
- }
1062
- done(undefined, ret);
1063
- }
1064
- async function _execManyAsync(ticks, tree, done, scope, context, inLoopOrSwitch) {
1065
- const ret = [];
1066
- for (let i = 0; i < tree.length; i++) {
1067
- let res;
1068
- try {
1069
- let ad;
1070
- res =
1071
- (ad = asyncDone((d) => execAsync(ticks, tree[i], scope, context, d, inLoopOrSwitch)))
1072
- .isInstant === true
1073
- ? ad.instant
1074
- : (await ad.p).result;
1075
- }
1076
- catch (e) {
1077
- done(e);
1078
- return;
1079
- }
1080
- if (res instanceof ExecReturn && (res.returned || res.breakLoop || res.continueLoop)) {
1081
- done(undefined, res);
1082
- return;
1083
- }
1084
- if (isLisp(tree[i]) && tree[i][0] === 8 /* LispType.Return */) {
1085
- done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
1086
- return;
1087
- }
1088
- ret.push(res);
1089
- }
1090
- done(undefined, ret);
1091
- }
1092
- function asyncDone(callback) {
1093
- let isInstant = false;
1094
- let instant;
1095
- const p = new Promise((resolve, reject) => {
1096
- callback((...args) => {
1097
- if (args.length === 1)
1098
- reject(args[0]);
1099
- else {
1100
- isInstant = true;
1101
- instant = args[1];
1102
- resolve({ result: args[1] });
1103
- }
1104
- });
1105
- });
1106
- return {
1107
- isInstant,
1108
- instant,
1109
- p,
1110
- };
1111
- }
1112
- function syncDone(callback) {
1113
- let result;
1114
- let err;
1115
- callback((...args) => {
1116
- err = args.length === 1 ? { error: args[0] } : undefined;
1117
- result = args[1];
1118
- });
1119
- if (err)
1120
- throw err.error;
1121
- return { result };
1122
- }
1123
- async function execAsync(ticks, tree, scope, context, doneOriginal, inLoopOrSwitch) {
1124
- let done = doneOriginal;
1125
- const p = new Promise((resolve) => {
1126
- done = (...args) => {
1127
- doneOriginal(...args);
1128
- resolve();
1129
- };
1130
- });
1131
- if (!_execNoneRecurse(ticks, tree, scope, context, done, true, inLoopOrSwitch) && isLisp(tree)) {
1132
- let op = tree[0];
1133
- let obj;
1134
- try {
1135
- let ad;
1136
- obj =
1137
- (ad = asyncDone((d) => execAsync(ticks, tree[1], scope, context, d, inLoopOrSwitch)))
1138
- .isInstant === true
1139
- ? ad.instant
1140
- : (await ad.p).result;
1141
- }
1142
- catch (e) {
1143
- done(e);
1144
- return;
1145
- }
1146
- let a = obj;
1147
- try {
1148
- a = obj instanceof Prop ? obj.get(context) : obj;
1149
- }
1150
- catch (e) {
1151
- done(e);
1152
- return;
1153
- }
1154
- if (op === 20 /* LispType.PropOptional */ || op === 21 /* LispType.CallOptional */) {
1155
- if (a === undefined || a === null) {
1156
- done(undefined, optional);
1157
- return;
1158
- }
1159
- op = op === 20 /* LispType.PropOptional */ ? 1 /* LispType.Prop */ : 5 /* LispType.Call */;
1160
- }
1161
- if (a === optional) {
1162
- if (op === 1 /* LispType.Prop */ || op === 5 /* LispType.Call */) {
1163
- done(undefined, a);
1164
- return;
1165
- }
1166
- else {
1167
- a = undefined;
1168
- }
1169
- }
1170
- // Short-circuit for nullish coalescing: if a is not null/undefined, return a without evaluating b
1171
- if (op === 89 /* LispType.NullishCoalescing */ && a !== undefined && a !== null) {
1172
- done(undefined, a);
1173
- return;
1174
- }
1175
- let bobj;
1176
- try {
1177
- let ad;
1178
- bobj =
1179
- (ad = asyncDone((d) => execAsync(ticks, tree[2], scope, context, d, inLoopOrSwitch)))
1180
- .isInstant === true
1181
- ? ad.instant
1182
- : (await ad.p).result;
1183
- }
1184
- catch (e) {
1185
- done(e);
1186
- return;
1187
- }
1188
- let b = bobj;
1189
- try {
1190
- b = bobj instanceof Prop ? bobj.get(context) : bobj;
1191
- }
1192
- catch (e) {
1193
- done(e);
1194
- return;
1195
- }
1196
- if (b === optional) {
1197
- b = undefined;
1198
- }
1199
- performOp({
1200
- op,
1201
- exec: execAsync,
1202
- done,
1203
- ticks,
1204
- a,
1205
- b,
1206
- obj,
1207
- context,
1208
- scope,
1209
- bobj,
1210
- inLoopOrSwitch,
1211
- tree,
1212
- });
1213
- }
1214
- await p;
1215
- }
1216
- function execSync(ticks, tree, scope, context, done, inLoopOrSwitch) {
1217
- if (!_execNoneRecurse(ticks, tree, scope, context, done, false, inLoopOrSwitch) && isLisp(tree)) {
1218
- let op = tree[0];
1219
- let obj = syncDone((d) => execSync(ticks, tree[1], scope, context, d, inLoopOrSwitch)).result;
1220
- let a = obj instanceof Prop ? obj.get(context) : obj;
1221
- if (op === 20 /* LispType.PropOptional */ || op === 21 /* LispType.CallOptional */) {
1222
- if (a === undefined || a === null) {
1223
- done(undefined, optional);
1224
- return;
1225
- }
1226
- op = op === 20 /* LispType.PropOptional */ ? 1 /* LispType.Prop */ : 5 /* LispType.Call */;
1227
- }
1228
- if (a === optional) {
1229
- if (op === 1 /* LispType.Prop */ || op === 5 /* LispType.Call */) {
1230
- done(undefined, a);
1231
- return;
1232
- }
1233
- else {
1234
- a = undefined;
1235
- }
1236
- }
1237
- // Short-circuit for nullish coalescing: if a is not null/undefined, return a without evaluating b
1238
- if (op === 89 /* LispType.NullishCoalescing */ && a !== undefined && a !== null) {
1239
- done(undefined, a);
1240
- return;
1241
- }
1242
- let bobj = syncDone((d) => execSync(ticks, tree[2], scope, context, d, inLoopOrSwitch)).result;
1243
- let b = bobj instanceof Prop ? bobj.get(context) : bobj;
1244
- if (b === optional) {
1245
- b = undefined;
1246
- }
1247
- performOp({
1248
- op,
1249
- exec: execSync,
1250
- done,
1251
- ticks,
1252
- a,
1253
- b,
1254
- obj,
1255
- context,
1256
- scope,
1257
- bobj,
1258
- inLoopOrSwitch,
1259
- tree,
1260
- });
1261
- }
1262
- }
1263
- function checkHaltExpectedTicks(params, expectTicks = 0) {
1264
- const sandbox = params.context.ctx.sandbox;
1265
- const options = params.context.ctx.options;
1266
- const { ticks, scope, context, done, op } = params;
1267
- if (sandbox.halted) {
1268
- const sub = sandbox.subscribeResume(() => {
1269
- sub.unsubscribe();
1270
- try {
1271
- const o = ops.get(op);
1272
- if (!o) {
1273
- done(new SyntaxError('Unknown operator: ' + op));
1274
- return;
1275
- }
1276
- o(params);
1277
- }
1278
- catch (err) {
1279
- if (options.haltOnSandboxError && err instanceof SandboxError) {
1280
- const sub = sandbox.subscribeResume(() => {
1281
- sub.unsubscribe();
1282
- done(err);
1283
- });
1284
- sandbox.haltExecution({
1285
- error: err,
1286
- ticks,
1287
- scope,
1288
- context,
1289
- });
1290
- }
1291
- else {
1292
- done(err);
1293
- }
1294
- }
1295
- });
1296
- return true;
1297
- }
1298
- else if (ticks.tickLimit && ticks.tickLimit <= ticks.ticks + BigInt(expectTicks)) {
1299
- const sub = sandbox.subscribeResume(() => {
1300
- sub.unsubscribe();
1301
- try {
1302
- const o = ops.get(op);
1303
- if (!o) {
1304
- done(new SyntaxError('Unknown operator: ' + op));
1305
- return;
1306
- }
1307
- o(params);
1308
- }
1309
- catch (err) {
1310
- if (context.ctx.options.haltOnSandboxError && err instanceof SandboxError) {
1311
- const sub = sandbox.subscribeResume(() => {
1312
- sub.unsubscribe();
1313
- done(err);
1314
- });
1315
- sandbox.haltExecution({
1316
- error: err,
1317
- ticks,
1318
- scope,
1319
- context,
1320
- });
1321
- }
1322
- else {
1323
- done(err);
1324
- }
1325
- }
1326
- });
1327
- const error = new SandboxExecutionQuotaExceededError('Execution quota exceeded');
1328
- sandbox.haltExecution({
1329
- error,
1330
- ticks,
1331
- scope: scope,
1332
- context,
1333
- });
1334
- return true;
1335
- }
1336
- return false;
1337
- }
1338
- function performOp(params) {
1339
- const { done, op, ticks, context, scope } = params;
1340
- ticks.ticks++;
1341
- const sandbox = context.ctx.sandbox;
1342
- if (checkHaltExpectedTicks(params)) {
1343
- return;
1344
- }
1345
- try {
1346
- const o = ops.get(op);
1347
- if (!o) {
1348
- done(new SandboxExecutionTreeError('Unknown operator: ' + op));
1349
- return;
1350
- }
1351
- o(params);
1352
- }
1353
- catch (err) {
1354
- if (context.ctx.options.haltOnSandboxError && err instanceof SandboxError) {
1355
- const sub = sandbox.subscribeResume(() => {
1356
- sub.unsubscribe();
1357
- done(err);
1358
- });
1359
- sandbox.haltExecution({
1360
- error: err,
1361
- ticks,
1362
- scope,
1363
- context,
1364
- });
1365
- }
1366
- else {
1367
- done(err);
1368
- }
1369
- }
1370
- }
1371
- const unexecTypes = new Set([
1372
- 11 /* LispType.ArrowFunction */,
1373
- 37 /* LispType.Function */,
1374
- 10 /* LispType.InlineFunction */,
1375
- 38 /* LispType.Loop */,
1376
- 39 /* LispType.Try */,
1377
- 40 /* LispType.Switch */,
1378
- 14 /* LispType.IfCase */,
1379
- 16 /* LispType.InlineIfCase */,
1380
- 60 /* LispType.Typeof */,
1381
- ]);
1382
- function _execNoneRecurse(ticks, tree, scope, context, done, isAsync, inLoopOrSwitch) {
1383
- const exec = isAsync ? execAsync : execSync;
1384
- if (tree instanceof Prop) {
1385
- done(undefined, tree.get(context));
1386
- }
1387
- else if (tree === optional) {
1388
- done();
1389
- }
1390
- else if (Array.isArray(tree) && !isLisp(tree)) {
1391
- if (tree[0] === 0 /* LispType.None */) {
1392
- done();
1393
- }
1394
- else {
1395
- execMany(ticks, exec, tree, done, scope, context, inLoopOrSwitch);
1396
- }
1397
- }
1398
- else if (!isLisp(tree)) {
1399
- done(undefined, tree);
1400
- }
1401
- else if (tree[0] === 42 /* LispType.Block */) {
1402
- execMany(ticks, exec, tree[1], done, scope, context, inLoopOrSwitch);
1403
- }
1404
- else if (tree[0] === 44 /* LispType.Await */) {
1405
- if (!isAsync) {
1406
- done(new SyntaxError("Illegal use of 'await', must be inside async function"));
1407
- }
1408
- else if (context.ctx.prototypeWhitelist?.has(Promise.prototype)) {
1409
- execAsync(ticks, tree[1], scope, context, async (...args) => {
1410
- if (args.length === 1)
1411
- done(args[0]);
1412
- else
1413
- try {
1414
- done(undefined, (await valueOrProp(args[1], context)));
1415
- }
1416
- catch (err) {
1417
- done(err);
1418
- }
1419
- }, inLoopOrSwitch).catch(done);
1420
- }
1421
- else {
1422
- done(new SandboxCapabilityError('Async/await is not permitted'));
1423
- }
1424
- }
1425
- else if (unexecTypes.has(tree[0])) {
1426
- performOp({
1427
- op: tree[0],
1428
- exec,
1429
- done,
1430
- ticks,
1431
- a: tree[1],
1432
- b: tree[2],
1433
- obj: tree,
1434
- tree,
1435
- context,
1436
- scope,
1437
- bobj: undefined,
1438
- inLoopOrSwitch,
1439
- });
1440
- }
1441
- else {
1442
- return false;
1443
- }
1444
- return true;
1445
- }
1446
- function executeTree(ticks, context, executionTree, scopes = [], inLoopOrSwitch) {
1447
- return syncDone((done) => executeTreeWithDone(execSync, done, ticks, context, executionTree, scopes, inLoopOrSwitch)).result;
1448
- }
1449
- async function executeTreeAsync(ticks, context, executionTree, scopes = [], inLoopOrSwitch) {
1450
- let ad;
1451
- return (ad = asyncDone((done) => executeTreeWithDone(execAsync, done, ticks, context, executionTree, scopes, inLoopOrSwitch))).isInstant === true
1452
- ? ad.instant
1453
- : (await ad.p).result;
1454
- }
1455
- function executeTreeWithDone(exec, done, ticks, context, executionTree, scopes = [], inLoopOrSwitch) {
1456
- if (!executionTree) {
1457
- done();
1458
- return;
1459
- }
1460
- if (!(executionTree instanceof Array)) {
1461
- throw new SyntaxError('Bad execution tree');
1462
- }
1463
- let scope = context.ctx.globalScope;
1464
- let s;
1465
- while ((s = scopes.shift())) {
1466
- if (typeof s !== 'object')
1467
- continue;
1468
- if (s instanceof Scope) {
1469
- scope = s;
1470
- }
1471
- else {
1472
- scope = new Scope(scope, s, s instanceof LocalScope ? undefined : null);
1473
- }
1474
- }
1475
- if (context.ctx.options.audit && !context.ctx.auditReport) {
1476
- context.ctx.auditReport = {
1477
- globalsAccess: new Set(),
1478
- prototypeAccess: {},
1479
- };
1480
- }
1481
- if (exec === execSync) {
1482
- _executeWithDoneSync(done, ticks, context, executionTree, scope, inLoopOrSwitch);
1483
- }
1484
- else {
1485
- _executeWithDoneAsync(done, ticks, context, executionTree, scope, inLoopOrSwitch).catch(done);
1486
- }
1487
- }
1488
- function _executeWithDoneSync(done, ticks, context, executionTree, scope, inLoopOrSwitch) {
1489
- if (!(executionTree instanceof Array))
1490
- throw new SyntaxError('Bad execution tree');
1491
- let i = 0;
1492
- for (i = 0; i < executionTree.length; i++) {
1493
- let res;
1494
- let err;
1495
- const current = executionTree[i];
1496
- try {
1497
- execSync(ticks, current, scope, context, (...args) => {
1498
- if (args.length === 1)
1499
- err = { error: args[0] };
1500
- else
1501
- res = args[1];
1502
- }, inLoopOrSwitch);
1503
- }
1504
- catch (e) {
1505
- err = { error: e };
1506
- }
1507
- if (err) {
1508
- done(err.error);
1509
- return;
1510
- }
1511
- if (res instanceof ExecReturn) {
1512
- done(undefined, res);
1513
- return;
1514
- }
1515
- if (isLisp(current) && current[0] === 8 /* LispType.Return */) {
1516
- done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
1517
- return;
1518
- }
1519
- }
1520
- done(undefined, new ExecReturn(context.ctx.auditReport, undefined, false));
1521
- }
1522
- async function _executeWithDoneAsync(done, ticks, context, executionTree, scope, inLoopOrSwitch) {
1523
- if (!(executionTree instanceof Array))
1524
- throw new SyntaxError('Bad execution tree');
1525
- let i = 0;
1526
- for (i = 0; i < executionTree.length; i++) {
1527
- let res;
1528
- let err;
1529
- const current = executionTree[i];
1530
- try {
1531
- await execAsync(ticks, current, scope, context, (...args) => {
1532
- if (args.length === 1)
1533
- err = { error: args[0] };
1534
- else
1535
- res = args[1];
1536
- }, inLoopOrSwitch);
1537
- }
1538
- catch (e) {
1539
- err = { error: e };
1540
- }
1541
- if (err) {
1542
- done(err.error);
1543
- return;
1544
- }
1545
- if (res instanceof ExecReturn) {
1546
- done(undefined, res);
1547
- return;
1548
- }
1549
- if (isLisp(current) && current[0] === 8 /* LispType.Return */) {
1550
- done(undefined, new ExecReturn(context.ctx.auditReport, res, true));
1551
- return;
1552
- }
1553
- }
1554
- done(undefined, new ExecReturn(context.ctx.auditReport, undefined, false));
1555
- }
1556
-
1557
- export { ExecReturn, If, KeyVal, SpreadArray, SpreadObject, addOps, assignCheck, asyncDone, createFunction, createFunctionAsync, execAsync, execMany, execSync, executeTree, executeTreeAsync, ops, syncDone };
1558
- //# sourceMappingURL=executor.js.map