greybel-interpreter 4.1.0 → 4.1.2

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
@@ -4,6 +4,7 @@ import { ObjectValue } from './utils/object-value';
4
4
  import { ContextTypeIntrinsics } from './context/types';
5
5
  import { Instruction } from './byte-compiler/instruction';
6
6
  import { CustomValueWithIntrinsicsResult } from './types/with-intrinsics';
7
+ import { Stack } from './utils/stack';
7
8
  export declare enum ContextType {
8
9
  Api = 0,
9
10
  Global = 1,
@@ -35,6 +36,9 @@ export interface ContextForkOptions {
35
36
  }
36
37
  export declare class OperationContext {
37
38
  previous: OperationContext;
39
+ iterators: Stack<Iterator<CustomValue> & {
40
+ index: number;
41
+ }>;
38
42
  readonly type: ContextType;
39
43
  readonly scope: Scope;
40
44
  isProtected: boolean;
package/dist/context.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OperationContext = exports.Scope = exports.ContextType = void 0;
4
4
  const map_1 = require("./types/map");
5
5
  const string_1 = require("./types/string");
6
+ const stack_1 = require("./utils/stack");
6
7
  var ContextType;
7
8
  (function (ContextType) {
8
9
  ContextType[ContextType["Api"] = 0] = "Api";
@@ -63,6 +64,7 @@ exports.Scope = Scope;
63
64
  class OperationContext {
64
65
  constructor(options) {
65
66
  var _a, _b, _c, _d, _e, _f, _g;
67
+ this.iterators = new stack_1.Stack();
66
68
  this.code = options.code;
67
69
  this.ip = 0;
68
70
  this.previous = (_a = options.previous) !== null && _a !== void 0 ? _a : null;
@@ -7,4 +7,6 @@ export declare class Stack<T> {
7
7
  peek(): T;
8
8
  includes(value: T): boolean;
9
9
  values(): T[];
10
+ get length(): number;
11
+ clear(): void;
10
12
  }
@@ -26,5 +26,12 @@ class Stack {
26
26
  values() {
27
27
  return this.stack.slice(0);
28
28
  }
29
+ get length() {
30
+ return this.stack.length;
31
+ }
32
+ clear() {
33
+ this.stack = [];
34
+ this.last = undefined;
35
+ }
29
36
  }
30
37
  exports.Stack = Stack;
package/dist/vm.d.ts CHANGED
@@ -50,7 +50,6 @@ export declare class VM {
50
50
  private readonly STACK_LIMIT;
51
51
  private sp;
52
52
  private stack;
53
- private iterators;
54
53
  private time;
55
54
  readonly target: string;
56
55
  readonly contextTypeIntrinsics: ContextTypeIntrinsics;
package/dist/vm.js CHANGED
@@ -102,7 +102,6 @@ class VM {
102
102
  this.handler = options.handler;
103
103
  this.debugger = options.debugger;
104
104
  this.environmentVariables = (_b = options.environmentVariables) !== null && _b !== void 0 ? _b : new Map();
105
- this.iterators = new stack_1.Stack();
106
105
  this.imports = (_c = options.imports) !== null && _c !== void 0 ? _c : new Map();
107
106
  this.externalFrames = (_d = options.externalFrames) !== null && _d !== void 0 ? _d : new stack_1.Stack();
108
107
  }
@@ -163,6 +162,9 @@ class VM {
163
162
  return ctx;
164
163
  }
165
164
  popFrame() {
165
+ if (this.frames.length === 1) {
166
+ return null;
167
+ }
166
168
  const frame = this.frames.pop();
167
169
  this.getFrame().ip++;
168
170
  return frame;
@@ -429,17 +431,17 @@ class VM {
429
431
  case instruction_1.OpCode.PUSH_ITERATOR: {
430
432
  const value = this.popStack();
431
433
  const iterator = value[Symbol.iterator]();
432
- this.iterators.push(iterator);
434
+ frame.iterators.push(iterator);
433
435
  break;
434
436
  }
435
437
  case instruction_1.OpCode.POP_ITERATOR: {
436
- this.iterators.pop();
438
+ frame.iterators.pop();
437
439
  break;
438
440
  }
439
441
  case instruction_1.OpCode.NEXT: {
440
442
  const nextInstruction = instruction;
441
443
  let idx = frame.get(nextInstruction.idxVariable, this.contextTypeIntrinsics).toNumber();
442
- const iterator = this.iterators.peek();
444
+ const iterator = frame.iterators.peek();
443
445
  iterator.index = ++idx;
444
446
  const iteratorResult = iterator.next();
445
447
  this.pushStack(new boolean_1.CustomBoolean(!iteratorResult.done));
@@ -684,6 +686,7 @@ class VM {
684
686
  }
685
687
  case instruction_1.OpCode.RETURN: {
686
688
  const value = this.popStack();
689
+ frame.iterators.clear();
687
690
  this.popFrame();
688
691
  this.pushStack(value !== null && value !== void 0 ? value : default_1.DefaultType.Void);
689
692
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "4.1.0",
3
+ "version": "4.1.2",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",