greybel-interpreter 1.0.4 → 1.1.2

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