greybel-interpreter 1.4.3 → 1.4.5

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 (104) 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 +110 -109
  6. package/dist/context.js +306 -383
  7. package/dist/cps.d.ts +14 -14
  8. package/dist/cps.js +160 -213
  9. package/dist/handler/error.d.ts +6 -6
  10. package/dist/handler/error.js +12 -35
  11. package/dist/handler/output.d.ts +18 -18
  12. package/dist/handler/output.js +29 -52
  13. package/dist/handler/resource.d.ts +12 -12
  14. package/dist/handler/resource.js +28 -51
  15. package/dist/handler-container.d.ts +14 -14
  16. package/dist/handler-container.js +14 -16
  17. package/dist/index.d.ts +46 -45
  18. package/dist/index.js +132 -131
  19. package/dist/interpreter.d.ts +41 -41
  20. package/dist/interpreter.js +217 -312
  21. package/dist/operations/assign.d.ts +13 -13
  22. package/dist/operations/assign.js +46 -111
  23. package/dist/operations/block.d.ts +12 -12
  24. package/dist/operations/block.js +52 -108
  25. package/dist/operations/break.d.ts +10 -10
  26. package/dist/operations/break.js +23 -41
  27. package/dist/operations/call.d.ts +13 -13
  28. package/dist/operations/call.js +49 -124
  29. package/dist/operations/chunk.d.ts +12 -12
  30. package/dist/operations/chunk.js +33 -85
  31. package/dist/operations/continue.d.ts +10 -10
  32. package/dist/operations/continue.js +23 -41
  33. package/dist/operations/debugger-statement.d.ts +10 -10
  34. package/dist/operations/debugger-statement.js +21 -39
  35. package/dist/operations/evaluate.d.ts +40 -40
  36. package/dist/operations/evaluate.js +334 -438
  37. package/dist/operations/for.d.ts +14 -14
  38. package/dist/operations/for.js +81 -152
  39. package/dist/operations/function-reference.d.ts +12 -12
  40. package/dist/operations/function-reference.js +44 -103
  41. package/dist/operations/function.d.ts +14 -14
  42. package/dist/operations/function.js +72 -200
  43. package/dist/operations/if-statement.d.ts +19 -19
  44. package/dist/operations/if-statement.js +85 -181
  45. package/dist/operations/import.d.ts +17 -17
  46. package/dist/operations/import.js +57 -119
  47. package/dist/operations/include.d.ts +13 -13
  48. package/dist/operations/include.js +41 -95
  49. package/dist/operations/list.d.ts +11 -11
  50. package/dist/operations/list.js +42 -111
  51. package/dist/operations/literal.d.ts +11 -11
  52. package/dist/operations/literal.js +40 -58
  53. package/dist/operations/map.d.ts +11 -11
  54. package/dist/operations/map.js +44 -171
  55. package/dist/operations/negated-binary.d.ts +11 -11
  56. package/dist/operations/negated-binary.js +42 -109
  57. package/dist/operations/new-instance.d.ts +11 -11
  58. package/dist/operations/new-instance.js +39 -100
  59. package/dist/operations/noop.d.ts +9 -9
  60. package/dist/operations/noop.js +19 -36
  61. package/dist/operations/not.d.ts +11 -11
  62. package/dist/operations/not.js +35 -96
  63. package/dist/operations/operation.d.ts +13 -13
  64. package/dist/operations/operation.js +9 -11
  65. package/dist/operations/reference.d.ts +9 -9
  66. package/dist/operations/reference.js +19 -37
  67. package/dist/operations/resolve.d.ts +38 -38
  68. package/dist/operations/resolve.js +210 -330
  69. package/dist/operations/return.d.ts +11 -11
  70. package/dist/operations/return.js +45 -107
  71. package/dist/operations/while.d.ts +13 -13
  72. package/dist/operations/while.js +65 -136
  73. package/dist/types/base.d.ts +9 -0
  74. package/dist/types/base.js +5 -0
  75. package/dist/types/boolean.d.ts +13 -11
  76. package/dist/types/boolean.js +34 -46
  77. package/dist/types/default.d.ts +8 -11
  78. package/dist/types/default.js +14 -20
  79. package/dist/types/function.d.ts +32 -32
  80. package/dist/types/function.js +104 -174
  81. package/dist/types/interface.d.ts +24 -23
  82. package/dist/types/interface.js +83 -103
  83. package/dist/types/list.d.ts +29 -31
  84. package/dist/types/list.js +158 -209
  85. package/dist/types/map.d.ts +33 -37
  86. package/dist/types/map.js +202 -366
  87. package/dist/types/nil.d.ts +11 -10
  88. package/dist/types/nil.js +33 -46
  89. package/dist/types/number.d.ts +25 -11
  90. package/dist/types/number.js +69 -46
  91. package/dist/types/string.d.ts +31 -33
  92. package/dist/types/string.js +121 -145
  93. package/dist/types/{generics.d.ts → with-intrinsics.d.ts} +15 -18
  94. package/dist/types/with-intrinsics.js +19 -0
  95. package/dist/utils/deep-equal.d.ts +1 -1
  96. package/dist/utils/deep-equal.js +37 -62
  97. package/dist/utils/object-value.d.ts +6 -0
  98. package/dist/utils/object-value.js +36 -0
  99. package/dist/utils/path.d.ts +10 -10
  100. package/dist/utils/path.js +32 -59
  101. package/package.json +58 -58
  102. package/dist/intrinsics-container.d.ts +0 -7
  103. package/dist/intrinsics-container.js +0 -19
  104. package/dist/types/generics.js +0 -40
@@ -1,41 +1,41 @@
1
- /// <reference types="node" />
2
- import EventEmitter from 'events';
3
- import OperationContext, { Debugger } from './context';
4
- import CPS from './cps';
5
- import HandlerContainer from './handler-container';
6
- import Operation from './operations/operation';
7
- import { CustomValue } from './types/generics';
8
- import CustomString from './types/string';
9
- export declare const PARAMS_PROPERTY: CustomString;
10
- export interface InterpreterOptions {
11
- target?: string;
12
- api?: Map<string, CustomValue>;
13
- params?: Array<string>;
14
- handler?: HandlerContainer;
15
- debugger?: Debugger;
16
- }
17
- export default class Interpreter extends EventEmitter {
18
- target: string;
19
- api: Map<string, CustomValue>;
20
- params: Array<string>;
21
- handler: HandlerContainer;
22
- debugger: Debugger;
23
- apiContext: OperationContext;
24
- globalContext: OperationContext;
25
- cps: CPS;
26
- constructor(options: InterpreterOptions);
27
- setTarget(target: string): Interpreter;
28
- setDebugger(dbgr: Debugger): Interpreter;
29
- setApi(newApi: Map<string, CustomValue>): Interpreter;
30
- setHandler(handler: HandlerContainer): Interpreter;
31
- prepare(code: string): Promise<Operation>;
32
- inject(code: string, context?: OperationContext): Promise<Interpreter>;
33
- injectInLastContext(code: string): Promise<Interpreter>;
34
- run(customCode?: string): Promise<Interpreter>;
35
- start(top: Operation): Promise<Interpreter>;
36
- resume(): Interpreter;
37
- pause(): Interpreter;
38
- exit(): Promise<OperationContext>;
39
- setGlobalVariable(path: string, value: CustomValue): Interpreter;
40
- getGlobalVariable(path: string): CustomValue;
41
- }
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import OperationContext, { Debugger } from './context';
4
+ import CPS from './cps';
5
+ import HandlerContainer from './handler-container';
6
+ import Operation from './operations/operation';
7
+ import CustomValue from './types/base';
8
+ import CustomString from './types/string';
9
+ export declare const PARAMS_PROPERTY: CustomString;
10
+ export interface InterpreterOptions {
11
+ target?: string;
12
+ api?: Map<CustomValue, CustomValue>;
13
+ params?: Array<string>;
14
+ handler?: HandlerContainer;
15
+ debugger?: Debugger;
16
+ }
17
+ export default class Interpreter extends EventEmitter {
18
+ target: string;
19
+ api: Map<CustomValue, CustomValue>;
20
+ params: Array<string>;
21
+ handler: HandlerContainer;
22
+ debugger: Debugger;
23
+ apiContext: OperationContext;
24
+ globalContext: OperationContext;
25
+ cps: CPS;
26
+ constructor(options: InterpreterOptions);
27
+ setTarget(target: string): Interpreter;
28
+ setDebugger(dbgr: Debugger): Interpreter;
29
+ setApi(newApi: Map<CustomValue, CustomValue>): Interpreter;
30
+ setHandler(handler: HandlerContainer): Interpreter;
31
+ prepare(code: string): Promise<Operation>;
32
+ inject(code: string, context?: OperationContext): Promise<Interpreter>;
33
+ injectInLastContext(code: string): Promise<Interpreter>;
34
+ run(customCode?: string): Promise<Interpreter>;
35
+ start(top: Operation): Promise<Interpreter>;
36
+ resume(): Interpreter;
37
+ pause(): Interpreter;
38
+ exit(): Promise<OperationContext>;
39
+ setGlobalVariable(path: string, value: CustomValue): Interpreter;
40
+ getGlobalVariable(path: string): CustomValue;
41
+ }
@@ -1,312 +1,217 @@
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- var desc = Object.getOwnPropertyDescriptor(m, k);
20
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
- desc = { enumerable: true, get: function() { return m[k]; } };
22
- }
23
- Object.defineProperty(o, k2, desc);
24
- }) : (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- o[k2] = m[k];
27
- }));
28
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
- Object.defineProperty(o, "default", { enumerable: true, value: v });
30
- }) : function(o, v) {
31
- o["default"] = v;
32
- });
33
- var __importStar = (this && this.__importStar) || function (mod) {
34
- if (mod && mod.__esModule) return mod;
35
- var result = {};
36
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
- __setModuleDefault(result, mod);
38
- return result;
39
- };
40
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
41
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
42
- return new (P || (P = Promise))(function (resolve, reject) {
43
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
44
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
45
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
46
- step((generator = generator.apply(thisArg, _arguments || [])).next());
47
- });
48
- };
49
- var __generator = (this && this.__generator) || function (thisArg, body) {
50
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
51
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
52
- function verb(n) { return function (v) { return step([n, v]); }; }
53
- function step(op) {
54
- if (f) throw new TypeError("Generator is already executing.");
55
- while (_) try {
56
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
57
- if (y = 0, t) op = [op[0] & 2, t.value];
58
- switch (op[0]) {
59
- case 0: case 1: t = op; break;
60
- case 4: _.label++; return { value: op[1], done: false };
61
- case 5: _.label++; y = op[1]; op = [0]; continue;
62
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
63
- default:
64
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
65
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
66
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
67
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
68
- if (t[2]) _.ops.pop();
69
- _.trys.pop(); continue;
70
- }
71
- op = body.call(thisArg, _);
72
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
73
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
74
- }
75
- };
76
- var __read = (this && this.__read) || function (o, n) {
77
- var m = typeof Symbol === "function" && o[Symbol.iterator];
78
- if (!m) return o;
79
- var i = m.call(o), r, ar = [], e;
80
- try {
81
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
82
- }
83
- catch (error) { e = { error: error }; }
84
- finally {
85
- try {
86
- if (r && !r.done && (m = i["return"])) m.call(i);
87
- }
88
- finally { if (e) throw e.error; }
89
- }
90
- return ar;
91
- };
92
- var __importDefault = (this && this.__importDefault) || function (mod) {
93
- return (mod && mod.__esModule) ? mod : { "default": mod };
94
- };
95
- Object.defineProperty(exports, "__esModule", { value: true });
96
- exports.PARAMS_PROPERTY = void 0;
97
- var events_1 = __importDefault(require("events"));
98
- var greybel_core_1 = require("greybel-core");
99
- var context_1 = __importStar(require("./context"));
100
- var cps_1 = __importStar(require("./cps"));
101
- var handler_container_1 = __importDefault(require("./handler-container"));
102
- var noop_1 = __importDefault(require("./operations/noop"));
103
- var default_1 = __importDefault(require("./types/default"));
104
- var list_1 = __importDefault(require("./types/list"));
105
- var string_1 = __importDefault(require("./types/string"));
106
- exports.PARAMS_PROPERTY = new string_1.default('params');
107
- var Interpreter = /** @class */ (function (_super) {
108
- __extends(Interpreter, _super);
109
- function Interpreter(options) {
110
- var _this = _super.call(this) || this;
111
- _this.handler = options.handler || new handler_container_1.default();
112
- _this.debugger = options.debugger || new context_1.Debugger();
113
- _this.api = options.api || new Map();
114
- _this.params = options.params || [];
115
- _this.apiContext = null;
116
- _this.globalContext = null;
117
- _this.setTarget(options.target || 'unknown');
118
- return _this;
119
- }
120
- Interpreter.prototype.setTarget = function (target) {
121
- if (this.apiContext !== null && this.apiContext.isPending()) {
122
- throw new Error('You cannot set a target while a process is running.');
123
- }
124
- this.target = target;
125
- var cpsCtx = new cps_1.CPSContext(target, this.handler);
126
- this.cps = new cps_1.default(cpsCtx);
127
- this.apiContext = new context_1.default({
128
- target: target,
129
- isProtected: true,
130
- debugger: this.debugger,
131
- handler: this.handler,
132
- cps: this.cps
133
- });
134
- this.globalContext = this.apiContext.fork({
135
- type: context_1.ContextType.Global,
136
- state: context_1.ContextState.Default
137
- });
138
- return this;
139
- };
140
- Interpreter.prototype.setDebugger = function (dbgr) {
141
- if (this.apiContext !== null && this.apiContext.isPending()) {
142
- throw new Error('You cannot set a target while a process is running.');
143
- }
144
- this.debugger = dbgr;
145
- this.apiContext.debugger = dbgr;
146
- this.globalContext.debugger = dbgr;
147
- return this;
148
- };
149
- Interpreter.prototype.setApi = function (newApi) {
150
- if (this.apiContext !== null && this.apiContext.isPending()) {
151
- throw new Error('You cannot set a target while a process is running.');
152
- }
153
- this.api = newApi;
154
- return this;
155
- };
156
- Interpreter.prototype.setHandler = function (handler) {
157
- if (this.apiContext !== null && this.apiContext.isPending()) {
158
- throw new Error('You cannot set a target while a process is running.');
159
- }
160
- this.handler = handler;
161
- this.apiContext.handler = handler;
162
- this.globalContext.handler = handler;
163
- return this;
164
- };
165
- Interpreter.prototype.prepare = function (code) {
166
- try {
167
- var parser = new greybel_core_1.Parser(code);
168
- var chunk = parser.parseChunk();
169
- return this.cps.visit(chunk);
170
- }
171
- catch (err) {
172
- this.handler.errorHandler.raise(err);
173
- }
174
- return Promise.resolve(new noop_1.default());
175
- };
176
- Interpreter.prototype.inject = function (code, context) {
177
- return __awaiter(this, void 0, void 0, function () {
178
- var top, injectionCtx, err_1;
179
- return __generator(this, function (_a) {
180
- switch (_a.label) {
181
- case 0:
182
- _a.trys.push([0, 3, , 4]);
183
- return [4 /*yield*/, this.prepare(code)];
184
- case 1:
185
- top = _a.sent();
186
- injectionCtx = (context || this.globalContext).fork({
187
- type: context_1.ContextType.Call,
188
- state: context_1.ContextState.Temporary,
189
- injected: true
190
- });
191
- return [4 /*yield*/, top.handle(injectionCtx)];
192
- case 2:
193
- _a.sent();
194
- return [3 /*break*/, 4];
195
- case 3:
196
- err_1 = _a.sent();
197
- this.handler.errorHandler.raise(err_1);
198
- return [3 /*break*/, 4];
199
- case 4: return [2 /*return*/, this];
200
- }
201
- });
202
- });
203
- };
204
- Interpreter.prototype.injectInLastContext = function (code) {
205
- return __awaiter(this, void 0, void 0, function () {
206
- var last;
207
- return __generator(this, function (_a) {
208
- last = this.apiContext.getLastActive();
209
- if (this.apiContext !== null && this.apiContext.isPending()) {
210
- return [2 /*return*/, this.inject(code, last)];
211
- }
212
- throw new Error('Unable to inject into last context.');
213
- });
214
- });
215
- };
216
- Interpreter.prototype.run = function (customCode) {
217
- return __awaiter(this, void 0, void 0, function () {
218
- var code, _a, top;
219
- return __generator(this, function (_b) {
220
- switch (_b.label) {
221
- case 0:
222
- _a = customCode;
223
- if (_a) return [3 /*break*/, 2];
224
- return [4 /*yield*/, this.handler.resourceHandler.get(this.target)];
225
- case 1:
226
- _a = (_b.sent());
227
- _b.label = 2;
228
- case 2:
229
- code = _a;
230
- return [4 /*yield*/, this.prepare(code)];
231
- case 3:
232
- top = _b.sent();
233
- return [2 /*return*/, this.start(top)];
234
- }
235
- });
236
- });
237
- };
238
- Interpreter.prototype.start = function (top) {
239
- return __awaiter(this, void 0, void 0, function () {
240
- var api, newParams, process_1, err_2;
241
- return __generator(this, function (_a) {
242
- switch (_a.label) {
243
- case 0:
244
- if (this.apiContext !== null && this.apiContext.isPending()) {
245
- throw new Error('Process already running.');
246
- }
247
- api = Array.from(this.api.entries()).reduce(function (result, _a) {
248
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
249
- result.set(new string_1.default(key), value);
250
- return result;
251
- }, new Map());
252
- this.apiContext.extend(api);
253
- newParams = new list_1.default(this.params.map(function (item) { return new string_1.default(item); }));
254
- this.globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
255
- _a.label = 1;
256
- case 1:
257
- _a.trys.push([1, 3, 4, 5]);
258
- this.apiContext.setPending(true);
259
- process_1 = top.handle(this.globalContext);
260
- this.emit('start', this);
261
- return [4 /*yield*/, process_1];
262
- case 2:
263
- _a.sent();
264
- return [3 /*break*/, 5];
265
- case 3:
266
- err_2 = _a.sent();
267
- this.handler.errorHandler.raise(err_2);
268
- return [3 /*break*/, 5];
269
- case 4:
270
- this.apiContext.setPending(false);
271
- this.emit('exit', this);
272
- return [7 /*endfinally*/];
273
- case 5: return [2 /*return*/, this];
274
- }
275
- });
276
- });
277
- };
278
- Interpreter.prototype.resume = function () {
279
- if (this.apiContext !== null && this.apiContext.isPending()) {
280
- this.debugger.setBreakpoint(false);
281
- }
282
- return this;
283
- };
284
- Interpreter.prototype.pause = function () {
285
- if (this.apiContext !== null && this.apiContext.isPending()) {
286
- this.debugger.setBreakpoint(true);
287
- }
288
- return this;
289
- };
290
- Interpreter.prototype.exit = function () {
291
- try {
292
- return this.apiContext.exit();
293
- }
294
- catch (err) {
295
- this.handler.errorHandler.raise(err);
296
- }
297
- };
298
- Interpreter.prototype.setGlobalVariable = function (path, value) {
299
- if (this.globalContext != null) {
300
- this.globalContext.set(new string_1.default(path), value);
301
- }
302
- return this;
303
- };
304
- Interpreter.prototype.getGlobalVariable = function (path) {
305
- if (this.globalContext != null) {
306
- this.globalContext.get(new string_1.default(path));
307
- }
308
- return default_1.default.Void;
309
- };
310
- return Interpreter;
311
- }(events_1.default));
312
- exports.default = Interpreter;
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.PARAMS_PROPERTY = void 0;
39
+ const events_1 = __importDefault(require("events"));
40
+ const greybel_core_1 = require("greybel-core");
41
+ const context_1 = __importStar(require("./context"));
42
+ const cps_1 = __importStar(require("./cps"));
43
+ const handler_container_1 = __importDefault(require("./handler-container"));
44
+ const noop_1 = __importDefault(require("./operations/noop"));
45
+ const default_1 = __importDefault(require("./types/default"));
46
+ const list_1 = __importDefault(require("./types/list"));
47
+ const map_1 = __importDefault(require("./types/map"));
48
+ const number_1 = __importDefault(require("./types/number"));
49
+ const string_1 = __importDefault(require("./types/string"));
50
+ exports.PARAMS_PROPERTY = new string_1.default('params');
51
+ class Interpreter extends events_1.default {
52
+ constructor(options) {
53
+ super();
54
+ this.handler = options.handler || new handler_container_1.default();
55
+ this.debugger = options.debugger || new context_1.Debugger();
56
+ this.api = options.api || new Map();
57
+ this.params = options.params || [];
58
+ this.apiContext = null;
59
+ this.globalContext = null;
60
+ this.setTarget(options.target || 'unknown');
61
+ }
62
+ setTarget(target) {
63
+ if (this.apiContext !== null && this.apiContext.isPending()) {
64
+ throw new Error('You cannot set a target while a process is running.');
65
+ }
66
+ this.target = target;
67
+ const cpsCtx = new cps_1.CPSContext(target, this.handler);
68
+ this.cps = new cps_1.default(cpsCtx);
69
+ this.apiContext = new context_1.default({
70
+ target,
71
+ isProtected: true,
72
+ debugger: this.debugger,
73
+ handler: this.handler,
74
+ cps: this.cps
75
+ });
76
+ this.globalContext = this.apiContext.fork({
77
+ type: context_1.ContextType.Global,
78
+ state: context_1.ContextState.Default
79
+ });
80
+ return this;
81
+ }
82
+ setDebugger(dbgr) {
83
+ if (this.apiContext !== null && this.apiContext.isPending()) {
84
+ throw new Error('You cannot set a target while a process is running.');
85
+ }
86
+ this.debugger = dbgr;
87
+ this.apiContext.debugger = dbgr;
88
+ this.globalContext.debugger = dbgr;
89
+ return this;
90
+ }
91
+ setApi(newApi) {
92
+ if (this.apiContext !== null && this.apiContext.isPending()) {
93
+ throw new Error('You cannot set a target while a process is running.');
94
+ }
95
+ this.api = newApi;
96
+ return this;
97
+ }
98
+ setHandler(handler) {
99
+ if (this.apiContext !== null && this.apiContext.isPending()) {
100
+ throw new Error('You cannot set a target while a process is running.');
101
+ }
102
+ this.handler = handler;
103
+ this.apiContext.handler = handler;
104
+ this.globalContext.handler = handler;
105
+ return this;
106
+ }
107
+ prepare(code) {
108
+ try {
109
+ const parser = new greybel_core_1.Parser(code);
110
+ const chunk = parser.parseChunk();
111
+ return this.cps.visit(chunk);
112
+ }
113
+ catch (err) {
114
+ this.handler.errorHandler.raise(err);
115
+ }
116
+ return Promise.resolve(new noop_1.default());
117
+ }
118
+ inject(code, context) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ try {
121
+ const top = yield this.prepare(code);
122
+ const injectionCtx = (context || this.globalContext).fork({
123
+ type: context_1.ContextType.Call,
124
+ state: context_1.ContextState.Temporary,
125
+ injected: true
126
+ });
127
+ yield top.handle(injectionCtx);
128
+ }
129
+ catch (err) {
130
+ this.handler.errorHandler.raise(err);
131
+ }
132
+ return this;
133
+ });
134
+ }
135
+ injectInLastContext(code) {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ const last = this.apiContext.getLastActive();
138
+ if (this.apiContext !== null && this.apiContext.isPending()) {
139
+ return this.inject(code, last);
140
+ }
141
+ throw new Error('Unable to inject into last context.');
142
+ });
143
+ }
144
+ run(customCode) {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ const code = customCode || (yield this.handler.resourceHandler.get(this.target));
147
+ const top = yield this.prepare(code);
148
+ return this.start(top);
149
+ });
150
+ }
151
+ start(top) {
152
+ return __awaiter(this, void 0, void 0, function* () {
153
+ if (this.apiContext !== null && this.apiContext.isPending()) {
154
+ throw new Error('Process already running.');
155
+ }
156
+ const stringIntrinsics = map_1.default.createWithInitialValue(string_1.default.intrinsics);
157
+ const numberIntrinsics = map_1.default.createWithInitialValue(number_1.default.intrinsics);
158
+ const listIntrinsics = map_1.default.createWithInitialValue(list_1.default.intrinsics);
159
+ const mapIntrinsics = map_1.default.createWithInitialValue(map_1.default.intrinsics);
160
+ this.apiContext.set(new string_1.default('string'), stringIntrinsics);
161
+ this.apiContext.set(new string_1.default('number'), numberIntrinsics);
162
+ this.apiContext.set(new string_1.default('list'), listIntrinsics);
163
+ this.apiContext.set(new string_1.default('map'), mapIntrinsics);
164
+ this.apiContext.extend(this.api);
165
+ const newParams = new list_1.default(this.params.map((item) => new string_1.default(item)));
166
+ this.globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
167
+ this.globalContext.set(new string_1.default('globals'), this.globalContext.scope);
168
+ try {
169
+ this.apiContext.setPending(true);
170
+ const process = top.handle(this.globalContext);
171
+ this.emit('start', this);
172
+ yield process;
173
+ }
174
+ catch (err) {
175
+ this.handler.errorHandler.raise(err);
176
+ }
177
+ finally {
178
+ this.apiContext.setPending(false);
179
+ this.emit('exit', this);
180
+ }
181
+ return this;
182
+ });
183
+ }
184
+ resume() {
185
+ if (this.apiContext !== null && this.apiContext.isPending()) {
186
+ this.debugger.setBreakpoint(false);
187
+ }
188
+ return this;
189
+ }
190
+ pause() {
191
+ if (this.apiContext !== null && this.apiContext.isPending()) {
192
+ this.debugger.setBreakpoint(true);
193
+ }
194
+ return this;
195
+ }
196
+ exit() {
197
+ try {
198
+ return this.apiContext.exit();
199
+ }
200
+ catch (err) {
201
+ this.handler.errorHandler.raise(err);
202
+ }
203
+ }
204
+ setGlobalVariable(path, value) {
205
+ if (this.globalContext != null) {
206
+ this.globalContext.set(new string_1.default(path), value);
207
+ }
208
+ return this;
209
+ }
210
+ getGlobalVariable(path) {
211
+ if (this.globalContext != null) {
212
+ this.globalContext.get(new string_1.default(path));
213
+ }
214
+ return default_1.default.Void;
215
+ }
216
+ }
217
+ exports.default = Interpreter;