@cuxt/sandboxjs 0.1.1 → 0.1.4

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