greybel-interpreter 4.0.15 → 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.
@@ -26,7 +26,7 @@ export declare class BytecodeGenerator {
26
26
  parse(code: string): ASTBase | import("greybel-core").ASTChunkAdvanced;
27
27
  compile(code: string): Promise<BytecodeCompileResult>;
28
28
  protected getCurrentPointer(): number;
29
- protected getSourceLocation(node: ASTBase): SourceLocation;
29
+ protected getSourceLocation(node: ASTBase, name?: string): SourceLocation;
30
30
  protected getInternalLocation(): SourceLocation;
31
31
  protected pushContext(): void;
32
32
  protected popContext(): BytecodeGeneratorContext;
@@ -72,10 +72,10 @@ class BytecodeGenerator {
72
72
  getCurrentPointer() {
73
73
  return this.context.peek().code.length - 1;
74
74
  }
75
- getSourceLocation(node) {
75
+ getSourceLocation(node, name) {
76
76
  const target = this.target.peek();
77
77
  return {
78
- name: node.type,
78
+ name: name !== null && name !== void 0 ? name : node.type,
79
79
  path: target,
80
80
  start: node.start,
81
81
  end: node.end
@@ -375,7 +375,7 @@ class BytecodeGenerator {
375
375
  yield this.processSubNode(node.index);
376
376
  this.push({
377
377
  op: instruction_1.OpCode.GET_SUPER_PROPERTY,
378
- source: this.getSourceLocation(node.index),
378
+ source: this.getSourceLocation(node.index, node.type),
379
379
  invoke: isInvoke
380
380
  });
381
381
  }
@@ -384,7 +384,7 @@ class BytecodeGenerator {
384
384
  yield this.processSubNode(node.index);
385
385
  this.push({
386
386
  op: instruction_1.OpCode.GET_PROPERTY,
387
- source: this.getSourceLocation(node.index),
387
+ source: this.getSourceLocation(node.index, node.type),
388
388
  invoke: isInvoke
389
389
  });
390
390
  }
@@ -898,7 +898,7 @@ class BytecodeGenerator {
898
898
  yield pushArgs();
899
899
  this.push({
900
900
  op: instruction_1.OpCode.CALL_SUPER_PROPERTY,
901
- source: this.getSourceLocation(node.base),
901
+ source: this.getSourceLocation(node.base, node.type),
902
902
  length: node.arguments.length
903
903
  });
904
904
  }
@@ -912,7 +912,7 @@ class BytecodeGenerator {
912
912
  yield pushArgs();
913
913
  this.push({
914
914
  op: instruction_1.OpCode.CALL_WITH_CONTEXT,
915
- source: this.getSourceLocation(left.identifier),
915
+ source: this.getSourceLocation(left.identifier, node.type),
916
916
  length: node.arguments.length
917
917
  });
918
918
  }
@@ -924,7 +924,7 @@ class BytecodeGenerator {
924
924
  yield pushArgs();
925
925
  this.push({
926
926
  op: instruction_1.OpCode.CALL_SUPER_PROPERTY,
927
- source: this.getSourceLocation(left.index),
927
+ source: this.getSourceLocation(left.index, node.type),
928
928
  length: node.arguments.length
929
929
  });
930
930
  }
@@ -934,7 +934,7 @@ class BytecodeGenerator {
934
934
  yield pushArgs();
935
935
  this.push({
936
936
  op: instruction_1.OpCode.CALL_WITH_CONTEXT,
937
- source: this.getSourceLocation(left.index),
937
+ source: this.getSourceLocation(left.index, node.type),
938
938
  length: node.arguments.length
939
939
  });
940
940
  }
@@ -944,7 +944,7 @@ class BytecodeGenerator {
944
944
  yield pushArgs();
945
945
  this.push({
946
946
  op: instruction_1.OpCode.CALL,
947
- source: this.getSourceLocation(left),
947
+ source: this.getSourceLocation(left, node.type),
948
948
  length: node.arguments.length
949
949
  });
950
950
  }
@@ -953,7 +953,7 @@ class BytecodeGenerator {
953
953
  yield pushArgs();
954
954
  this.push({
955
955
  op: instruction_1.OpCode.CALL,
956
- source: this.getSourceLocation(left),
956
+ source: this.getSourceLocation(left, node.type),
957
957
  length: node.arguments.length
958
958
  });
959
959
  }
@@ -148,9 +148,8 @@ class Interpreter extends events_1.EventEmitter {
148
148
  start() {
149
149
  return __awaiter(this, void 0, void 0, function* () {
150
150
  try {
151
- const process = this.vm.exec();
152
151
  this.emit('start', this);
153
- yield process;
152
+ yield this.vm.exec();
154
153
  }
155
154
  catch (err) {
156
155
  if (err instanceof error_1.PrepareError || err instanceof error_1.RuntimeError) {
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;
@@ -37,9 +38,11 @@ export interface VMOptions {
37
38
  environmentVariables?: Map<string, string>;
38
39
  imports?: Map<string, Instruction[]>;
39
40
  externalFrames?: Stack<OperationContext>;
41
+ maxActionsPerLoop?: number;
40
42
  }
41
43
  export declare class VM {
42
44
  private readonly ACTIONS_PER_LOOP;
45
+ private maxActionsPerLoop;
43
46
  private actionCount;
44
47
  private state;
45
48
  private frames;
@@ -60,6 +63,7 @@ export declare class VM {
60
63
  getTime(): number;
61
64
  isPending(): boolean;
62
65
  getStacktrace(): Instruction[];
66
+ setMaxActionsPerLoop(actions: number): this;
63
67
  getOpenHandles(): number;
64
68
  getSignal(): EventEmitter;
65
69
  exit(): void;
@@ -70,4 +74,5 @@ export declare class VM {
70
74
  private createFrame;
71
75
  private popFrame;
72
76
  exec(): Promise<void>;
77
+ private resume;
73
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,15 +82,17 @@ 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
- var _a, _b, _c;
89
- this.ACTIONS_PER_LOOP = 120;
89
+ var _a, _b, _c, _d;
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();
93
94
  this.state = VMState.PREPARATION;
95
+ this.maxActionsPerLoop = (_a = options.maxActionsPerLoop) !== null && _a !== void 0 ? _a : this.ACTIONS_PER_LOOP;
94
96
  this.actionCount = 0;
95
97
  this.sp = 0;
96
98
  this.time = -1;
@@ -99,10 +101,10 @@ class VM {
99
101
  this.contextTypeIntrinsics = options.contextTypeIntrinsics;
100
102
  this.handler = options.handler;
101
103
  this.debugger = options.debugger;
102
- this.environmentVariables = (_a = options.environmentVariables) !== null && _a !== void 0 ? _a : new Map();
104
+ this.environmentVariables = (_b = options.environmentVariables) !== null && _b !== void 0 ? _b : new Map();
103
105
  this.iterators = new stack_1.Stack();
104
- this.imports = (_b = options.imports) !== null && _b !== void 0 ? _b : new Map();
105
- this.externalFrames = (_c = options.externalFrames) !== null && _c !== void 0 ? _c : new stack_1.Stack();
106
+ this.imports = (_c = options.imports) !== null && _c !== void 0 ? _c : new Map();
107
+ this.externalFrames = (_d = options.externalFrames) !== null && _d !== void 0 ? _d : new stack_1.Stack();
106
108
  }
107
109
  getTime() {
108
110
  return this.time;
@@ -122,6 +124,10 @@ class VM {
122
124
  }
123
125
  return stacktrace;
124
126
  }
127
+ setMaxActionsPerLoop(actions) {
128
+ this.maxActionsPerLoop = actions;
129
+ return this;
130
+ }
125
131
  getOpenHandles() {
126
132
  return this.sp;
127
133
  }
@@ -129,6 +135,7 @@ class VM {
129
135
  return this.signal;
130
136
  }
131
137
  exit() {
138
+ this.state = VMState.STOPPED;
132
139
  this.signal.emit('exit');
133
140
  }
134
141
  getFrame() {
@@ -161,16 +168,28 @@ class VM {
161
168
  return frame;
162
169
  }
163
170
  exec() {
164
- var _a, _b, _c, _d, _e, _f, _g, _h;
165
171
  return __awaiter(this, void 0, void 0, function* () {
166
- let shouldContinue = true;
167
- const exitCallback = () => {
168
- shouldContinue = false;
169
- };
170
- this.time = Date.now();
171
- this.state = VMState.PENDING;
172
- this.signal.once('exit', exitCallback);
173
- 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
+ }
174
193
  const frame = this.getFrame();
175
194
  if (frame.code.length === frame.ip) {
176
195
  this.popFrame();
@@ -499,8 +518,11 @@ class VM {
499
518
  const value = frame.scope.value.get(arg.name);
500
519
  args.set(arg.name.toString(), value);
501
520
  }
502
- this.pushStack(yield callback(this, frame.self, args));
503
- break;
521
+ callback(this, frame.self, args).then((result) => {
522
+ this.pushStack(result);
523
+ this.resume(done);
524
+ }).catch(done);
525
+ return;
504
526
  }
505
527
  case instruction_1.OpCode.SLICE: {
506
528
  const b = this.popStack();
@@ -669,9 +691,11 @@ class VM {
669
691
  case instruction_1.OpCode.BREAKPOINT: {
670
692
  if (this.debugger.getBreakpoint(this)) {
671
693
  this.debugger.interact(this, instruction.source);
672
- yield this.debugger.resume();
694
+ this.debugger.resume().then(() => {
695
+ this.resume(done);
696
+ }).catch(done);
673
697
  }
674
- break;
698
+ return;
675
699
  }
676
700
  case instruction_1.OpCode.BREAKPOINT_ENABLE: {
677
701
  this.debugger.setBreakpoint(true);
@@ -679,16 +703,23 @@ class VM {
679
703
  }
680
704
  case instruction_1.OpCode.HALT: {
681
705
  this.state = VMState.FINISHED;
682
- this.signal.off('exit', exitCallback);
706
+ this.signal.emit('done');
707
+ done();
683
708
  return;
684
709
  }
685
710
  }
686
- if (this.actionCount++ === this.ACTIONS_PER_LOOP) {
711
+ if (this.actionCount++ === this.maxActionsPerLoop) {
687
712
  this.actionCount = 0;
688
- yield (0, next_tick_1.nextTick)();
713
+ (0, set_immediate_1.setImmediate)(() => {
714
+ this.resume(done);
715
+ });
716
+ return;
689
717
  }
690
718
  }
691
- });
719
+ }
720
+ catch (err) {
721
+ done(err);
722
+ }
692
723
  }
693
724
  }
694
725
  exports.VM = VM;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "4.0.15",
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;