greybel-interpreter 4.0.16 → 4.0.17

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,8 +518,11 @@ 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));
508
- break;
521
+ callback(this, frame.self, args).then((result) => {
522
+ this.pushStack(result);
523
+ this.resume(done);
524
+ }).catch(done);
525
+ return;
509
526
  }
510
527
  case instruction_1.OpCode.SLICE: {
511
528
  const b = this.popStack();
@@ -674,9 +691,11 @@ class VM {
674
691
  case instruction_1.OpCode.BREAKPOINT: {
675
692
  if (this.debugger.getBreakpoint(this)) {
676
693
  this.debugger.interact(this, instruction.source);
677
- yield this.debugger.resume();
694
+ this.debugger.resume().then(() => {
695
+ this.resume(done);
696
+ }).catch(done);
678
697
  }
679
- break;
698
+ return;
680
699
  }
681
700
  case instruction_1.OpCode.BREAKPOINT_ENABLE: {
682
701
  this.debugger.setBreakpoint(true);
@@ -684,16 +703,23 @@ class VM {
684
703
  }
685
704
  case instruction_1.OpCode.HALT: {
686
705
  this.state = VMState.FINISHED;
687
- this.signal.off('exit', exitCallback);
706
+ this.signal.emit('done');
707
+ done();
688
708
  return;
689
709
  }
690
710
  }
691
711
  if (this.actionCount++ === this.maxActionsPerLoop) {
692
712
  this.actionCount = 0;
693
- yield (0, next_tick_1.nextTick)();
713
+ (0, set_immediate_1.setImmediate)(() => {
714
+ this.resume(done);
715
+ });
716
+ return;
694
717
  }
695
718
  }
696
- });
719
+ }
720
+ catch (err) {
721
+ done(err);
722
+ }
697
723
  }
698
724
  }
699
725
  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.17",
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;