greybel-interpreter 0.3.9 → 0.4.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.
package/dist/context.d.ts CHANGED
@@ -6,7 +6,6 @@ export declare enum ContextType {
6
6
  API = "API",
7
7
  GLOBAL = "GLOBAL",
8
8
  FUNCTION = "FUNCTION",
9
- INJECTION = "INJECTION",
10
9
  EXTERNAL = "EXTERNAL",
11
10
  LOOP = "LOOP",
12
11
  MAP = "MAP",
@@ -35,15 +34,16 @@ export declare class Debugger {
35
34
  next(): Debugger;
36
35
  resume(): Promise<void>;
37
36
  interact(operationContext: OperationContext, item: ASTBase): void;
38
- run(code: string): Promise<void>;
39
37
  }
40
38
  export interface OperationContextProcessState {
41
39
  exit: boolean;
42
40
  pending: boolean;
41
+ last: OperationContext | null;
43
42
  }
44
43
  export interface OperationContextOptions {
45
44
  target?: string;
46
45
  isProtected?: boolean;
46
+ injected?: boolean;
47
47
  type?: ContextType;
48
48
  state?: ContextState;
49
49
  previous?: OperationContext;
@@ -55,6 +55,7 @@ export interface OperationContextForkOptions {
55
55
  type: ContextType;
56
56
  state: ContextState;
57
57
  target?: string;
58
+ injected?: boolean;
58
59
  }
59
60
  export declare class OperationContext {
60
61
  target: string;
@@ -64,19 +65,22 @@ export declare class OperationContext {
64
65
  type: ContextType;
65
66
  state: ContextState;
66
67
  scope: Scope;
67
- isProtected: boolean;
68
68
  memory: Map<string, any>;
69
69
  cps: CPS | null;
70
70
  processState: OperationContextProcessState;
71
+ isProtected: boolean;
72
+ injected: boolean;
71
73
  api: Scope | null;
72
74
  locals: Scope | null;
73
75
  globals: Scope | null;
74
76
  constructor(options: OperationContextOptions);
75
77
  step(item: ASTBase): Promise<void>;
76
- exit(): Promise<OperationContext>;
77
- setPending(pending: boolean): OperationContext;
78
+ setLastActive(opc: OperationContext): OperationContext;
79
+ getLastActive(): OperationContext | null;
78
80
  isExit(): boolean;
81
+ exit(): Promise<OperationContext>;
79
82
  isPending(): boolean;
83
+ setPending(pending: boolean): OperationContext;
80
84
  lookupType(validate: (type: ContextType) => boolean): Scope;
81
85
  lookupAPI(): Scope;
82
86
  lookupGlobals(): Scope;
@@ -88,5 +92,5 @@ export declare class OperationContext {
88
92
  getCallable(path: string[]): Promise<Callable>;
89
93
  setMemory(key: string, value: any): OperationContext;
90
94
  getMemory(key: string): any;
91
- fork({ type, state, target }: OperationContextForkOptions): OperationContext;
95
+ fork({ type, state, target, injected }: OperationContextForkOptions): OperationContext;
92
96
  }
package/dist/context.js CHANGED
@@ -14,42 +14,6 @@ var __extends = (this && this.__extends) || (function () {
14
14
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
15
  };
16
16
  })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
33
- 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;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
17
  var __read = (this && this.__read) || function (o, n) {
54
18
  var m = typeof Symbol === "function" && o[Symbol.iterator];
55
19
  if (!m) return o;
@@ -81,13 +45,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
81
45
  Object.defineProperty(exports, "__esModule", { value: true });
82
46
  exports.OperationContext = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
83
47
  var map_1 = __importDefault(require("./custom-types/map"));
84
- var greybel_core_1 = require("greybel-core");
85
48
  var ContextType;
86
49
  (function (ContextType) {
87
50
  ContextType["API"] = "API";
88
51
  ContextType["GLOBAL"] = "GLOBAL";
89
52
  ContextType["FUNCTION"] = "FUNCTION";
90
- ContextType["INJECTION"] = "INJECTION";
91
53
  ContextType["EXTERNAL"] = "EXTERNAL";
92
54
  ContextType["LOOP"] = "LOOP";
93
55
  ContextType["MAP"] = "MAP";
@@ -218,7 +180,7 @@ var Debugger = /** @class */ (function () {
218
180
  return me;
219
181
  };
220
182
  Debugger.prototype.getBreakpoint = function (operationContext) {
221
- return operationContext.type !== ContextType.INJECTION && this.breakpoint;
183
+ return this.breakpoint;
222
184
  };
223
185
  Debugger.prototype.next = function () {
224
186
  var me = this;
@@ -250,41 +212,8 @@ var Debugger = /** @class */ (function () {
250
212
  var me = this;
251
213
  console.warn("Debugger is not setup.");
252
214
  console.info(operationContext);
253
- me.lastContext = operationContext;
254
215
  me.breakpoint = false;
255
216
  };
256
- Debugger.prototype.run = function (code) {
257
- return __awaiter(this, void 0, void 0, function () {
258
- var me, parser, chunk, item, context, err_1;
259
- return __generator(this, function (_a) {
260
- switch (_a.label) {
261
- case 0:
262
- me = this;
263
- _a.label = 1;
264
- case 1:
265
- _a.trys.push([1, 4, , 5]);
266
- parser = new greybel_core_1.Parser(code);
267
- chunk = parser.parseChunk();
268
- return [4 /*yield*/, me.lastContext.cps.visit(chunk)];
269
- case 2:
270
- item = _a.sent();
271
- context = me.lastContext.fork({
272
- type: ContextType.INJECTION,
273
- state: ContextState.TEMPORARY
274
- });
275
- return [4 /*yield*/, item.run(context)];
276
- case 3:
277
- _a.sent();
278
- return [3 /*break*/, 5];
279
- case 4:
280
- err_1 = _a.sent();
281
- console.error(err_1);
282
- return [3 /*break*/, 5];
283
- case 5: return [2 /*return*/];
284
- }
285
- });
286
- });
287
- };
288
217
  return Debugger;
289
218
  }());
290
219
  exports.Debugger = Debugger;
@@ -298,12 +227,14 @@ var OperationContext = /** @class */ (function () {
298
227
  me.state = options.state || ContextState.DEFAULT;
299
228
  me.scope = new Scope(me);
300
229
  me.isProtected = options.isProtected || false;
230
+ me.injected = options.injected || false;
301
231
  me.memory = new Map();
302
232
  me.debugger = options.debugger || new Debugger();
303
233
  me.cps = options.cps;
304
234
  me.processState = options.processState || {
305
235
  exit: false,
306
- pending: false
236
+ pending: false,
237
+ last: null
307
238
  };
308
239
  me.api = me.lookupAPI();
309
240
  me.globals = me.lookupGlobals();
@@ -312,13 +243,29 @@ var OperationContext = /** @class */ (function () {
312
243
  OperationContext.prototype.step = function (item) {
313
244
  var me = this;
314
245
  var dbgr = me.debugger;
315
- me.stackItem = item;
316
- if (dbgr.getBreakpoint(me)) {
317
- dbgr.interact(me, item);
318
- return dbgr.resume();
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
+ }
319
253
  }
320
254
  return Promise.resolve();
321
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
+ };
322
269
  OperationContext.prototype.exit = function () {
323
270
  var me = this;
324
271
  var state = me.processState;
@@ -335,17 +282,14 @@ var OperationContext = /** @class */ (function () {
335
282
  }
336
283
  return Promise.reject(new Error('No running process found.'));
337
284
  };
285
+ OperationContext.prototype.isPending = function () {
286
+ return this.processState.pending;
287
+ };
338
288
  OperationContext.prototype.setPending = function (pending) {
339
289
  var me = this;
340
290
  me.processState.pending = pending;
341
291
  return me;
342
292
  };
343
- OperationContext.prototype.isExit = function () {
344
- return this.processState.exit;
345
- };
346
- OperationContext.prototype.isPending = function () {
347
- return this.processState.exit;
348
- };
349
293
  OperationContext.prototype.lookupType = function (validate) {
350
294
  var me = this;
351
295
  if (validate(me.type)) {
@@ -419,7 +363,7 @@ var OperationContext = /** @class */ (function () {
419
363
  return me.memory.get(key);
420
364
  };
421
365
  OperationContext.prototype.fork = function (_a) {
422
- var type = _a.type, state = _a.state, target = _a.target;
366
+ var type = _a.type, state = _a.state, target = _a.target, injected = _a.injected;
423
367
  var me = this;
424
368
  var opc = new OperationContext({
425
369
  target: target || me.target,
@@ -428,7 +372,8 @@ var OperationContext = /** @class */ (function () {
428
372
  state: state,
429
373
  debugger: me.debugger,
430
374
  cps: me.cps,
431
- processState: me.processState
375
+ processState: me.processState,
376
+ injected: injected || me.injected
432
377
  });
433
378
  if (type !== ContextType.FUNCTION) {
434
379
  if (type !== ContextType.LOOP) {
@@ -22,7 +22,8 @@ export default class Interpreter extends EventEmitter {
22
22
  constructor(options: InterpreterOptions);
23
23
  setTarget(target: string): Interpreter;
24
24
  setDebugger(dbgr: Debugger): Interpreter;
25
- inject(code: string): Promise<Interpreter>;
25
+ inject(code: string, context?: OperationContext): Promise<Interpreter>;
26
+ injectInLastContext(code: string): Promise<Interpreter>;
26
27
  digest(customCode?: string): Promise<Interpreter>;
27
28
  exit(): Promise<OperationContext>;
28
29
  }
@@ -99,26 +99,27 @@ var Interpreter = /** @class */ (function (_super) {
99
99
  me.globalContext.debugger = dbgr;
100
100
  return me;
101
101
  };
102
- Interpreter.prototype.inject = function (code) {
102
+ Interpreter.prototype.inject = function (code, context) {
103
103
  return __awaiter(this, void 0, void 0, function () {
104
- var me, parser, chunk, body, context, err_1;
104
+ var me, parser, chunk, body, injectionCtx, err_1;
105
105
  return __generator(this, function (_a) {
106
106
  switch (_a.label) {
107
107
  case 0:
108
108
  me = this;
109
+ _a.label = 1;
110
+ case 1:
111
+ _a.trys.push([1, 4, , 5]);
109
112
  parser = new greybel_core_1.Parser(code);
110
113
  chunk = parser.parseChunk();
111
114
  return [4 /*yield*/, me.cps.visit(chunk)];
112
- case 1:
113
- body = _a.sent();
114
- _a.label = 2;
115
115
  case 2:
116
- _a.trys.push([2, 4, , 5]);
117
- context = me.globalContext.fork({
118
- type: context_1.ContextType.INJECTION,
119
- state: context_1.ContextState.TEMPORARY
116
+ body = _a.sent();
117
+ injectionCtx = (context || me.globalContext).fork({
118
+ type: context_1.ContextType.CALL,
119
+ state: context_1.ContextState.TEMPORARY,
120
+ injected: true
120
121
  });
121
- return [4 /*yield*/, body.run(context)];
122
+ return [4 /*yield*/, body.run(injectionCtx)];
122
123
  case 3:
123
124
  _a.sent();
124
125
  return [3 /*break*/, 5];
@@ -131,6 +132,19 @@ var Interpreter = /** @class */ (function (_super) {
131
132
  });
132
133
  });
133
134
  };
135
+ Interpreter.prototype.injectInLastContext = function (code) {
136
+ return __awaiter(this, void 0, void 0, function () {
137
+ var me, last;
138
+ return __generator(this, function (_a) {
139
+ me = this;
140
+ last = me.apiContext.getLastActive();
141
+ if (me.apiContext.isPending()) {
142
+ me.inject(code, last);
143
+ }
144
+ return [2 /*return*/, me];
145
+ });
146
+ });
147
+ };
134
148
  Interpreter.prototype.digest = function (customCode) {
135
149
  return __awaiter(this, void 0, void 0, function () {
136
150
  var me, code, _a, parser, chunk, body, process_1, err_2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "0.3.9",
3
+ "version": "0.4.3",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",