greybel-interpreter 4.0.16 → 4.0.18

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/vm.d.ts CHANGED
@@ -26,7 +26,8 @@ export interface FrameOptions {
26
26
  export declare enum VMState {
27
27
  PREPARATION = 0,
28
28
  PENDING = 1,
29
- FINISHED = 2
29
+ FINISHED = 2,
30
+ STOPPED = 3
30
31
  }
31
32
  export interface VMOptions {
32
33
  target: string;
@@ -73,4 +74,5 @@ export declare class VM {
73
74
  private createFrame;
74
75
  private popFrame;
75
76
  exec(): Promise<void>;
77
+ private resume;
76
78
  }
package/dist/vm.js CHANGED
@@ -25,7 +25,7 @@ const evaluation_1 = require("./vm/evaluation");
25
25
  const number_1 = require("./types/number");
26
26
  const map_1 = require("./types/map");
27
27
  const list_1 = require("./types/list");
28
- const next_tick_1 = require("./utils/next-tick");
28
+ const set_immediate_1 = require("./utils/set-immediate");
29
29
  const string_1 = require("./types/string");
30
30
  const events_1 = __importDefault(require("events"));
31
31
  const object_value_1 = require("./utils/object-value");
@@ -63,7 +63,7 @@ class Debugger {
63
63
  resolve();
64
64
  }
65
65
  else {
66
- setImmediate(check);
66
+ (0, set_immediate_1.setImmediate)(check);
67
67
  }
68
68
  };
69
69
  check();
@@ -82,11 +82,12 @@ var VMState;
82
82
  VMState[VMState["PREPARATION"] = 0] = "PREPARATION";
83
83
  VMState[VMState["PENDING"] = 1] = "PENDING";
84
84
  VMState[VMState["FINISHED"] = 2] = "FINISHED";
85
+ VMState[VMState["STOPPED"] = 3] = "STOPPED";
85
86
  })(VMState = exports.VMState || (exports.VMState = {}));
86
87
  class VM {
87
88
  constructor(options) {
88
89
  var _a, _b, _c, _d;
89
- this.ACTIONS_PER_LOOP = 1200;
90
+ this.ACTIONS_PER_LOOP = 80000;
90
91
  this.STACK_LIMIT = 512;
91
92
  this.stack = new Array(this.STACK_LIMIT);
92
93
  this.signal = new events_1.default();
@@ -134,6 +135,7 @@ class VM {
134
135
  return this.signal;
135
136
  }
136
137
  exit() {
138
+ this.state = VMState.STOPPED;
137
139
  this.signal.emit('exit');
138
140
  }
139
141
  getFrame() {
@@ -166,16 +168,28 @@ class VM {
166
168
  return frame;
167
169
  }
168
170
  exec() {
169
- var _a, _b, _c, _d, _e, _f, _g, _h;
170
171
  return __awaiter(this, void 0, void 0, function* () {
171
- let shouldContinue = true;
172
- const exitCallback = () => {
173
- shouldContinue = false;
174
- };
175
- this.time = Date.now();
176
- this.state = VMState.PENDING;
177
- this.signal.once('exit', exitCallback);
178
- while (shouldContinue) {
172
+ return new Promise((resolve, reject) => {
173
+ this.state = VMState.PENDING;
174
+ this.time = Date.now();
175
+ this.resume((err) => {
176
+ if (err) {
177
+ reject(err);
178
+ return;
179
+ }
180
+ resolve();
181
+ });
182
+ });
183
+ });
184
+ }
185
+ resume(done) {
186
+ var _a, _b, _c, _d, _e, _f, _g, _h;
187
+ try {
188
+ while (true) {
189
+ if (!this.isPending()) {
190
+ done();
191
+ return;
192
+ }
179
193
  const frame = this.getFrame();
180
194
  if (frame.code.length === frame.ip) {
181
195
  this.popFrame();
@@ -504,7 +518,16 @@ class VM {
504
518
  const value = frame.scope.value.get(arg.name);
505
519
  args.set(arg.name.toString(), value);
506
520
  }
507
- this.pushStack(yield callback(this, frame.self, args));
521
+ const immediateRet = callback(this, frame.self, args);
522
+ // Due to transformations it can happen that return values may not be PromiseLike
523
+ if (immediateRet === null || immediateRet === void 0 ? void 0 : immediateRet.then) {
524
+ immediateRet.then((ret) => {
525
+ this.pushStack(ret);
526
+ this.resume(done);
527
+ }).catch(done);
528
+ return;
529
+ }
530
+ this.pushStack(immediateRet);
508
531
  break;
509
532
  }
510
533
  case instruction_1.OpCode.SLICE: {
@@ -674,9 +697,11 @@ class VM {
674
697
  case instruction_1.OpCode.BREAKPOINT: {
675
698
  if (this.debugger.getBreakpoint(this)) {
676
699
  this.debugger.interact(this, instruction.source);
677
- yield this.debugger.resume();
700
+ this.debugger.resume().then(() => {
701
+ this.resume(done);
702
+ }).catch(done);
678
703
  }
679
- break;
704
+ return;
680
705
  }
681
706
  case instruction_1.OpCode.BREAKPOINT_ENABLE: {
682
707
  this.debugger.setBreakpoint(true);
@@ -684,16 +709,23 @@ class VM {
684
709
  }
685
710
  case instruction_1.OpCode.HALT: {
686
711
  this.state = VMState.FINISHED;
687
- this.signal.off('exit', exitCallback);
712
+ this.signal.emit('done');
713
+ done();
688
714
  return;
689
715
  }
690
716
  }
691
717
  if (this.actionCount++ === this.maxActionsPerLoop) {
692
718
  this.actionCount = 0;
693
- yield (0, next_tick_1.nextTick)();
719
+ (0, set_immediate_1.setImmediate)(() => {
720
+ this.resume(done);
721
+ });
722
+ return;
694
723
  }
695
724
  }
696
- });
725
+ }
726
+ catch (err) {
727
+ done(err);
728
+ }
697
729
  }
698
730
  }
699
731
  exports.VM = VM;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "4.0.16",
3
+ "version": "4.0.18",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -1 +0,0 @@
1
- export declare const nextTick: () => Promise<void>;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.nextTick = void 0;
4
- const set_immediate_1 = require("./set-immediate");
5
- const nextTick = () => {
6
- return new Promise((resolve) => {
7
- (0, set_immediate_1.setImmediate)(resolve);
8
- });
9
- };
10
- exports.nextTick = nextTick;