greybel-interpreter 1.4.6 → 1.4.7

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.
Files changed (101) hide show
  1. package/.editorconfig +14 -14
  2. package/.eslintrc +47 -47
  3. package/LICENSE +21 -21
  4. package/README.md +2 -2
  5. package/dist/context.d.ts +110 -110
  6. package/dist/context.js +306 -306
  7. package/dist/cps.d.ts +14 -14
  8. package/dist/cps.js +160 -160
  9. package/dist/handler/error.d.ts +6 -6
  10. package/dist/handler/error.js +12 -12
  11. package/dist/handler/output.d.ts +18 -18
  12. package/dist/handler/output.js +29 -29
  13. package/dist/handler/resource.d.ts +12 -12
  14. package/dist/handler/resource.js +28 -28
  15. package/dist/handler-container.d.ts +14 -14
  16. package/dist/handler-container.js +14 -14
  17. package/dist/index.d.ts +46 -46
  18. package/dist/index.js +132 -132
  19. package/dist/interpreter.d.ts +42 -42
  20. package/dist/interpreter.js +218 -218
  21. package/dist/operations/assign.d.ts +13 -13
  22. package/dist/operations/assign.js +46 -46
  23. package/dist/operations/block.d.ts +12 -12
  24. package/dist/operations/block.js +52 -52
  25. package/dist/operations/break.d.ts +10 -10
  26. package/dist/operations/break.js +23 -23
  27. package/dist/operations/call.d.ts +13 -13
  28. package/dist/operations/call.js +49 -49
  29. package/dist/operations/chunk.d.ts +12 -12
  30. package/dist/operations/chunk.js +33 -33
  31. package/dist/operations/continue.d.ts +10 -10
  32. package/dist/operations/continue.js +23 -23
  33. package/dist/operations/debugger-statement.d.ts +10 -10
  34. package/dist/operations/debugger-statement.js +21 -21
  35. package/dist/operations/evaluate.d.ts +40 -40
  36. package/dist/operations/evaluate.js +334 -334
  37. package/dist/operations/for.d.ts +14 -14
  38. package/dist/operations/for.js +81 -81
  39. package/dist/operations/function-reference.d.ts +12 -12
  40. package/dist/operations/function-reference.js +44 -44
  41. package/dist/operations/function.d.ts +14 -14
  42. package/dist/operations/function.js +72 -72
  43. package/dist/operations/if-statement.d.ts +19 -19
  44. package/dist/operations/if-statement.js +85 -85
  45. package/dist/operations/import.d.ts +17 -17
  46. package/dist/operations/import.js +57 -57
  47. package/dist/operations/include.d.ts +13 -13
  48. package/dist/operations/include.js +41 -41
  49. package/dist/operations/list.d.ts +11 -11
  50. package/dist/operations/list.js +42 -42
  51. package/dist/operations/literal.d.ts +11 -11
  52. package/dist/operations/literal.js +40 -40
  53. package/dist/operations/map.d.ts +11 -11
  54. package/dist/operations/map.js +44 -44
  55. package/dist/operations/negated-binary.d.ts +11 -11
  56. package/dist/operations/negated-binary.js +42 -42
  57. package/dist/operations/new-instance.d.ts +11 -11
  58. package/dist/operations/new-instance.js +39 -39
  59. package/dist/operations/noop.d.ts +9 -9
  60. package/dist/operations/noop.js +19 -19
  61. package/dist/operations/not.d.ts +11 -11
  62. package/dist/operations/not.js +35 -35
  63. package/dist/operations/operation.d.ts +13 -13
  64. package/dist/operations/operation.js +9 -9
  65. package/dist/operations/reference.d.ts +9 -9
  66. package/dist/operations/reference.js +19 -19
  67. package/dist/operations/resolve.d.ts +38 -38
  68. package/dist/operations/resolve.js +210 -210
  69. package/dist/operations/return.d.ts +11 -11
  70. package/dist/operations/return.js +45 -45
  71. package/dist/operations/while.d.ts +13 -13
  72. package/dist/operations/while.js +65 -65
  73. package/dist/types/base.d.ts +9 -9
  74. package/dist/types/base.js +5 -5
  75. package/dist/types/boolean.d.ts +13 -13
  76. package/dist/types/boolean.js +34 -34
  77. package/dist/types/default.d.ts +8 -8
  78. package/dist/types/default.js +14 -14
  79. package/dist/types/function.d.ts +32 -32
  80. package/dist/types/function.js +104 -104
  81. package/dist/types/interface.d.ts +24 -24
  82. package/dist/types/interface.js +83 -83
  83. package/dist/types/list.d.ts +29 -29
  84. package/dist/types/list.js +158 -158
  85. package/dist/types/map.d.ts +33 -33
  86. package/dist/types/map.js +200 -200
  87. package/dist/types/nil.d.ts +11 -11
  88. package/dist/types/nil.js +33 -33
  89. package/dist/types/number.d.ts +25 -25
  90. package/dist/types/number.js +69 -69
  91. package/dist/types/string.d.ts +31 -31
  92. package/dist/types/string.js +121 -121
  93. package/dist/types/with-intrinsics.d.ts +15 -15
  94. package/dist/types/with-intrinsics.js +19 -19
  95. package/dist/utils/deep-equal.d.ts +1 -1
  96. package/dist/utils/deep-equal.js +37 -37
  97. package/dist/utils/object-value.d.ts +7 -7
  98. package/dist/utils/object-value.js +42 -42
  99. package/dist/utils/path.d.ts +10 -10
  100. package/dist/utils/path.js +32 -32
  101. package/package.json +58 -58
@@ -1,65 +1,65 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const context_1 = require("../context");
16
- const default_1 = __importDefault(require("../types/default"));
17
- const block_1 = __importDefault(require("./block"));
18
- const operation_1 = __importDefault(require("./operation"));
19
- class While extends operation_1.default {
20
- constructor(item, target) {
21
- super(null, target);
22
- this.item = item;
23
- }
24
- build(visit) {
25
- return __awaiter(this, void 0, void 0, function* () {
26
- const stack = yield Promise.all(this.item.body.map((child) => visit(child)));
27
- this.block = new block_1.default(stack);
28
- this.condition = yield visit(this.item.condition);
29
- return this;
30
- });
31
- }
32
- handle(ctx) {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- const whileCtx = ctx.fork({
35
- type: context_1.ContextType.Loop,
36
- state: context_1.ContextState.Temporary
37
- });
38
- const loopState = new context_1.LoopState();
39
- whileCtx.loopState = loopState;
40
- return new Promise((resolve, reject) => {
41
- const iteration = () => __awaiter(this, void 0, void 0, function* () {
42
- try {
43
- const conditionResult = yield this.condition.handle(whileCtx);
44
- if (!conditionResult.toTruthy()) {
45
- resolve(default_1.default.Void);
46
- return;
47
- }
48
- loopState.isContinue = false;
49
- yield this.block.handle(whileCtx);
50
- if (loopState.isBreak || ctx.isExit()) {
51
- resolve(default_1.default.Void);
52
- return;
53
- }
54
- process.nextTick(iteration);
55
- }
56
- catch (err) {
57
- reject(err);
58
- }
59
- });
60
- iteration();
61
- });
62
- });
63
- }
64
- }
65
- exports.default = While;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const context_1 = require("../context");
16
+ const default_1 = __importDefault(require("../types/default"));
17
+ const block_1 = __importDefault(require("./block"));
18
+ const operation_1 = __importDefault(require("./operation"));
19
+ class While extends operation_1.default {
20
+ constructor(item, target) {
21
+ super(null, target);
22
+ this.item = item;
23
+ }
24
+ build(visit) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const stack = yield Promise.all(this.item.body.map((child) => visit(child)));
27
+ this.block = new block_1.default(stack);
28
+ this.condition = yield visit(this.item.condition);
29
+ return this;
30
+ });
31
+ }
32
+ handle(ctx) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const whileCtx = ctx.fork({
35
+ type: context_1.ContextType.Loop,
36
+ state: context_1.ContextState.Temporary
37
+ });
38
+ const loopState = new context_1.LoopState();
39
+ whileCtx.loopState = loopState;
40
+ return new Promise((resolve, reject) => {
41
+ const iteration = () => __awaiter(this, void 0, void 0, function* () {
42
+ try {
43
+ const conditionResult = yield this.condition.handle(whileCtx);
44
+ if (!conditionResult.toTruthy()) {
45
+ resolve(default_1.default.Void);
46
+ return;
47
+ }
48
+ loopState.isContinue = false;
49
+ yield this.block.handle(whileCtx);
50
+ if (loopState.isBreak || ctx.isExit()) {
51
+ resolve(default_1.default.Void);
52
+ return;
53
+ }
54
+ process.nextTick(iteration);
55
+ }
56
+ catch (err) {
57
+ reject(err);
58
+ }
59
+ });
60
+ iteration();
61
+ });
62
+ });
63
+ }
64
+ }
65
+ exports.default = While;
@@ -1,9 +1,9 @@
1
- export default abstract class CustomValue {
2
- abstract value: any;
3
- abstract getCustomType(): string;
4
- abstract toNumber(): number;
5
- abstract toInt(): number;
6
- abstract toString(): string;
7
- abstract toTruthy(): boolean;
8
- abstract fork(): CustomValue;
9
- }
1
+ export default abstract class CustomValue {
2
+ abstract value: any;
3
+ abstract getCustomType(): string;
4
+ abstract toNumber(): number;
5
+ abstract toInt(): number;
6
+ abstract toString(): string;
7
+ abstract toTruthy(): boolean;
8
+ abstract fork(): CustomValue;
9
+ }
@@ -1,5 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class CustomValue {
4
- }
5
- exports.default = CustomValue;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class CustomValue {
4
+ }
5
+ exports.default = CustomValue;
@@ -1,13 +1,13 @@
1
- import CustomValue from './base';
2
- export default class CustomBoolean extends CustomValue {
3
- readonly value: boolean;
4
- constructor(value: boolean);
5
- getCustomType(): string;
6
- toString(): string;
7
- fork(): CustomBoolean;
8
- toNumber(): number;
9
- toInt(): number;
10
- toTruthy(): boolean;
11
- }
12
- export declare const DefaultTrue: CustomBoolean;
13
- export declare const DefaultFalse: CustomBoolean;
1
+ import CustomValue from './base';
2
+ export default class CustomBoolean extends CustomValue {
3
+ readonly value: boolean;
4
+ constructor(value: boolean);
5
+ getCustomType(): string;
6
+ toString(): string;
7
+ fork(): CustomBoolean;
8
+ toNumber(): number;
9
+ toInt(): number;
10
+ toTruthy(): boolean;
11
+ }
12
+ export declare const DefaultTrue: CustomBoolean;
13
+ export declare const DefaultFalse: CustomBoolean;
@@ -1,34 +1,34 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DefaultFalse = exports.DefaultTrue = void 0;
7
- const base_1 = __importDefault(require("./base"));
8
- class CustomBoolean extends base_1.default {
9
- constructor(value) {
10
- super();
11
- this.value = value;
12
- }
13
- getCustomType() {
14
- return 'boolean';
15
- }
16
- toString() {
17
- return this.value.toString();
18
- }
19
- fork() {
20
- return new CustomBoolean(this.value);
21
- }
22
- toNumber() {
23
- return this.value ? 1.0 : 0.0;
24
- }
25
- toInt() {
26
- return this.value ? 1 : 0;
27
- }
28
- toTruthy() {
29
- return this.value;
30
- }
31
- }
32
- exports.default = CustomBoolean;
33
- exports.DefaultTrue = new CustomBoolean(true);
34
- exports.DefaultFalse = new CustomBoolean(false);
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DefaultFalse = exports.DefaultTrue = void 0;
7
+ const base_1 = __importDefault(require("./base"));
8
+ class CustomBoolean extends base_1.default {
9
+ constructor(value) {
10
+ super();
11
+ this.value = value;
12
+ }
13
+ getCustomType() {
14
+ return 'boolean';
15
+ }
16
+ toString() {
17
+ return this.value.toString();
18
+ }
19
+ fork() {
20
+ return new CustomBoolean(this.value);
21
+ }
22
+ toNumber() {
23
+ return this.value ? 1.0 : 0.0;
24
+ }
25
+ toInt() {
26
+ return this.value ? 1 : 0;
27
+ }
28
+ toTruthy() {
29
+ return this.value;
30
+ }
31
+ }
32
+ exports.default = CustomBoolean;
33
+ exports.DefaultTrue = new CustomBoolean(true);
34
+ exports.DefaultFalse = new CustomBoolean(false);
@@ -1,8 +1,8 @@
1
- export default class Defaults {
2
- static readonly Void: import("./nil").default;
3
- static readonly True: import("./boolean").default;
4
- static readonly False: import("./boolean").default;
5
- static readonly NegativeOne: import("./number").default;
6
- static readonly PositiveOne: import("./number").default;
7
- static readonly Zero: import("./number").default;
8
- }
1
+ export default class Defaults {
2
+ static readonly Void: import("./nil").default;
3
+ static readonly True: import("./boolean").default;
4
+ static readonly False: import("./boolean").default;
5
+ static readonly NegativeOne: import("./number").default;
6
+ static readonly PositiveOne: import("./number").default;
7
+ static readonly Zero: import("./number").default;
8
+ }
@@ -1,14 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const boolean_1 = require("./boolean");
4
- const nil_1 = require("./nil");
5
- const number_1 = require("./number");
6
- class Defaults {
7
- }
8
- exports.default = Defaults;
9
- Defaults.Void = nil_1.Void;
10
- Defaults.True = boolean_1.DefaultTrue;
11
- Defaults.False = boolean_1.DefaultFalse;
12
- Defaults.NegativeOne = number_1.NegativeOne;
13
- Defaults.PositiveOne = number_1.PositiveOne;
14
- Defaults.Zero = number_1.Zero;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const boolean_1 = require("./boolean");
4
+ const nil_1 = require("./nil");
5
+ const number_1 = require("./number");
6
+ class Defaults {
7
+ }
8
+ exports.default = Defaults;
9
+ Defaults.Void = nil_1.Void;
10
+ Defaults.True = boolean_1.DefaultTrue;
11
+ Defaults.False = boolean_1.DefaultFalse;
12
+ Defaults.NegativeOne = number_1.NegativeOne;
13
+ Defaults.PositiveOne = number_1.PositiveOne;
14
+ Defaults.Zero = number_1.Zero;
@@ -1,32 +1,32 @@
1
- import OperationContext from '../context';
2
- import Operation from '../operations/operation';
3
- import CustomValue from './base';
4
- export interface Callback {
5
- (ctx: OperationContext, self: CustomValue, args: Map<string, CustomValue>): Promise<NonNullable<CustomValue>>;
6
- }
7
- export declare class Argument {
8
- readonly name: string;
9
- readonly defaultValue: Operation;
10
- static createWithCustomValue(name: string, defaultValue: CustomValue): Argument;
11
- constructor(name: string, defaultValue?: Operation | CustomValue);
12
- }
13
- export default class CustomFunction extends CustomValue {
14
- readonly scope?: OperationContext;
15
- readonly name: string;
16
- readonly value: Callback;
17
- private injectSelf;
18
- readonly argumentDefs: Array<Argument>;
19
- static createExternalAnonymous(callback: Callback): CustomFunction;
20
- static createExternal(name: string, callback: Callback): CustomFunction;
21
- static createExternalWithSelf(name: string, callback: Callback): CustomFunction;
22
- constructor(scope: OperationContext, name: string, callback: Callback, injectSelf?: boolean);
23
- setInjectSelf(injectSelf: boolean): CustomFunction;
24
- addArgument(name: string, defaultValue?: Operation | CustomValue): CustomFunction;
25
- fork(): CustomValue;
26
- getCustomType(): string;
27
- toNumber(): number;
28
- toInt(): number;
29
- toString(): string;
30
- toTruthy(): boolean;
31
- run(self: CustomValue, args: Array<CustomValue>, callContext: OperationContext): Promise<CustomValue>;
32
- }
1
+ import OperationContext from '../context';
2
+ import Operation from '../operations/operation';
3
+ import CustomValue from './base';
4
+ export interface Callback {
5
+ (ctx: OperationContext, self: CustomValue, args: Map<string, CustomValue>): Promise<NonNullable<CustomValue>>;
6
+ }
7
+ export declare class Argument {
8
+ readonly name: string;
9
+ readonly defaultValue: Operation;
10
+ static createWithCustomValue(name: string, defaultValue: CustomValue): Argument;
11
+ constructor(name: string, defaultValue?: Operation | CustomValue);
12
+ }
13
+ export default class CustomFunction extends CustomValue {
14
+ readonly scope?: OperationContext;
15
+ readonly name: string;
16
+ readonly value: Callback;
17
+ private injectSelf;
18
+ readonly argumentDefs: Array<Argument>;
19
+ static createExternalAnonymous(callback: Callback): CustomFunction;
20
+ static createExternal(name: string, callback: Callback): CustomFunction;
21
+ static createExternalWithSelf(name: string, callback: Callback): CustomFunction;
22
+ constructor(scope: OperationContext, name: string, callback: Callback, injectSelf?: boolean);
23
+ setInjectSelf(injectSelf: boolean): CustomFunction;
24
+ addArgument(name: string, defaultValue?: Operation | CustomValue): CustomFunction;
25
+ fork(): CustomValue;
26
+ getCustomType(): string;
27
+ toNumber(): number;
28
+ toInt(): number;
29
+ toString(): string;
30
+ toTruthy(): boolean;
31
+ run(self: CustomValue, args: Array<CustomValue>, callContext: OperationContext): Promise<CustomValue>;
32
+ }
@@ -1,104 +1,104 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Argument = void 0;
16
- const context_1 = require("../context");
17
- const operation_1 = __importDefault(require("../operations/operation"));
18
- const reference_1 = __importDefault(require("../operations/reference"));
19
- const base_1 = __importDefault(require("./base"));
20
- const default_1 = __importDefault(require("./default"));
21
- const nil_1 = __importDefault(require("./nil"));
22
- class Argument {
23
- constructor(name, defaultValue = default_1.default.Void) {
24
- this.name = name;
25
- if (defaultValue instanceof base_1.default) {
26
- this.defaultValue = new reference_1.default(defaultValue);
27
- }
28
- else if (defaultValue instanceof operation_1.default) {
29
- this.defaultValue = defaultValue;
30
- }
31
- else {
32
- throw new Error('Invalid defaultValue in argument.');
33
- }
34
- }
35
- static createWithCustomValue(name, defaultValue) {
36
- return new Argument(name, new reference_1.default(defaultValue));
37
- }
38
- }
39
- exports.Argument = Argument;
40
- class CustomFunction extends base_1.default {
41
- constructor(scope, name, callback, injectSelf = false) {
42
- super();
43
- this.scope = scope;
44
- this.name = name;
45
- this.value = callback;
46
- this.injectSelf = injectSelf;
47
- this.argumentDefs = [];
48
- }
49
- static createExternalAnonymous(callback) {
50
- return new CustomFunction(null, 'anonymous', callback);
51
- }
52
- static createExternal(name, callback) {
53
- return new CustomFunction(null, name, callback);
54
- }
55
- static createExternalWithSelf(name, callback) {
56
- return new CustomFunction(null, name, callback, true).addArgument('self');
57
- }
58
- setInjectSelf(injectSelf) {
59
- this.injectSelf = injectSelf;
60
- return this;
61
- }
62
- addArgument(name, defaultValue = default_1.default.Void) {
63
- this.argumentDefs.push(new Argument(name, defaultValue));
64
- return this;
65
- }
66
- fork() {
67
- return new CustomFunction(this.scope, this.name, this.value);
68
- }
69
- getCustomType() {
70
- return 'function';
71
- }
72
- toNumber() {
73
- return Number.NaN;
74
- }
75
- toInt() {
76
- return 0;
77
- }
78
- toString() {
79
- const args = this.argumentDefs.map((item) => item.name);
80
- return `function ${this.name}(${args.join(', ')})`;
81
- }
82
- toTruthy() {
83
- return true;
84
- }
85
- run(self, args, callContext) {
86
- var _a;
87
- return __awaiter(this, void 0, void 0, function* () {
88
- const fnCtx = (_a = this.scope) === null || _a === void 0 ? void 0 : _a.fork({
89
- type: context_1.ContextType.Function,
90
- state: context_1.ContextState.Default
91
- });
92
- const argMap = new Map();
93
- if (this.injectSelf && !(self instanceof nil_1.default)) {
94
- args.unshift(self);
95
- }
96
- for (let index = 0; index < this.argumentDefs.length; index++) {
97
- const item = this.argumentDefs[index];
98
- argMap.set(item.name, args[index] || (yield item.defaultValue.handle(fnCtx)));
99
- }
100
- return this.value(fnCtx || callContext, self, argMap);
101
- });
102
- }
103
- }
104
- exports.default = CustomFunction;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Argument = void 0;
16
+ const context_1 = require("../context");
17
+ const operation_1 = __importDefault(require("../operations/operation"));
18
+ const reference_1 = __importDefault(require("../operations/reference"));
19
+ const base_1 = __importDefault(require("./base"));
20
+ const default_1 = __importDefault(require("./default"));
21
+ const nil_1 = __importDefault(require("./nil"));
22
+ class Argument {
23
+ constructor(name, defaultValue = default_1.default.Void) {
24
+ this.name = name;
25
+ if (defaultValue instanceof base_1.default) {
26
+ this.defaultValue = new reference_1.default(defaultValue);
27
+ }
28
+ else if (defaultValue instanceof operation_1.default) {
29
+ this.defaultValue = defaultValue;
30
+ }
31
+ else {
32
+ throw new Error('Invalid defaultValue in argument.');
33
+ }
34
+ }
35
+ static createWithCustomValue(name, defaultValue) {
36
+ return new Argument(name, new reference_1.default(defaultValue));
37
+ }
38
+ }
39
+ exports.Argument = Argument;
40
+ class CustomFunction extends base_1.default {
41
+ constructor(scope, name, callback, injectSelf = false) {
42
+ super();
43
+ this.scope = scope;
44
+ this.name = name;
45
+ this.value = callback;
46
+ this.injectSelf = injectSelf;
47
+ this.argumentDefs = [];
48
+ }
49
+ static createExternalAnonymous(callback) {
50
+ return new CustomFunction(null, 'anonymous', callback);
51
+ }
52
+ static createExternal(name, callback) {
53
+ return new CustomFunction(null, name, callback);
54
+ }
55
+ static createExternalWithSelf(name, callback) {
56
+ return new CustomFunction(null, name, callback, true).addArgument('self');
57
+ }
58
+ setInjectSelf(injectSelf) {
59
+ this.injectSelf = injectSelf;
60
+ return this;
61
+ }
62
+ addArgument(name, defaultValue = default_1.default.Void) {
63
+ this.argumentDefs.push(new Argument(name, defaultValue));
64
+ return this;
65
+ }
66
+ fork() {
67
+ return new CustomFunction(this.scope, this.name, this.value);
68
+ }
69
+ getCustomType() {
70
+ return 'function';
71
+ }
72
+ toNumber() {
73
+ return Number.NaN;
74
+ }
75
+ toInt() {
76
+ return 0;
77
+ }
78
+ toString() {
79
+ const args = this.argumentDefs.map((item) => item.name);
80
+ return `function ${this.name}(${args.join(', ')})`;
81
+ }
82
+ toTruthy() {
83
+ return true;
84
+ }
85
+ run(self, args, callContext) {
86
+ var _a;
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ const fnCtx = (_a = this.scope) === null || _a === void 0 ? void 0 : _a.fork({
89
+ type: context_1.ContextType.Function,
90
+ state: context_1.ContextState.Default
91
+ });
92
+ const argMap = new Map();
93
+ if (this.injectSelf && !(self instanceof nil_1.default)) {
94
+ args.unshift(self);
95
+ }
96
+ for (let index = 0; index < this.argumentDefs.length; index++) {
97
+ const item = this.argumentDefs[index];
98
+ argMap.set(item.name, args[index] || (yield item.defaultValue.handle(fnCtx)));
99
+ }
100
+ return this.value(fnCtx || callContext, self, argMap);
101
+ });
102
+ }
103
+ }
104
+ exports.default = CustomFunction;
@@ -1,24 +1,24 @@
1
- import Path from '../utils/path';
2
- import CustomValue from './base';
3
- import CustomFunction from './function';
4
- import { CustomObject } from './with-intrinsics';
5
- export declare class CustomInterfaceIterator implements Iterator<CustomValue> {
6
- next(): IteratorResult<CustomValue>;
7
- }
8
- export default class CustomInterface extends CustomObject {
9
- private readonly interfaceFns;
10
- private readonly type;
11
- readonly value: Object;
12
- constructor(type: string);
13
- getCustomType(): string;
14
- toString(): string;
15
- fork(): CustomInterface;
16
- toNumber(): number;
17
- toInt(): number;
18
- toTruthy(): boolean;
19
- [Symbol.iterator](): CustomInterfaceIterator;
20
- has(path: Path<CustomValue> | CustomValue): boolean;
21
- set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
22
- get(path: Path<CustomValue> | CustomValue): CustomValue;
23
- addFunction(name: string, fn: CustomFunction): CustomInterface;
24
- }
1
+ import Path from '../utils/path';
2
+ import CustomValue from './base';
3
+ import CustomFunction from './function';
4
+ import { CustomObject } from './with-intrinsics';
5
+ export declare class CustomInterfaceIterator implements Iterator<CustomValue> {
6
+ next(): IteratorResult<CustomValue>;
7
+ }
8
+ export default class CustomInterface extends CustomObject {
9
+ private readonly interfaceFns;
10
+ private readonly type;
11
+ readonly value: Object;
12
+ constructor(type: string);
13
+ getCustomType(): string;
14
+ toString(): string;
15
+ fork(): CustomInterface;
16
+ toNumber(): number;
17
+ toInt(): number;
18
+ toTruthy(): boolean;
19
+ [Symbol.iterator](): CustomInterfaceIterator;
20
+ has(path: Path<CustomValue> | CustomValue): boolean;
21
+ set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
22
+ get(path: Path<CustomValue> | CustomValue): CustomValue;
23
+ addFunction(name: string, fn: CustomFunction): CustomInterface;
24
+ }