greybel-interpreter 2.7.2 → 2.8.0

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.
@@ -0,0 +1,8 @@
1
+ import { ObjectValue } from '../utils/object-value';
2
+ export type ContextTypeIntrinsics = {
3
+ map: ObjectValue;
4
+ list: ObjectValue;
5
+ number: ObjectValue;
6
+ string: ObjectValue;
7
+ function: ObjectValue;
8
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/context.d.ts CHANGED
@@ -9,6 +9,7 @@ import { CustomMap } from './types/map';
9
9
  import { CustomNil } from './types/nil';
10
10
  import { ObjectValue } from './utils/object-value';
11
11
  import { Path } from './utils/path';
12
+ import { ContextTypeIntrinsics } from './context/types';
12
13
  export declare enum ContextType {
13
14
  Api = 0,
14
15
  Global = 1,
@@ -25,7 +26,7 @@ export declare enum ContextState {
25
26
  export declare class Scope extends CustomMap {
26
27
  private readonly context;
27
28
  constructor(context: OperationContext);
28
- get(path: Path<CustomValue> | CustomValue): CustomValue;
29
+ get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
29
30
  }
30
31
  export declare class Debugger {
31
32
  private breakpoint;
@@ -72,6 +73,7 @@ export interface ContextOptions {
72
73
  processState?: ProcessState;
73
74
  environmentVariables?: Map<string, string>;
74
75
  ignoreOuter?: boolean;
76
+ contextTypeIntrinsics?: ContextTypeIntrinsics;
75
77
  }
76
78
  export interface ContextForkOptions {
77
79
  type: ContextType;
@@ -87,6 +89,7 @@ export declare class OperationContext {
87
89
  debugger: Debugger;
88
90
  environmentVariables: Map<string, string>;
89
91
  handler: HandlerContainer;
92
+ contextTypeIntrinsics: ContextTypeIntrinsics;
90
93
  previous: OperationContext;
91
94
  readonly type: ContextType;
92
95
  readonly state: ContextState;
package/dist/context.js CHANGED
@@ -39,10 +39,10 @@ class Scope extends map_1.CustomMap {
39
39
  super();
40
40
  this.context = context;
41
41
  }
42
- get(path) {
43
- var _a, _b, _c;
42
+ get(path, typeIntrinsics) {
43
+ var _a, _b, _c, _d;
44
44
  if (path instanceof base_1.CustomValue) {
45
- return this.get(new path_1.Path([path]));
45
+ return this.get(new path_1.Path([path]), typeIntrinsics);
46
46
  }
47
47
  if (path.count() === 0) {
48
48
  return this;
@@ -50,19 +50,20 @@ class Scope extends map_1.CustomMap {
50
50
  const traversalPath = path.clone();
51
51
  const current = traversalPath.next();
52
52
  if (this.has(path)) {
53
- return super.get(path);
53
+ return super.get(path, typeIntrinsics);
54
54
  }
55
55
  else if ((_a = this.context.outer) === null || _a === void 0 ? void 0 : _a.scope.has(path)) {
56
- return this.context.outer.scope.get(path);
56
+ return this.context.outer.scope.get(path, typeIntrinsics);
57
57
  }
58
58
  else if ((_b = this.context.globals) === null || _b === void 0 ? void 0 : _b.scope.has(path)) {
59
- return this.context.globals.scope.get(path);
59
+ return this.context.globals.scope.get(path, typeIntrinsics);
60
60
  }
61
61
  else if ((_c = this.context.api) === null || _c === void 0 ? void 0 : _c.scope.has(path)) {
62
- return this.context.api.scope.get(path);
62
+ return this.context.api.scope.get(path, typeIntrinsics);
63
63
  }
64
- else if (traversalPath.count() === 0 && map_1.CustomMap.getIntrinsics().has(current)) {
65
- return map_1.CustomMap.getIntrinsics().get(current);
64
+ const intrinsics = (_d = typeIntrinsics.map) !== null && _d !== void 0 ? _d : map_1.CustomMap.getIntrinsics();
65
+ if (traversalPath.count() === 0 && intrinsics.has(current)) {
66
+ return intrinsics.get(current);
66
67
  }
67
68
  throw new Error(`Unknown path ${path.toString()}.`);
68
69
  }
@@ -161,7 +162,7 @@ class FunctionState {
161
162
  exports.FunctionState = FunctionState;
162
163
  class OperationContext {
163
164
  constructor(options = {}) {
164
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
165
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
165
166
  this.target = (_a = options.target) !== null && _a !== void 0 ? _a : 'unknown';
166
167
  this.stackTrace = (_b = options.stackTrace) !== null && _b !== void 0 ? _b : [];
167
168
  this.previous = (_c = options.previous) !== null && _c !== void 0 ? _c : null;
@@ -172,15 +173,22 @@ class OperationContext {
172
173
  this.injected = (_g = options.injected) !== null && _g !== void 0 ? _g : false;
173
174
  this.debugger = (_h = options.debugger) !== null && _h !== void 0 ? _h : new Debugger();
174
175
  this.handler = (_j = options.handler) !== null && _j !== void 0 ? _j : new handler_container_1.HandlerContainer();
175
- this.cps = (_k = options.cps) !== null && _k !== void 0 ? _k : null;
176
- this.processState = (_l = options.processState) !== null && _l !== void 0 ? _l : new ProcessState();
177
- this.environmentVariables = (_m = options.environmentVariables) !== null && _m !== void 0 ? _m : new Map();
176
+ this.contextTypeIntrinsics = (_k = options.contextTypeIntrinsics) !== null && _k !== void 0 ? _k : {
177
+ string: null,
178
+ number: null,
179
+ list: null,
180
+ map: null,
181
+ function: null
182
+ };
183
+ this.cps = (_l = options.cps) !== null && _l !== void 0 ? _l : null;
184
+ this.processState = (_m = options.processState) !== null && _m !== void 0 ? _m : new ProcessState();
185
+ this.environmentVariables = (_o = options.environmentVariables) !== null && _o !== void 0 ? _o : new Map();
178
186
  this.functionState = new FunctionState();
179
187
  this.loopState = new LoopState();
180
188
  this.api = this.lookupApi();
181
189
  this.globals = this.lookupGlobals();
182
- this.locals = (_o = this.lookupLocals()) !== null && _o !== void 0 ? _o : this;
183
- this.outer = (_p = (options.ignoreOuter ? null : this.lookupOuter())) !== null && _p !== void 0 ? _p : this.globals;
190
+ this.locals = (_p = this.lookupLocals()) !== null && _p !== void 0 ? _p : this;
191
+ this.outer = (_q = (options.ignoreOuter ? null : this.lookupOuter())) !== null && _q !== void 0 ? _q : this.globals;
184
192
  }
185
193
  isIgnoredInDebugging(op) {
186
194
  return op instanceof operation_1.OperationBlock || op instanceof noop_1.Noop;
@@ -317,7 +325,7 @@ class OperationContext {
317
325
  if (this.state === ContextState.Temporary) {
318
326
  return (_a = this.previous) === null || _a === void 0 ? void 0 : _a.get(path);
319
327
  }
320
- return this.locals.scope.get(path);
328
+ return this.locals.scope.get(path, this.contextTypeIntrinsics);
321
329
  }
322
330
  fork(options) {
323
331
  var _a, _b;
@@ -334,7 +342,8 @@ class OperationContext {
334
342
  handler: this.handler,
335
343
  cps: this.cps,
336
344
  processState: (_b = options.processState) !== null && _b !== void 0 ? _b : this.processState,
337
- environmentVariables: this.environmentVariables
345
+ environmentVariables: this.environmentVariables,
346
+ contextTypeIntrinsics: this.contextTypeIntrinsics
338
347
  });
339
348
  if (options.type !== ContextType.Function) {
340
349
  if (options.type !== ContextType.Loop) {
@@ -18,7 +18,6 @@ export interface InterpreterOptions {
18
18
  environmentVariables?: Map<string, string>;
19
19
  }
20
20
  export declare class Interpreter extends EventEmitter {
21
- static clearAllIntrinsics(): void;
22
21
  target: string;
23
22
  api: ObjectValue;
24
23
  params: Array<string>;
@@ -36,6 +35,7 @@ export declare class Interpreter extends EventEmitter {
36
35
  prepare(code: string): Promise<Operation>;
37
36
  inject(code: string, context?: OperationContext): Promise<Interpreter>;
38
37
  injectInLastContext(code: string): Promise<Interpreter>;
38
+ private initScopes;
39
39
  run(customCode?: string): Promise<Interpreter>;
40
40
  start(top: Operation): Promise<Interpreter>;
41
41
  resume(): Interpreter;
@@ -28,13 +28,6 @@ const boolean_1 = require("./types/boolean");
28
28
  exports.PARAMS_PROPERTY = new string_1.CustomString('params');
29
29
  exports.IS_GREYBEL_PROPERTY = new string_1.CustomString('IS_GREYBEL');
30
30
  class Interpreter extends events_1.EventEmitter {
31
- static clearAllIntrinsics() {
32
- number_1.CustomNumber.clearIntrinsics();
33
- string_1.CustomString.clearIntrinsics();
34
- function_1.CustomFunction.intrinsics.clear();
35
- list_1.CustomList.clearIntrinsics();
36
- map_1.CustomMap.clearIntrinsics();
37
- }
38
31
  constructor(options) {
39
32
  var _a, _b, _c, _d, _e, _f;
40
33
  super();
@@ -52,25 +45,11 @@ class Interpreter extends events_1.EventEmitter {
52
45
  throw new Error('You cannot set a target while a process is running.');
53
46
  }
54
47
  this.target = target;
55
- const cpsCtx = new cps_1.CPSContext(target, this.handler);
56
- this.cps = new cps_1.CPS(cpsCtx);
57
- this.apiContext = new context_1.OperationContext({
58
- target,
59
- isProtected: true,
60
- debugger: this.debugger,
61
- handler: this.handler,
62
- cps: this.cps,
63
- environmentVariables: this.environmentVariables
64
- });
65
- this.globalContext = this.apiContext.fork({
66
- type: context_1.ContextType.Global,
67
- state: context_1.ContextState.Default
68
- });
69
48
  return this;
70
49
  }
71
50
  setDebugger(dbgr) {
72
51
  if (this.apiContext !== null && this.apiContext.isPending()) {
73
- throw new Error('You cannot set a target while a process is running.');
52
+ throw new Error('You cannot set a debugger while a process is running.');
74
53
  }
75
54
  this.debugger = dbgr;
76
55
  this.apiContext.debugger = dbgr;
@@ -79,14 +58,14 @@ class Interpreter extends events_1.EventEmitter {
79
58
  }
80
59
  setApi(newApi) {
81
60
  if (this.apiContext !== null && this.apiContext.isPending()) {
82
- throw new Error('You cannot set a target while a process is running.');
61
+ throw new Error('You cannot set an api object while a process is running.');
83
62
  }
84
63
  this.api = newApi;
85
64
  return this;
86
65
  }
87
66
  setHandler(handler) {
88
67
  if (this.apiContext !== null && this.apiContext.isPending()) {
89
- throw new Error('You cannot set a target while a process is running.');
68
+ throw new Error('You cannot set a handler while a process is running.');
90
69
  }
91
70
  this.handler = handler;
92
71
  this.apiContext.handler = handler;
@@ -143,8 +122,46 @@ class Interpreter extends events_1.EventEmitter {
143
122
  throw new Error('Unable to inject into last context.');
144
123
  });
145
124
  }
125
+ initScopes() {
126
+ const cpsCtx = new cps_1.CPSContext(this.target, this.handler);
127
+ 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;
146
+ apiContext.scope.set(new string_1.CustomString('string'), stringIntrinsics);
147
+ apiContext.scope.set(new string_1.CustomString('number'), numberIntrinsics);
148
+ apiContext.scope.set(new string_1.CustomString('list'), listIntrinsics);
149
+ apiContext.scope.set(new string_1.CustomString('map'), mapIntrinsics);
150
+ apiContext.scope.set(new string_1.CustomString('funcRef'), funcRefIntrinsics);
151
+ apiContext.scope.extend(this.api);
152
+ const globalContext = apiContext.fork({
153
+ type: context_1.ContextType.Global,
154
+ state: context_1.ContextState.Default
155
+ });
156
+ const newParams = new list_1.CustomList(this.params.map((item) => new string_1.CustomString(item)));
157
+ globalContext.scope.set(exports.IS_GREYBEL_PROPERTY, new boolean_1.CustomBoolean(true));
158
+ globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
159
+ this.apiContext = apiContext;
160
+ this.globalContext = globalContext;
161
+ }
146
162
  run(customCode) {
147
163
  return __awaiter(this, void 0, void 0, function* () {
164
+ this.initScopes();
148
165
  const code = customCode !== null && customCode !== void 0 ? customCode : (yield this.handler.resourceHandler.get(this.target));
149
166
  const top = yield this.prepare(code);
150
167
  return this.start(top);
@@ -155,20 +172,6 @@ class Interpreter extends events_1.EventEmitter {
155
172
  if (this.apiContext !== null && this.apiContext.isPending()) {
156
173
  throw new Error('Process already running.');
157
174
  }
158
- const stringIntrinsics = map_1.CustomMap.createWithInitialValue(string_1.CustomString.intrinsics);
159
- const numberIntrinsics = map_1.CustomMap.createWithInitialValue(number_1.CustomNumber.intrinsics);
160
- const listIntrinsics = map_1.CustomMap.createWithInitialValue(list_1.CustomList.intrinsics);
161
- const mapIntrinsics = map_1.CustomMap.createWithInitialValue(map_1.CustomMap.intrinsics);
162
- const funcRefIntrinsics = map_1.CustomMap.createWithInitialValue(function_1.CustomFunction.intrinsics);
163
- this.apiContext.set(new string_1.CustomString('string'), stringIntrinsics);
164
- this.apiContext.set(new string_1.CustomString('number'), numberIntrinsics);
165
- this.apiContext.set(new string_1.CustomString('list'), listIntrinsics);
166
- this.apiContext.set(new string_1.CustomString('map'), mapIntrinsics);
167
- this.apiContext.set(new string_1.CustomString('funcRef'), funcRefIntrinsics);
168
- this.apiContext.extend(this.api);
169
- const newParams = new list_1.CustomList(this.params.map((item) => new string_1.CustomString(item)));
170
- this.globalContext.scope.set(exports.IS_GREYBEL_PROPERTY, new boolean_1.CustomBoolean(true));
171
- this.globalContext.scope.set(exports.PARAMS_PROPERTY, newParams);
172
175
  try {
173
176
  this.apiContext.setPending(true);
174
177
  const process = top.handle(this.globalContext);
@@ -43,8 +43,7 @@ class Call extends operation_1.Operation {
43
43
  }
44
44
  if (valueRef instanceof function_1.CustomFunction) {
45
45
  const func = valueRef;
46
- const next = func.getNextContext();
47
- if (this.fnRef.path.isSuper() && ctx.functionState.context && next) {
46
+ if (this.fnRef.path.isSuper() && ctx.functionState.context) {
48
47
  return func.run(ctx.functionState.context, fnArgs, ctx);
49
48
  }
50
49
  return func.run(resolveResult.handle, fnArgs, ctx);
@@ -330,7 +330,7 @@ class Evaluate extends operation_1.Operation {
330
330
  case greyscript_core_1.ASTType.IsaExpression: {
331
331
  const left = yield this.resolve(ctx, expr.left);
332
332
  const right = yield this.resolve(ctx, expr.right);
333
- return new boolean_1.CustomBoolean(left.instanceOf(right));
333
+ return new boolean_1.CustomBoolean(left.instanceOf(right, ctx.contextTypeIntrinsics));
334
334
  }
335
335
  case greyscript_core_1.ASTType.BinaryExpression:
336
336
  return this.resolveBinaryExpression(ctx, expr);
@@ -39,7 +39,7 @@ class FunctionReference extends operation_1.Operation {
39
39
  return refResult.handle;
40
40
  }
41
41
  const customValueCtx = refResult.handle;
42
- return customValueCtx.get(refResult.path);
42
+ return customValueCtx.get(refResult.path, ctx.contextTypeIntrinsics);
43
43
  }
44
44
  return ctx.get(refResult.path);
45
45
  });
@@ -45,7 +45,7 @@ class Import extends operation_1.Operation {
45
45
  importCtx.locals.scope.set(exports.MODULE_PROPERTY, new map_1.CustomMap());
46
46
  yield this.top.handle(importCtx);
47
47
  const item = importCtx.locals.scope.has(exports.EXPORTS_PATH)
48
- ? importCtx.scope.get(exports.EXPORTS_PATH)
48
+ ? importCtx.scope.get(exports.EXPORTS_PATH, ctx.contextTypeIntrinsics)
49
49
  : default_1.DefaultType.Void;
50
50
  const identifier = this.item.name;
51
51
  ctx.set(new string_1.CustomString(identifier.name), item);
@@ -179,7 +179,7 @@ class Resolve extends operation_1.Operation {
179
179
  if (!(handle instanceof ResolveNil)) {
180
180
  if (handle instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
181
181
  const customValueCtx = handle;
182
- handle = customValueCtx.get(traversedPath);
182
+ handle = customValueCtx.get(traversedPath, ctx.contextTypeIntrinsics);
183
183
  }
184
184
  else {
185
185
  throw new Error(`Unknown path ${traversedPath.toString()}.`);
@@ -237,7 +237,7 @@ class Resolve extends operation_1.Operation {
237
237
  }
238
238
  if (result.handle instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
239
239
  const customValueCtx = result.handle;
240
- const { value: child, origin } = customValueCtx.getWithOrigin(result.path);
240
+ const { value: child, origin } = customValueCtx.getWithOrigin(result.path, ctx.contextTypeIntrinsics);
241
241
  const next = (0, get_super_1.getSuper)(origin);
242
242
  if (child instanceof function_1.CustomFunction) {
243
243
  child.setNextContext(next);
@@ -1,3 +1,4 @@
1
+ import { ContextTypeIntrinsics } from '../context/types';
1
2
  export declare abstract class CustomValue {
2
3
  abstract value: any;
3
4
  abstract getCustomType(): string;
@@ -7,6 +8,6 @@ export declare abstract class CustomValue {
7
8
  abstract toString(): string;
8
9
  abstract toTruthy(): boolean;
9
10
  abstract fork(): CustomValue;
10
- abstract instanceOf(value: CustomValue): boolean;
11
+ abstract instanceOf(value: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
11
12
  abstract hash(recursionDepth?: number): number;
12
13
  }
@@ -1,4 +1,5 @@
1
1
  import { OperationContext } from '../context';
2
+ import { ContextTypeIntrinsics } from '../context/types';
2
3
  import { Operation } from '../operations/operation';
3
4
  import { ObjectValue } from '../utils/object-value';
4
5
  import { CustomValue } from './base';
@@ -35,7 +36,7 @@ export declare class CustomFunction extends CustomValue {
35
36
  toJSON(): string;
36
37
  toString(): string;
37
38
  toTruthy(): boolean;
38
- instanceOf(v: CustomValue): boolean;
39
+ instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
39
40
  setNextContext(value: CustomValue): this;
40
41
  getNextContext(): CustomValue;
41
42
  run(self: CustomValue, args: Array<CustomValue>, callContext: OperationContext): Promise<CustomValue>;
@@ -106,8 +106,9 @@ class CustomFunction extends base_1.CustomValue {
106
106
  toTruthy() {
107
107
  return true;
108
108
  }
109
- instanceOf(v) {
110
- return v.value === CustomFunction.intrinsics;
109
+ instanceOf(v, typeIntrinsics) {
110
+ var _a;
111
+ return v.value === ((_a = typeIntrinsics.function) !== null && _a !== void 0 ? _a : CustomFunction.intrinsics);
111
112
  }
112
113
  setNextContext(value) {
113
114
  this._nextContext = value;
@@ -1,3 +1,4 @@
1
+ import { ContextTypeIntrinsics } from '../context/types';
1
2
  import { ObjectValue } from '../utils/object-value';
2
3
  import { Path } from '../utils/path';
3
4
  import { CustomValue } from './base';
@@ -20,14 +21,14 @@ export declare class CustomList extends CustomObject {
20
21
  toNumber(): number;
21
22
  toInt(): number;
22
23
  toTruthy(): boolean;
23
- instanceOf(v: CustomValue): boolean;
24
+ instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
24
25
  slice(a: CustomValue, b: CustomValue): CustomList;
25
26
  extend(list: CustomList | Array<CustomValue>): CustomList;
26
27
  [Symbol.iterator](): CustomListIterator;
27
28
  getItemIndex(index: number): number;
28
29
  has(path: Path<CustomValue> | CustomValue): boolean;
29
30
  set(path: Path<CustomValue> | CustomValue, newValue: CustomValue): void;
30
- get(path: Path<CustomValue> | CustomValue): CustomValue;
31
- getWithOrigin(path: Path<CustomValue> | CustomValue): CustomValueWithIntrinsicsResult;
31
+ get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
32
+ getWithOrigin(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValueWithIntrinsicsResult;
32
33
  hash(recursionDepth?: number): number;
33
34
  }
@@ -62,8 +62,9 @@ class CustomList extends with_intrinsics_1.CustomObject {
62
62
  toTruthy() {
63
63
  return this.value.length > 0;
64
64
  }
65
- instanceOf(v) {
66
- return v.value === CustomList.intrinsics;
65
+ instanceOf(v, typeIntrinsics) {
66
+ var _a;
67
+ return v.value === ((_a = typeIntrinsics.list) !== null && _a !== void 0 ? _a : CustomList.intrinsics);
67
68
  }
68
69
  slice(a, b) {
69
70
  return new CustomList(this.value.slice(a.toNumber(), b.toNumber()));
@@ -131,9 +132,10 @@ class CustomList extends with_intrinsics_1.CustomObject {
131
132
  }
132
133
  throw new Error(`Index is not a number.`);
133
134
  }
134
- get(path) {
135
+ get(path, typeIntrinsics) {
136
+ var _a;
135
137
  if (path instanceof base_1.CustomValue) {
136
- return this.get(new path_1.Path([path]));
138
+ return this.get(new path_1.Path([path]), typeIntrinsics);
137
139
  }
138
140
  const traversalPath = path.clone();
139
141
  const current = traversalPath.next();
@@ -143,7 +145,7 @@ class CustomList extends with_intrinsics_1.CustomObject {
143
145
  const sub = this.value[currentIndex];
144
146
  if (traversalPath.count() > 0) {
145
147
  if (sub instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
146
- return sub.get(traversalPath);
148
+ return sub.get(traversalPath, typeIntrinsics);
147
149
  }
148
150
  }
149
151
  else if (traversalPath.count() === 0) {
@@ -152,14 +154,15 @@ class CustomList extends with_intrinsics_1.CustomObject {
152
154
  }
153
155
  throw new Error(`Index error (list index ${currentIndex} out of range).`);
154
156
  }
155
- else if (path.count() === 1 && CustomList.getIntrinsics().has(current)) {
156
- return CustomList.getIntrinsics().get(current);
157
+ const intrinsics = (_a = typeIntrinsics.list) !== null && _a !== void 0 ? _a : CustomList.getIntrinsics();
158
+ if (path.count() === 1 && intrinsics.has(current)) {
159
+ return intrinsics.get(current);
157
160
  }
158
161
  throw new Error(`Unknown path in list ${path.toString()}.`);
159
162
  }
160
- getWithOrigin(path) {
163
+ getWithOrigin(path, typeIntrinsics) {
161
164
  return {
162
- value: this.get(path),
165
+ value: this.get(path, typeIntrinsics),
163
166
  origin: null
164
167
  };
165
168
  }
@@ -1,3 +1,4 @@
1
+ import { ContextTypeIntrinsics } from '../context/types';
1
2
  import { ObjectValue } from '../utils/object-value';
2
3
  import { Path } from '../utils/path';
3
4
  import { CustomValue } from './base';
@@ -26,13 +27,13 @@ export declare class CustomMap extends CustomObject {
26
27
  toNumber(): number;
27
28
  toInt(): number;
28
29
  toTruthy(): boolean;
29
- instanceOf(v: CustomValue): boolean;
30
+ instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
30
31
  [Symbol.iterator](): CustomMapIterator;
31
32
  extend(map: CustomMap | ObjectValue): CustomMap;
32
33
  has(path: Path<CustomValue> | CustomValue): boolean;
33
34
  set(path: Path<CustomValue> | CustomValue, newValue: CustomValue): void;
34
- get(path: Path<CustomValue> | CustomValue): CustomValue;
35
- getWithOrigin(path: Path<CustomValue> | CustomValue): CustomValueWithIntrinsicsResult;
35
+ get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
36
+ getWithOrigin(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValueWithIntrinsicsResult;
36
37
  createInstance(): CustomMap;
37
38
  getIsa(): CustomMap | null;
38
39
  hash(recursionDepth?: number): number;
package/dist/types/map.js CHANGED
@@ -82,7 +82,8 @@ class CustomMap extends with_intrinsics_1.CustomObject {
82
82
  toTruthy() {
83
83
  return this.value.size > 0;
84
84
  }
85
- instanceOf(v) {
85
+ instanceOf(v, typeIntrinsics) {
86
+ var _a;
86
87
  if (v instanceof CustomMap) {
87
88
  let current = this;
88
89
  while ((current = current.getIsa())) {
@@ -90,7 +91,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
90
91
  return true;
91
92
  }
92
93
  }
93
- return v.value === CustomMap.intrinsics;
94
+ return v.value === ((_a = typeIntrinsics.map) !== null && _a !== void 0 ? _a : CustomMap.getIntrinsics());
94
95
  }
95
96
  return false;
96
97
  }
@@ -143,9 +144,10 @@ class CustomMap extends with_intrinsics_1.CustomObject {
143
144
  }
144
145
  this.value.set(last, newValue);
145
146
  }
146
- get(path) {
147
+ get(path, typeIntrinsics) {
148
+ var _a;
147
149
  if (path instanceof base_1.CustomValue) {
148
- return this.get(new path_1.Path([path]));
150
+ return this.get(new path_1.Path([path]), typeIntrinsics);
149
151
  }
150
152
  const traversalPath = path.clone();
151
153
  const current = traversalPath.next();
@@ -155,7 +157,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
155
157
  const sub = this.value.get(current);
156
158
  if (traversalPath.count() > 0) {
157
159
  if (sub instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
158
- return sub.get(traversalPath);
160
+ return sub.get(traversalPath, typeIntrinsics);
159
161
  }
160
162
  }
161
163
  else if (traversalPath.count() === 0) {
@@ -163,18 +165,19 @@ class CustomMap extends with_intrinsics_1.CustomObject {
163
165
  }
164
166
  }
165
167
  else if (isa === null || isa === void 0 ? void 0 : isa.has(current)) {
166
- return isa.get(current);
168
+ return isa.get(current, typeIntrinsics);
167
169
  }
168
- else if (traversalPath.count() === 0 &&
169
- CustomMap.getIntrinsics().has(current)) {
170
- return CustomMap.getIntrinsics().get(current);
170
+ const intrinsics = (_a = typeIntrinsics.map) !== null && _a !== void 0 ? _a : CustomMap.getIntrinsics();
171
+ if (traversalPath.count() === 0 && intrinsics.has(current)) {
172
+ return intrinsics.get(current);
171
173
  }
172
174
  }
173
175
  throw new Error(`Unknown path in map ${path.toString()}.`);
174
176
  }
175
- getWithOrigin(path) {
177
+ getWithOrigin(path, typeIntrinsics) {
178
+ var _a;
176
179
  if (path instanceof base_1.CustomValue) {
177
- return this.getWithOrigin(new path_1.Path([path]));
180
+ return this.getWithOrigin(new path_1.Path([path]), typeIntrinsics);
178
181
  }
179
182
  const traversalPath = path.clone();
180
183
  const current = traversalPath.next();
@@ -184,11 +187,11 @@ class CustomMap extends with_intrinsics_1.CustomObject {
184
187
  const sub = this.value.get(current);
185
188
  if (traversalPath.count() > 0) {
186
189
  if (sub instanceof CustomMap) {
187
- return sub.getWithOrigin(traversalPath);
190
+ return sub.getWithOrigin(traversalPath, typeIntrinsics);
188
191
  }
189
192
  if (sub instanceof with_intrinsics_1.CustomValueWithIntrinsics) {
190
193
  return {
191
- value: sub.get(traversalPath),
194
+ value: sub.get(traversalPath, typeIntrinsics),
192
195
  origin: this
193
196
  };
194
197
  }
@@ -201,11 +204,10 @@ class CustomMap extends with_intrinsics_1.CustomObject {
201
204
  }
202
205
  }
203
206
  else if (isa === null || isa === void 0 ? void 0 : isa.has(current)) {
204
- return isa.getWithOrigin(current);
207
+ return isa.getWithOrigin(current, typeIntrinsics);
205
208
  }
206
- else if (traversalPath.count() === 0 &&
207
- CustomMap.getIntrinsics().has(current)) {
208
- const intrinsics = CustomMap.getIntrinsics();
209
+ const intrinsics = (_a = typeIntrinsics.map) !== null && _a !== void 0 ? _a : CustomMap.getIntrinsics();
210
+ if (traversalPath.count() === 0 && intrinsics.has(current)) {
209
211
  return {
210
212
  value: intrinsics.get(current),
211
213
  origin: null
@@ -1,3 +1,4 @@
1
+ import { ContextTypeIntrinsics } from '../context/types';
1
2
  import { ObjectValue } from '../utils/object-value';
2
3
  import { Path } from '../utils/path';
3
4
  import { CustomValue } from './base';
@@ -17,12 +18,12 @@ export declare class CustomNumber extends CustomValueWithIntrinsics {
17
18
  toInt(): number;
18
19
  toNumber(): number;
19
20
  toTruthy(): boolean;
20
- instanceOf(v: CustomValue): boolean;
21
+ instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
21
22
  [Symbol.iterator](): CustomNumberIterator;
22
23
  has(_path: Path<CustomValue> | CustomValue): boolean;
23
24
  set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
24
- get(path: Path<CustomValue> | CustomValue): CustomValue;
25
- getWithOrigin(path: Path<CustomValue> | CustomValue): CustomValueWithIntrinsicsResult;
25
+ get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
26
+ getWithOrigin(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValueWithIntrinsicsResult;
26
27
  hash(): number;
27
28
  }
28
29
  export declare const NegativeOne: CustomNumber;
@@ -44,8 +44,9 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
44
44
  toTruthy() {
45
45
  return !!this.value;
46
46
  }
47
- instanceOf(v) {
48
- return v.value === CustomNumber.intrinsics;
47
+ instanceOf(v, typeIntrinsics) {
48
+ var _a;
49
+ return v.value === ((_a = typeIntrinsics.number) !== null && _a !== void 0 ? _a : CustomNumber.intrinsics);
49
50
  }
50
51
  [Symbol.iterator]() {
51
52
  return new CustomNumberIterator();
@@ -56,21 +57,22 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
56
57
  set(_path, _newValue) {
57
58
  throw new Error('Mutable operations are not allowed on a number.');
58
59
  }
59
- get(path) {
60
+ get(path, typeIntrinsics) {
61
+ var _a;
60
62
  if (path instanceof base_1.CustomValue) {
61
- return this.get(new path_1.Path([path]));
63
+ return this.get(new path_1.Path([path]), typeIntrinsics);
62
64
  }
63
65
  const traversalPath = path.clone();
64
66
  const current = traversalPath.next();
65
- if (traversalPath.count() === 0 &&
66
- CustomNumber.getIntrinsics().has(current)) {
67
- return CustomNumber.intrinsics.get(current);
67
+ const intrinsics = (_a = typeIntrinsics.number) !== null && _a !== void 0 ? _a : CustomNumber.getIntrinsics();
68
+ if (traversalPath.count() === 0 && intrinsics.has(current)) {
69
+ return intrinsics.get(current);
68
70
  }
69
71
  throw new Error(`Unknown path in number ${path.toString()}.`);
70
72
  }
71
- getWithOrigin(path) {
73
+ getWithOrigin(path, typeIntrinsics) {
72
74
  return {
73
- value: this.get(path),
75
+ value: this.get(path, typeIntrinsics),
74
76
  origin: null
75
77
  };
76
78
  }
@@ -1,3 +1,4 @@
1
+ import { ContextTypeIntrinsics } from '../context/types';
1
2
  import { ObjectValue } from '../utils/object-value';
2
3
  import { Path } from '../utils/path';
3
4
  import { CustomValue } from './base';
@@ -23,13 +24,13 @@ export declare class CustomString extends CustomValueWithIntrinsics {
23
24
  toNumber(): number;
24
25
  toInt(): number;
25
26
  toTruthy(): boolean;
26
- instanceOf(v: CustomValue): boolean;
27
+ instanceOf(v: CustomValue, typeIntrinsics: ContextTypeIntrinsics): boolean;
27
28
  slice(a: CustomValue, b: CustomValue): CustomString;
28
29
  [Symbol.iterator](): CustomStringIterator;
29
30
  getCharIndex(index: number): number;
30
31
  has(path: Path<CustomValue> | CustomValue): boolean;
31
32
  set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
32
- get(path: Path<CustomValue> | CustomValue): CustomValue;
33
- getWithOrigin(path: Path<CustomValue> | CustomValue): CustomValueWithIntrinsicsResult;
33
+ get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
34
+ getWithOrigin(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValueWithIntrinsicsResult;
34
35
  hash(): number;
35
36
  }
@@ -72,8 +72,9 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
72
72
  toTruthy() {
73
73
  return this.value.length > 0;
74
74
  }
75
- instanceOf(v) {
76
- return v.value === CustomString.intrinsics;
75
+ instanceOf(v, typeIntrinsics) {
76
+ var _a;
77
+ return v.value === ((_a = typeIntrinsics.string) !== null && _a !== void 0 ? _a : CustomString.intrinsics);
77
78
  }
78
79
  slice(a, b) {
79
80
  return new CustomString(this.value.slice(a.toNumber(), b.toNumber()));
@@ -99,9 +100,10 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
99
100
  set(_path, _newValue) {
100
101
  throw new Error('Mutable operations are not allowed on a string.');
101
102
  }
102
- get(path) {
103
+ get(path, typeIntrinsics) {
104
+ var _a;
103
105
  if (path instanceof base_1.CustomValue) {
104
- return this.get(new path_1.Path([path]));
106
+ return this.get(new path_1.Path([path]), typeIntrinsics);
105
107
  }
106
108
  const traversalPath = path.clone();
107
109
  const current = traversalPath.next();
@@ -113,15 +115,15 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
113
115
  }
114
116
  throw new Error(`Index error (string index ${currentIndex} out of range).`);
115
117
  }
116
- else if (traversalPath.count() === 0 &&
117
- CustomString.getIntrinsics().has(current)) {
118
- return CustomString.intrinsics.get(current);
118
+ const intrinsics = (_a = typeIntrinsics.string) !== null && _a !== void 0 ? _a : CustomString.getIntrinsics();
119
+ if (traversalPath.count() === 0 && intrinsics.has(current)) {
120
+ return intrinsics.get(current);
119
121
  }
120
122
  throw new Error(`Unknown path in string ${path.toString()}.`);
121
123
  }
122
- getWithOrigin(path) {
124
+ getWithOrigin(path, typeIntrinsics) {
123
125
  return {
124
- value: this.get(path),
126
+ value: this.get(path, typeIntrinsics),
125
127
  origin: null
126
128
  };
127
129
  }
@@ -1,3 +1,4 @@
1
+ import { ContextTypeIntrinsics } from '../context/types';
1
2
  import { ObjectValue } from '../utils/object-value';
2
3
  import { Path } from '../utils/path';
3
4
  import { CustomValue } from './base';
@@ -9,8 +10,8 @@ export type CustomValueWithIntrinsicsResult = {
9
10
  export declare abstract class CustomValueWithIntrinsics extends CustomValue {
10
11
  abstract has(path: Path<CustomValue> | CustomValue): boolean;
11
12
  abstract set(path: Path<CustomValue> | CustomValue, value: CustomValue): void;
12
- abstract get(path: Path<CustomValue> | CustomValue): CustomValue;
13
- abstract getWithOrigin(path: Path<CustomValue> | CustomValue): CustomValueWithIntrinsicsResult;
13
+ abstract get(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValue;
14
+ abstract getWithOrigin(path: Path<CustomValue> | CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValueWithIntrinsicsResult;
14
15
  abstract [Symbol.iterator](): Iterator<CustomValue> & {
15
16
  index: number;
16
17
  };
@@ -5,7 +5,7 @@ const map_1 = require("../types/map");
5
5
  function getSuper(item) {
6
6
  var _a;
7
7
  if (item instanceof map_1.CustomMap) {
8
- return (_a = item.getIsa()) !== null && _a !== void 0 ? _a : new map_1.CustomMap(item.getIntrinsics());
8
+ return (_a = item.getIsa()) !== null && _a !== void 0 ? _a : new map_1.CustomMap();
9
9
  }
10
10
  return null;
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.7.2",
3
+ "version": "2.8.0",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",