greybel-interpreter 1.3.6 → 1.3.8

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 (99) hide show
  1. package/.editorconfig +14 -14
  2. package/.eslintrc +47 -47
  3. package/LICENSE +21 -21
  4. package/README.md +2 -2
  5. package/dist/context.d.ts +109 -109
  6. package/dist/context.js +383 -382
  7. package/dist/cps.d.ts +14 -14
  8. package/dist/cps.js +213 -213
  9. package/dist/handler/error.d.ts +6 -6
  10. package/dist/handler/error.js +35 -35
  11. package/dist/handler/output.d.ts +18 -18
  12. package/dist/handler/output.js +52 -52
  13. package/dist/handler/resource.d.ts +12 -12
  14. package/dist/handler/resource.js +51 -51
  15. package/dist/handler-container.d.ts +14 -14
  16. package/dist/handler-container.js +16 -16
  17. package/dist/index.d.ts +45 -45
  18. package/dist/index.js +131 -131
  19. package/dist/interpreter.d.ts +41 -41
  20. package/dist/interpreter.js +312 -312
  21. package/dist/intrinsics-container.d.ts +7 -7
  22. package/dist/intrinsics-container.js +19 -19
  23. package/dist/operations/assign.d.ts +13 -13
  24. package/dist/operations/assign.js +111 -111
  25. package/dist/operations/block.d.ts +12 -12
  26. package/dist/operations/block.js +108 -108
  27. package/dist/operations/break.d.ts +10 -10
  28. package/dist/operations/break.js +41 -41
  29. package/dist/operations/call.d.ts +13 -13
  30. package/dist/operations/call.js +124 -124
  31. package/dist/operations/chunk.d.ts +12 -12
  32. package/dist/operations/chunk.js +85 -85
  33. package/dist/operations/continue.d.ts +10 -10
  34. package/dist/operations/continue.js +41 -41
  35. package/dist/operations/debugger-statement.d.ts +10 -10
  36. package/dist/operations/debugger-statement.js +39 -39
  37. package/dist/operations/evaluate.d.ts +41 -39
  38. package/dist/operations/evaluate.js +433 -367
  39. package/dist/operations/for.d.ts +14 -14
  40. package/dist/operations/for.js +152 -152
  41. package/dist/operations/function-reference.d.ts +12 -12
  42. package/dist/operations/function-reference.js +103 -103
  43. package/dist/operations/function.d.ts +14 -14
  44. package/dist/operations/function.js +200 -200
  45. package/dist/operations/if-statement.d.ts +19 -19
  46. package/dist/operations/if-statement.js +181 -181
  47. package/dist/operations/import.d.ts +17 -17
  48. package/dist/operations/import.js +119 -119
  49. package/dist/operations/include.d.ts +13 -13
  50. package/dist/operations/include.js +95 -95
  51. package/dist/operations/list.d.ts +11 -11
  52. package/dist/operations/list.js +111 -111
  53. package/dist/operations/literal.d.ts +11 -11
  54. package/dist/operations/literal.js +58 -58
  55. package/dist/operations/map.d.ts +11 -11
  56. package/dist/operations/map.js +171 -171
  57. package/dist/operations/negated-binary.d.ts +11 -11
  58. package/dist/operations/negated-binary.js +109 -109
  59. package/dist/operations/new-instance.d.ts +11 -11
  60. package/dist/operations/new-instance.js +100 -100
  61. package/dist/operations/noop.d.ts +9 -9
  62. package/dist/operations/noop.js +36 -36
  63. package/dist/operations/not.d.ts +11 -11
  64. package/dist/operations/not.js +96 -96
  65. package/dist/operations/operation.d.ts +13 -13
  66. package/dist/operations/operation.js +11 -11
  67. package/dist/operations/reference.d.ts +9 -9
  68. package/dist/operations/reference.js +37 -37
  69. package/dist/operations/resolve.d.ts +38 -38
  70. package/dist/operations/resolve.js +330 -330
  71. package/dist/operations/return.d.ts +11 -11
  72. package/dist/operations/return.js +107 -107
  73. package/dist/operations/while.d.ts +13 -13
  74. package/dist/operations/while.js +136 -136
  75. package/dist/types/boolean.d.ts +11 -11
  76. package/dist/types/boolean.js +46 -46
  77. package/dist/types/default.d.ts +11 -11
  78. package/dist/types/default.js +20 -20
  79. package/dist/types/function.d.ts +32 -32
  80. package/dist/types/function.js +174 -174
  81. package/dist/types/generics.d.ts +18 -18
  82. package/dist/types/generics.js +40 -40
  83. package/dist/types/interface.d.ts +23 -23
  84. package/dist/types/interface.js +103 -103
  85. package/dist/types/list.d.ts +31 -31
  86. package/dist/types/list.js +209 -209
  87. package/dist/types/map.d.ts +37 -37
  88. package/dist/types/map.js +366 -366
  89. package/dist/types/nil.d.ts +10 -10
  90. package/dist/types/nil.js +46 -46
  91. package/dist/types/number.d.ts +11 -11
  92. package/dist/types/number.js +46 -46
  93. package/dist/types/string.d.ts +33 -33
  94. package/dist/types/string.js +145 -145
  95. package/dist/utils/deep-equal.d.ts +1 -1
  96. package/dist/utils/deep-equal.js +62 -62
  97. package/dist/utils/path.d.ts +10 -10
  98. package/dist/utils/path.js +59 -59
  99. package/package.json +58 -58
package/dist/context.js CHANGED
@@ -1,382 +1,383 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
- var __importDefault = (this && this.__importDefault) || function (mod) {
43
- return (mod && mod.__esModule) ? mod : { "default": mod };
44
- };
45
- Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.FunctionState = exports.LoopState = exports.ProcessState = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
47
- var handler_container_1 = __importDefault(require("./handler-container"));
48
- var default_1 = __importDefault(require("./types/default"));
49
- var generics_1 = require("./types/generics");
50
- var map_1 = __importDefault(require("./types/map"));
51
- var path_1 = __importDefault(require("./utils/path"));
52
- var ContextType;
53
- (function (ContextType) {
54
- ContextType[ContextType["Api"] = 0] = "Api";
55
- ContextType[ContextType["Global"] = 1] = "Global";
56
- ContextType[ContextType["Function"] = 2] = "Function";
57
- ContextType[ContextType["External"] = 3] = "External";
58
- ContextType[ContextType["Loop"] = 4] = "Loop";
59
- ContextType[ContextType["Map"] = 5] = "Map";
60
- ContextType[ContextType["Call"] = 6] = "Call";
61
- })(ContextType = exports.ContextType || (exports.ContextType = {}));
62
- var ContextState;
63
- (function (ContextState) {
64
- ContextState[ContextState["Temporary"] = 0] = "Temporary";
65
- ContextState[ContextState["Default"] = 1] = "Default";
66
- })(ContextState = exports.ContextState || (exports.ContextState = {}));
67
- var Scope = /** @class */ (function (_super) {
68
- __extends(Scope, _super);
69
- function Scope(context) {
70
- var _this = _super.call(this) || this;
71
- _this.context = context;
72
- return _this;
73
- }
74
- Scope.prototype.get = function (path) {
75
- if (path instanceof generics_1.CustomValue) {
76
- return this.get(new path_1.default([path]));
77
- }
78
- if (path.count() === 0) {
79
- return this;
80
- }
81
- var traversalPath = path.clone();
82
- var current = traversalPath.next();
83
- if (current.value === 'locals' || current.value === 'globals') {
84
- return this.context.get(traversalPath);
85
- }
86
- else if (this.has(path)) {
87
- return _super.prototype.get.call(this, path);
88
- }
89
- else if (this.context.api.scope.has(path)) {
90
- return this.context.api.scope.get(path);
91
- }
92
- else if (path.count() === 1 && map_1.default.getIntrinsics().has(current.toString())) {
93
- return map_1.default.getIntrinsics().get(current.toString());
94
- }
95
- else if (this.context.previous !== null) {
96
- return this.context.previous.get(path);
97
- }
98
- throw new Error("Unknown path ".concat(path.toString(), "."));
99
- };
100
- return Scope;
101
- }(map_1.default));
102
- exports.Scope = Scope;
103
- var Debugger = /** @class */ (function () {
104
- function Debugger() {
105
- this.breakpoint = false;
106
- this.nextStep = false;
107
- /* eslint-disable no-use-before-define */
108
- this.lastContext = null;
109
- }
110
- Debugger.prototype.getLastContext = function () {
111
- return this.lastContext;
112
- };
113
- Debugger.prototype.debug = function () {
114
- var segments = [];
115
- for (var _i = 0; _i < arguments.length; _i++) {
116
- segments[_i] = arguments[_i];
117
- }
118
- console.debug.apply(console, __spreadArray([], __read(segments), false));
119
- return default_1.default.Void;
120
- };
121
- Debugger.prototype.setBreakpoint = function (breakpoint) {
122
- this.breakpoint = breakpoint;
123
- return this;
124
- };
125
- Debugger.prototype.getBreakpoint = function (_ctx) {
126
- return this.breakpoint;
127
- };
128
- Debugger.prototype.next = function () {
129
- this.nextStep = true;
130
- return this;
131
- };
132
- Debugger.prototype.resume = function () {
133
- var _this = this;
134
- if (!this.breakpoint) {
135
- return Promise.resolve();
136
- }
137
- return new Promise(function (resolve) {
138
- var check = function () {
139
- if (!_this.breakpoint) {
140
- resolve();
141
- }
142
- else if (_this.nextStep) {
143
- _this.nextStep = false;
144
- resolve();
145
- }
146
- else {
147
- setTimeout(check);
148
- }
149
- };
150
- check();
151
- });
152
- };
153
- Debugger.prototype.interact = function (ctx, _ast, _op) {
154
- var me = this;
155
- console.warn('Debugger is not setup.');
156
- console.info(ctx);
157
- me.breakpoint = false;
158
- };
159
- return Debugger;
160
- }());
161
- exports.Debugger = Debugger;
162
- var ProcessState = /** @class */ (function () {
163
- function ProcessState() {
164
- this.isExit = false;
165
- this.isPending = false;
166
- /* eslint-disable no-use-before-define */
167
- this.last = null;
168
- }
169
- return ProcessState;
170
- }());
171
- exports.ProcessState = ProcessState;
172
- var LoopState = /** @class */ (function () {
173
- function LoopState() {
174
- this.isBreak = false;
175
- this.isContinue = false;
176
- }
177
- return LoopState;
178
- }());
179
- exports.LoopState = LoopState;
180
- var FunctionState = /** @class */ (function () {
181
- function FunctionState() {
182
- this.value = default_1.default.Void;
183
- this.isReturn = false;
184
- }
185
- return FunctionState;
186
- }());
187
- exports.FunctionState = FunctionState;
188
- var OperationContext = /** @class */ (function () {
189
- function OperationContext(options) {
190
- if (options === void 0) { options = {}; }
191
- this.target = options.target || 'unknown';
192
- this.stackItem = null;
193
- this.previous = options.previous || null;
194
- this.type = options.type || ContextType.Api;
195
- this.state = options.state || ContextState.Default;
196
- this.scope = new Scope(this);
197
- this.isProtected = options.isProtected || false;
198
- this.injected = options.injected || false;
199
- this.debugger = options.debugger || new Debugger();
200
- this.handler = options.handler || new handler_container_1.default();
201
- this.cps = options.cps || null;
202
- this.processState = options.processState || new ProcessState();
203
- this.api = this.lookupApi();
204
- this.globals = this.lookupGlobals();
205
- this.locals = this.lookupLocals() || this;
206
- }
207
- OperationContext.prototype.step = function (op) {
208
- if (!this.injected) {
209
- this.stackItem = op.item;
210
- this.target = op.target || this.target;
211
- this.setLastActive(this);
212
- if (this.debugger.getBreakpoint(this)) {
213
- this.debugger.interact(this, op.item, op);
214
- return this.debugger.resume();
215
- }
216
- }
217
- return Promise.resolve();
218
- };
219
- OperationContext.prototype.setLastActive = function (ctx) {
220
- if (!ctx.injected) {
221
- this.processState.last = ctx;
222
- }
223
- return this;
224
- };
225
- OperationContext.prototype.getLastActive = function () {
226
- return this.processState.last;
227
- };
228
- OperationContext.prototype.isExit = function () {
229
- return this.processState.isExit;
230
- };
231
- OperationContext.prototype.isPending = function () {
232
- return this.processState.isPending;
233
- };
234
- OperationContext.prototype.setPending = function (pending) {
235
- this.processState.isPending = pending;
236
- return this;
237
- };
238
- OperationContext.prototype.lookupAllOfType = function (validate) {
239
- var me = this;
240
- var result = [];
241
- if (validate(me.type)) {
242
- result.push(me);
243
- }
244
- var current = me.previous;
245
- while (current) {
246
- if (validate(current.type)) {
247
- result.push(current);
248
- }
249
- current = current.previous;
250
- }
251
- return result;
252
- };
253
- OperationContext.prototype.exit = function () {
254
- var _this = this;
255
- if (this.processState.isPending) {
256
- this.processState.isExit = true;
257
- return new Promise(function (resolve) {
258
- var check = function () {
259
- if (!_this.processState.isPending) {
260
- _this.processState.isExit = false;
261
- resolve(_this);
262
- }
263
- else {
264
- setTimeout(check);
265
- }
266
- };
267
- check();
268
- });
269
- }
270
- return Promise.reject(new Error('No running process found.'));
271
- };
272
- OperationContext.prototype.lookupType = function (allowedTypes) {
273
- if (allowedTypes.includes(this.type)) {
274
- return this;
275
- }
276
- var current = this.previous;
277
- while (current !== null) {
278
- if (allowedTypes.includes(current.type)) {
279
- return current;
280
- }
281
- current = current.previous;
282
- }
283
- return null;
284
- };
285
- OperationContext.prototype.lookupAllScopes = function () {
286
- return this.lookupAllOfType(function (type) {
287
- return [ContextType.Global, ContextType.Function].includes(type);
288
- });
289
- };
290
- OperationContext.prototype.lookupApi = function () {
291
- return this.lookupType(OperationContext.lookupApiType);
292
- };
293
- OperationContext.prototype.lookupGlobals = function () {
294
- return this.lookupType(OperationContext.lookupGlobalsType);
295
- };
296
- OperationContext.prototype.lookupLocals = function () {
297
- return this.lookupType(OperationContext.lookupLocalsType);
298
- };
299
- OperationContext.prototype.extend = function (map) {
300
- var _a;
301
- if (this.state === ContextState.Temporary) {
302
- (_a = this.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
303
- }
304
- else {
305
- this.scope.extend(map);
306
- }
307
- return this;
308
- };
309
- OperationContext.prototype.set = function (path, value) {
310
- var _a;
311
- if (path instanceof generics_1.CustomValue) {
312
- this.set(new path_1.default([path]), value);
313
- return;
314
- }
315
- var traversalPath = path.clone();
316
- var current = traversalPath.next();
317
- if (current.value === 'locals') {
318
- this.locals.set(traversalPath, value);
319
- }
320
- else if (current.value === 'globals') {
321
- this.globals.set(traversalPath, value);
322
- }
323
- else if (this.state === ContextState.Temporary) {
324
- (_a = this.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
325
- }
326
- else {
327
- this.locals.scope.set(path, value);
328
- }
329
- };
330
- OperationContext.prototype.get = function (path) {
331
- var _a;
332
- if (path instanceof generics_1.CustomValue) {
333
- return this.get(new path_1.default([path]));
334
- }
335
- if (path.count() === 0) {
336
- return this.scope;
337
- }
338
- var traversalPath = path.clone();
339
- var current = traversalPath.next();
340
- if (current.value === 'locals') {
341
- return this.locals.get(traversalPath);
342
- }
343
- else if (current.value === 'globals') {
344
- return this.globals.get(traversalPath);
345
- }
346
- else if (this.state === ContextState.Temporary) {
347
- return (_a = this.previous) === null || _a === void 0 ? void 0 : _a.get(path);
348
- }
349
- return this.locals.scope.get(path);
350
- };
351
- OperationContext.prototype.fork = function (options) {
352
- var newContext = new OperationContext({
353
- target: options.target || this.target,
354
- previous: this,
355
- type: options.type,
356
- state: options.state,
357
- isProtected: false,
358
- injected: this.injected,
359
- debugger: this.debugger,
360
- handler: this.handler,
361
- cps: this.cps,
362
- processState: this.processState
363
- });
364
- if (this.type !== ContextType.Function) {
365
- if (this.type !== ContextType.Loop) {
366
- newContext.loopState = this.loopState;
367
- }
368
- newContext.functionState = this.functionState;
369
- }
370
- return newContext;
371
- };
372
- OperationContext.lookupApiType = [ContextType.Api];
373
- OperationContext.lookupGlobalsType = [
374
- ContextType.Global
375
- ];
376
- OperationContext.lookupLocalsType = [
377
- ContextType.Global,
378
- ContextType.Function
379
- ];
380
- return OperationContext;
381
- }());
382
- exports.default = OperationContext;
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __read = (this && this.__read) || function (o, n) {
18
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
19
+ if (!m) return o;
20
+ var i = m.call(o), r, ar = [], e;
21
+ try {
22
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
+ }
24
+ catch (error) { e = { error: error }; }
25
+ finally {
26
+ try {
27
+ if (r && !r.done && (m = i["return"])) m.call(i);
28
+ }
29
+ finally { if (e) throw e.error; }
30
+ }
31
+ return ar;
32
+ };
33
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
+ if (ar || !(i in from)) {
36
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
+ ar[i] = from[i];
38
+ }
39
+ }
40
+ return to.concat(ar || Array.prototype.slice.call(from));
41
+ };
42
+ var __importDefault = (this && this.__importDefault) || function (mod) {
43
+ return (mod && mod.__esModule) ? mod : { "default": mod };
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.FunctionState = exports.LoopState = exports.ProcessState = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
47
+ var handler_container_1 = __importDefault(require("./handler-container"));
48
+ var default_1 = __importDefault(require("./types/default"));
49
+ var generics_1 = require("./types/generics");
50
+ var map_1 = __importDefault(require("./types/map"));
51
+ var path_1 = __importDefault(require("./utils/path"));
52
+ var ContextType;
53
+ (function (ContextType) {
54
+ ContextType[ContextType["Api"] = 0] = "Api";
55
+ ContextType[ContextType["Global"] = 1] = "Global";
56
+ ContextType[ContextType["Function"] = 2] = "Function";
57
+ ContextType[ContextType["External"] = 3] = "External";
58
+ ContextType[ContextType["Loop"] = 4] = "Loop";
59
+ ContextType[ContextType["Map"] = 5] = "Map";
60
+ ContextType[ContextType["Call"] = 6] = "Call";
61
+ })(ContextType = exports.ContextType || (exports.ContextType = {}));
62
+ var ContextState;
63
+ (function (ContextState) {
64
+ ContextState[ContextState["Temporary"] = 0] = "Temporary";
65
+ ContextState[ContextState["Default"] = 1] = "Default";
66
+ })(ContextState = exports.ContextState || (exports.ContextState = {}));
67
+ var Scope = /** @class */ (function (_super) {
68
+ __extends(Scope, _super);
69
+ function Scope(context) {
70
+ var _this = _super.call(this) || this;
71
+ _this.context = context;
72
+ return _this;
73
+ }
74
+ Scope.prototype.get = function (path) {
75
+ if (path instanceof generics_1.CustomValue) {
76
+ return this.get(new path_1.default([path]));
77
+ }
78
+ if (path.count() === 0) {
79
+ return this;
80
+ }
81
+ var traversalPath = path.clone();
82
+ var current = traversalPath.next();
83
+ if (current.value === 'locals' || current.value === 'globals') {
84
+ return this.context.get(traversalPath);
85
+ }
86
+ else if (this.has(path)) {
87
+ return _super.prototype.get.call(this, path);
88
+ }
89
+ else if (this.context.api.scope.has(path)) {
90
+ return this.context.api.scope.get(path);
91
+ }
92
+ else if (path.count() === 1 &&
93
+ map_1.default.getIntrinsics().has(current.toString())) {
94
+ return map_1.default.getIntrinsics().get(current.toString());
95
+ }
96
+ else if (this.context.previous !== null) {
97
+ return this.context.previous.get(path);
98
+ }
99
+ throw new Error("Unknown path ".concat(path.toString(), "."));
100
+ };
101
+ return Scope;
102
+ }(map_1.default));
103
+ exports.Scope = Scope;
104
+ var Debugger = /** @class */ (function () {
105
+ function Debugger() {
106
+ this.breakpoint = false;
107
+ this.nextStep = false;
108
+ /* eslint-disable no-use-before-define */
109
+ this.lastContext = null;
110
+ }
111
+ Debugger.prototype.getLastContext = function () {
112
+ return this.lastContext;
113
+ };
114
+ Debugger.prototype.debug = function () {
115
+ var segments = [];
116
+ for (var _i = 0; _i < arguments.length; _i++) {
117
+ segments[_i] = arguments[_i];
118
+ }
119
+ console.debug.apply(console, __spreadArray([], __read(segments), false));
120
+ return default_1.default.Void;
121
+ };
122
+ Debugger.prototype.setBreakpoint = function (breakpoint) {
123
+ this.breakpoint = breakpoint;
124
+ return this;
125
+ };
126
+ Debugger.prototype.getBreakpoint = function (_ctx) {
127
+ return this.breakpoint;
128
+ };
129
+ Debugger.prototype.next = function () {
130
+ this.nextStep = true;
131
+ return this;
132
+ };
133
+ Debugger.prototype.resume = function () {
134
+ var _this = this;
135
+ if (!this.breakpoint) {
136
+ return Promise.resolve();
137
+ }
138
+ return new Promise(function (resolve) {
139
+ var check = function () {
140
+ if (!_this.breakpoint) {
141
+ resolve();
142
+ }
143
+ else if (_this.nextStep) {
144
+ _this.nextStep = false;
145
+ resolve();
146
+ }
147
+ else {
148
+ setTimeout(check);
149
+ }
150
+ };
151
+ check();
152
+ });
153
+ };
154
+ Debugger.prototype.interact = function (ctx, _ast, _op) {
155
+ var me = this;
156
+ console.warn('Debugger is not setup.');
157
+ console.info(ctx);
158
+ me.breakpoint = false;
159
+ };
160
+ return Debugger;
161
+ }());
162
+ exports.Debugger = Debugger;
163
+ var ProcessState = /** @class */ (function () {
164
+ function ProcessState() {
165
+ this.isExit = false;
166
+ this.isPending = false;
167
+ /* eslint-disable no-use-before-define */
168
+ this.last = null;
169
+ }
170
+ return ProcessState;
171
+ }());
172
+ exports.ProcessState = ProcessState;
173
+ var LoopState = /** @class */ (function () {
174
+ function LoopState() {
175
+ this.isBreak = false;
176
+ this.isContinue = false;
177
+ }
178
+ return LoopState;
179
+ }());
180
+ exports.LoopState = LoopState;
181
+ var FunctionState = /** @class */ (function () {
182
+ function FunctionState() {
183
+ this.value = default_1.default.Void;
184
+ this.isReturn = false;
185
+ }
186
+ return FunctionState;
187
+ }());
188
+ exports.FunctionState = FunctionState;
189
+ var OperationContext = /** @class */ (function () {
190
+ function OperationContext(options) {
191
+ if (options === void 0) { options = {}; }
192
+ this.target = options.target || 'unknown';
193
+ this.stackItem = null;
194
+ this.previous = options.previous || null;
195
+ this.type = options.type || ContextType.Api;
196
+ this.state = options.state || ContextState.Default;
197
+ this.scope = new Scope(this);
198
+ this.isProtected = options.isProtected || false;
199
+ this.injected = options.injected || false;
200
+ this.debugger = options.debugger || new Debugger();
201
+ this.handler = options.handler || new handler_container_1.default();
202
+ this.cps = options.cps || null;
203
+ this.processState = options.processState || new ProcessState();
204
+ this.api = this.lookupApi();
205
+ this.globals = this.lookupGlobals();
206
+ this.locals = this.lookupLocals() || this;
207
+ }
208
+ OperationContext.prototype.step = function (op) {
209
+ if (!this.injected) {
210
+ this.stackItem = op.item;
211
+ this.target = op.target || this.target;
212
+ this.setLastActive(this);
213
+ if (this.debugger.getBreakpoint(this)) {
214
+ this.debugger.interact(this, op.item, op);
215
+ return this.debugger.resume();
216
+ }
217
+ }
218
+ return Promise.resolve();
219
+ };
220
+ OperationContext.prototype.setLastActive = function (ctx) {
221
+ if (!ctx.injected) {
222
+ this.processState.last = ctx;
223
+ }
224
+ return this;
225
+ };
226
+ OperationContext.prototype.getLastActive = function () {
227
+ return this.processState.last;
228
+ };
229
+ OperationContext.prototype.isExit = function () {
230
+ return this.processState.isExit;
231
+ };
232
+ OperationContext.prototype.isPending = function () {
233
+ return this.processState.isPending;
234
+ };
235
+ OperationContext.prototype.setPending = function (pending) {
236
+ this.processState.isPending = pending;
237
+ return this;
238
+ };
239
+ OperationContext.prototype.lookupAllOfType = function (validate) {
240
+ var me = this;
241
+ var result = [];
242
+ if (validate(me.type)) {
243
+ result.push(me);
244
+ }
245
+ var current = me.previous;
246
+ while (current) {
247
+ if (validate(current.type)) {
248
+ result.push(current);
249
+ }
250
+ current = current.previous;
251
+ }
252
+ return result;
253
+ };
254
+ OperationContext.prototype.exit = function () {
255
+ var _this = this;
256
+ if (this.processState.isPending) {
257
+ this.processState.isExit = true;
258
+ return new Promise(function (resolve) {
259
+ var check = function () {
260
+ if (!_this.processState.isPending) {
261
+ _this.processState.isExit = false;
262
+ resolve(_this);
263
+ }
264
+ else {
265
+ setTimeout(check);
266
+ }
267
+ };
268
+ check();
269
+ });
270
+ }
271
+ return Promise.reject(new Error('No running process found.'));
272
+ };
273
+ OperationContext.prototype.lookupType = function (allowedTypes) {
274
+ if (allowedTypes.includes(this.type)) {
275
+ return this;
276
+ }
277
+ var current = this.previous;
278
+ while (current !== null) {
279
+ if (allowedTypes.includes(current.type)) {
280
+ return current;
281
+ }
282
+ current = current.previous;
283
+ }
284
+ return null;
285
+ };
286
+ OperationContext.prototype.lookupAllScopes = function () {
287
+ return this.lookupAllOfType(function (type) {
288
+ return [ContextType.Global, ContextType.Function].includes(type);
289
+ });
290
+ };
291
+ OperationContext.prototype.lookupApi = function () {
292
+ return this.lookupType(OperationContext.lookupApiType);
293
+ };
294
+ OperationContext.prototype.lookupGlobals = function () {
295
+ return this.lookupType(OperationContext.lookupGlobalsType);
296
+ };
297
+ OperationContext.prototype.lookupLocals = function () {
298
+ return this.lookupType(OperationContext.lookupLocalsType);
299
+ };
300
+ OperationContext.prototype.extend = function (map) {
301
+ var _a;
302
+ if (this.state === ContextState.Temporary) {
303
+ (_a = this.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
304
+ }
305
+ else {
306
+ this.scope.extend(map);
307
+ }
308
+ return this;
309
+ };
310
+ OperationContext.prototype.set = function (path, value) {
311
+ var _a;
312
+ if (path instanceof generics_1.CustomValue) {
313
+ this.set(new path_1.default([path]), value);
314
+ return;
315
+ }
316
+ var traversalPath = path.clone();
317
+ var current = traversalPath.next();
318
+ if (current.value === 'locals') {
319
+ this.locals.set(traversalPath, value);
320
+ }
321
+ else if (current.value === 'globals') {
322
+ this.globals.set(traversalPath, value);
323
+ }
324
+ else if (this.state === ContextState.Temporary) {
325
+ (_a = this.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
326
+ }
327
+ else {
328
+ this.locals.scope.set(path, value);
329
+ }
330
+ };
331
+ OperationContext.prototype.get = function (path) {
332
+ var _a;
333
+ if (path instanceof generics_1.CustomValue) {
334
+ return this.get(new path_1.default([path]));
335
+ }
336
+ if (path.count() === 0) {
337
+ return this.scope;
338
+ }
339
+ var traversalPath = path.clone();
340
+ var current = traversalPath.next();
341
+ if (current.value === 'locals') {
342
+ return this.locals.get(traversalPath);
343
+ }
344
+ else if (current.value === 'globals') {
345
+ return this.globals.get(traversalPath);
346
+ }
347
+ else if (this.state === ContextState.Temporary) {
348
+ return (_a = this.previous) === null || _a === void 0 ? void 0 : _a.get(path);
349
+ }
350
+ return this.locals.scope.get(path);
351
+ };
352
+ OperationContext.prototype.fork = function (options) {
353
+ var newContext = new OperationContext({
354
+ target: options.target || this.target,
355
+ previous: this,
356
+ type: options.type,
357
+ state: options.state,
358
+ isProtected: false,
359
+ injected: this.injected,
360
+ debugger: this.debugger,
361
+ handler: this.handler,
362
+ cps: this.cps,
363
+ processState: this.processState
364
+ });
365
+ if (this.type !== ContextType.Function) {
366
+ if (this.type !== ContextType.Loop) {
367
+ newContext.loopState = this.loopState;
368
+ }
369
+ newContext.functionState = this.functionState;
370
+ }
371
+ return newContext;
372
+ };
373
+ OperationContext.lookupApiType = [ContextType.Api];
374
+ OperationContext.lookupGlobalsType = [
375
+ ContextType.Global
376
+ ];
377
+ OperationContext.lookupLocalsType = [
378
+ ContextType.Global,
379
+ ContextType.Function
380
+ ];
381
+ return OperationContext;
382
+ }());
383
+ exports.default = OperationContext;