greybel-interpreter 2.6.12 → 2.6.14

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
@@ -39,10 +39,14 @@ export declare class Debugger {
39
39
  resume(): Promise<void>;
40
40
  interact(ctx: OperationContext, _ast: ASTBase, _op: Operation): void;
41
41
  }
42
+ export interface ExitObserver {
43
+ occured: () => boolean;
44
+ close: () => void;
45
+ }
42
46
  export declare class ProcessState extends EventEmitter {
43
- isExit: boolean;
44
47
  isPending: boolean;
45
48
  last: OperationContext;
49
+ createExitObserver(): ExitObserver;
46
50
  }
47
51
  export declare class LoopState {
48
52
  isBreak: boolean;
@@ -75,6 +79,7 @@ export interface ContextForkOptions {
75
79
  target?: string;
76
80
  injected?: boolean;
77
81
  ignoreOuter?: boolean;
82
+ processState?: ProcessState;
78
83
  }
79
84
  export declare class OperationContext {
80
85
  target: string;
@@ -104,7 +109,6 @@ export declare class OperationContext {
104
109
  step(op: Operation): Promise<CustomValue>;
105
110
  setLastActive(ctx: OperationContext): OperationContext;
106
111
  getLastActive(): OperationContext;
107
- isExit(): boolean;
108
112
  isPending(): boolean;
109
113
  setPending(pending: boolean): OperationContext;
110
114
  lookupAllOfType(validate: (type: ContextType) => boolean): OperationContext[];
package/dist/context.js CHANGED
@@ -124,11 +124,23 @@ exports.Debugger = Debugger;
124
124
  class ProcessState extends events_1.EventEmitter {
125
125
  constructor() {
126
126
  super(...arguments);
127
- this.isExit = false;
128
127
  this.isPending = false;
129
128
  /* eslint-disable no-use-before-define */
130
129
  this.last = null;
131
130
  }
131
+ createExitObserver() {
132
+ let isExit = false;
133
+ const listener = () => {
134
+ isExit = true;
135
+ };
136
+ this.once('exit', listener);
137
+ return {
138
+ occured: () => isExit,
139
+ close: () => {
140
+ this.off('exit', listener);
141
+ }
142
+ };
143
+ }
132
144
  }
133
145
  exports.ProcessState = ProcessState;
134
146
  class LoopState {
@@ -203,17 +215,11 @@ class OperationContext {
203
215
  getLastActive() {
204
216
  return this.processState.last;
205
217
  }
206
- isExit() {
207
- return this.processState.isExit;
208
- }
209
218
  isPending() {
210
219
  return this.processState.isPending;
211
220
  }
212
221
  setPending(pending) {
213
222
  this.processState.isPending = pending;
214
- if (!this.processState.isPending && this.processState.isExit) {
215
- this.processState.isExit = false;
216
- }
217
223
  return this;
218
224
  }
219
225
  lookupAllOfType(validate) {
@@ -233,7 +239,6 @@ class OperationContext {
233
239
  }
234
240
  exit() {
235
241
  if (this.processState.isPending) {
236
- this.processState.isExit = true;
237
242
  this.processState.emit('exit');
238
243
  return new Promise((resolve) => {
239
244
  const check = () => {
@@ -315,7 +320,7 @@ class OperationContext {
315
320
  return this.locals.scope.get(path);
316
321
  }
317
322
  fork(options) {
318
- var _a;
323
+ var _a, _b;
319
324
  const newContext = new OperationContext({
320
325
  target: (_a = options.target) !== null && _a !== void 0 ? _a : this.target,
321
326
  stackTrace: this.stackTrace,
@@ -328,7 +333,7 @@ class OperationContext {
328
333
  debugger: this.debugger,
329
334
  handler: this.handler,
330
335
  cps: this.cps,
331
- processState: this.processState,
336
+ processState: (_b = options.processState) !== null && _b !== void 0 ? _b : this.processState,
332
337
  environmentVariables: this.environmentVariables
333
338
  });
334
339
  if (options.type !== ContextType.Function) {
@@ -30,8 +30,10 @@ class Assign extends operation_1.Operation {
30
30
  }
31
31
  handle(ctx) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
+ const exitObserver = ctx.processState.createExitObserver();
33
34
  const resolveResult = yield this.left.getResult(ctx);
34
- if (ctx.isExit()) {
35
+ exitObserver.close();
36
+ if (exitObserver.occured()) {
35
37
  return default_1.DefaultType.Void;
36
38
  }
37
39
  if (resolveResult.path.count() === 0) {
@@ -32,13 +32,15 @@ class Block extends operation_1.Operation {
32
32
  else if (ctx.type === context_1.ContextType.Function) {
33
33
  isEOL = () => ctx.functionState.isReturn;
34
34
  }
35
+ const exitObserver = ctx.processState.createExitObserver();
35
36
  for (let index = 0; index < this.stack.length; index++) {
36
- if (isEOL() || ctx.isExit()) {
37
+ if (isEOL() || exitObserver.occured()) {
37
38
  break;
38
39
  }
39
40
  const op = this.stack[index];
40
41
  yield ctx.step(op);
41
42
  }
43
+ exitObserver.close();
42
44
  return default_1.DefaultType.Void;
43
45
  });
44
46
  }
@@ -31,8 +31,10 @@ class Call extends operation_1.Operation {
31
31
  }
32
32
  handle(ctx) {
33
33
  return __awaiter(this, void 0, void 0, function* () {
34
+ const exitObserver = ctx.processState.createExitObserver();
34
35
  const resolveResult = yield this.fnRef.getResult(ctx);
35
- if (ctx.isExit()) {
36
+ exitObserver.close();
37
+ if (exitObserver.occured()) {
36
38
  return default_1.DefaultType.Void;
37
39
  }
38
40
  const valueRef = yield this.fnRef.handle(ctx, resolveResult, false);
@@ -43,6 +43,7 @@ class For extends operation_1.OperationBlock {
43
43
  return Promise.resolve(default_1.DefaultType.Void);
44
44
  }
45
45
  const loopState = new context_1.LoopState();
46
+ const exitObserver = ctx.processState.createExitObserver();
46
47
  forCtx.loopState = loopState;
47
48
  return new Promise((resolve, reject) => {
48
49
  const iterator = iteratorValue[Symbol.iterator]();
@@ -52,6 +53,7 @@ class For extends operation_1.OperationBlock {
52
53
  const iteration = () => __awaiter(this, void 0, void 0, function* () {
53
54
  try {
54
55
  if (iteratorResult.done) {
56
+ exitObserver.close();
55
57
  resolve(default_1.DefaultType.Void);
56
58
  return;
57
59
  }
@@ -62,7 +64,8 @@ class For extends operation_1.OperationBlock {
62
64
  yield this.block.handle(forCtx);
63
65
  if (loopState.isBreak ||
64
66
  forCtx.functionState.isReturn ||
65
- ctx.isExit()) {
67
+ exitObserver.occured()) {
68
+ exitObserver.close();
66
69
  resolve(default_1.DefaultType.Void);
67
70
  return;
68
71
  }
@@ -72,6 +75,7 @@ class For extends operation_1.OperationBlock {
72
75
  (0, set_immediate_1.setImmediate)(iteration);
73
76
  }
74
77
  catch (err) {
78
+ exitObserver.close();
75
79
  reject(err);
76
80
  }
77
81
  });
@@ -28,8 +28,10 @@ class FunctionReference extends operation_1.Operation {
28
28
  }
29
29
  handle(ctx) {
30
30
  return __awaiter(this, void 0, void 0, function* () {
31
+ const exitObserver = ctx.processState.createExitObserver();
31
32
  const refResult = yield this.ref.getResult(ctx);
32
- if (ctx.isExit()) {
33
+ exitObserver.close();
34
+ if (exitObserver.occured()) {
33
35
  return default_1.DefaultType.Void;
34
36
  }
35
37
  if (!(refResult.handle instanceof resolve_1.ResolveNil)) {
@@ -157,10 +157,12 @@ class Resolve extends operation_1.Operation {
157
157
  return __awaiter(this, void 0, void 0, function* () {
158
158
  let traversedPath = new path_1.Path();
159
159
  let index = 0;
160
+ const exitObserver = ctx.processState.createExitObserver();
160
161
  const maxIndex = this.path.count();
161
162
  const lastIndex = maxIndex - 1;
162
163
  for (; index < maxIndex; index++) {
163
- if (ctx.isExit()) {
164
+ if (exitObserver.occured()) {
165
+ exitObserver.close();
164
166
  return new ResolveResult(null, new ResolveNil());
165
167
  }
166
168
  const current = this.path.at(index);
@@ -211,6 +213,7 @@ class Resolve extends operation_1.Operation {
211
213
  }
212
214
  }
213
215
  }
216
+ exitObserver.close();
214
217
  return new ResolveResult(traversedPath, handle);
215
218
  });
216
219
  }
@@ -218,8 +221,10 @@ class Resolve extends operation_1.Operation {
218
221
  var _a;
219
222
  return __awaiter(this, void 0, void 0, function* () {
220
223
  if (result === null) {
224
+ const exitObserver = ctx.processState.createExitObserver();
221
225
  result = yield this.getResult(ctx);
222
- if (ctx.isExit()) {
226
+ exitObserver.close();
227
+ if (exitObserver.occured()) {
223
228
  return default_1.DefaultType.Void;
224
229
  }
225
230
  }
@@ -35,12 +35,14 @@ class While extends operation_1.OperationBlock {
35
35
  state: context_1.ContextState.Temporary
36
36
  });
37
37
  const loopState = new context_1.LoopState();
38
+ const exitObserver = ctx.processState.createExitObserver();
38
39
  whileCtx.loopState = loopState;
39
40
  return new Promise((resolve, reject) => {
40
41
  const iteration = () => __awaiter(this, void 0, void 0, function* () {
41
42
  try {
42
43
  const conditionResult = yield whileCtx.step(this.condition);
43
44
  if (!conditionResult.toTruthy()) {
45
+ exitObserver.close();
44
46
  resolve(default_1.DefaultType.Void);
45
47
  return;
46
48
  }
@@ -48,13 +50,15 @@ class While extends operation_1.OperationBlock {
48
50
  yield this.block.handle(whileCtx);
49
51
  if (loopState.isBreak ||
50
52
  whileCtx.functionState.isReturn ||
51
- ctx.isExit()) {
53
+ exitObserver.occured()) {
54
+ exitObserver.close();
52
55
  resolve(default_1.DefaultType.Void);
53
56
  return;
54
57
  }
55
58
  (0, set_immediate_1.setImmediate)(iteration);
56
59
  }
57
60
  catch (err) {
61
+ exitObserver.close();
58
62
  reject(err);
59
63
  }
60
64
  });
@@ -115,7 +115,8 @@ class CustomFunction extends base_1.CustomValue {
115
115
  const fnCtx = (_a = this.scope) === null || _a === void 0 ? void 0 : _a.fork({
116
116
  type: context_1.ContextType.Function,
117
117
  state: context_1.ContextState.Default,
118
- ignoreOuter: !this.assignOuter
118
+ ignoreOuter: !this.assignOuter,
119
+ processState: callContext.processState
119
120
  });
120
121
  const argMap = new Map();
121
122
  const hasSelf = !(self instanceof nil_1.CustomNil);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.6.12",
3
+ "version": "2.6.14",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",