greybel-interpreter 0.4.9 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +2 -2
  3. package/dist/context.d.ts +96 -96
  4. package/dist/context.js +392 -392
  5. package/dist/cps.d.ts +15 -15
  6. package/dist/cps.js +849 -849
  7. package/dist/custom-types/boolean.d.ts +10 -10
  8. package/dist/custom-types/boolean.js +41 -41
  9. package/dist/custom-types/list.d.ts +25 -25
  10. package/dist/custom-types/list.js +288 -289
  11. package/dist/custom-types/map.d.ts +28 -28
  12. package/dist/custom-types/map.js +238 -238
  13. package/dist/custom-types/nil.d.ts +8 -8
  14. package/dist/custom-types/nil.js +38 -38
  15. package/dist/custom-types/number.d.ts +10 -10
  16. package/dist/custom-types/number.js +41 -41
  17. package/dist/custom-types/string.d.ts +21 -21
  18. package/dist/custom-types/string.js +131 -131
  19. package/dist/expressions/assign.d.ts +13 -13
  20. package/dist/expressions/assign.js +174 -174
  21. package/dist/expressions/binary-negated-expression.d.ts +19 -19
  22. package/dist/expressions/binary-negated-expression.js +131 -131
  23. package/dist/expressions/call.d.ts +14 -14
  24. package/dist/expressions/call.js +207 -207
  25. package/dist/expressions/import.d.ts +14 -14
  26. package/dist/expressions/import.js +155 -155
  27. package/dist/expressions/include.d.ts +13 -13
  28. package/dist/expressions/include.js +132 -132
  29. package/dist/expressions/list.d.ts +14 -14
  30. package/dist/expressions/list.js +134 -134
  31. package/dist/expressions/logical-and-binary.d.ts +23 -23
  32. package/dist/expressions/logical-and-binary.js +216 -212
  33. package/dist/expressions/map.d.ts +19 -19
  34. package/dist/expressions/map.js +190 -190
  35. package/dist/expressions/path.d.ts +31 -31
  36. package/dist/expressions/path.js +364 -364
  37. package/dist/index.d.ts +36 -36
  38. package/dist/index.js +89 -89
  39. package/dist/interpreter.d.ts +29 -29
  40. package/dist/interpreter.js +210 -210
  41. package/dist/operations/argument.d.ts +8 -8
  42. package/dist/operations/argument.js +145 -145
  43. package/dist/operations/body.d.ts +8 -8
  44. package/dist/operations/body.js +140 -140
  45. package/dist/operations/break.d.ts +7 -7
  46. package/dist/operations/break.js +34 -34
  47. package/dist/operations/continue.d.ts +7 -7
  48. package/dist/operations/continue.js +34 -34
  49. package/dist/operations/debugger.d.ts +7 -7
  50. package/dist/operations/debugger.js +32 -32
  51. package/dist/operations/else-if.d.ts +12 -12
  52. package/dist/operations/else-if.js +31 -31
  53. package/dist/operations/else.d.ts +10 -10
  54. package/dist/operations/else.js +30 -30
  55. package/dist/operations/for.d.ts +16 -16
  56. package/dist/operations/for.js +143 -143
  57. package/dist/operations/function.d.ts +21 -21
  58. package/dist/operations/function.js +133 -133
  59. package/dist/operations/if-statement.d.ts +8 -8
  60. package/dist/operations/if-statement.js +153 -153
  61. package/dist/operations/if.d.ts +12 -12
  62. package/dist/operations/if.js +31 -31
  63. package/dist/operations/new.d.ts +11 -11
  64. package/dist/operations/new.js +96 -96
  65. package/dist/operations/not.d.ts +11 -11
  66. package/dist/operations/not.js +95 -95
  67. package/dist/operations/reference.d.ts +11 -11
  68. package/dist/operations/reference.js +102 -102
  69. package/dist/operations/return.d.ts +11 -11
  70. package/dist/operations/return.js +102 -102
  71. package/dist/operations/while.d.ts +14 -14
  72. package/dist/operations/while.js +127 -127
  73. package/dist/resource.d.ts +9 -9
  74. package/dist/resource.js +29 -29
  75. package/dist/typer.d.ts +6 -6
  76. package/dist/typer.js +95 -95
  77. package/dist/types/custom-type.d.ts +19 -19
  78. package/dist/types/custom-type.js +58 -58
  79. package/dist/types/expression.d.ts +6 -6
  80. package/dist/types/expression.js +9 -9
  81. package/dist/types/operation.d.ts +9 -9
  82. package/dist/types/operation.js +38 -38
  83. package/package.json +45 -45
package/dist/context.js CHANGED
@@ -1,392 +1,392 @@
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.OperationContext = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
47
- var map_1 = __importDefault(require("./custom-types/map"));
48
- var ContextType;
49
- (function (ContextType) {
50
- ContextType["API"] = "API";
51
- ContextType["GLOBAL"] = "GLOBAL";
52
- ContextType["FUNCTION"] = "FUNCTION";
53
- ContextType["EXTERNAL"] = "EXTERNAL";
54
- ContextType["LOOP"] = "LOOP";
55
- ContextType["MAP"] = "MAP";
56
- ContextType["CALL"] = "CALL";
57
- })(ContextType = exports.ContextType || (exports.ContextType = {}));
58
- var ContextState;
59
- (function (ContextState) {
60
- ContextState["TEMPORARY"] = "TEMPORARY";
61
- ContextState["DEFAULT"] = "DEFAULT";
62
- })(ContextState = exports.ContextState || (exports.ContextState = {}));
63
- var Scope = /** @class */ (function (_super) {
64
- __extends(Scope, _super);
65
- function Scope(context) {
66
- var _this = _super.call(this) || this;
67
- _this.context = context;
68
- return _this;
69
- }
70
- Scope.prototype.set = function (path, value) {
71
- var me = this;
72
- var traversalPath = [].concat(path);
73
- var current = traversalPath.shift();
74
- if (current != null) {
75
- if (current === 'locals') {
76
- return me.context.locals.set(traversalPath, value);
77
- }
78
- else if (current === 'globals') {
79
- return me.context.globals.set(traversalPath, value);
80
- }
81
- else if (me.context.locals != null) {
82
- return map_1.default.prototype.set.call(me.context.locals, path, value);
83
- }
84
- else {
85
- throw new Error("Cannot set path ".concat(path.join('.')));
86
- }
87
- }
88
- };
89
- Scope.prototype.get = function (path) {
90
- var _a, _b, _c;
91
- var me = this;
92
- var traversalPath = [].concat(path);
93
- var current = traversalPath.shift();
94
- if (current != null) {
95
- if (current === 'locals') {
96
- return traversalPath.length === 0
97
- ? Promise.resolve(me.context.locals)
98
- : me.context.locals.get(traversalPath);
99
- }
100
- else if (current === 'globals') {
101
- return traversalPath.length === 0
102
- ? Promise.resolve(me.context.globals)
103
- : me.context.globals.get(traversalPath);
104
- }
105
- else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
106
- return map_1.default.prototype.get.call(me.context.locals, path);
107
- }
108
- else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
109
- return map_1.default.prototype.get.call(me.context.globals, path);
110
- }
111
- else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
112
- return map_1.default.prototype.get.call(me.context.api, path);
113
- }
114
- else if (me.value.has(current)) {
115
- return _super.prototype.get.call(this, path);
116
- }
117
- else {
118
- throw new Error("Cannot get path ".concat(path.join('.')));
119
- }
120
- }
121
- return null;
122
- };
123
- Scope.prototype.getCallable = function (path) {
124
- var _a, _b, _c;
125
- var me = this;
126
- var traversalPath = [].concat(path);
127
- var current = traversalPath.shift();
128
- if (current != null) {
129
- if (current === 'locals') {
130
- return me.context.locals.getCallable(traversalPath);
131
- }
132
- else if (current === 'globals') {
133
- return me.context.globals.getCallable(traversalPath);
134
- }
135
- else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
136
- return map_1.default.prototype.getCallable.call(me.context.locals, path);
137
- }
138
- else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
139
- return map_1.default.prototype.getCallable.call(me.context.globals, path);
140
- }
141
- else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
142
- return map_1.default.prototype.getCallable.call(me.context.api, path);
143
- }
144
- else {
145
- return _super.prototype.getCallable.call(this, path);
146
- }
147
- }
148
- return Promise.resolve({
149
- origin: me,
150
- context: null
151
- });
152
- };
153
- return Scope;
154
- }(map_1.default));
155
- exports.Scope = Scope;
156
- var Debugger = /** @class */ (function () {
157
- function Debugger() {
158
- var me = this;
159
- me.breakpoint = false;
160
- me.nextStep = false;
161
- me.lastContext = null;
162
- }
163
- Debugger.prototype.raise = function (message) {
164
- var args = [];
165
- for (var _i = 1; _i < arguments.length; _i++) {
166
- args[_i - 1] = arguments[_i];
167
- }
168
- throw new Error(message);
169
- };
170
- Debugger.prototype.debug = function (message) {
171
- var args = [];
172
- for (var _i = 1; _i < arguments.length; _i++) {
173
- args[_i - 1] = arguments[_i];
174
- }
175
- console.info.apply(console, __spreadArray([message], __read(args), false));
176
- };
177
- Debugger.prototype.setBreakpoint = function (state) {
178
- var me = this;
179
- me.breakpoint = state;
180
- return me;
181
- };
182
- Debugger.prototype.getBreakpoint = function (operationContext) {
183
- return this.breakpoint;
184
- };
185
- Debugger.prototype.next = function () {
186
- var me = this;
187
- me.nextStep = true;
188
- return me;
189
- };
190
- Debugger.prototype.resume = function () {
191
- var me = this;
192
- if (!me.breakpoint) {
193
- return Promise.resolve();
194
- }
195
- return new Promise(function (resolve) {
196
- var check = function () {
197
- if (!me.breakpoint) {
198
- resolve();
199
- }
200
- else if (me.nextStep) {
201
- me.nextStep = false;
202
- resolve();
203
- }
204
- else {
205
- setTimeout(check, 10);
206
- }
207
- };
208
- setTimeout(check, 10);
209
- });
210
- };
211
- Debugger.prototype.interact = function (operationContext, item) {
212
- var me = this;
213
- console.warn("Debugger is not setup.");
214
- console.info(operationContext);
215
- me.breakpoint = false;
216
- };
217
- return Debugger;
218
- }());
219
- exports.Debugger = Debugger;
220
- var OperationContext = /** @class */ (function () {
221
- function OperationContext(options) {
222
- var me = this;
223
- me.target = options.target || 'unknown';
224
- me.stackItem = null;
225
- me.previous = options.previous || null;
226
- me.type = options.type || ContextType.API;
227
- me.state = options.state || ContextState.DEFAULT;
228
- me.scope = new Scope(me);
229
- me.isProtected = options.isProtected || false;
230
- me.injected = options.injected || false;
231
- me.memory = new Map();
232
- me.debugger = options.debugger || new Debugger();
233
- me.cps = options.cps;
234
- me.processState = options.processState || {
235
- exit: false,
236
- pending: false,
237
- last: null
238
- };
239
- me.api = me.lookupAPI();
240
- me.globals = me.lookupGlobals();
241
- me.locals = me.lookupLocals();
242
- }
243
- OperationContext.prototype.step = function (item) {
244
- var me = this;
245
- var dbgr = me.debugger;
246
- if (!me.injected) {
247
- me.stackItem = item;
248
- me.setLastActive(me);
249
- if (dbgr.getBreakpoint(me)) {
250
- dbgr.interact(me, item);
251
- return dbgr.resume();
252
- }
253
- }
254
- return Promise.resolve();
255
- };
256
- OperationContext.prototype.setLastActive = function (opc) {
257
- var me = this;
258
- if (!opc.injected) {
259
- me.processState.last = opc;
260
- }
261
- return me;
262
- };
263
- OperationContext.prototype.getLastActive = function () {
264
- return this.processState.last;
265
- };
266
- OperationContext.prototype.isExit = function () {
267
- return this.processState.exit;
268
- };
269
- OperationContext.prototype.exit = function () {
270
- var me = this;
271
- var state = me.processState;
272
- if (state.pending) {
273
- state.exit = true;
274
- return new Promise(function (resolve) {
275
- var check = function () {
276
- if (!state.pending) {
277
- state.exit = false;
278
- resolve(me);
279
- }
280
- else {
281
- setTimeout(check, 10);
282
- }
283
- };
284
- setTimeout(check, 10);
285
- });
286
- }
287
- return Promise.reject(new Error('No running process found.'));
288
- };
289
- OperationContext.prototype.isPending = function () {
290
- return this.processState.pending;
291
- };
292
- OperationContext.prototype.setPending = function (pending) {
293
- var me = this;
294
- me.processState.pending = pending;
295
- return me;
296
- };
297
- OperationContext.prototype.lookupType = function (validate) {
298
- var me = this;
299
- if (validate(me.type)) {
300
- return me.scope;
301
- }
302
- var current = me.previous;
303
- while (current) {
304
- if (validate(current.type)) {
305
- return current.scope;
306
- }
307
- current = current.previous;
308
- }
309
- return null;
310
- };
311
- OperationContext.prototype.lookupAPI = function () {
312
- return this.lookupType(function (type) { return [ContextType.API].includes(type); });
313
- };
314
- OperationContext.prototype.lookupGlobals = function () {
315
- return this.lookupType(function (type) { return [ContextType.GLOBAL].includes(type); });
316
- };
317
- OperationContext.prototype.lookupLocals = function () {
318
- return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
319
- };
320
- OperationContext.prototype.valueOf = function () {
321
- return this.scope.valueOf();
322
- };
323
- OperationContext.prototype.extend = function (map) {
324
- var _a;
325
- var me = this;
326
- if (me.state === ContextState.TEMPORARY) {
327
- (_a = me.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
328
- }
329
- else {
330
- me.scope.extend(map);
331
- }
332
- return me;
333
- };
334
- OperationContext.prototype.set = function (path, value) {
335
- var _a;
336
- var me = this;
337
- if (me.state === ContextState.TEMPORARY) {
338
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
339
- }
340
- else {
341
- return me.scope.set(path, value);
342
- }
343
- };
344
- OperationContext.prototype.get = function (path) {
345
- var _a;
346
- var me = this;
347
- if (me.state === ContextState.TEMPORARY) {
348
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
349
- }
350
- return me.scope.get(path);
351
- };
352
- OperationContext.prototype.getCallable = function (path) {
353
- var _a;
354
- var me = this;
355
- if (me.state === ContextState.TEMPORARY) {
356
- return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.getCallable(path);
357
- }
358
- return me.scope.getCallable(path);
359
- };
360
- OperationContext.prototype.setMemory = function (key, value) {
361
- var me = this;
362
- me.memory.set(key, value);
363
- return me;
364
- };
365
- OperationContext.prototype.getMemory = function (key) {
366
- var me = this;
367
- return me.memory.get(key);
368
- };
369
- OperationContext.prototype.fork = function (_a) {
370
- var type = _a.type, state = _a.state, target = _a.target, injected = _a.injected;
371
- var me = this;
372
- var opc = new OperationContext({
373
- target: target || me.target,
374
- previous: me,
375
- type: type,
376
- state: state,
377
- debugger: me.debugger,
378
- cps: me.cps,
379
- processState: me.processState,
380
- injected: injected || me.injected
381
- });
382
- if (type !== ContextType.FUNCTION) {
383
- if (type !== ContextType.LOOP) {
384
- opc.setMemory('loopContext', me.getMemory('loopContext'));
385
- }
386
- opc.setMemory('functionContext', me.getMemory('functionContext'));
387
- }
388
- return opc;
389
- };
390
- return OperationContext;
391
- }());
392
- exports.OperationContext = 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.OperationContext = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
47
+ var map_1 = __importDefault(require("./custom-types/map"));
48
+ var ContextType;
49
+ (function (ContextType) {
50
+ ContextType["API"] = "API";
51
+ ContextType["GLOBAL"] = "GLOBAL";
52
+ ContextType["FUNCTION"] = "FUNCTION";
53
+ ContextType["EXTERNAL"] = "EXTERNAL";
54
+ ContextType["LOOP"] = "LOOP";
55
+ ContextType["MAP"] = "MAP";
56
+ ContextType["CALL"] = "CALL";
57
+ })(ContextType = exports.ContextType || (exports.ContextType = {}));
58
+ var ContextState;
59
+ (function (ContextState) {
60
+ ContextState["TEMPORARY"] = "TEMPORARY";
61
+ ContextState["DEFAULT"] = "DEFAULT";
62
+ })(ContextState = exports.ContextState || (exports.ContextState = {}));
63
+ var Scope = /** @class */ (function (_super) {
64
+ __extends(Scope, _super);
65
+ function Scope(context) {
66
+ var _this = _super.call(this) || this;
67
+ _this.context = context;
68
+ return _this;
69
+ }
70
+ Scope.prototype.set = function (path, value) {
71
+ var me = this;
72
+ var traversalPath = [].concat(path);
73
+ var current = traversalPath.shift();
74
+ if (current != null) {
75
+ if (current === 'locals') {
76
+ return me.context.locals.set(traversalPath, value);
77
+ }
78
+ else if (current === 'globals') {
79
+ return me.context.globals.set(traversalPath, value);
80
+ }
81
+ else if (me.context.locals != null) {
82
+ return map_1.default.prototype.set.call(me.context.locals, path, value);
83
+ }
84
+ else {
85
+ throw new Error("Cannot set path ".concat(path.join('.')));
86
+ }
87
+ }
88
+ };
89
+ Scope.prototype.get = function (path) {
90
+ var _a, _b, _c;
91
+ var me = this;
92
+ var traversalPath = [].concat(path);
93
+ var current = traversalPath.shift();
94
+ if (current != null) {
95
+ if (current === 'locals') {
96
+ return traversalPath.length === 0
97
+ ? Promise.resolve(me.context.locals)
98
+ : me.context.locals.get(traversalPath);
99
+ }
100
+ else if (current === 'globals') {
101
+ return traversalPath.length === 0
102
+ ? Promise.resolve(me.context.globals)
103
+ : me.context.globals.get(traversalPath);
104
+ }
105
+ else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
106
+ return map_1.default.prototype.get.call(me.context.locals, path);
107
+ }
108
+ else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
109
+ return map_1.default.prototype.get.call(me.context.globals, path);
110
+ }
111
+ else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
112
+ return map_1.default.prototype.get.call(me.context.api, path);
113
+ }
114
+ else if (me.value.has(current)) {
115
+ return _super.prototype.get.call(this, path);
116
+ }
117
+ else {
118
+ throw new Error("Cannot get path ".concat(path.join('.')));
119
+ }
120
+ }
121
+ return null;
122
+ };
123
+ Scope.prototype.getCallable = function (path) {
124
+ var _a, _b, _c;
125
+ var me = this;
126
+ var traversalPath = [].concat(path);
127
+ var current = traversalPath.shift();
128
+ if (current != null) {
129
+ if (current === 'locals') {
130
+ return me.context.locals.getCallable(traversalPath);
131
+ }
132
+ else if (current === 'globals') {
133
+ return me.context.globals.getCallable(traversalPath);
134
+ }
135
+ else if ((_a = me.context.locals) === null || _a === void 0 ? void 0 : _a.value.has(current)) {
136
+ return map_1.default.prototype.getCallable.call(me.context.locals, path);
137
+ }
138
+ else if ((_b = me.context.globals) === null || _b === void 0 ? void 0 : _b.value.has(current)) {
139
+ return map_1.default.prototype.getCallable.call(me.context.globals, path);
140
+ }
141
+ else if ((_c = me.context.api) === null || _c === void 0 ? void 0 : _c.value.has(current)) {
142
+ return map_1.default.prototype.getCallable.call(me.context.api, path);
143
+ }
144
+ else {
145
+ return _super.prototype.getCallable.call(this, path);
146
+ }
147
+ }
148
+ return Promise.resolve({
149
+ origin: me,
150
+ context: null
151
+ });
152
+ };
153
+ return Scope;
154
+ }(map_1.default));
155
+ exports.Scope = Scope;
156
+ var Debugger = /** @class */ (function () {
157
+ function Debugger() {
158
+ var me = this;
159
+ me.breakpoint = false;
160
+ me.nextStep = false;
161
+ me.lastContext = null;
162
+ }
163
+ Debugger.prototype.raise = function (message) {
164
+ var args = [];
165
+ for (var _i = 1; _i < arguments.length; _i++) {
166
+ args[_i - 1] = arguments[_i];
167
+ }
168
+ throw new Error(message);
169
+ };
170
+ Debugger.prototype.debug = function (message) {
171
+ var args = [];
172
+ for (var _i = 1; _i < arguments.length; _i++) {
173
+ args[_i - 1] = arguments[_i];
174
+ }
175
+ console.info.apply(console, __spreadArray([message], __read(args), false));
176
+ };
177
+ Debugger.prototype.setBreakpoint = function (state) {
178
+ var me = this;
179
+ me.breakpoint = state;
180
+ return me;
181
+ };
182
+ Debugger.prototype.getBreakpoint = function (operationContext) {
183
+ return this.breakpoint;
184
+ };
185
+ Debugger.prototype.next = function () {
186
+ var me = this;
187
+ me.nextStep = true;
188
+ return me;
189
+ };
190
+ Debugger.prototype.resume = function () {
191
+ var me = this;
192
+ if (!me.breakpoint) {
193
+ return Promise.resolve();
194
+ }
195
+ return new Promise(function (resolve) {
196
+ var check = function () {
197
+ if (!me.breakpoint) {
198
+ resolve();
199
+ }
200
+ else if (me.nextStep) {
201
+ me.nextStep = false;
202
+ resolve();
203
+ }
204
+ else {
205
+ setTimeout(check, 10);
206
+ }
207
+ };
208
+ setTimeout(check, 10);
209
+ });
210
+ };
211
+ Debugger.prototype.interact = function (operationContext, item) {
212
+ var me = this;
213
+ console.warn("Debugger is not setup.");
214
+ console.info(operationContext);
215
+ me.breakpoint = false;
216
+ };
217
+ return Debugger;
218
+ }());
219
+ exports.Debugger = Debugger;
220
+ var OperationContext = /** @class */ (function () {
221
+ function OperationContext(options) {
222
+ var me = this;
223
+ me.target = options.target || 'unknown';
224
+ me.stackItem = null;
225
+ me.previous = options.previous || null;
226
+ me.type = options.type || ContextType.API;
227
+ me.state = options.state || ContextState.DEFAULT;
228
+ me.scope = new Scope(me);
229
+ me.isProtected = options.isProtected || false;
230
+ me.injected = options.injected || false;
231
+ me.memory = new Map();
232
+ me.debugger = options.debugger || new Debugger();
233
+ me.cps = options.cps;
234
+ me.processState = options.processState || {
235
+ exit: false,
236
+ pending: false,
237
+ last: null
238
+ };
239
+ me.api = me.lookupAPI();
240
+ me.globals = me.lookupGlobals();
241
+ me.locals = me.lookupLocals();
242
+ }
243
+ OperationContext.prototype.step = function (item) {
244
+ var me = this;
245
+ var dbgr = me.debugger;
246
+ if (!me.injected) {
247
+ me.stackItem = item;
248
+ me.setLastActive(me);
249
+ if (dbgr.getBreakpoint(me)) {
250
+ dbgr.interact(me, item);
251
+ return dbgr.resume();
252
+ }
253
+ }
254
+ return Promise.resolve();
255
+ };
256
+ OperationContext.prototype.setLastActive = function (opc) {
257
+ var me = this;
258
+ if (!opc.injected) {
259
+ me.processState.last = opc;
260
+ }
261
+ return me;
262
+ };
263
+ OperationContext.prototype.getLastActive = function () {
264
+ return this.processState.last;
265
+ };
266
+ OperationContext.prototype.isExit = function () {
267
+ return this.processState.exit;
268
+ };
269
+ OperationContext.prototype.exit = function () {
270
+ var me = this;
271
+ var state = me.processState;
272
+ if (state.pending) {
273
+ state.exit = true;
274
+ return new Promise(function (resolve) {
275
+ var check = function () {
276
+ if (!state.pending) {
277
+ state.exit = false;
278
+ resolve(me);
279
+ }
280
+ else {
281
+ setTimeout(check, 10);
282
+ }
283
+ };
284
+ setTimeout(check, 10);
285
+ });
286
+ }
287
+ return Promise.reject(new Error('No running process found.'));
288
+ };
289
+ OperationContext.prototype.isPending = function () {
290
+ return this.processState.pending;
291
+ };
292
+ OperationContext.prototype.setPending = function (pending) {
293
+ var me = this;
294
+ me.processState.pending = pending;
295
+ return me;
296
+ };
297
+ OperationContext.prototype.lookupType = function (validate) {
298
+ var me = this;
299
+ if (validate(me.type)) {
300
+ return me.scope;
301
+ }
302
+ var current = me.previous;
303
+ while (current) {
304
+ if (validate(current.type)) {
305
+ return current.scope;
306
+ }
307
+ current = current.previous;
308
+ }
309
+ return null;
310
+ };
311
+ OperationContext.prototype.lookupAPI = function () {
312
+ return this.lookupType(function (type) { return [ContextType.API].includes(type); });
313
+ };
314
+ OperationContext.prototype.lookupGlobals = function () {
315
+ return this.lookupType(function (type) { return [ContextType.GLOBAL].includes(type); });
316
+ };
317
+ OperationContext.prototype.lookupLocals = function () {
318
+ return this.lookupType(function (type) { return [ContextType.GLOBAL, ContextType.FUNCTION].includes(type); });
319
+ };
320
+ OperationContext.prototype.valueOf = function () {
321
+ return this.scope.valueOf();
322
+ };
323
+ OperationContext.prototype.extend = function (map) {
324
+ var _a;
325
+ var me = this;
326
+ if (me.state === ContextState.TEMPORARY) {
327
+ (_a = me.previous) === null || _a === void 0 ? void 0 : _a.extend(map);
328
+ }
329
+ else {
330
+ me.scope.extend(map);
331
+ }
332
+ return me;
333
+ };
334
+ OperationContext.prototype.set = function (path, value) {
335
+ var _a;
336
+ var me = this;
337
+ if (me.state === ContextState.TEMPORARY) {
338
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.set(path, value);
339
+ }
340
+ else {
341
+ return me.scope.set(path, value);
342
+ }
343
+ };
344
+ OperationContext.prototype.get = function (path) {
345
+ var _a;
346
+ var me = this;
347
+ if (me.state === ContextState.TEMPORARY) {
348
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.get(path);
349
+ }
350
+ return me.scope.get(path);
351
+ };
352
+ OperationContext.prototype.getCallable = function (path) {
353
+ var _a;
354
+ var me = this;
355
+ if (me.state === ContextState.TEMPORARY) {
356
+ return (_a = me.previous) === null || _a === void 0 ? void 0 : _a.getCallable(path);
357
+ }
358
+ return me.scope.getCallable(path);
359
+ };
360
+ OperationContext.prototype.setMemory = function (key, value) {
361
+ var me = this;
362
+ me.memory.set(key, value);
363
+ return me;
364
+ };
365
+ OperationContext.prototype.getMemory = function (key) {
366
+ var me = this;
367
+ return me.memory.get(key);
368
+ };
369
+ OperationContext.prototype.fork = function (_a) {
370
+ var type = _a.type, state = _a.state, target = _a.target, injected = _a.injected;
371
+ var me = this;
372
+ var opc = new OperationContext({
373
+ target: target || me.target,
374
+ previous: me,
375
+ type: type,
376
+ state: state,
377
+ debugger: me.debugger,
378
+ cps: me.cps,
379
+ processState: me.processState,
380
+ injected: injected || me.injected
381
+ });
382
+ if (type !== ContextType.FUNCTION) {
383
+ if (type !== ContextType.LOOP) {
384
+ opc.setMemory('loopContext', me.getMemory('loopContext'));
385
+ }
386
+ opc.setMemory('functionContext', me.getMemory('functionContext'));
387
+ }
388
+ return opc;
389
+ };
390
+ return OperationContext;
391
+ }());
392
+ exports.OperationContext = OperationContext;