greybel-interpreter 2.8.1 → 2.8.3

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,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import { Debugger, OperationContext } from './context';
3
+ import { ContextOptions, Debugger, OperationContext } from './context';
4
4
  import { CPS } from './cps';
5
5
  import { HandlerContainer } from './handler-container';
6
6
  import { Operation } from './operations/operation';
@@ -17,6 +17,10 @@ export interface InterpreterOptions {
17
17
  debugger?: Debugger;
18
18
  environmentVariables?: Map<string, string>;
19
19
  }
20
+ export interface InterpreterRunOptions {
21
+ customCode?: string;
22
+ ctxOptions?: ContextOptions;
23
+ }
20
24
  export declare class Interpreter extends EventEmitter {
21
25
  target: string;
22
26
  api: ObjectValue;
@@ -36,8 +40,8 @@ export declare class Interpreter extends EventEmitter {
36
40
  inject(code: string, context?: OperationContext): Promise<Interpreter>;
37
41
  injectInLastContext(code: string): Promise<Interpreter>;
38
42
  private initScopes;
39
- run(customCode?: string): Promise<Interpreter>;
40
- start(top: Operation): Promise<Interpreter>;
43
+ private start;
44
+ run({ customCode, ctxOptions }?: InterpreterRunOptions): Promise<Interpreter>;
41
45
  resume(): Interpreter;
42
46
  pause(): Interpreter;
43
47
  exit(): Promise<OperationContext>;
@@ -52,8 +52,6 @@ class Interpreter extends events_1.EventEmitter {
52
52
  throw new Error('You cannot set a debugger while a process is running.');
53
53
  }
54
54
  this.debugger = dbgr;
55
- this.apiContext.debugger = dbgr;
56
- this.globalContext.debugger = dbgr;
57
55
  return this;
58
56
  }
59
57
  setApi(newApi) {
@@ -68,8 +66,6 @@ class Interpreter extends events_1.EventEmitter {
68
66
  throw new Error('You cannot set a handler while a process is running.');
69
67
  }
70
68
  this.handler = handler;
71
- this.apiContext.handler = handler;
72
- this.globalContext.handler = handler;
73
69
  return this;
74
70
  }
75
71
  prepare(code) {
@@ -122,27 +118,21 @@ class Interpreter extends events_1.EventEmitter {
122
118
  throw new Error('Unable to inject into last context.');
123
119
  });
124
120
  }
125
- initScopes() {
121
+ initScopes(ctxOptions) {
126
122
  const cpsCtx = new cps_1.CPSContext(this.target, this.handler);
127
123
  this.cps = new cps_1.CPS(cpsCtx);
128
- const apiContext = new context_1.OperationContext({
129
- target: this.target,
130
- isProtected: true,
131
- debugger: this.debugger,
132
- handler: this.handler,
133
- cps: this.cps,
134
- environmentVariables: this.environmentVariables
135
- });
136
- const stringIntrinsics = new map_1.CustomMap(string_1.CustomString.intrinsics);
137
- const numberIntrinsics = new map_1.CustomMap(number_1.CustomNumber.intrinsics);
138
- const listIntrinsics = new map_1.CustomMap(list_1.CustomList.intrinsics);
139
- const mapIntrinsics = new map_1.CustomMap(map_1.CustomMap.intrinsics);
140
- const funcRefIntrinsics = new map_1.CustomMap(function_1.CustomFunction.intrinsics);
141
- apiContext.contextTypeIntrinsics.string = stringIntrinsics.value;
142
- apiContext.contextTypeIntrinsics.number = numberIntrinsics.value;
143
- apiContext.contextTypeIntrinsics.list = listIntrinsics.value;
144
- apiContext.contextTypeIntrinsics.map = mapIntrinsics.value;
145
- apiContext.contextTypeIntrinsics.function = funcRefIntrinsics.value;
124
+ const apiContext = new context_1.OperationContext(Object.assign({ target: this.target, isProtected: true, debugger: this.debugger, handler: this.handler, cps: this.cps, environmentVariables: this.environmentVariables, contextTypeIntrinsics: {
125
+ string: string_1.CustomString.getIntrinsics().fork(),
126
+ number: number_1.CustomNumber.getIntrinsics().fork(),
127
+ list: list_1.CustomList.getIntrinsics().fork(),
128
+ map: map_1.CustomMap.getIntrinsics().fork(),
129
+ function: function_1.CustomFunction.intrinsics.fork()
130
+ } }, ctxOptions));
131
+ const stringIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.string);
132
+ const numberIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.number);
133
+ const listIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.list);
134
+ const mapIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.map);
135
+ const funcRefIntrinsics = map_1.CustomMap.createWithInitialValue(apiContext.contextTypeIntrinsics.function);
146
136
  apiContext.scope.set(new string_1.CustomString('string'), stringIntrinsics);
147
137
  apiContext.scope.set(new string_1.CustomString('number'), numberIntrinsics);
148
138
  apiContext.scope.set(new string_1.CustomString('list'), listIntrinsics);
@@ -159,19 +149,8 @@ class Interpreter extends events_1.EventEmitter {
159
149
  this.apiContext = apiContext;
160
150
  this.globalContext = globalContext;
161
151
  }
162
- run(customCode) {
163
- return __awaiter(this, void 0, void 0, function* () {
164
- this.initScopes();
165
- const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
166
- const top = yield this.prepare(code);
167
- return this.start(top);
168
- });
169
- }
170
152
  start(top) {
171
153
  return __awaiter(this, void 0, void 0, function* () {
172
- if (this.apiContext !== null && this.apiContext.isPending()) {
173
- throw new Error('Process already running.');
174
- }
175
154
  try {
176
155
  this.apiContext.setPending(true);
177
156
  const process = top.handle(this.globalContext);
@@ -193,6 +172,17 @@ class Interpreter extends events_1.EventEmitter {
193
172
  return this;
194
173
  });
195
174
  }
175
+ run({ customCode, ctxOptions } = {}) {
176
+ return __awaiter(this, void 0, void 0, function* () {
177
+ if (this.apiContext !== null && this.apiContext.isPending()) {
178
+ throw new Error('Process already running.');
179
+ }
180
+ this.initScopes(ctxOptions);
181
+ const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
182
+ const top = yield this.prepare(code);
183
+ return this.start(top);
184
+ });
185
+ }
196
186
  resume() {
197
187
  if (this.apiContext !== null && this.apiContext.isPending()) {
198
188
  this.debugger.setBreakpoint(false);
@@ -74,7 +74,7 @@ class FunctionOperation extends operation_1.Operation {
74
74
  fnCtx.functionState = functionState;
75
75
  yield this.block.handle(fnCtx);
76
76
  return functionState.value;
77
- }), assignOuter);
77
+ }), [], assignOuter);
78
78
  for (const item of this.args) {
79
79
  func.addArgument(item.name, item.op);
80
80
  }
@@ -26,7 +26,7 @@ export declare class CustomFunction extends CustomValue {
26
26
  static createExternalAnonymous(callback: Callback): CustomFunction;
27
27
  static createExternal(name: string, callback: Callback): CustomFunction;
28
28
  static createExternalWithSelf(name: string, callback: Callback): CustomFunction;
29
- constructor(scope: OperationContext, name: string, callback: Callback, assignOuter?: boolean);
29
+ constructor(scope: OperationContext, name: string, callback: Callback, argumentDefs?: Array<Argument>, assignOuter?: boolean);
30
30
  addArgument(name: string, defaultValue?: Operation | CustomValue): CustomFunction;
31
31
  fork(): CustomFunction;
32
32
  forkAs(name: string): CustomFunction;
@@ -51,12 +51,12 @@ class CustomFunction extends base_1.CustomValue {
51
51
  static createExternalWithSelf(name, callback) {
52
52
  return new CustomFunction(null, name, callback).addArgument(exports.SELF_NAMESPACE);
53
53
  }
54
- constructor(scope, name, callback, assignOuter = false) {
54
+ constructor(scope, name, callback, argumentDefs = [], assignOuter = false) {
55
55
  super();
56
56
  this.scope = scope;
57
57
  this.name = name;
58
58
  this.value = callback;
59
- this.argumentDefs = [];
59
+ this.argumentDefs = argumentDefs;
60
60
  this.assignOuter = assignOuter;
61
61
  this._nextContext = null;
62
62
  }
@@ -65,10 +65,10 @@ class CustomFunction extends base_1.CustomValue {
65
65
  return this;
66
66
  }
67
67
  fork() {
68
- return new CustomFunction(this.scope, this.name, this.value);
68
+ return new CustomFunction(this.scope, this.name, this.value, this.argumentDefs);
69
69
  }
70
70
  forkAs(name) {
71
- return new CustomFunction(this.scope, name, this.value);
71
+ return new CustomFunction(this.scope, name, this.value, this.argumentDefs);
72
72
  }
73
73
  getCustomType() {
74
74
  return 'function';
@@ -12,6 +12,7 @@ export declare class ObjectValue {
12
12
  entries(): ObjectValueKeyPair[];
13
13
  get size(): number;
14
14
  forEach(callback: (value: CustomValue, key: CustomValue, map: ObjectValue) => any): void;
15
+ fork(): ObjectValue;
15
16
  extend(objVal: ObjectValue): this;
16
17
  clear(): void;
17
18
  }
@@ -56,6 +56,13 @@ class ObjectValue {
56
56
  callback(value, key, this);
57
57
  }
58
58
  }
59
+ fork() {
60
+ const newObject = new ObjectValue();
61
+ for (const [key, value] of this.entries()) {
62
+ newObject.set(key.fork(), value.fork());
63
+ }
64
+ return newObject;
65
+ }
59
66
  extend(objVal) {
60
67
  for (const [key, value] of objVal.entries()) {
61
68
  this.set(key, value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.8.1",
3
+ "version": "2.8.3",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",