greybel-interpreter 4.0.4 → 4.0.5

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.
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import { ContextOptions, OperationContext } from './context';
3
+ import { OperationContext } from './context';
4
4
  import { HandlerContainer } from './handler-container';
5
5
  import { CustomValue } from './types/base';
6
6
  import { CustomString } from './types/string';
7
7
  import { ObjectValue } from './utils/object-value';
8
- import { Debugger, VM } from './vm';
8
+ import { Debugger, VM, VMOptions } from './vm';
9
9
  import { BytecodeCompileResult } from './bytecode-generator';
10
10
  export declare const PARAMS_PROPERTY: CustomString;
11
11
  export declare const IS_GREYBEL_PROPERTY: CustomString;
@@ -20,7 +20,7 @@ export interface InterpreterOptions {
20
20
  }
21
21
  export interface InterpreterRunOptions {
22
22
  customCode?: string;
23
- ctxOptions?: ContextOptions;
23
+ vmOptions?: VMOptions;
24
24
  }
25
25
  export declare class Interpreter extends EventEmitter {
26
26
  target: string;
@@ -40,9 +40,9 @@ export declare class Interpreter extends EventEmitter {
40
40
  setHandler(handler: HandlerContainer): Interpreter;
41
41
  inject(code: string, context?: OperationContext): Promise<Interpreter>;
42
42
  injectInLastContext(code: string): Promise<Interpreter>;
43
- protected initVM(result: BytecodeCompileResult): void;
43
+ protected initVM(result: BytecodeCompileResult, options?: VMOptions): void;
44
44
  protected start(): Promise<Interpreter>;
45
- run({ customCode }?: InterpreterRunOptions): Promise<Interpreter>;
45
+ run({ customCode, vmOptions }?: InterpreterRunOptions): Promise<Interpreter>;
46
46
  resume(): Interpreter;
47
47
  pause(): Interpreter;
48
48
  exit(): void;
@@ -111,7 +111,7 @@ class Interpreter extends events_1.EventEmitter {
111
111
  throw new Error('Unable to inject into last context.');
112
112
  });
113
113
  }
114
- initVM(result) {
114
+ initVM(result, options) {
115
115
  const apiContext = new context_1.OperationContext({
116
116
  isProtected: true,
117
117
  code: [],
@@ -120,21 +120,13 @@ class Interpreter extends events_1.EventEmitter {
120
120
  type: context_1.ContextType.Global,
121
121
  code: result.code
122
122
  });
123
- const vm = new vm_1.VM({
124
- target: this.target,
125
- debugger: this.debugger,
126
- handler: this.handler,
127
- environmentVariables: this.environmentVariables,
128
- contextTypeIntrinsics: {
123
+ const vm = new vm_1.VM(Object.assign({ target: this.target, debugger: this.debugger, handler: this.handler, environmentVariables: this.environmentVariables, contextTypeIntrinsics: {
129
124
  string: string_1.CustomString.getIntrinsics().fork(),
130
125
  number: number_1.CustomNumber.getIntrinsics().fork(),
131
126
  list: list_1.CustomList.getIntrinsics().fork(),
132
127
  map: map_1.CustomMap.getIntrinsics().fork(),
133
128
  function: function_1.CustomFunction.intrinsics.fork()
134
- },
135
- globals: globalContext,
136
- imports: result.imports
137
- });
129
+ }, globals: globalContext, imports: result.imports }, options));
138
130
  const stringIntrinsics = map_1.CustomMap.createWithInitialValue(vm.contextTypeIntrinsics.string);
139
131
  const numberIntrinsics = map_1.CustomMap.createWithInitialValue(vm.contextTypeIntrinsics.number);
140
132
  const listIntrinsics = map_1.CustomMap.createWithInitialValue(vm.contextTypeIntrinsics.list);
@@ -174,7 +166,7 @@ class Interpreter extends events_1.EventEmitter {
174
166
  return this;
175
167
  });
176
168
  }
177
- run({ customCode } = {}) {
169
+ run({ customCode, vmOptions } = {}) {
178
170
  return __awaiter(this, void 0, void 0, function* () {
179
171
  const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
180
172
  const bytecodeConverter = new bytecode_generator_1.BytecodeGenerator({
@@ -183,7 +175,7 @@ class Interpreter extends events_1.EventEmitter {
183
175
  debugMode: this.debugMode
184
176
  });
185
177
  const bytecode = yield bytecodeConverter.compile(code);
186
- this.initVM(bytecode);
178
+ this.initVM(bytecode, vmOptions);
187
179
  return this.start();
188
180
  });
189
181
  }
package/dist/vm.d.ts CHANGED
@@ -1,8 +1,11 @@
1
+ /// <reference types="node" />
1
2
  import { OperationContext } from "./context";
2
3
  import { ContextTypeIntrinsics } from "./context/types";
3
4
  import { HandlerContainer } from "./handler-container";
4
5
  import { CustomValue } from "./types/base";
5
6
  import { Instruction, SourceLocation } from "./byte-compiler/instruction";
7
+ import { Stack } from "./utils/stack";
8
+ import EventEmitter from "events";
6
9
  export declare class Debugger {
7
10
  private breakpoint;
8
11
  private nextStep;
@@ -33,6 +36,7 @@ export interface VMOptions {
33
36
  debugger: Debugger;
34
37
  environmentVariables?: Map<string, string>;
35
38
  imports?: Map<string, Instruction[]>;
39
+ frames?: Stack<OperationContext>;
36
40
  }
37
41
  export declare class VM {
38
42
  private readonly ACTIONS_PER_LOOP;
@@ -56,8 +60,10 @@ export declare class VM {
56
60
  isPending(): boolean;
57
61
  getStacktrace(): Instruction[];
58
62
  getOpenHandles(): number;
63
+ getSignal(): EventEmitter;
59
64
  exit(): void;
60
65
  getFrame(): OperationContext;
66
+ getFrames(): Stack<OperationContext>;
61
67
  private pushStack;
62
68
  private popStack;
63
69
  private createFrame;
package/dist/vm.js CHANGED
@@ -86,7 +86,7 @@ var VMState;
86
86
  })(VMState = exports.VMState || (exports.VMState = {}));
87
87
  class VM {
88
88
  constructor(options) {
89
- var _a, _b;
89
+ var _a, _b, _c;
90
90
  this.ACTIONS_PER_LOOP = 120;
91
91
  this.STACK_LIMIT = 512;
92
92
  this.stack = new Array(this.STACK_LIMIT);
@@ -96,13 +96,13 @@ class VM {
96
96
  this.sp = 0;
97
97
  this.time = -1;
98
98
  this.target = options.target;
99
- this.frames = new stack_1.Stack(options.globals);
99
+ this.frames = (_a = options.frames) !== null && _a !== void 0 ? _a : new stack_1.Stack(options.globals);
100
100
  this.contextTypeIntrinsics = options.contextTypeIntrinsics;
101
101
  this.handler = options.handler;
102
102
  this.debugger = options.debugger;
103
- 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();
104
104
  this.iterators = new stack_1.Stack();
105
- this.imports = (_b = options.imports) !== null && _b !== void 0 ? _b : new Map();
105
+ this.imports = (_c = options.imports) !== null && _c !== void 0 ? _c : new Map();
106
106
  }
107
107
  getTime() {
108
108
  return this.time;
@@ -121,12 +121,18 @@ class VM {
121
121
  getOpenHandles() {
122
122
  return this.sp;
123
123
  }
124
+ getSignal() {
125
+ return this.signal;
126
+ }
124
127
  exit() {
125
128
  this.signal.emit('exit');
126
129
  }
127
130
  getFrame() {
128
131
  return this.frames.peek();
129
132
  }
133
+ getFrames() {
134
+ return this.frames;
135
+ }
130
136
  pushStack(value) {
131
137
  this.stack[this.sp++] = value;
132
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "4.0.4",
3
+ "version": "4.0.5",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",