greybel-interpreter 4.1.2 → 4.1.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.
@@ -0,0 +1,8 @@
1
+ export declare enum RuntimeKeyword {
2
+ Self = "self",
3
+ Super = "super",
4
+ Outer = "outer",
5
+ Locals = "locals",
6
+ Globals = "globals",
7
+ Isa = "__isa"
8
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RuntimeKeyword = void 0;
4
+ var RuntimeKeyword;
5
+ (function (RuntimeKeyword) {
6
+ RuntimeKeyword["Self"] = "self";
7
+ RuntimeKeyword["Super"] = "super";
8
+ RuntimeKeyword["Outer"] = "outer";
9
+ RuntimeKeyword["Locals"] = "locals";
10
+ RuntimeKeyword["Globals"] = "globals";
11
+ RuntimeKeyword["Isa"] = "__isa";
12
+ })(RuntimeKeyword = exports.RuntimeKeyword || (exports.RuntimeKeyword = {}));
@@ -19,6 +19,7 @@ const boolean_1 = require("./types/boolean");
19
19
  const string_1 = require("./types/string");
20
20
  const default_1 = require("./types/default");
21
21
  const stack_1 = require("./utils/stack");
22
+ const keywords_1 = require("./byte-compiler/keywords");
22
23
  function generateCustomValueFromASTLiteral(node) {
23
24
  switch (node.type) {
24
25
  case miniscript_core_1.ASTType.BooleanLiteral:
@@ -358,7 +359,7 @@ class BytecodeGenerator {
358
359
  processMemberExpression(node, isInvoke = true) {
359
360
  return __awaiter(this, void 0, void 0, function* () {
360
361
  const base = unwrap(node.base);
361
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === 'super') {
362
+ if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
362
363
  this.push({
363
364
  op: instruction_1.OpCode.PUSH,
364
365
  source: this.getSourceLocation(node.identifier),
@@ -384,7 +385,7 @@ class BytecodeGenerator {
384
385
  processIndexExpression(node, isInvoke = true) {
385
386
  return __awaiter(this, void 0, void 0, function* () {
386
387
  const base = unwrap(node.base);
387
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === 'super') {
388
+ if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
388
389
  yield this.processSubNode(node.index);
389
390
  this.push({
390
391
  op: instruction_1.OpCode.GET_SUPER_PROPERTY,
@@ -418,35 +419,35 @@ class BytecodeGenerator {
418
419
  return __awaiter(this, void 0, void 0, function* () {
419
420
  if (isFirst) {
420
421
  switch (node.name) {
421
- case 'self': {
422
+ case keywords_1.RuntimeKeyword.Self: {
422
423
  this.push({
423
424
  op: instruction_1.OpCode.GET_SELF,
424
425
  source: this.getSourceLocation(node)
425
426
  });
426
427
  return;
427
428
  }
428
- case 'super': {
429
+ case keywords_1.RuntimeKeyword.Super: {
429
430
  this.push({
430
431
  op: instruction_1.OpCode.GET_SUPER,
431
432
  source: this.getSourceLocation(node)
432
433
  });
433
434
  return;
434
435
  }
435
- case 'outer': {
436
+ case keywords_1.RuntimeKeyword.Outer: {
436
437
  this.push({
437
438
  op: instruction_1.OpCode.GET_OUTER,
438
439
  source: this.getSourceLocation(node)
439
440
  });
440
441
  return;
441
442
  }
442
- case 'locals': {
443
+ case keywords_1.RuntimeKeyword.Locals: {
443
444
  this.push({
444
445
  op: instruction_1.OpCode.GET_LOCALS,
445
446
  source: this.getSourceLocation(node)
446
447
  });
447
448
  return;
448
449
  }
449
- case 'globals': {
450
+ case keywords_1.RuntimeKeyword.Globals: {
450
451
  this.push({
451
452
  op: instruction_1.OpCode.GET_GLOBALS,
452
453
  source: this.getSourceLocation(node)
@@ -902,7 +903,7 @@ class BytecodeGenerator {
902
903
  const left = unwrap(node.base);
903
904
  if (left instanceof miniscript_core_1.ASTMemberExpression) {
904
905
  const base = unwrap(left.base);
905
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === 'super') {
906
+ if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
906
907
  this.push({
907
908
  op: instruction_1.OpCode.PUSH,
908
909
  source: this.getSourceLocation(left.identifier),
@@ -932,7 +933,7 @@ class BytecodeGenerator {
932
933
  }
933
934
  else if (left instanceof miniscript_core_1.ASTIndexExpression) {
934
935
  const base = unwrap(left.base);
935
- if (base instanceof miniscript_core_1.ASTIdentifier && base.name === 'super') {
936
+ if (base instanceof miniscript_core_1.ASTIdentifier && base.name === keywords_1.RuntimeKeyword.Super) {
936
937
  yield this.processSubNode(left.index);
937
938
  yield pushArgs();
938
939
  this.push({
package/dist/context.js CHANGED
@@ -134,9 +134,9 @@ class OperationContext {
134
134
  }
135
135
  injectContext() {
136
136
  if (this.self)
137
- this.set(new string_1.CustomString('self'), this.self);
137
+ this.set(string_1.Self, this.self);
138
138
  if (this.super)
139
- this.set(new string_1.CustomString('super'), this.super);
139
+ this.set(string_1.Super, this.super);
140
140
  }
141
141
  getCurrentInstruction() {
142
142
  return this.code[this.ip];
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CustomFunction = void 0;
4
4
  const instruction_1 = require("../byte-compiler/instruction");
5
+ const keywords_1 = require("../byte-compiler/keywords");
5
6
  const hash_1 = require("../utils/hash");
6
7
  const object_value_1 = require("../utils/object-value");
7
8
  const uuid_1 = require("../utils/uuid");
@@ -36,7 +37,7 @@ class CustomFunction extends base_1.CustomValue {
36
37
  ], args);
37
38
  }
38
39
  static createExternalWithSelf(name, callback) {
39
- return this.createExternal(name, callback).addArgument('self');
40
+ return this.createExternal(name, callback).addArgument(keywords_1.RuntimeKeyword.Self);
40
41
  }
41
42
  constructor(name, value, args = [], outer) {
42
43
  super();
@@ -1,9 +1,7 @@
1
1
  import { ContextTypeIntrinsics } from '../context/types';
2
2
  import { ObjectValue } from '../utils/object-value';
3
3
  import { CustomValue } from './base';
4
- import { CustomString } from './string';
5
4
  import { CustomObject, CustomValueWithIntrinsicsResult } from './with-intrinsics';
6
- export declare const ISA_PROPERTY: CustomString;
7
5
  export declare const CUSTOM_MAP_MAX_DEPTH = 2;
8
6
  export declare const CUSTOM_MAP_MAX_DEPTH_VALUE = "{...}";
9
7
  export declare class CustomMapIterator implements Iterator<CustomValue> {
package/dist/types/map.js CHANGED
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomMap = exports.CustomMapIterator = exports.CUSTOM_MAP_MAX_DEPTH_VALUE = exports.CUSTOM_MAP_MAX_DEPTH = exports.ISA_PROPERTY = void 0;
3
+ exports.CustomMap = exports.CustomMapIterator = exports.CUSTOM_MAP_MAX_DEPTH_VALUE = exports.CUSTOM_MAP_MAX_DEPTH = void 0;
4
4
  const hash_1 = require("../utils/hash");
5
5
  const object_value_1 = require("../utils/object-value");
6
6
  const uuid_1 = require("../utils/uuid");
7
7
  const nil_1 = require("./nil");
8
8
  const string_1 = require("./string");
9
9
  const with_intrinsics_1 = require("./with-intrinsics");
10
- exports.ISA_PROPERTY = new string_1.CustomString('__isa');
11
10
  exports.CUSTOM_MAP_MAX_DEPTH = 2;
12
11
  exports.CUSTOM_MAP_MAX_DEPTH_VALUE = '{...}';
13
12
  class CustomMapIterator {
@@ -160,12 +159,12 @@ class CustomMap extends with_intrinsics_1.CustomObject {
160
159
  }
161
160
  createInstance() {
162
161
  const newInstance = new CustomMap(new object_value_1.ObjectValue());
163
- newInstance.value.set(exports.ISA_PROPERTY, this);
162
+ newInstance.value.set(string_1.Isa, this);
164
163
  newInstance.isInstance = true;
165
164
  return newInstance;
166
165
  }
167
166
  getIsa() {
168
- const isa = this.value.get(exports.ISA_PROPERTY);
167
+ const isa = this.value.get(string_1.Isa);
169
168
  return isa instanceof CustomMap ? isa : null;
170
169
  }
171
170
  hash(recursionDepth = 0) {
@@ -34,3 +34,9 @@ export declare class CustomString extends CustomValueWithIntrinsics {
34
34
  getWithOrigin(current: CustomValue, typeIntrinsics: ContextTypeIntrinsics): CustomValueWithIntrinsicsResult;
35
35
  hash(): number;
36
36
  }
37
+ export declare const Self: CustomString;
38
+ export declare const Isa: CustomString;
39
+ export declare const Super: CustomString;
40
+ export declare const Globals: CustomString;
41
+ export declare const Locals: CustomString;
42
+ export declare const Outer: CustomString;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CustomString = exports.CustomStringIterator = void 0;
3
+ exports.Outer = exports.Locals = exports.Globals = exports.Super = exports.Isa = exports.Self = exports.CustomString = exports.CustomStringIterator = void 0;
4
+ const keywords_1 = require("../byte-compiler/keywords");
4
5
  const hash_1 = require("../utils/hash");
5
6
  const object_value_1 = require("../utils/object-value");
6
7
  const number_1 = require("./number");
@@ -122,3 +123,9 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
122
123
  }
123
124
  CustomString.intrinsics = new object_value_1.ObjectValue();
124
125
  exports.CustomString = CustomString;
126
+ exports.Self = new CustomString(keywords_1.RuntimeKeyword.Self);
127
+ exports.Isa = new CustomString(keywords_1.RuntimeKeyword.Isa);
128
+ exports.Super = new CustomString(keywords_1.RuntimeKeyword.Super);
129
+ exports.Globals = new CustomString(keywords_1.RuntimeKeyword.Globals);
130
+ exports.Locals = new CustomString(keywords_1.RuntimeKeyword.Locals);
131
+ exports.Outer = new CustomString(keywords_1.RuntimeKeyword.Outer);
@@ -0,0 +1,5 @@
1
+ import { OperationContext } from '../context';
2
+ import { CustomValue } from '../types/base';
3
+ import { CustomFunction } from '../types/function';
4
+ export declare function call(frame: OperationContext, fn: CustomFunction, args: CustomValue[]): void;
5
+ export declare function callWithContext(frame: OperationContext, fn: CustomFunction, args: CustomValue[]): void;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.callWithContext = exports.call = void 0;
4
+ const keywords_1 = require("../byte-compiler/keywords");
5
+ const map_1 = require("../types/map");
6
+ function call(frame, fn, args) {
7
+ var _a;
8
+ const argsCount = args.length;
9
+ for (let index = 0; index < argsCount; index++) {
10
+ const argument = args.shift();
11
+ const paramNum = argsCount - 1 - index;
12
+ if (paramNum >= fn.arguments.length) {
13
+ throw new Error('Too many arguments.');
14
+ }
15
+ const param = fn.arguments[paramNum].name;
16
+ if (param.toString() === keywords_1.RuntimeKeyword.Self) {
17
+ frame.self = argument;
18
+ frame.super =
19
+ argument instanceof map_1.CustomMap
20
+ ? (_a = argument.getIsa()) !== null && _a !== void 0 ? _a : new map_1.CustomMap()
21
+ : null;
22
+ }
23
+ else {
24
+ frame.set(param, argument);
25
+ }
26
+ }
27
+ for (let paramNum = argsCount; paramNum < fn.arguments.length; paramNum++) {
28
+ frame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
29
+ }
30
+ frame.injectContext();
31
+ }
32
+ exports.call = call;
33
+ function callWithContext(frame, fn, args) {
34
+ const argsCount = args.length;
35
+ const selfParam = fn.arguments.length > 0 &&
36
+ fn.arguments[0].name.toString() === keywords_1.RuntimeKeyword.Self
37
+ ? 1
38
+ : 0;
39
+ for (let index = 0; index < argsCount; index++) {
40
+ const argument = args.shift();
41
+ const paramNum = argsCount - 1 - index + selfParam;
42
+ if (paramNum >= fn.arguments.length) {
43
+ throw new Error('Too many arguments.');
44
+ }
45
+ const param = fn.arguments[paramNum].name;
46
+ if (param.toString() !== keywords_1.RuntimeKeyword.Self)
47
+ frame.set(param, argument);
48
+ }
49
+ for (let paramNum = argsCount + selfParam; paramNum < fn.arguments.length; paramNum++) {
50
+ frame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
51
+ }
52
+ frame.injectContext();
53
+ }
54
+ exports.callWithContext = callWithContext;
package/dist/vm.js CHANGED
@@ -29,6 +29,7 @@ const set_immediate_1 = require("./utils/set-immediate");
29
29
  const string_1 = require("./types/string");
30
30
  const events_1 = __importDefault(require("events"));
31
31
  const object_value_1 = require("./utils/object-value");
32
+ const call_1 = require("./vm/call");
32
33
  class Debugger {
33
34
  constructor() {
34
35
  this.breakpoint = false;
@@ -185,7 +186,7 @@ class VM {
185
186
  });
186
187
  }
187
188
  resume(done) {
188
- var _a, _b, _c, _d, _e, _f, _g, _h;
189
+ var _a, _b, _c, _d, _e, _f;
189
190
  try {
190
191
  while (true) {
191
192
  if (!this.isPending()) {
@@ -224,11 +225,11 @@ class VM {
224
225
  break;
225
226
  }
226
227
  case instruction_1.OpCode.GET_SELF: {
227
- this.pushStack((_b = frame.self) !== null && _b !== void 0 ? _b : default_1.DefaultType.Void);
228
+ this.pushStack(frame.locals.get(string_1.Self, this.contextTypeIntrinsics));
228
229
  break;
229
230
  }
230
231
  case instruction_1.OpCode.GET_SUPER: {
231
- this.pushStack((_c = frame.super) !== null && _c !== void 0 ? _c : default_1.DefaultType.Void);
232
+ this.pushStack((_b = frame.super) !== null && _b !== void 0 ? _b : default_1.DefaultType.Void);
232
233
  break;
233
234
  }
234
235
  case instruction_1.OpCode.IMPORT: {
@@ -265,26 +266,7 @@ class VM {
265
266
  throw new Error('Too many arguments.');
266
267
  }
267
268
  const newFrame = this.createFrame({ code: fn.value, outer: fn.outer });
268
- const argsCount = args.length;
269
- for (let index = 0; index < argsCount; index++) {
270
- const argument = args.shift();
271
- const paramNum = argsCount - 1 - index;
272
- if (paramNum >= fn.arguments.length) {
273
- throw new Error('Too many arguments.');
274
- }
275
- const param = fn.arguments[paramNum].name;
276
- if (param.toString() == "self") {
277
- newFrame.self = argument;
278
- newFrame.super = argument instanceof map_1.CustomMap ? ((_d = argument.getIsa()) !== null && _d !== void 0 ? _d : new map_1.CustomMap()) : null;
279
- }
280
- else {
281
- newFrame.set(param, argument);
282
- }
283
- }
284
- for (let paramNum = argsCount; paramNum < fn.arguments.length; paramNum++) {
285
- newFrame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
286
- }
287
- newFrame.injectContext();
269
+ (0, call_1.call)(newFrame, fn, args);
288
270
  }
289
271
  break;
290
272
  }
@@ -301,25 +283,10 @@ class VM {
301
283
  const newFrame = this.createFrame({
302
284
  code: fn.value,
303
285
  self: context,
304
- super: context instanceof map_1.CustomMap ? ((_e = context.getIsa()) !== null && _e !== void 0 ? _e : new map_1.CustomMap()) : null,
286
+ super: context instanceof map_1.CustomMap ? ((_c = context.getIsa()) !== null && _c !== void 0 ? _c : new map_1.CustomMap()) : null,
305
287
  outer: fn.outer
306
288
  });
307
- const argsCount = args.length;
308
- let selfParam = fn.arguments.length > 0 && fn.arguments[0].name.toString() == "self" ? 1 : 0;
309
- for (let index = 0; index < argsCount; index++) {
310
- const argument = args.shift();
311
- const paramNum = argsCount - 1 - index + selfParam;
312
- if (paramNum >= fn.arguments.length) {
313
- throw new Error('Too many arguments.');
314
- }
315
- const param = fn.arguments[paramNum].name;
316
- if (param.toString() !== "self")
317
- newFrame.set(param, argument);
318
- }
319
- for (let paramNum = argsCount + selfParam; paramNum < fn.arguments.length; paramNum++) {
320
- newFrame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
321
- }
322
- newFrame.injectContext();
289
+ (0, call_1.callWithContext)(newFrame, fn, args);
323
290
  }
324
291
  break;
325
292
  }
@@ -340,25 +307,10 @@ class VM {
340
307
  const newFrame = this.createFrame({
341
308
  code: fn.value,
342
309
  self: frame.self,
343
- super: ret.origin instanceof map_1.CustomMap ? ((_f = ret.origin.getIsa()) !== null && _f !== void 0 ? _f : new map_1.CustomMap()) : null,
310
+ super: ret.origin instanceof map_1.CustomMap ? ((_d = ret.origin.getIsa()) !== null && _d !== void 0 ? _d : new map_1.CustomMap()) : null,
344
311
  outer: fn.outer
345
312
  });
346
- let selfParam = fn.arguments.length > 0 && fn.arguments[0].name.toString() == "self" ? 1 : 0;
347
- const argsCount = args.length;
348
- for (let index = 0; index < argsCount; index++) {
349
- const argument = args.shift();
350
- const paramNum = argsCount - 1 - index + selfParam;
351
- if (paramNum >= fn.arguments.length) {
352
- throw new Error('Too many arguments.');
353
- }
354
- const param = fn.arguments[paramNum].name;
355
- if (param.toString() !== "self")
356
- newFrame.set(param, argument);
357
- }
358
- for (let paramNum = argsCount + selfParam; paramNum < fn.arguments.length; paramNum++) {
359
- newFrame.set(fn.arguments[paramNum].name, fn.arguments[paramNum].defaultValue);
360
- }
361
- newFrame.injectContext();
313
+ (0, call_1.callWithContext)(newFrame, fn, args);
362
314
  }
363
315
  break;
364
316
  }
@@ -459,10 +411,7 @@ class VM {
459
411
  code: ret.value,
460
412
  outer: ret.outer
461
413
  });
462
- for (let paramNum = 0; paramNum < ret.arguments.length; paramNum++) {
463
- newFrame.set(ret.arguments[paramNum].name, ret.arguments[paramNum].defaultValue);
464
- }
465
- newFrame.injectContext();
414
+ (0, call_1.call)(newFrame, ret, []);
466
415
  break;
467
416
  }
468
417
  this.pushStack(ret);
@@ -480,13 +429,10 @@ class VM {
480
429
  const newFrame = this.createFrame({
481
430
  code: ret.value.value,
482
431
  self: context,
483
- super: ret.origin instanceof map_1.CustomMap ? ((_g = ret.origin.getIsa()) !== null && _g !== void 0 ? _g : new map_1.CustomMap()) : null,
432
+ super: ret.origin instanceof map_1.CustomMap ? ((_e = ret.origin.getIsa()) !== null && _e !== void 0 ? _e : new map_1.CustomMap()) : null,
484
433
  outer: ret.value.outer
485
434
  });
486
- for (let paramNum = 0; paramNum < ret.value.arguments.length; paramNum++) {
487
- newFrame.set(ret.value.arguments[paramNum].name, ret.value.arguments[paramNum].defaultValue);
488
- }
489
- newFrame.injectContext();
435
+ (0, call_1.callWithContext)(newFrame, ret.value, []);
490
436
  break;
491
437
  }
492
438
  this.pushStack(ret.value);
@@ -500,13 +446,10 @@ class VM {
500
446
  const newFrame = this.createFrame({
501
447
  code: ret.value.value,
502
448
  self: frame.self,
503
- super: ret.origin instanceof map_1.CustomMap ? ((_h = ret.origin.getIsa()) !== null && _h !== void 0 ? _h : new map_1.CustomMap()) : null,
449
+ super: ret.origin instanceof map_1.CustomMap ? ((_f = ret.origin.getIsa()) !== null && _f !== void 0 ? _f : new map_1.CustomMap()) : null,
504
450
  outer: ret.value.outer
505
451
  });
506
- for (let paramNum = 0; paramNum < ret.value.arguments.length; paramNum++) {
507
- newFrame.set(ret.value.arguments[paramNum].name, ret.value.arguments[paramNum].defaultValue);
508
- }
509
- newFrame.injectContext();
452
+ (0, call_1.callWithContext)(newFrame, ret.value, []);
510
453
  break;
511
454
  }
512
455
  this.pushStack(ret.value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "4.1.2",
3
+ "version": "4.1.3",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",