greybel-interpreter 4.0.14 → 4.0.16

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(base),
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),
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(node),
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(node),
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(node),
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(node),
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(node),
956
+ source: this.getSourceLocation(left, node.type),
957
957
  length: node.arguments.length
958
958
  });
959
959
  }
package/dist/context.js CHANGED
@@ -137,7 +137,7 @@ class OperationContext {
137
137
  this.set(new string_1.CustomString('super'), this.super);
138
138
  }
139
139
  getCurrentInstruction() {
140
- return this.code[this.ip - 1];
140
+ return this.code[this.ip];
141
141
  }
142
142
  fork(options) {
143
143
  return new OperationContext({
@@ -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) {
@@ -1,6 +1,9 @@
1
1
  import { ASTRange } from 'miniscript-core';
2
2
  import { Instruction } from '../byte-compiler/instruction';
3
3
  interface RuntimeVM {
4
+ getFrame(): {
5
+ getCurrentInstruction: () => Instruction;
6
+ };
4
7
  getStacktrace?: () => Instruction[];
5
8
  target: string;
6
9
  }
@@ -5,7 +5,7 @@ class RuntimeError extends Error {
5
5
  constructor(message, vm, source) {
6
6
  var _a;
7
7
  super(message);
8
- this.target = vm === null || vm === void 0 ? void 0 : vm.target;
8
+ this.target = vm === null || vm === void 0 ? void 0 : vm.getFrame().getCurrentInstruction().source.path;
9
9
  this.stackTrace = (_a = vm === null || vm === void 0 ? void 0 : vm.getStacktrace()) !== null && _a !== void 0 ? _a : [];
10
10
  this.stack = this.createTrace();
11
11
  this.source = source;
package/dist/vm.d.ts CHANGED
@@ -37,9 +37,11 @@ export interface VMOptions {
37
37
  environmentVariables?: Map<string, string>;
38
38
  imports?: Map<string, Instruction[]>;
39
39
  externalFrames?: Stack<OperationContext>;
40
+ maxActionsPerLoop?: number;
40
41
  }
41
42
  export declare class VM {
42
43
  private readonly ACTIONS_PER_LOOP;
44
+ private maxActionsPerLoop;
43
45
  private actionCount;
44
46
  private state;
45
47
  private frames;
@@ -60,6 +62,7 @@ export declare class VM {
60
62
  getTime(): number;
61
63
  isPending(): boolean;
62
64
  getStacktrace(): Instruction[];
65
+ setMaxActionsPerLoop(actions: number): this;
63
66
  getOpenHandles(): number;
64
67
  getSignal(): EventEmitter;
65
68
  exit(): void;
package/dist/vm.js CHANGED
@@ -85,12 +85,13 @@ var VMState;
85
85
  })(VMState = exports.VMState || (exports.VMState = {}));
86
86
  class VM {
87
87
  constructor(options) {
88
- var _a, _b, _c;
89
- this.ACTIONS_PER_LOOP = 120;
88
+ var _a, _b, _c, _d;
89
+ this.ACTIONS_PER_LOOP = 1200;
90
90
  this.STACK_LIMIT = 512;
91
91
  this.stack = new Array(this.STACK_LIMIT);
92
92
  this.signal = new events_1.default();
93
93
  this.state = VMState.PREPARATION;
94
+ this.maxActionsPerLoop = (_a = options.maxActionsPerLoop) !== null && _a !== void 0 ? _a : this.ACTIONS_PER_LOOP;
94
95
  this.actionCount = 0;
95
96
  this.sp = 0;
96
97
  this.time = -1;
@@ -99,10 +100,10 @@ class VM {
99
100
  this.contextTypeIntrinsics = options.contextTypeIntrinsics;
100
101
  this.handler = options.handler;
101
102
  this.debugger = options.debugger;
102
- this.environmentVariables = (_a = options.environmentVariables) !== null && _a !== void 0 ? _a : new Map();
103
+ this.environmentVariables = (_b = options.environmentVariables) !== null && _b !== void 0 ? _b : new Map();
103
104
  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();
105
+ this.imports = (_c = options.imports) !== null && _c !== void 0 ? _c : new Map();
106
+ this.externalFrames = (_d = options.externalFrames) !== null && _d !== void 0 ? _d : new stack_1.Stack();
106
107
  }
107
108
  getTime() {
108
109
  return this.time;
@@ -122,6 +123,10 @@ class VM {
122
123
  }
123
124
  return stacktrace;
124
125
  }
126
+ setMaxActionsPerLoop(actions) {
127
+ this.maxActionsPerLoop = actions;
128
+ return this;
129
+ }
125
130
  getOpenHandles() {
126
131
  return this.sp;
127
132
  }
@@ -144,6 +149,7 @@ class VM {
144
149
  return this.stack[--this.sp];
145
150
  }
146
151
  createFrame(options) {
152
+ this.getFrame().ip--;
147
153
  const ctx = this.getFrame().fork({
148
154
  code: options.code,
149
155
  type: context_1.ContextType.Function,
@@ -155,7 +161,9 @@ class VM {
155
161
  return ctx;
156
162
  }
157
163
  popFrame() {
158
- return this.frames.pop();
164
+ const frame = this.frames.pop();
165
+ this.getFrame().ip++;
166
+ return frame;
159
167
  }
160
168
  exec() {
161
169
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -176,7 +184,7 @@ class VM {
176
184
  const instruction = frame.code[frame.ip++];
177
185
  switch (instruction.op) {
178
186
  case instruction_1.OpCode.NOOP: {
179
- continue;
187
+ break;
180
188
  }
181
189
  case instruction_1.OpCode.PUSH: {
182
190
  const pushInstruction = instruction;
@@ -680,7 +688,7 @@ class VM {
680
688
  return;
681
689
  }
682
690
  }
683
- if (this.actionCount++ === this.ACTIONS_PER_LOOP) {
691
+ if (this.actionCount++ === this.maxActionsPerLoop) {
684
692
  this.actionCount = 0;
685
693
  yield (0, next_tick_1.nextTick)();
686
694
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "4.0.14",
3
+ "version": "4.0.16",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",