@zvenigora/ng-eval-core 0.1.104 → 0.1.105

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,28 +1,9 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { Injectable } from '@angular/core';
3
3
  import { sha256 } from 'js-sha256';
4
- import { parse as parse$1, defaultOptions as defaultOptions$1, version } from 'acorn';
4
+ import { defaultOptions as defaultOptions$1, parse as parse$1, version } from 'acorn';
5
5
  import * as walk from 'acorn-walk';
6
6
 
7
- /**
8
- * Base class for evaluation.
9
- */
10
- class BaseEval {
11
- /**
12
- * Gets the parser options.
13
- */
14
- get parserOptions() {
15
- return this._parserOptions;
16
- }
17
- /**
18
- * Sets the parser options.
19
- * @param value The parser options to set.
20
- */
21
- set parserOptions(value) {
22
- this._parserOptions = value;
23
- }
24
- }
25
-
26
7
  /**
27
8
  * Represents a registry that stores key-value pairs.
28
9
  */
@@ -968,1222 +949,1288 @@ const getContextKey = (context, key, caseInsensitive) => {
968
949
  };
969
950
 
970
951
  /**
971
- * @description
972
- * Parses expression and returns ES6/ES2020 AST.
973
- * @param expr expression to parse
974
- * @param options parser options
975
- */
976
- const parse = (expr, options) => {
977
- options = { ecmaVersion: 2020, ...options };
978
- const acornOptions = { ecmaVersion: 2020, ...options };
979
- const program = parse$1(expr, acornOptions);
980
- if (!options.extractExpressions) {
981
- return program;
982
- }
983
- return extractExpression(program);
984
- };
985
- /**
986
- * @description
987
- * Walks through the AST and extracts first expression.
988
- * @param expr expression to parse
989
- */
990
- const extractExpression = (program) => {
991
- if (program.body.length === 1) {
992
- const expression = program.body[0];
993
- if (expression.type === 'ExpressionStatement') {
994
- return expression.expression;
995
- }
996
- }
997
- return undefined;
998
- };
999
-
1000
- const pushVisitorResult = (node, st, value) => {
1001
- const key = getKey(node);
1002
- const result = st.result;
1003
- result.stack.push(value);
1004
- result.trace.set(key, value);
1005
- return value;
1006
- };
1007
- const popVisitorResult = (node, st) => {
1008
- const result = st.result;
1009
- const value = result.stack.pop();
1010
- return value;
1011
- };
1012
- const pushVisitorResultAsync = (node, st, value) => {
1013
- const key = getKey(node);
1014
- const result = st.result;
1015
- result.stack.push(value);
1016
- result.trace.set(key, value);
1017
- return value;
1018
- };
1019
- const popVisitorResultAsync = (node, st) => {
1020
- const result = st.result;
1021
- const value = result.stack.pop();
1022
- return value;
1023
- };
1024
- const getKey = (node) => {
1025
- const key = `${node.type}(${node.start}:${node.end})`;
1026
- return key;
1027
- };
1028
-
1029
- const beforeVisitor = (node, st) => {
1030
- const options = st.options;
1031
- if (options?.['trackTime']) {
1032
- const t = performance.now();
1033
- console.time('before: ' + node.type);
1034
- return t;
1035
- }
1036
- return undefined;
1037
- };
1038
-
1039
- const afterVisitor = (node, st) => {
1040
- const options = st.options;
1041
- if (options?.['trackTime']) {
1042
- const t = performance.now();
1043
- console.time('after: ' + node.type);
1044
- return t;
952
+ * Represents the evaluation context for the code execution.
953
+ */
954
+ class EvalContext {
955
+ /**
956
+ * Gets the original registry.
957
+ */
958
+ get original() {
959
+ return this._original;
1045
960
  }
1046
- return undefined;
1047
- };
1048
-
1049
- var RecursiveVisitorResultType;
1050
- (function (RecursiveVisitorResultType) {
1051
- RecursiveVisitorResultType[RecursiveVisitorResultType["Stack"] = 0] = "Stack";
1052
- RecursiveVisitorResultType[RecursiveVisitorResultType["Registry"] = 1] = "Registry";
1053
- })(RecursiveVisitorResultType || (RecursiveVisitorResultType = {}));
1054
-
1055
- const literals = Registry.fromObject({
1056
- 'undefined': undefined,
1057
- 'null': null,
1058
- 'true': true,
1059
- 'false': false,
1060
- }, { caseInsensitive: true });
1061
- const identifierVisitor = (node, st) => {
1062
- if (st.options?.caseInsensitive) {
1063
- return identifierVisitorCaseInsensitive(node, st);
961
+ /**
962
+ * Gets the scopes registry.
963
+ */
964
+ get priorScopes() {
965
+ return this._priorScopes;
1064
966
  }
1065
- beforeVisitor(node, st);
1066
- const context = st.context;
1067
- const value = node.name === 'this'
1068
- ? st.context
1069
- : context?.get(node.name);
1070
- pushVisitorResult(node, st, value);
1071
- afterVisitor(node, st);
1072
- };
1073
- const identifierVisitorCaseInsensitive = (node, st) => {
1074
- beforeVisitor(node, st);
1075
- const context = st.context;
1076
- const value = equalIgnoreCase(node.name, 'this')
1077
- ? st.context
1078
- : context?.get(node.name) ?? literals.get(node.name);
1079
- pushVisitorResult(node, st, value);
1080
- afterVisitor(node, st);
1081
- };
1082
-
1083
- const literalVisitor = (node, st) => {
1084
- beforeVisitor(node, st);
1085
- const value = node.value;
1086
- pushVisitorResult(node, st, value);
1087
- afterVisitor(node, st);
1088
- };
1089
-
1090
- const binaryExpressionVisitor = (node, st, callback) => {
1091
- beforeVisitor(node, st);
1092
- callback(node.left, st);
1093
- const left = popVisitorResult(node, st);
1094
- callback(node.right, st);
1095
- const right = popVisitorResult(node, st);
1096
- const value = node.operator === '+' ? left + right :
1097
- node.operator === '-' ? left - right :
1098
- node.operator === '*' ? left * right :
1099
- node.operator === '/' ? left / right :
1100
- node.operator === '%' ? left % right :
1101
- node.operator === '**' ? left ** right :
1102
- node.operator === '<<' ? left << right :
1103
- node.operator === '>>' ? left >> right :
1104
- node.operator === '>>>' ? left >>> right :
1105
- node.operator === '&' ? left & right :
1106
- node.operator === '^' ? left ^ right :
1107
- node.operator === '|' ? left | right :
1108
- node.operator === '==' ? left == right :
1109
- node.operator === '!=' ? left != right :
1110
- node.operator === '===' ? left === right :
1111
- node.operator === '!==' ? left !== right :
1112
- node.operator === '<' ? left < right :
1113
- node.operator === '<=' ? left <= right :
1114
- node.operator === '>' ? left > right :
1115
- node.operator === '>=' ? left >= right :
1116
- node.operator === 'in' ? left in right :
1117
- node.operator === 'instanceof' ? left instanceof right :
1118
- node.operator === '??' ? left ?? right :
1119
- undefined;
1120
- pushVisitorResult(node, st, value);
1121
- afterVisitor(node, st);
1122
- };
1123
-
1124
- const memberExpressionVisitor = (node, st, callback) => {
1125
- beforeVisitor(node, st);
1126
- const [, , value] = evaluateMember(node, st, callback);
1127
- pushVisitorResult(node, st, value);
1128
- afterVisitor(node, st);
1129
- };
1130
- const evaluateMember = (node, st, callback) => {
1131
- // Get the object
1132
- callback(node.object, st);
1133
- const obj = popVisitorResult(node, st);
1134
- const object = (node.optional ? (obj || {}) : obj);
1135
- // Get the property
1136
- let key;
1137
- if (node.property.type === 'Identifier') {
1138
- key = node.property.name;
967
+ /**
968
+ * Gets the scopes registry.
969
+ */
970
+ get scopes() {
971
+ return this._scopes;
1139
972
  }
1140
- else if (node.property.type === 'Literal') {
1141
- key = node.property.value;
973
+ /**
974
+ * Gets the lookups registry.
975
+ */
976
+ get lookups() {
977
+ return this._lookups;
1142
978
  }
1143
- else {
1144
- callback(node.property, st);
1145
- key = popVisitorResult(node, st);
979
+ /**
980
+ * Gets the evaluation options.
981
+ */
982
+ get options() {
983
+ return this._options;
1146
984
  }
1147
- // Get the value
1148
- if (object === st.context) {
1149
- const value = st.context.get(key);
1150
- const thisValue = st.context.getThis(key);
1151
- return [thisValue ?? object, key, value];
985
+ /**
986
+ * Creates a new instance of EvalContext.
987
+ * @param original - The original registry or an object to create a registry from.
988
+ * @param options - The evaluation options.
989
+ */
990
+ constructor(original, options) {
991
+ this.type = 'EvalContext';
992
+ this._original = original;
993
+ this._priorScopes = [];
994
+ this._scopes = new Stack();
995
+ this._options = options;
996
+ this._lookups = new Registry();
1152
997
  }
1153
- else {
1154
- const caseInsesitive = !!(st.options?.caseInsensitive);
1155
- const pair = getKeyValue(object, key, caseInsesitive);
1156
- if (pair) {
1157
- const [key, value] = pair;
1158
- return [object, key, value];
998
+ /**
999
+ * Converts the given context to an instance of EvalContext.
1000
+ * @param context - The context to convert.
1001
+ * @param options - The evaluation options.
1002
+ * @returns The converted EvalContext instance.
1003
+ */
1004
+ static fromContext(context, options) {
1005
+ if (context instanceof EvalContext) {
1006
+ return context;
1159
1007
  }
1008
+ const ctx = fromContext(context ?? {}, options);
1009
+ return new EvalContext(ctx, options ?? {});
1160
1010
  }
1161
- return [object, key, undefined];
1162
- };
1163
-
1164
- const arrayExpressionVisitor = (node, st, callback) => {
1165
- beforeVisitor(node, st);
1166
- const elements = evaluateArray(node, node.elements, st, callback);
1167
- pushVisitorResult(node, st, elements);
1168
- afterVisitor(node, st);
1169
- };
1170
- const evaluateArray = (node, items, st, callback) => {
1171
- const elements = [];
1172
- for (const element of items) {
1173
- if (element === null || typeof element === 'undefined') {
1174
- elements.push(element);
1011
+ /**
1012
+ * Gets the value associated with the specified key from the evaluation context.
1013
+ * @param key - The key to retrieve the value for.
1014
+ * @returns The value associated with the key, or undefined if not found.
1015
+ */
1016
+ get(key) {
1017
+ for (const scope of this._scopes.asArray()) {
1018
+ const value = getContextValue(scope, key);
1019
+ if (value !== undefined) {
1020
+ return value;
1021
+ }
1175
1022
  }
1176
- else if (element.type === 'SpreadElement') {
1177
- callback(element, st);
1178
- const values = popVisitorResult(node, st);
1179
- elements.push(...values);
1023
+ if (this._original) {
1024
+ const value = getContextValue(this._original, key);
1025
+ if (value !== undefined) {
1026
+ return value;
1027
+ }
1180
1028
  }
1181
- else {
1182
- callback(element, st);
1183
- const value = popVisitorResult(node, st);
1184
- elements.push(value);
1029
+ for (const scope of this._priorScopes) {
1030
+ const value = getContextValue(scope, key);
1031
+ if (value !== undefined) {
1032
+ return value;
1033
+ }
1034
+ }
1035
+ for (const lookup of Array.from(this._lookups.values)) {
1036
+ const value = lookup(key, this, this._options);
1037
+ if (value !== undefined) {
1038
+ return value;
1039
+ }
1185
1040
  }
1041
+ return undefined;
1186
1042
  }
1187
- return elements;
1188
- };
1189
-
1190
- const callExpressionVisitor = (node, st, callback) => {
1191
- beforeVisitor(node, st);
1192
- const args = evaluateArray(node, node.arguments, st, callback);
1193
- if (node.callee.type === 'MemberExpression') {
1194
- const [object, , fn] = evaluateMember(node.callee, st, callback);
1195
- const caller = fn;
1196
- const value = !caller && node.callee.optional
1197
- ? undefined : caller.apply(object, args);
1198
- pushVisitorResult(node, st, value);
1043
+ /**
1044
+ * Retrieves the `this` value of the specified key from the evaluation context.
1045
+ * The value is searched in the original context, prior scopes, and lookup functions.
1046
+ * @param key - The key to retrieve the value for.
1047
+ * @returns The `this` value associated with the key, or undefined if not found.
1048
+ */
1049
+ getThis(key) {
1050
+ if (this._original) {
1051
+ const value = getContextValue(this._original, key);
1052
+ if (value !== undefined) {
1053
+ return this._original;
1054
+ }
1055
+ }
1056
+ for (const scope of this._priorScopes) {
1057
+ const value = getContextValue(this._original, key);
1058
+ if (value !== undefined) {
1059
+ return scope;
1060
+ }
1061
+ }
1062
+ for (const lookup of Array.from(this._lookups.values)) {
1063
+ const value = lookup(key, this, this._options);
1064
+ if (value !== undefined) {
1065
+ return lookup;
1066
+ }
1067
+ }
1068
+ return undefined;
1199
1069
  }
1200
- else {
1201
- callback(node.callee, st);
1202
- const caller = popVisitorResult(node, st);
1203
- const value = !caller && node.optional
1204
- ? undefined : caller.apply(st.context, args);
1205
- pushVisitorResult(node, st, value);
1070
+ /**
1071
+ * Retrieves the value associated with the specified key from the evaluation context.
1072
+ *
1073
+ * @param key - The key to retrieve the value for.
1074
+ * @returns The value associated with the key, or undefined if the key is not found.
1075
+ */
1076
+ getKey(key) {
1077
+ const caseInsensitive = !!this._options.caseInsensitive;
1078
+ for (const scope of this._scopes.asArray()) {
1079
+ const foundKey = getContextKey(scope, key, caseInsensitive);
1080
+ if (foundKey !== undefined) {
1081
+ return foundKey;
1082
+ }
1083
+ }
1084
+ if (this._original) {
1085
+ const foundKey = getContextKey(this._original, key, caseInsensitive);
1086
+ if (foundKey !== undefined) {
1087
+ return foundKey;
1088
+ }
1089
+ }
1090
+ for (const scope of this._priorScopes) {
1091
+ const foundKey = getContextKey(scope, key, caseInsensitive);
1092
+ if (foundKey !== undefined) {
1093
+ return foundKey;
1094
+ }
1095
+ }
1096
+ return undefined;
1206
1097
  }
1207
- afterVisitor(node, st);
1208
- };
1209
- const evaluateCall = (node, st, callback) => {
1210
- callback(node.callee, st);
1211
- const caller = popVisitorResult(node, st);
1212
- const args = node.arguments.map((argument) => {
1213
- callback(argument, st);
1214
- const value = popVisitorResult(node, st);
1215
- return value;
1216
- });
1217
- const value = caller.apply(st.context, args);
1218
- return value;
1219
- };
1220
-
1221
- const awaitVisitor = (node, st, callback) => {
1222
- beforeVisitor(node, st);
1223
- const promise = new Promise((resolve) => {
1224
- callback(node.argument, st);
1225
- const value = popVisitorResult(node, st);
1226
- resolve(value);
1227
- });
1228
- pushVisitorResult(node, st, promise);
1229
- afterVisitor(node, st);
1230
- };
1231
-
1232
- const conditionalExpressionVisitor = (node, st, callback) => {
1233
- beforeVisitor(node, st);
1234
- callback(node.test, st);
1235
- const test = popVisitorResult(node, st);
1236
- if (test) {
1237
- callback(node.consequent, st);
1098
+ /**
1099
+ * Sets a key-value pair in the context.
1100
+ * If the original context is a Registry, the key-value pair is set using the Registry's set method.
1101
+ * If the original context is an Object, the key-value pair is set directly on the object.
1102
+ * @param key - The key of the pair.
1103
+ * @param value - The value of the pair.
1104
+ */
1105
+ set(key, value) {
1106
+ if (this._original && this._original instanceof Registry) {
1107
+ const registry = this._original;
1108
+ registry.set(key, value);
1109
+ }
1110
+ else if (this._original && this._original instanceof Object) {
1111
+ const obj = this._original;
1112
+ obj[key] = value;
1113
+ }
1238
1114
  }
1239
- else {
1240
- callback(node.alternate, st);
1115
+ /**
1116
+ * Pushes a context to the scope stack.
1117
+ * @param context - The context to push.
1118
+ * @param options - The evaluation options.
1119
+ */
1120
+ push(context, options) {
1121
+ const ctx = fromContext(context, options);
1122
+ this._scopes.push(ctx);
1241
1123
  }
1242
- const value = popVisitorResult(node, st);
1243
- pushVisitorResult(node, st, value);
1244
- afterVisitor(node, st);
1245
- };
1246
-
1247
- const unaryExpressionVisitor = (node, st, callback) => {
1248
- beforeVisitor(node, st);
1249
- callback(node.argument, st);
1250
- const argument = popVisitorResult(node, st);
1251
- const value = node.operator === '-' ? -argument :
1252
- node.operator === '+' ? +argument :
1253
- node.operator === '~' ? ~argument :
1254
- node.operator === '!' ? !argument :
1255
- node.operator === 'typeof' ? typeof argument :
1256
- node.operator === 'void' ? void argument :
1257
- // node.operator === 'delete' ? delete argument :
1258
- undefined;
1259
- pushVisitorResult(node, st, value);
1260
- afterVisitor(node, st);
1261
- };
1124
+ /**
1125
+ * Pops a context from the scope stack.
1126
+ */
1127
+ pop() {
1128
+ this._scopes.pop();
1129
+ }
1130
+ }
1262
1131
 
1263
- const logicalExpressionVisitor = (node, st, callback) => {
1264
- beforeVisitor(node, st);
1265
- callback(node.left, st);
1266
- const left = popVisitorResult(node, st);
1267
- if (node.operator === '&&' && !left) {
1268
- pushVisitorResult(node, st, left);
1269
- afterVisitor(node, st);
1270
- return;
1132
+ /**
1133
+ * Represents the result of an evaluation.
1134
+ */
1135
+ class EvalResult {
1136
+ /**
1137
+ * Gets the stack of evaluated values.
1138
+ */
1139
+ get stack() {
1140
+ return this._stack;
1271
1141
  }
1272
- else if (node.operator === '||' && left) {
1273
- pushVisitorResult(node, st, true);
1274
- afterVisitor(node, st);
1275
- return;
1142
+ /**
1143
+ * Gets the evaluated value.
1144
+ */
1145
+ get value() {
1146
+ return this._value;
1276
1147
  }
1277
- callback(node.right, st);
1278
- const right = popVisitorResult(node, st);
1279
- const value = node.operator === '&&' ? left && right :
1280
- node.operator === '||' ? left || right :
1281
- undefined;
1282
- pushVisitorResult(node, st, value);
1283
- afterVisitor(node, st);
1284
- };
1285
-
1286
- const thisExpressionVisitor = (node, st) => {
1287
- beforeVisitor(node, st);
1288
- const value = st.context;
1289
- pushVisitorResult(node, st, value);
1290
- afterVisitor(node, st);
1291
- };
1292
-
1293
- const newExpressionVisitor = (node, st, callback) => {
1294
- beforeVisitor(node, st);
1295
- const args = evaluateArray(node, node.arguments, st, callback);
1296
- callback(node.callee, st);
1297
- const constructor = popVisitorResult(node, st);
1298
- const value = newFunc(constructor, ...args);
1299
- pushVisitorResult(node, st, value);
1300
- afterVisitor(node, st);
1301
- };
1302
- // based on Reflect.construct()
1303
- // https://stackoverflow.com/questions/10428603/simulate-the-new-operator-in-javascript
1304
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct
1305
- const newFunc = (constructor, ...args) => {
1306
- const obj = Reflect.construct(constructor, args);
1307
- return obj;
1308
- };
1309
-
1310
- const templateLiteralVisitor = (node, st, callback) => {
1311
- beforeVisitor(node, st);
1312
- const expressions = node.expressions.map((expression) => {
1313
- callback(expression, st);
1314
- const value = popVisitorResult(node, st);
1315
- return value;
1316
- });
1317
- const value = node.quasis.reduce((accumulator, templateElement, index) => {
1318
- accumulator += templateElement.value.cooked;
1319
- if (!templateElement.tail) {
1320
- accumulator += expressions[index] ?? '';
1321
- }
1322
- return accumulator;
1323
- }, '');
1324
- pushVisitorResult(node, st, value);
1325
- afterVisitor(node, st);
1326
- };
1327
-
1328
- const taggedTemplateExpressionVisitor = (node, st, callback) => {
1329
- beforeVisitor(node, st);
1330
- callback(node.tag, st);
1331
- const tag = popVisitorResult(node, st);
1332
- const quasiExpressions = node.quasi.expressions.map((expression) => {
1333
- callback(expression, st);
1334
- const value = popVisitorResult(node, st);
1335
- return value;
1336
- });
1337
- const quasi = node.quasi.quasis.map((templateElement) => {
1338
- return templateElement.value.cooked;
1339
- });
1340
- const value = tag.apply(st.context, [quasi, ...quasiExpressions]);
1341
- pushVisitorResult(node, st, value);
1342
- afterVisitor(node, st);
1343
- };
1344
-
1345
- const objectExpressionVisitor = (node, st, callback) => {
1346
- beforeVisitor(node, st);
1347
- const map = new Map();
1348
- for (const property of node.properties) {
1349
- if (property.type === 'SpreadElement') {
1350
- callback(property.argument, st);
1351
- const value = popVisitorResult(property, st);
1352
- for (const [key, val] of Object.entries(value)) {
1353
- map.set(key, val);
1354
- }
1355
- }
1356
- else {
1357
- const pair = evalProperty(property, st, callback);
1358
- map.set(pair.key, pair.value);
1359
- }
1148
+ /**
1149
+ * Gets the error that occurred during evaluation.
1150
+ */
1151
+ get error() {
1152
+ return this._error;
1360
1153
  }
1361
- const obj = Object.fromEntries(map);
1362
- pushVisitorResult(node, st, obj);
1363
- afterVisitor(node, st);
1364
- };
1365
- const evalProperty = (property, st, callback) => {
1366
- // if (property.shorthand) {
1367
- // callback(property.value, st);
1368
- // const value = popVisitorResult(property, st);
1369
- // return { key: value, value };
1370
- // } else
1371
- if (property.computed) {
1372
- callback(property.key, st);
1373
- const key = popVisitorResult(property, st);
1374
- callback(property.value, st);
1375
- const value = popVisitorResult(property, st);
1376
- return { key, value };
1154
+ /**
1155
+ * Gets the error message associated with the evaluation error.
1156
+ */
1157
+ get errorMessage() {
1158
+ return this._errorMessage;
1377
1159
  }
1378
- else if (property.key.type === 'Identifier') {
1379
- const key = property.key.name;
1380
- callback(property.value, st);
1381
- const value = popVisitorResult(property, st);
1382
- return { key, value };
1160
+ /**
1161
+ * Gets whether the evaluation resulted in an error.
1162
+ */
1163
+ get isError() {
1164
+ return this._isError;
1383
1165
  }
1384
- else if (property.key.type === 'Literal') {
1385
- const key = property.key.value;
1386
- callback(property.value, st);
1387
- const value = popVisitorResult(property, st);
1388
- return { key, value };
1166
+ /**
1167
+ * Gets whether the evaluation was successful.
1168
+ */
1169
+ get isSuccess() {
1170
+ return this._isSuccess;
1389
1171
  }
1390
- else {
1391
- throw new Error(`Unsupported property key type: ${property.key.type}`);
1172
+ /**
1173
+ * Gets whether the evaluated value is undefined.
1174
+ */
1175
+ get isUndefined() {
1176
+ return this._isUndefined;
1392
1177
  }
1393
- };
1394
-
1395
- /**
1396
- * Returns the default visitors for the recursive traversal of an abstract syntax tree.
1397
- * @returns The default visitors object.
1398
- */
1399
- const getDefaultVisitors = () => {
1400
- const visitors = {};
1401
- visitors['BinaryExpression'] = binaryExpressionVisitor;
1402
- visitors['Identifier'] = identifierVisitor;
1403
- visitors['Literal'] = literalVisitor;
1404
- visitors['CallExpression'] = callExpressionVisitor;
1405
- visitors['AwaitExpression'] = awaitVisitor;
1406
- visitors['ConditionalExpression'] = conditionalExpressionVisitor;
1407
- visitors['MemberExpression'] = memberExpressionVisitor;
1408
- visitors['ArrayExpression'] = arrayExpressionVisitor;
1409
- visitors['UnaryExpression'] = unaryExpressionVisitor;
1410
- visitors['LogicalExpression'] = logicalExpressionVisitor;
1411
- visitors['ThisExpression'] = thisExpressionVisitor;
1412
- visitors['NewExpression'] = newExpressionVisitor;
1413
- visitors['TemplateLiteral'] = templateLiteralVisitor;
1414
- visitors['TaggedTemplateExpression'] = taggedTemplateExpressionVisitor;
1415
- visitors['ObjectExpression'] = objectExpressionVisitor;
1416
- visitors['AssignmentExpression'] = assignmentExpressionVisitor;
1417
- visitors['UpdateExpression'] = updateExpressionVisitor;
1418
- visitors['ArrowFunctionExpression'] = arrowFunctionExpressionVisitor;
1419
- return visitors;
1420
- };
1421
-
1422
- const assignmentOperators = {
1423
- '=': (left, value) => { return left = value; },
1424
- '+=': (left, value) => { return left += value; },
1425
- '-=': (left, value) => { return left -= value; },
1426
- '*=': (left, value) => { return left *= value; },
1427
- '/=': (left, value) => { return left /= value; },
1428
- '%=': (left, value) => { return left %= value; },
1429
- '**=': (left, value) => { return left **= value; },
1430
- '<<=': (left, value) => { return left <<= value; },
1431
- '>>=': (left, value) => { return left >>= value; },
1432
- '>>>=': (left, value) => { return left >>>= value; },
1433
- '&=': (left, value) => { return left &= value; },
1434
- '^=': (left, value) => { return left ^= value; },
1435
- '|=': (left, value) => { return left |= value; },
1436
- '&&=': (left, value) => { return left &&= value; },
1437
- '||=': (left, value) => { return left ||= value; },
1438
- '??=': (left, value) => { return left ??= value; },
1439
- };
1440
- const assignmentExpressionVisitor = (node, st, callback) => {
1441
- beforeVisitor(node, st);
1442
- callback(node.left, st);
1443
- const left = popVisitorResult(node, st);
1444
- callback(node.right, st);
1445
- const right = popVisitorResult(node, st);
1446
- const func = assignmentOperators[node.operator];
1447
- if (!func) {
1448
- throw new Error(`Unsupported assignment operator: ${node.operator}`);
1178
+ /**
1179
+ * Gets the trace of evaluated values.
1180
+ */
1181
+ get trace() {
1182
+ return this._trace;
1449
1183
  }
1450
- else if (!st.context) {
1451
- throw new Error(`Context is not set.`);
1184
+ /**
1185
+ * Gets the evaluation context.
1186
+ */
1187
+ get context() {
1188
+ return this._context;
1452
1189
  }
1453
- else if (node.left.type === 'Identifier') {
1454
- const key = st.options?.caseInsensitive
1455
- ? st.context.getKey(node.left.name)
1456
- : node.left.name;
1457
- const value = func(left, right);
1458
- st.context.set(key, value);
1459
- pushVisitorResult(node, st, value);
1190
+ /**
1191
+ * Gets the evaluation options.
1192
+ */
1193
+ get options() {
1194
+ return this._context.options;
1460
1195
  }
1461
- else if (node.left.type === 'MemberExpression') {
1462
- const [object, key,] = evaluateMember(node.left, st, callback);
1463
- const value = func(left, right);
1464
- object[key] = value;
1465
- pushVisitorResult(node, st, value);
1196
+ /**
1197
+ * Gets the duration of the evaluation result.
1198
+ * @returns The duration in milliseconds.
1199
+ */
1200
+ get duration() {
1201
+ if (!this._startDate || !this._endDate) {
1202
+ return undefined;
1203
+ }
1204
+ return this._endDate - this._startDate;
1466
1205
  }
1467
- afterVisitor(node, st);
1468
- };
1469
-
1470
- const updateOperators = {
1471
- '++': (value) => { return value + 1; },
1472
- '--': (value) => { return value - 1; },
1473
- };
1474
- const updateExpressionVisitor = (node, st, callback) => {
1475
- beforeVisitor(node, st);
1476
- if (!st.context) {
1477
- throw new Error(`Context is not set.`);
1206
+ /**
1207
+ * Creates a new instance of EvalResult.
1208
+ * @param trace The trace of evaluated values.
1209
+ * @param context The evaluation context.
1210
+ */
1211
+ constructor(context) {
1212
+ this._stack = new Stack();
1213
+ this._trace = new Registry;
1214
+ this._context = context;
1478
1215
  }
1479
- else if (node.argument.type === 'Identifier') {
1480
- const key = st.options?.caseInsensitive
1481
- ? st.context.getKey(node.argument.name)
1482
- : node.argument.name;
1483
- const value = st.context?.get(key);
1484
- const newValue = updateOperators[node.operator](value);
1485
- st.context?.set(key, newValue);
1486
- pushVisitorResult(node, st, node.prefix ? newValue : value);
1216
+ /**
1217
+ * Starts the evaluation process.
1218
+ */
1219
+ start() {
1220
+ this._startDate = performance.now();
1487
1221
  }
1488
- else if (node.argument.type === 'MemberExpression') {
1489
- callback(node.argument, st);
1490
- const [, property, value] = evaluateMember(node.argument, st, callback);
1491
- const newValue = updateOperators[node.operator](value);
1492
- st.context?.set(property, newValue);
1493
- pushVisitorResult(node, st, node.prefix ? newValue : value);
1222
+ /**
1223
+ * Stops the evaluation and records the end date.
1224
+ */
1225
+ stop() {
1226
+ this._endDate = performance.now();
1227
+ return this.duration;
1494
1228
  }
1495
- afterVisitor(node, st);
1496
- };
1497
-
1498
- const arrowFunctionExpressionVisitor = (node, st, callback) => {
1499
- beforeVisitor(node, st);
1500
- const fn = (...arrowArgs) => {
1501
- const newContext = evaluatePatterns(node.params, st, callback, arrowArgs);
1502
- st.context?.push(newContext);
1503
- const value = evaluate(node.body, st);
1504
- st.context?.pop();
1505
- return value;
1506
- };
1507
- pushVisitorResult(node, st, fn);
1508
- afterVisitor(node, st);
1509
- };
1510
-
1511
- // based on evalArrowContext
1512
- // https://github.com/6utt3rfly/jse-eval/blob/main/index.ts
1513
- const evaluatePatterns = (patterns, st, callback, args) => {
1514
- let context = {};
1515
- patterns.map((pattern, i) => {
1516
- switch (pattern.type) {
1517
- case 'Identifier':
1518
- {
1519
- const object = evaluateIdentifier(pattern, st, args[i]);
1520
- context = { ...context, ...object };
1521
- }
1522
- break;
1523
- case 'MemberExpression':
1524
- {
1525
- const object = evaluateMemberExpression(pattern, st, callback, args[i]);
1526
- context = { ...context, ...object };
1527
- }
1528
- break;
1529
- case 'ObjectPattern':
1530
- {
1531
- const object = evaluateObjectPattern(pattern, st, callback, args[i]);
1532
- context = { ...context, ...object };
1533
- }
1534
- break;
1535
- case 'ArrayPattern':
1536
- {
1537
- const object = evaluateArrayPattern(pattern, st, callback, args[i]);
1538
- context = { ...context, ...object };
1539
- }
1540
- break;
1541
- case 'RestElement':
1542
- {
1543
- const object = evaluateRestElement(pattern, st, callback, args.slice(i));
1544
- context = { ...context, ...object };
1545
- }
1546
- break;
1547
- // case 'AssignmentPattern': {
1548
- // const object = evaluateAssignmentPattern(pattern, st, callback, args[i]);
1549
- // context = {...context, ...object};
1550
- // }
1551
- // break;
1552
- }
1553
- });
1554
- return context;
1555
- };
1556
- const evaluatePattern = (pattern, st, callback, arg) => {
1557
- switch (pattern.type) {
1558
- case 'Identifier':
1559
- return evaluateIdentifier(pattern, st, arg);
1560
- case 'MemberExpression':
1561
- return evaluateMemberExpression(pattern, st, callback, arg);
1562
- case 'ObjectPattern':
1563
- return evaluateObjectPattern(pattern, st, callback, arg);
1564
- case 'ArrayPattern':
1565
- return evaluateArrayPattern(pattern, st, callback, arg);
1566
- case 'RestElement':
1567
- return evaluateRestElement(pattern, st, callback, arg);
1568
- // case 'AssignmentPattern':
1569
- // return evaluateAssignmentPattern(pattern, st, callback, arg);
1570
- }
1571
- return {};
1572
- };
1573
- const evaluateIdentifier = (pattern, st, arg) => {
1574
- const object = {};
1575
- object[pattern.name] = arg;
1576
- return object;
1577
- };
1578
- const evaluateMemberExpression = (pattern, st, callback, arg) => {
1579
- if (pattern.type === 'MemberExpression') {
1580
- console.log(pattern, st, callback, arg);
1581
- throw new Error('evaluateMemberExpression is not implemented.');
1229
+ /**
1230
+ * Sets the success value of the evaluation result.
1231
+ *
1232
+ * @param value The value to set as the success value.
1233
+ * @returns void
1234
+ */
1235
+ setSuccess(value) {
1236
+ this._value = value;
1237
+ this._isSuccess = true;
1238
+ this._isError = false;
1239
+ this._isUndefined = false;
1582
1240
  }
1583
- return {};
1584
- };
1585
- const evaluateObjectPattern = (pattern, st, callback, arg) => {
1586
- let context = {};
1587
- pattern.properties.map((pattern) => {
1588
- switch (pattern.type) {
1589
- case 'Property':
1590
- {
1591
- let key;
1592
- if (pattern.key.type === 'Identifier') {
1593
- key = pattern.key.name;
1594
- }
1595
- else if (pattern.key.type === 'Literal') {
1596
- key = pattern.key.value;
1597
- }
1598
- else {
1599
- callback(pattern.key, st);
1600
- key = popVisitorResult(pattern, st);
1601
- if (typeof key !== 'string' && typeof key !== 'number')
1602
- throw new Error(`Unsupported property key type: ${pattern.key.type}`);
1603
- }
1604
- const ctx = arg;
1605
- st.context?.push(ctx);
1606
- callback(pattern.value, st);
1607
- const value = popVisitorResult(pattern, st);
1608
- st.context?.pop();
1609
- const pair = {};
1610
- pair[key] = value;
1611
- context = { ...context, ...pair };
1612
- }
1613
- break;
1614
- case 'RestElement':
1615
- {
1616
- const object = evaluateRestElement(pattern, st, callback, arg);
1617
- context = { ...context, ...object };
1618
- }
1619
- break;
1241
+ /**
1242
+ * Sets the failure state of the evaluation result.
1243
+ * @param error The error object associated with the failure.
1244
+ */
1245
+ setFailure(error) {
1246
+ this._error = error;
1247
+ if (error instanceof Error) {
1248
+ this._errorMessage = error.message;
1620
1249
  }
1621
- });
1622
- return context;
1623
- };
1624
- const evaluateArrayPattern = (pattern, st, callback, args) => {
1625
- const patterns = pattern.elements.filter(s => !!s);
1626
- const object = evaluatePatterns(patterns, st, callback, args);
1627
- return object;
1628
- };
1629
- const evaluateRestElement = (pattern, st, callback, args) => {
1630
- const argument = pattern.argument;
1631
- const object = evaluatePattern(argument, st, callback, args);
1632
- return object;
1633
- };
1634
- // const evaluateAssignmentPattern = (pattern: AssignmentPattern, st: EvalState, callback: walk.WalkerCallback<EvalState>, args: unknown) => {
1635
- // const left = evaluatePattern(pattern.left, st, callback, args);
1636
- // callback(pattern.right, st);
1637
- // const value = popVisitorResult(pattern, st);
1638
- // const object = {...left};
1639
- // return object;
1640
- // }
1250
+ this._isSuccess = false;
1251
+ this._isError = true;
1252
+ this._isUndefined = false;
1253
+ }
1254
+ }
1641
1255
 
1642
1256
  /**
1643
- * Evaluates the given node and returns the result.
1644
- *
1645
- * @param node The node to evaluate.
1646
- * @param state The evaluation state.
1647
- * @returns The result of the evaluation.
1257
+ * Represents the evaluation state, which includes the context, result, and options.
1648
1258
  */
1649
- const evaluate = (node, state) => {
1650
- if (node) {
1651
- state.result.start();
1652
- try {
1653
- const visitors = getDefaultVisitors();
1654
- walk.recursive(node, state, visitors);
1655
- const value = popVisitorResult(node, state);
1656
- state.result.stop();
1657
- state.result.setSuccess(value);
1658
- return value;
1659
- }
1660
- catch (error) {
1661
- state.result.stop();
1662
- state.result.setFailure(error);
1663
- throw error;
1664
- }
1259
+ class EvalState {
1260
+ /**
1261
+ * Gets the evaluation context.
1262
+ */
1263
+ get context() {
1264
+ return this._context;
1665
1265
  }
1666
- return undefined;
1667
- };
1668
- /**
1669
- * Asynchronously evaluates the given abstract syntax tree (AST) using the provided evaluation state.
1670
- * @param ast The abstract syntax tree to evaluate.
1671
- * @param state The evaluation state.
1672
- * @returns A promise that resolves to the evaluated value or undefined.
1673
- */
1674
- const evaluateAsync = (ast, state) => {
1675
- const promise = new Promise((resolve) => {
1676
- const value = evaluate(ast, state);
1677
- resolve(value);
1678
- });
1679
- return promise;
1266
+ /**
1267
+ * Gets the evaluation result.
1268
+ */
1269
+ get result() {
1270
+ return this._result;
1271
+ }
1272
+ /**
1273
+ * Gets the evaluation options.
1274
+ */
1275
+ get options() {
1276
+ return this._options;
1277
+ }
1278
+ /**
1279
+ * Gets whether the evaluation is asynchronous.
1280
+ */
1281
+ get isAsync() {
1282
+ return this._isAsync;
1283
+ }
1284
+ /**
1285
+ * Represents the state of an evaluation.
1286
+ * @param context The evaluation context.
1287
+ * @param result The evaluation result.
1288
+ * @param options The evaluation options.
1289
+ * @param isAsync Indicates whether the evaluation is asynchronous.
1290
+ */
1291
+ constructor(context, result, options, isAsync) {
1292
+ this._context = context;
1293
+ this._result = result;
1294
+ this._options = options;
1295
+ this._isAsync = isAsync;
1296
+ }
1297
+ /**
1298
+ * Creates an instance of EvalState from a given context, options, and async flag.
1299
+ *
1300
+ * @param context - The evaluation context or context object.
1301
+ * @param options - The evaluation options.
1302
+ * @param isAsync - A flag indicating whether the evaluation is asynchronous.
1303
+ * @returns The created EvalState instance.
1304
+ */
1305
+ static fromContext(context, options, isAsync) {
1306
+ const ctx = EvalContext.fromContext(context, options);
1307
+ const result = new EvalResult(ctx);
1308
+ const state = new EvalState(ctx, result, options, isAsync);
1309
+ return state;
1310
+ }
1311
+ }
1312
+
1313
+ const defaultParserOptions = {
1314
+ ...defaultOptions$1,
1315
+ ecmaVersion: 2020,
1316
+ extractExpressions: false,
1317
+ cacheSize: 100
1680
1318
  };
1681
1319
 
1682
1320
  /**
1683
- * Compiles the given node and returns a function that can be used to evaluate it.
1684
- *
1685
- * @param node The node to compile.
1686
- * @returns A function that takes an `EvalState` and returns the result of evaluating the node.
1321
+ * Base class for evaluation.
1687
1322
  */
1688
- const compile = (node) => {
1689
- return evaluate.bind(null, node);
1690
- };
1691
- /**
1692
- * Compiles the given node asynchronously.
1693
- *
1694
- * @param node - The node to compile.
1695
- * @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
1696
- */
1697
- const compileAsync = (node) => {
1698
- return evaluateAsync.bind(null, node);
1699
- };
1700
- /**
1701
- * Calls the provided function with the given state and returns the result.
1702
- *
1703
- * @param fn The function to be called.
1704
- * @param state The state to be passed to the function.
1705
- * @returns The result of calling the function.
1706
- */
1707
- const call = (fn, state) => {
1708
- const value = fn(state);
1709
- return value;
1710
- };
1711
- /**
1712
- * Calls an asynchronous function with the given state and returns a promise that resolves to the result.
1713
- * @param fn The asynchronous function to call.
1714
- * @param state The state to pass to the function.
1715
- * @returns A promise that resolves to the result of the function.
1716
- */
1717
- const callAsync = (fn, state) => {
1718
- const promise = fn(state);
1719
- return promise;
1720
- };
1721
-
1722
- /**
1723
- * Finds all nodes of a specific type in the given AST.
1724
- *
1725
- * @param ast The AST to search in.
1726
- * @param searchType The type of nodes to search for.
1727
- * @returns An array of nodes of the specified type if found, otherwise undefined.
1728
- */
1729
- const extract = (node, searchType) => {
1730
- if (node) {
1731
- const state = [];
1732
- const visitors = {
1733
- [searchType]: (node) => { state.push(node); return; }
1734
- };
1735
- walk.simple(node, visitors);
1736
- return state.length > 0 ? state : undefined;
1737
- }
1738
- return undefined;
1739
- };
1740
-
1741
- /**
1742
- * @description
1743
- * Service for parsing expressions and returning ES6/ES2020 AST.
1744
- */
1745
- class ParserService extends BaseEval {
1746
- /**
1747
- * Constructs a new instance of the ParserService class.
1748
- */
1749
- constructor() {
1750
- super();
1751
- this.parserOptions = {
1752
- ...defaultOptions$1,
1753
- ecmaVersion: 2020,
1754
- extractExpressions: true,
1755
- cacheSize: 100
1756
- };
1757
- if (this.parserOptions.cacheSize) {
1758
- this._cache = new Cache(this.parserOptions.cacheSize);
1759
- }
1323
+ class BaseEval {
1324
+ constructor(parserService) {
1325
+ this.parserService = parserService;
1760
1326
  }
1761
1327
  /**
1762
- * @description
1763
- * Returns acorn version.
1328
+ * Gets the parser options.
1764
1329
  */
1765
- get version() {
1766
- return version;
1330
+ get parserOptions() {
1331
+ return this._parserOptions;
1767
1332
  }
1768
1333
  /**
1769
- * Gets the parser options.
1770
- * @returns The parser options.
1334
+ * Sets the parser options.
1335
+ * @param value The parser options to set.
1771
1336
  */
1772
- get options() {
1773
- return this.parserOptions;
1337
+ set parserOptions(value) {
1338
+ this._parserOptions = value;
1774
1339
  }
1775
1340
  /**
1776
- * @description
1777
- * Parses expression and returns ES6/ES2023 AST.
1778
- * @param expr expression to parse
1779
- * @param options optional parser options
1780
- * @returns The parsed AST (Abstract Syntax Tree).
1341
+ * Parses the given expression into an abstract syntax tree (AST).
1342
+ * @param expression - The expression to parse.
1343
+ * @returns The parsed AST.
1781
1344
  */
1782
- parse(expr, options) {
1783
- if (expr) {
1784
- try {
1785
- const parserOptions = { ...this.parserOptions, ...options };
1786
- const isCache = this.parserOptions.cacheSize && this._cache;
1787
- const ast = isCache
1788
- ? this.fromCacheOrParse(expr, parserOptions)
1789
- : parse(expr, parserOptions);
1790
- return ast;
1791
- }
1792
- catch (error) {
1793
- if (error instanceof Error) {
1794
- throw new Error(error.message);
1795
- }
1796
- else {
1797
- throw error;
1798
- }
1799
- }
1345
+ parse(expression) {
1346
+ if (!expression) {
1347
+ return undefined;
1800
1348
  }
1801
- return undefined;
1802
- }
1803
- //#region Private methods
1804
- fromCacheOrParse(expr, options) {
1805
- if (this._cache) {
1806
- const hashKey = this._cache.getHashKey('', expr);
1807
- let ast = this._cache.get(hashKey);
1808
- if (!ast) {
1809
- ast = parse(expr, options);
1810
- this._cache.set(hashKey, ast);
1811
- }
1349
+ else if (typeof expression === 'string') {
1350
+ const ast = this.parserService.parse(expression, this.parserOptions);
1812
1351
  return ast;
1813
1352
  }
1814
- return undefined;
1815
- }
1816
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
1817
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, providedIn: 'root' }); }
1818
- }
1819
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, decorators: [{
1820
- type: Injectable,
1821
- args: [{
1822
- providedIn: 'root'
1823
- }]
1824
- }], ctorParameters: () => [] });
1825
-
1826
- /**
1827
- * Represents the evaluation context for the code execution.
1828
- */
1829
- class EvalContext {
1830
- /**
1831
- * Gets the original registry.
1832
- */
1833
- get original() {
1834
- return this._original;
1835
- }
1836
- /**
1837
- * Gets the scopes registry.
1838
- */
1839
- get priorScopes() {
1840
- return this._priorScopes;
1841
- }
1842
- /**
1843
- * Gets the scopes registry.
1844
- */
1845
- get scopes() {
1846
- return this._scopes;
1847
- }
1848
- /**
1849
- * Gets the lookups registry.
1850
- */
1851
- get lookups() {
1852
- return this._lookups;
1853
- }
1854
- /**
1855
- * Gets the evaluation options.
1856
- */
1857
- get options() {
1858
- return this._options;
1353
+ else {
1354
+ return expression;
1355
+ }
1859
1356
  }
1860
1357
  /**
1861
- * Creates a new instance of EvalContext.
1862
- * @param original - The original registry or an object to create a registry from.
1358
+ * Creates an instance of EvalState.
1359
+ * @param context - The evaluation context or context object.
1863
1360
  * @param options - The evaluation options.
1361
+ * @returns The created EvalState instance.
1864
1362
  */
1865
- constructor(original, options) {
1866
- this.type = 'EvalContext';
1867
- this._original = original;
1868
- this._priorScopes = [];
1869
- this._scopes = new Stack();
1870
- this._options = options;
1871
- this._lookups = new Registry();
1363
+ createState(context, options) {
1364
+ const state = EvalState.fromContext(context, options);
1365
+ return state;
1872
1366
  }
1873
- /**
1874
- * Converts the given context to an instance of EvalContext.
1875
- * @param context - The context to convert.
1876
- * @param options - The evaluation options.
1877
- * @returns The converted EvalContext instance.
1878
- */
1879
- static fromContext(context, options) {
1880
- if (context instanceof EvalContext) {
1881
- return context;
1882
- }
1883
- const ctx = fromContext(context ?? {}, options);
1884
- return new EvalContext(ctx, options ?? {});
1367
+ }
1368
+
1369
+ /**
1370
+ * @description
1371
+ * Parses expression and returns ES6/ES2020 AST.
1372
+ * @param expr expression to parse
1373
+ * @param options parser options
1374
+ */
1375
+ const parse = (expr, options) => {
1376
+ options = { ecmaVersion: 2020, ...options };
1377
+ const acornOptions = { ecmaVersion: 2020, ...options };
1378
+ const program = parse$1(expr, acornOptions);
1379
+ if (!options.extractExpressions) {
1380
+ return program;
1885
1381
  }
1886
- /**
1887
- * Gets the value associated with the specified key from the evaluation context.
1888
- * @param key - The key to retrieve the value for.
1889
- * @returns The value associated with the key, or undefined if not found.
1890
- */
1891
- get(key) {
1892
- for (const scope of this._scopes.asArray()) {
1893
- const value = getContextValue(scope, key);
1894
- if (value !== undefined) {
1895
- return value;
1896
- }
1897
- }
1898
- if (this._original) {
1899
- const value = getContextValue(this._original, key);
1900
- if (value !== undefined) {
1901
- return value;
1902
- }
1903
- }
1904
- for (const scope of this._priorScopes) {
1905
- const value = getContextValue(scope, key);
1906
- if (value !== undefined) {
1907
- return value;
1908
- }
1909
- }
1910
- for (const lookup of Array.from(this._lookups.values)) {
1911
- const value = lookup(key, this, this._options);
1912
- if (value !== undefined) {
1913
- return value;
1914
- }
1382
+ return extractExpression(program);
1383
+ };
1384
+ /**
1385
+ * @description
1386
+ * Walks through the AST and extracts first expression.
1387
+ * @param expr expression to parse
1388
+ */
1389
+ const extractExpression = (program) => {
1390
+ if (program.body.length === 1) {
1391
+ const expression = program.body[0];
1392
+ if (expression.type === 'ExpressionStatement') {
1393
+ return expression.expression;
1915
1394
  }
1916
- return undefined;
1917
1395
  }
1918
- /**
1919
- * Retrieves the `this` value of the specified key from the evaluation context.
1920
- * The value is searched in the original context, prior scopes, and lookup functions.
1921
- * @param key - The key to retrieve the value for.
1922
- * @returns The `this` value associated with the key, or undefined if not found.
1923
- */
1924
- getThis(key) {
1925
- if (this._original) {
1926
- const value = getContextValue(this._original, key);
1927
- if (value !== undefined) {
1928
- return this._original;
1929
- }
1930
- }
1931
- for (const scope of this._priorScopes) {
1932
- const value = getContextValue(this._original, key);
1933
- if (value !== undefined) {
1934
- return scope;
1935
- }
1936
- }
1937
- for (const lookup of Array.from(this._lookups.values)) {
1938
- const value = lookup(key, this, this._options);
1939
- if (value !== undefined) {
1940
- return lookup;
1941
- }
1942
- }
1943
- return undefined;
1944
- }
1945
- /**
1946
- * Retrieves the value associated with the specified key from the evaluation context.
1947
- *
1948
- * @param key - The key to retrieve the value for.
1949
- * @returns The value associated with the key, or undefined if the key is not found.
1950
- */
1951
- getKey(key) {
1952
- const caseInsensitive = !!this._options.caseInsensitive;
1953
- for (const scope of this._scopes.asArray()) {
1954
- const foundKey = getContextKey(scope, key, caseInsensitive);
1955
- if (foundKey !== undefined) {
1956
- return foundKey;
1957
- }
1958
- }
1959
- if (this._original) {
1960
- const foundKey = getContextKey(this._original, key, caseInsensitive);
1961
- if (foundKey !== undefined) {
1962
- return foundKey;
1963
- }
1964
- }
1965
- for (const scope of this._priorScopes) {
1966
- const foundKey = getContextKey(scope, key, caseInsensitive);
1967
- if (foundKey !== undefined) {
1968
- return foundKey;
1969
- }
1970
- }
1971
- return undefined;
1972
- }
1973
- /**
1974
- * Sets a key-value pair in the context.
1975
- * If the original context is a Registry, the key-value pair is set using the Registry's set method.
1976
- * If the original context is an Object, the key-value pair is set directly on the object.
1977
- * @param key - The key of the pair.
1978
- * @param value - The value of the pair.
1979
- */
1980
- set(key, value) {
1981
- if (this._original && this._original instanceof Registry) {
1982
- const registry = this._original;
1983
- registry.set(key, value);
1984
- }
1985
- else if (this._original && this._original instanceof Object) {
1986
- const obj = this._original;
1987
- obj[key] = value;
1988
- }
1396
+ return undefined;
1397
+ };
1398
+
1399
+ const pushVisitorResult = (node, st, value) => {
1400
+ const key = getKey(node);
1401
+ const result = st.result;
1402
+ result.stack.push(value);
1403
+ result.trace.set(key, value);
1404
+ return value;
1405
+ };
1406
+ const popVisitorResult = (node, st) => {
1407
+ const result = st.result;
1408
+ const value = result.stack.pop();
1409
+ return value;
1410
+ };
1411
+ const pushVisitorResultAsync = (node, st, value) => {
1412
+ const key = getKey(node);
1413
+ const result = st.result;
1414
+ result.stack.push(value);
1415
+ result.trace.set(key, value);
1416
+ return value;
1417
+ };
1418
+ const popVisitorResultAsync = (node, st) => {
1419
+ const result = st.result;
1420
+ const value = result.stack.pop();
1421
+ return value;
1422
+ };
1423
+ const getKey = (node) => {
1424
+ const key = `${node.type}(${node.start}:${node.end})`;
1425
+ return key;
1426
+ };
1427
+
1428
+ const beforeVisitor = (node, st) => {
1429
+ const options = st.options;
1430
+ if (options?.['trackTime']) {
1431
+ const t = performance.now();
1432
+ console.time('before: ' + node.type);
1433
+ return t;
1989
1434
  }
1990
- /**
1991
- * Pushes a context to the scope stack.
1992
- * @param context - The context to push.
1993
- * @param options - The evaluation options.
1994
- */
1995
- push(context, options) {
1996
- const ctx = fromContext(context, options);
1997
- this._scopes.push(ctx);
1435
+ return undefined;
1436
+ };
1437
+
1438
+ const afterVisitor = (node, st) => {
1439
+ const options = st.options;
1440
+ if (options?.['trackTime']) {
1441
+ const t = performance.now();
1442
+ console.time('after: ' + node.type);
1443
+ return t;
1998
1444
  }
1999
- /**
2000
- * Pops a context from the scope stack.
2001
- */
2002
- pop() {
2003
- this._scopes.pop();
1445
+ return undefined;
1446
+ };
1447
+
1448
+ var RecursiveVisitorResultType;
1449
+ (function (RecursiveVisitorResultType) {
1450
+ RecursiveVisitorResultType[RecursiveVisitorResultType["Stack"] = 0] = "Stack";
1451
+ RecursiveVisitorResultType[RecursiveVisitorResultType["Registry"] = 1] = "Registry";
1452
+ })(RecursiveVisitorResultType || (RecursiveVisitorResultType = {}));
1453
+
1454
+ const literals = Registry.fromObject({
1455
+ 'undefined': undefined,
1456
+ 'null': null,
1457
+ 'true': true,
1458
+ 'false': false,
1459
+ }, { caseInsensitive: true });
1460
+ const identifierVisitor = (node, st) => {
1461
+ if (st.options?.caseInsensitive) {
1462
+ return identifierVisitorCaseInsensitive(node, st);
2004
1463
  }
2005
- }
1464
+ beforeVisitor(node, st);
1465
+ const context = st.context;
1466
+ const value = node.name === 'this'
1467
+ ? st.context
1468
+ : context?.get(node.name);
1469
+ pushVisitorResult(node, st, value);
1470
+ afterVisitor(node, st);
1471
+ };
1472
+ const identifierVisitorCaseInsensitive = (node, st) => {
1473
+ beforeVisitor(node, st);
1474
+ const context = st.context;
1475
+ const value = equalIgnoreCase(node.name, 'this')
1476
+ ? st.context
1477
+ : context?.get(node.name) ?? literals.get(node.name);
1478
+ pushVisitorResult(node, st, value);
1479
+ afterVisitor(node, st);
1480
+ };
2006
1481
 
2007
- /**
2008
- * Represents the result of an evaluation.
2009
- */
2010
- class EvalResult {
2011
- /**
2012
- * Gets the stack of evaluated values.
2013
- */
2014
- get stack() {
2015
- return this._stack;
1482
+ const literalVisitor = (node, st) => {
1483
+ beforeVisitor(node, st);
1484
+ const value = node.value;
1485
+ pushVisitorResult(node, st, value);
1486
+ afterVisitor(node, st);
1487
+ };
1488
+
1489
+ const binaryExpressionVisitor = (node, st, callback) => {
1490
+ beforeVisitor(node, st);
1491
+ callback(node.left, st);
1492
+ const left = popVisitorResult(node, st);
1493
+ callback(node.right, st);
1494
+ const right = popVisitorResult(node, st);
1495
+ const value = node.operator === '+' ? left + right :
1496
+ node.operator === '-' ? left - right :
1497
+ node.operator === '*' ? left * right :
1498
+ node.operator === '/' ? left / right :
1499
+ node.operator === '%' ? left % right :
1500
+ node.operator === '**' ? left ** right :
1501
+ node.operator === '<<' ? left << right :
1502
+ node.operator === '>>' ? left >> right :
1503
+ node.operator === '>>>' ? left >>> right :
1504
+ node.operator === '&' ? left & right :
1505
+ node.operator === '^' ? left ^ right :
1506
+ node.operator === '|' ? left | right :
1507
+ node.operator === '==' ? left == right :
1508
+ node.operator === '!=' ? left != right :
1509
+ node.operator === '===' ? left === right :
1510
+ node.operator === '!==' ? left !== right :
1511
+ node.operator === '<' ? left < right :
1512
+ node.operator === '<=' ? left <= right :
1513
+ node.operator === '>' ? left > right :
1514
+ node.operator === '>=' ? left >= right :
1515
+ node.operator === 'in' ? left in right :
1516
+ node.operator === 'instanceof' ? left instanceof right :
1517
+ node.operator === '??' ? left ?? right :
1518
+ undefined;
1519
+ pushVisitorResult(node, st, value);
1520
+ afterVisitor(node, st);
1521
+ };
1522
+
1523
+ const memberExpressionVisitor = (node, st, callback) => {
1524
+ beforeVisitor(node, st);
1525
+ const [, , value] = evaluateMember(node, st, callback);
1526
+ pushVisitorResult(node, st, value);
1527
+ afterVisitor(node, st);
1528
+ };
1529
+ const evaluateMember = (node, st, callback) => {
1530
+ // Get the object
1531
+ callback(node.object, st);
1532
+ const obj = popVisitorResult(node, st);
1533
+ const object = (node.optional ? (obj || {}) : obj);
1534
+ // Get the property
1535
+ let key;
1536
+ if (node.property.type === 'Identifier') {
1537
+ key = node.property.name;
2016
1538
  }
2017
- /**
2018
- * Gets the evaluated value.
2019
- */
2020
- get value() {
2021
- return this._value;
1539
+ else if (node.property.type === 'Literal') {
1540
+ key = node.property.value;
2022
1541
  }
2023
- /**
2024
- * Gets the error that occurred during evaluation.
2025
- */
2026
- get error() {
2027
- return this._error;
1542
+ else {
1543
+ callback(node.property, st);
1544
+ key = popVisitorResult(node, st);
2028
1545
  }
2029
- /**
2030
- * Gets the error message associated with the evaluation error.
2031
- */
2032
- get errorMessage() {
2033
- return this._errorMessage;
1546
+ // Get the value
1547
+ if (object === st.context) {
1548
+ const value = st.context.get(key);
1549
+ const thisValue = st.context.getThis(key);
1550
+ return [thisValue ?? object, key, value];
2034
1551
  }
2035
- /**
2036
- * Gets whether the evaluation resulted in an error.
2037
- */
2038
- get isError() {
2039
- return this._isError;
2040
- }
2041
- /**
2042
- * Gets whether the evaluation was successful.
2043
- */
2044
- get isSuccess() {
2045
- return this._isSuccess;
2046
- }
2047
- /**
2048
- * Gets whether the evaluated value is undefined.
2049
- */
2050
- get isUndefined() {
2051
- return this._isUndefined;
2052
- }
2053
- /**
2054
- * Gets the trace of evaluated values.
2055
- */
2056
- get trace() {
2057
- return this._trace;
1552
+ else {
1553
+ const caseInsesitive = !!(st.options?.caseInsensitive);
1554
+ const pair = getKeyValue(object, key, caseInsesitive);
1555
+ if (pair) {
1556
+ const [key, value] = pair;
1557
+ return [object, key, value];
1558
+ }
2058
1559
  }
2059
- /**
2060
- * Gets the evaluation context.
2061
- */
2062
- get context() {
2063
- return this._context;
1560
+ return [object, key, undefined];
1561
+ };
1562
+
1563
+ const arrayExpressionVisitor = (node, st, callback) => {
1564
+ beforeVisitor(node, st);
1565
+ const elements = evaluateArray(node, node.elements, st, callback);
1566
+ pushVisitorResult(node, st, elements);
1567
+ afterVisitor(node, st);
1568
+ };
1569
+ const evaluateArray = (node, items, st, callback) => {
1570
+ const elements = [];
1571
+ for (const element of items) {
1572
+ if (element === null || typeof element === 'undefined') {
1573
+ elements.push(element);
1574
+ }
1575
+ else if (element.type === 'SpreadElement') {
1576
+ callback(element, st);
1577
+ const values = popVisitorResult(node, st);
1578
+ elements.push(...values);
1579
+ }
1580
+ else {
1581
+ callback(element, st);
1582
+ const value = popVisitorResult(node, st);
1583
+ elements.push(value);
1584
+ }
2064
1585
  }
2065
- /**
2066
- * Gets the evaluation options.
2067
- */
2068
- get options() {
2069
- return this._context.options;
1586
+ return elements;
1587
+ };
1588
+
1589
+ const callExpressionVisitor = (node, st, callback) => {
1590
+ beforeVisitor(node, st);
1591
+ const args = evaluateArray(node, node.arguments, st, callback);
1592
+ if (node.callee.type === 'MemberExpression') {
1593
+ const [object, , fn] = evaluateMember(node.callee, st, callback);
1594
+ const caller = fn;
1595
+ const value = !caller && node.callee.optional
1596
+ ? undefined : caller.apply(object, args);
1597
+ pushVisitorResult(node, st, value);
2070
1598
  }
2071
- /**
2072
- * Gets the duration of the evaluation result.
2073
- * @returns The duration in milliseconds.
2074
- */
2075
- get duration() {
2076
- if (!this._startDate || !this._endDate) {
2077
- return undefined;
2078
- }
2079
- return this._endDate - this._startDate;
1599
+ else {
1600
+ callback(node.callee, st);
1601
+ const caller = popVisitorResult(node, st);
1602
+ const value = !caller && node.optional
1603
+ ? undefined : caller.apply(st.context, args);
1604
+ pushVisitorResult(node, st, value);
2080
1605
  }
2081
- /**
2082
- * Creates a new instance of EvalResult.
2083
- * @param trace The trace of evaluated values.
2084
- * @param context The evaluation context.
2085
- */
2086
- constructor(context) {
2087
- this._stack = new Stack();
2088
- this._trace = new Registry;
2089
- this._context = context;
1606
+ afterVisitor(node, st);
1607
+ };
1608
+ const evaluateCall = (node, st, callback) => {
1609
+ callback(node.callee, st);
1610
+ const caller = popVisitorResult(node, st);
1611
+ const args = node.arguments.map((argument) => {
1612
+ callback(argument, st);
1613
+ const value = popVisitorResult(node, st);
1614
+ return value;
1615
+ });
1616
+ const value = caller.apply(st.context, args);
1617
+ return value;
1618
+ };
1619
+
1620
+ const awaitVisitor = (node, st, callback) => {
1621
+ beforeVisitor(node, st);
1622
+ const promise = new Promise((resolve) => {
1623
+ callback(node.argument, st);
1624
+ const value = popVisitorResult(node, st);
1625
+ resolve(value);
1626
+ });
1627
+ pushVisitorResult(node, st, promise);
1628
+ afterVisitor(node, st);
1629
+ };
1630
+
1631
+ const conditionalExpressionVisitor = (node, st, callback) => {
1632
+ beforeVisitor(node, st);
1633
+ callback(node.test, st);
1634
+ const test = popVisitorResult(node, st);
1635
+ if (test) {
1636
+ callback(node.consequent, st);
2090
1637
  }
2091
- /**
2092
- * Starts the evaluation process.
2093
- */
2094
- start() {
2095
- this._startDate = performance.now();
1638
+ else {
1639
+ callback(node.alternate, st);
2096
1640
  }
2097
- /**
2098
- * Stops the evaluation and records the end date.
2099
- */
2100
- stop() {
2101
- this._endDate = performance.now();
2102
- return this.duration;
1641
+ const value = popVisitorResult(node, st);
1642
+ pushVisitorResult(node, st, value);
1643
+ afterVisitor(node, st);
1644
+ };
1645
+
1646
+ const unaryExpressionVisitor = (node, st, callback) => {
1647
+ beforeVisitor(node, st);
1648
+ callback(node.argument, st);
1649
+ const argument = popVisitorResult(node, st);
1650
+ const value = node.operator === '-' ? -argument :
1651
+ node.operator === '+' ? +argument :
1652
+ node.operator === '~' ? ~argument :
1653
+ node.operator === '!' ? !argument :
1654
+ node.operator === 'typeof' ? typeof argument :
1655
+ node.operator === 'void' ? void argument :
1656
+ // node.operator === 'delete' ? delete argument :
1657
+ undefined;
1658
+ pushVisitorResult(node, st, value);
1659
+ afterVisitor(node, st);
1660
+ };
1661
+
1662
+ const logicalExpressionVisitor = (node, st, callback) => {
1663
+ beforeVisitor(node, st);
1664
+ callback(node.left, st);
1665
+ const left = popVisitorResult(node, st);
1666
+ if (node.operator === '&&' && !left) {
1667
+ pushVisitorResult(node, st, left);
1668
+ afterVisitor(node, st);
1669
+ return;
2103
1670
  }
2104
- /**
2105
- * Sets the success value of the evaluation result.
2106
- *
2107
- * @param value The value to set as the success value.
2108
- * @returns void
2109
- */
2110
- setSuccess(value) {
2111
- this._value = value;
2112
- this._isSuccess = true;
2113
- this._isError = false;
2114
- this._isUndefined = false;
1671
+ else if (node.operator === '||' && left) {
1672
+ pushVisitorResult(node, st, true);
1673
+ afterVisitor(node, st);
1674
+ return;
2115
1675
  }
2116
- /**
2117
- * Sets the failure state of the evaluation result.
2118
- * @param error The error object associated with the failure.
2119
- */
2120
- setFailure(error) {
2121
- this._error = error;
2122
- if (error instanceof Error) {
2123
- this._errorMessage = error.message;
1676
+ callback(node.right, st);
1677
+ const right = popVisitorResult(node, st);
1678
+ const value = node.operator === '&&' ? left && right :
1679
+ node.operator === '||' ? left || right :
1680
+ undefined;
1681
+ pushVisitorResult(node, st, value);
1682
+ afterVisitor(node, st);
1683
+ };
1684
+
1685
+ const thisExpressionVisitor = (node, st) => {
1686
+ beforeVisitor(node, st);
1687
+ const value = st.context;
1688
+ pushVisitorResult(node, st, value);
1689
+ afterVisitor(node, st);
1690
+ };
1691
+
1692
+ const newExpressionVisitor = (node, st, callback) => {
1693
+ beforeVisitor(node, st);
1694
+ const args = evaluateArray(node, node.arguments, st, callback);
1695
+ callback(node.callee, st);
1696
+ const constructor = popVisitorResult(node, st);
1697
+ const value = newFunc(constructor, ...args);
1698
+ pushVisitorResult(node, st, value);
1699
+ afterVisitor(node, st);
1700
+ };
1701
+ // based on Reflect.construct()
1702
+ // https://stackoverflow.com/questions/10428603/simulate-the-new-operator-in-javascript
1703
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct
1704
+ const newFunc = (constructor, ...args) => {
1705
+ const obj = Reflect.construct(constructor, args);
1706
+ return obj;
1707
+ };
1708
+
1709
+ const templateLiteralVisitor = (node, st, callback) => {
1710
+ beforeVisitor(node, st);
1711
+ const expressions = node.expressions.map((expression) => {
1712
+ callback(expression, st);
1713
+ const value = popVisitorResult(node, st);
1714
+ return value;
1715
+ });
1716
+ const value = node.quasis.reduce((accumulator, templateElement, index) => {
1717
+ accumulator += templateElement.value.cooked;
1718
+ if (!templateElement.tail) {
1719
+ accumulator += expressions[index] ?? '';
2124
1720
  }
2125
- this._isSuccess = false;
2126
- this._isError = true;
2127
- this._isUndefined = false;
1721
+ return accumulator;
1722
+ }, '');
1723
+ pushVisitorResult(node, st, value);
1724
+ afterVisitor(node, st);
1725
+ };
1726
+
1727
+ const taggedTemplateExpressionVisitor = (node, st, callback) => {
1728
+ beforeVisitor(node, st);
1729
+ callback(node.tag, st);
1730
+ const tag = popVisitorResult(node, st);
1731
+ const quasiExpressions = node.quasi.expressions.map((expression) => {
1732
+ callback(expression, st);
1733
+ const value = popVisitorResult(node, st);
1734
+ return value;
1735
+ });
1736
+ const quasi = node.quasi.quasis.map((templateElement) => {
1737
+ return templateElement.value.cooked;
1738
+ });
1739
+ const value = tag.apply(st.context, [quasi, ...quasiExpressions]);
1740
+ pushVisitorResult(node, st, value);
1741
+ afterVisitor(node, st);
1742
+ };
1743
+
1744
+ const objectExpressionVisitor = (node, st, callback) => {
1745
+ beforeVisitor(node, st);
1746
+ const map = new Map();
1747
+ for (const property of node.properties) {
1748
+ if (property.type === 'SpreadElement') {
1749
+ callback(property.argument, st);
1750
+ const value = popVisitorResult(property, st);
1751
+ for (const [key, val] of Object.entries(value)) {
1752
+ map.set(key, val);
1753
+ }
1754
+ }
1755
+ else {
1756
+ const pair = evalProperty(property, st, callback);
1757
+ map.set(pair.key, pair.value);
1758
+ }
1759
+ }
1760
+ const obj = Object.fromEntries(map);
1761
+ pushVisitorResult(node, st, obj);
1762
+ afterVisitor(node, st);
1763
+ };
1764
+ const evalProperty = (property, st, callback) => {
1765
+ // if (property.shorthand) {
1766
+ // callback(property.value, st);
1767
+ // const value = popVisitorResult(property, st);
1768
+ // return { key: value, value };
1769
+ // } else
1770
+ if (property.computed) {
1771
+ callback(property.key, st);
1772
+ const key = popVisitorResult(property, st);
1773
+ callback(property.value, st);
1774
+ const value = popVisitorResult(property, st);
1775
+ return { key, value };
1776
+ }
1777
+ else if (property.key.type === 'Identifier') {
1778
+ const key = property.key.name;
1779
+ callback(property.value, st);
1780
+ const value = popVisitorResult(property, st);
1781
+ return { key, value };
1782
+ }
1783
+ else if (property.key.type === 'Literal') {
1784
+ const key = property.key.value;
1785
+ callback(property.value, st);
1786
+ const value = popVisitorResult(property, st);
1787
+ return { key, value };
1788
+ }
1789
+ else {
1790
+ throw new Error(`Unsupported property key type: ${property.key.type}`);
1791
+ }
1792
+ };
1793
+
1794
+ /**
1795
+ * Returns the default visitors for the recursive traversal of an abstract syntax tree.
1796
+ * @returns The default visitors object.
1797
+ */
1798
+ const getDefaultVisitors = () => {
1799
+ const visitors = {};
1800
+ visitors['BinaryExpression'] = binaryExpressionVisitor;
1801
+ visitors['Identifier'] = identifierVisitor;
1802
+ visitors['Literal'] = literalVisitor;
1803
+ visitors['CallExpression'] = callExpressionVisitor;
1804
+ visitors['AwaitExpression'] = awaitVisitor;
1805
+ visitors['ConditionalExpression'] = conditionalExpressionVisitor;
1806
+ visitors['MemberExpression'] = memberExpressionVisitor;
1807
+ visitors['ArrayExpression'] = arrayExpressionVisitor;
1808
+ visitors['UnaryExpression'] = unaryExpressionVisitor;
1809
+ visitors['LogicalExpression'] = logicalExpressionVisitor;
1810
+ visitors['ThisExpression'] = thisExpressionVisitor;
1811
+ visitors['NewExpression'] = newExpressionVisitor;
1812
+ visitors['TemplateLiteral'] = templateLiteralVisitor;
1813
+ visitors['TaggedTemplateExpression'] = taggedTemplateExpressionVisitor;
1814
+ visitors['ObjectExpression'] = objectExpressionVisitor;
1815
+ visitors['AssignmentExpression'] = assignmentExpressionVisitor;
1816
+ visitors['UpdateExpression'] = updateExpressionVisitor;
1817
+ visitors['ArrowFunctionExpression'] = arrowFunctionExpressionVisitor;
1818
+ return visitors;
1819
+ };
1820
+
1821
+ const assignmentOperators = {
1822
+ '=': (left, value) => { return left = value; },
1823
+ '+=': (left, value) => { return left += value; },
1824
+ '-=': (left, value) => { return left -= value; },
1825
+ '*=': (left, value) => { return left *= value; },
1826
+ '/=': (left, value) => { return left /= value; },
1827
+ '%=': (left, value) => { return left %= value; },
1828
+ '**=': (left, value) => { return left **= value; },
1829
+ '<<=': (left, value) => { return left <<= value; },
1830
+ '>>=': (left, value) => { return left >>= value; },
1831
+ '>>>=': (left, value) => { return left >>>= value; },
1832
+ '&=': (left, value) => { return left &= value; },
1833
+ '^=': (left, value) => { return left ^= value; },
1834
+ '|=': (left, value) => { return left |= value; },
1835
+ '&&=': (left, value) => { return left &&= value; },
1836
+ '||=': (left, value) => { return left ||= value; },
1837
+ '??=': (left, value) => { return left ??= value; },
1838
+ };
1839
+ const assignmentExpressionVisitor = (node, st, callback) => {
1840
+ beforeVisitor(node, st);
1841
+ callback(node.left, st);
1842
+ const left = popVisitorResult(node, st);
1843
+ callback(node.right, st);
1844
+ const right = popVisitorResult(node, st);
1845
+ const func = assignmentOperators[node.operator];
1846
+ if (!func) {
1847
+ throw new Error(`Unsupported assignment operator: ${node.operator}`);
1848
+ }
1849
+ else if (!st.context) {
1850
+ throw new Error(`Context is not set.`);
1851
+ }
1852
+ else if (node.left.type === 'Identifier') {
1853
+ const key = st.options?.caseInsensitive
1854
+ ? st.context.getKey(node.left.name)
1855
+ : node.left.name;
1856
+ const value = func(left, right);
1857
+ st.context.set(key, value);
1858
+ pushVisitorResult(node, st, value);
1859
+ }
1860
+ else if (node.left.type === 'MemberExpression') {
1861
+ const [object, key,] = evaluateMember(node.left, st, callback);
1862
+ const value = func(left, right);
1863
+ object[key] = value;
1864
+ pushVisitorResult(node, st, value);
1865
+ }
1866
+ afterVisitor(node, st);
1867
+ };
1868
+
1869
+ const updateOperators = {
1870
+ '++': (value) => { return value + 1; },
1871
+ '--': (value) => { return value - 1; },
1872
+ };
1873
+ const updateExpressionVisitor = (node, st, callback) => {
1874
+ beforeVisitor(node, st);
1875
+ if (!st.context) {
1876
+ throw new Error(`Context is not set.`);
1877
+ }
1878
+ else if (node.argument.type === 'Identifier') {
1879
+ const key = st.options?.caseInsensitive
1880
+ ? st.context.getKey(node.argument.name)
1881
+ : node.argument.name;
1882
+ const value = st.context?.get(key);
1883
+ const newValue = updateOperators[node.operator](value);
1884
+ st.context?.set(key, newValue);
1885
+ pushVisitorResult(node, st, node.prefix ? newValue : value);
1886
+ }
1887
+ else if (node.argument.type === 'MemberExpression') {
1888
+ callback(node.argument, st);
1889
+ const [, property, value] = evaluateMember(node.argument, st, callback);
1890
+ const newValue = updateOperators[node.operator](value);
1891
+ st.context?.set(property, newValue);
1892
+ pushVisitorResult(node, st, node.prefix ? newValue : value);
1893
+ }
1894
+ afterVisitor(node, st);
1895
+ };
1896
+
1897
+ const arrowFunctionExpressionVisitor = (node, st, callback) => {
1898
+ beforeVisitor(node, st);
1899
+ const fn = (...arrowArgs) => {
1900
+ const newContext = evaluatePatterns(node.params, st, callback, arrowArgs);
1901
+ st.context?.push(newContext);
1902
+ const value = evaluate(node.body, st);
1903
+ st.context?.pop();
1904
+ return value;
1905
+ };
1906
+ pushVisitorResult(node, st, fn);
1907
+ afterVisitor(node, st);
1908
+ };
1909
+
1910
+ // based on evalArrowContext
1911
+ // https://github.com/6utt3rfly/jse-eval/blob/main/index.ts
1912
+ const evaluatePatterns = (patterns, st, callback, args) => {
1913
+ let context = {};
1914
+ patterns.map((pattern, i) => {
1915
+ switch (pattern.type) {
1916
+ case 'Identifier':
1917
+ {
1918
+ const object = evaluateIdentifier(pattern, st, args[i]);
1919
+ context = { ...context, ...object };
1920
+ }
1921
+ break;
1922
+ case 'MemberExpression':
1923
+ {
1924
+ const object = evaluateMemberExpression(pattern, st, callback, args[i]);
1925
+ context = { ...context, ...object };
1926
+ }
1927
+ break;
1928
+ case 'ObjectPattern':
1929
+ {
1930
+ const object = evaluateObjectPattern(pattern, st, callback, args[i]);
1931
+ context = { ...context, ...object };
1932
+ }
1933
+ break;
1934
+ case 'ArrayPattern':
1935
+ {
1936
+ const object = evaluateArrayPattern(pattern, st, callback, args[i]);
1937
+ context = { ...context, ...object };
1938
+ }
1939
+ break;
1940
+ case 'RestElement':
1941
+ {
1942
+ const object = evaluateRestElement(pattern, st, callback, args.slice(i));
1943
+ context = { ...context, ...object };
1944
+ }
1945
+ break;
1946
+ // case 'AssignmentPattern': {
1947
+ // const object = evaluateAssignmentPattern(pattern, st, callback, args[i]);
1948
+ // context = {...context, ...object};
1949
+ // }
1950
+ // break;
1951
+ }
1952
+ });
1953
+ return context;
1954
+ };
1955
+ const evaluatePattern = (pattern, st, callback, arg) => {
1956
+ switch (pattern.type) {
1957
+ case 'Identifier':
1958
+ return evaluateIdentifier(pattern, st, arg);
1959
+ case 'MemberExpression':
1960
+ return evaluateMemberExpression(pattern, st, callback, arg);
1961
+ case 'ObjectPattern':
1962
+ return evaluateObjectPattern(pattern, st, callback, arg);
1963
+ case 'ArrayPattern':
1964
+ return evaluateArrayPattern(pattern, st, callback, arg);
1965
+ case 'RestElement':
1966
+ return evaluateRestElement(pattern, st, callback, arg);
1967
+ // case 'AssignmentPattern':
1968
+ // return evaluateAssignmentPattern(pattern, st, callback, arg);
1969
+ }
1970
+ return {};
1971
+ };
1972
+ const evaluateIdentifier = (pattern, st, arg) => {
1973
+ const object = {};
1974
+ object[pattern.name] = arg;
1975
+ return object;
1976
+ };
1977
+ const evaluateMemberExpression = (pattern, st, callback, arg) => {
1978
+ if (pattern.type === 'MemberExpression') {
1979
+ console.log(pattern, st, callback, arg);
1980
+ throw new Error('evaluateMemberExpression is not implemented.');
1981
+ }
1982
+ return {};
1983
+ };
1984
+ const evaluateObjectPattern = (pattern, st, callback, arg) => {
1985
+ let context = {};
1986
+ pattern.properties.map((pattern) => {
1987
+ switch (pattern.type) {
1988
+ case 'Property':
1989
+ {
1990
+ let key;
1991
+ if (pattern.key.type === 'Identifier') {
1992
+ key = pattern.key.name;
1993
+ }
1994
+ else if (pattern.key.type === 'Literal') {
1995
+ key = pattern.key.value;
1996
+ }
1997
+ else {
1998
+ callback(pattern.key, st);
1999
+ key = popVisitorResult(pattern, st);
2000
+ if (typeof key !== 'string' && typeof key !== 'number')
2001
+ throw new Error(`Unsupported property key type: ${pattern.key.type}`);
2002
+ }
2003
+ const ctx = arg;
2004
+ st.context?.push(ctx);
2005
+ callback(pattern.value, st);
2006
+ const value = popVisitorResult(pattern, st);
2007
+ st.context?.pop();
2008
+ const pair = {};
2009
+ pair[key] = value;
2010
+ context = { ...context, ...pair };
2011
+ }
2012
+ break;
2013
+ case 'RestElement':
2014
+ {
2015
+ const object = evaluateRestElement(pattern, st, callback, arg);
2016
+ context = { ...context, ...object };
2017
+ }
2018
+ break;
2019
+ }
2020
+ });
2021
+ return context;
2022
+ };
2023
+ const evaluateArrayPattern = (pattern, st, callback, args) => {
2024
+ const patterns = pattern.elements.filter(s => !!s);
2025
+ const object = evaluatePatterns(patterns, st, callback, args);
2026
+ return object;
2027
+ };
2028
+ const evaluateRestElement = (pattern, st, callback, args) => {
2029
+ const argument = pattern.argument;
2030
+ const object = evaluatePattern(argument, st, callback, args);
2031
+ return object;
2032
+ };
2033
+ // const evaluateAssignmentPattern = (pattern: AssignmentPattern, st: EvalState, callback: walk.WalkerCallback<EvalState>, args: unknown) => {
2034
+ // const left = evaluatePattern(pattern.left, st, callback, args);
2035
+ // callback(pattern.right, st);
2036
+ // const value = popVisitorResult(pattern, st);
2037
+ // const object = {...left};
2038
+ // return object;
2039
+ // }
2040
+
2041
+ /**
2042
+ * Evaluates the given node and returns the result.
2043
+ *
2044
+ * @param node The node to evaluate.
2045
+ * @param state The evaluation state.
2046
+ * @returns The result of the evaluation.
2047
+ */
2048
+ const evaluate = (node, state) => {
2049
+ if (node) {
2050
+ state.result.start();
2051
+ try {
2052
+ const visitors = getDefaultVisitors();
2053
+ walk.recursive(node, state, visitors);
2054
+ const value = popVisitorResult(node, state);
2055
+ state.result.stop();
2056
+ state.result.setSuccess(value);
2057
+ return value;
2058
+ }
2059
+ catch (error) {
2060
+ state.result.stop();
2061
+ state.result.setFailure(error);
2062
+ throw error;
2063
+ }
2064
+ }
2065
+ return undefined;
2066
+ };
2067
+ /**
2068
+ * Asynchronously evaluates the given abstract syntax tree (AST) using the provided evaluation state.
2069
+ * @param ast The abstract syntax tree to evaluate.
2070
+ * @param state The evaluation state.
2071
+ * @returns A promise that resolves to the evaluated value or undefined.
2072
+ */
2073
+ const evaluateAsync = (ast, state) => {
2074
+ const promise = new Promise((resolve) => {
2075
+ const value = evaluate(ast, state);
2076
+ resolve(value);
2077
+ });
2078
+ return promise;
2079
+ };
2080
+
2081
+ /**
2082
+ * Compiles the given node and returns a function that can be used to evaluate it.
2083
+ *
2084
+ * @param node The node to compile.
2085
+ * @returns A function that takes an `EvalState` and returns the result of evaluating the node.
2086
+ */
2087
+ const compile = (node) => {
2088
+ return evaluate.bind(null, node);
2089
+ };
2090
+ /**
2091
+ * Compiles the given node asynchronously.
2092
+ *
2093
+ * @param node - The node to compile.
2094
+ * @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
2095
+ */
2096
+ const compileAsync = (node) => {
2097
+ return evaluateAsync.bind(null, node);
2098
+ };
2099
+ /**
2100
+ * Calls the provided function with the given state and returns the result.
2101
+ *
2102
+ * @param fn The function to be called.
2103
+ * @param state The state to be passed to the function.
2104
+ * @returns The result of calling the function.
2105
+ */
2106
+ const call = (fn, state) => {
2107
+ const value = fn(state);
2108
+ return value;
2109
+ };
2110
+ /**
2111
+ * Calls an asynchronous function with the given state and returns a promise that resolves to the result.
2112
+ * @param fn The asynchronous function to call.
2113
+ * @param state The state to pass to the function.
2114
+ * @returns A promise that resolves to the result of the function.
2115
+ */
2116
+ const callAsync = (fn, state) => {
2117
+ const promise = fn(state);
2118
+ return promise;
2119
+ };
2120
+
2121
+ /**
2122
+ * Finds all nodes of a specific type in the given AST.
2123
+ *
2124
+ * @param ast The AST to search in.
2125
+ * @param searchType The type of nodes to search for.
2126
+ * @returns An array of nodes of the specified type if found, otherwise undefined.
2127
+ */
2128
+ const extract = (node, searchType) => {
2129
+ if (node) {
2130
+ const state = [];
2131
+ const visitors = {
2132
+ [searchType]: (node) => { state.push(node); return; }
2133
+ };
2134
+ walk.simple(node, visitors);
2135
+ return state.length > 0 ? state : undefined;
2128
2136
  }
2129
- }
2137
+ return undefined;
2138
+ };
2130
2139
 
2131
2140
  /**
2132
- * Represents the evaluation state, which includes the context, result, and options.
2141
+ * @description
2142
+ * Service for parsing expressions and returning ES6/ES2020 AST.
2133
2143
  */
2134
- class EvalState {
2144
+ class ParserService {
2135
2145
  /**
2136
- * Gets the evaluation context.
2146
+ * Gets the parser options.
2137
2147
  */
2138
- get context() {
2139
- return this._context;
2148
+ get parserOptions() {
2149
+ return this._parserOptions;
2140
2150
  }
2141
2151
  /**
2142
- * Gets the evaluation result.
2152
+ * Sets the parser options.
2153
+ * @param value The parser options to set.
2143
2154
  */
2144
- get result() {
2145
- return this._result;
2155
+ set parserOptions(value) {
2156
+ this._parserOptions = value;
2146
2157
  }
2147
2158
  /**
2148
- * Gets the evaluation options.
2159
+ * Constructs a new instance of the ParserService class.
2149
2160
  */
2150
- get options() {
2151
- return this._options;
2161
+ constructor() {
2162
+ this.parserOptions = {
2163
+ ...defaultParserOptions,
2164
+ extractExpressions: true
2165
+ };
2166
+ if (this.parserOptions.cacheSize) {
2167
+ this._cache = new Cache(this.parserOptions.cacheSize);
2168
+ }
2152
2169
  }
2153
2170
  /**
2154
- * Gets whether the evaluation is asynchronous.
2171
+ * @description
2172
+ * Returns acorn version.
2155
2173
  */
2156
- get isAsync() {
2157
- return this._isAsync;
2174
+ get version() {
2175
+ return version;
2158
2176
  }
2159
2177
  /**
2160
- * Represents the state of an evaluation.
2161
- * @param context The evaluation context.
2162
- * @param result The evaluation result.
2163
- * @param options The evaluation options.
2164
- * @param isAsync Indicates whether the evaluation is asynchronous.
2178
+ * Gets the parser options.
2179
+ * @returns The parser options.
2165
2180
  */
2166
- constructor(context, result, options, isAsync) {
2167
- this._context = context;
2168
- this._result = result;
2169
- this._options = options;
2170
- this._isAsync = isAsync;
2181
+ get options() {
2182
+ return this.parserOptions;
2171
2183
  }
2172
2184
  /**
2173
- * Creates an instance of EvalState from a given context, options, and async flag.
2174
- *
2175
- * @param context - The evaluation context or context object.
2176
- * @param options - The evaluation options.
2177
- * @param isAsync - A flag indicating whether the evaluation is asynchronous.
2178
- * @returns The created EvalState instance.
2185
+ * @description
2186
+ * Parses expression and returns ES6/ES2023 AST.
2187
+ * @param expr expression to parse
2188
+ * @param options optional parser options
2189
+ * @returns The parsed AST (Abstract Syntax Tree).
2179
2190
  */
2180
- static fromContext(context, options, isAsync) {
2181
- const ctx = EvalContext.fromContext(context, options);
2182
- const result = new EvalResult(ctx);
2183
- const state = new EvalState(ctx, result, options, isAsync);
2184
- return state;
2191
+ parse(expr, options) {
2192
+ if (expr) {
2193
+ try {
2194
+ const parserOptions = { ...this.parserOptions, ...options };
2195
+ const isCache = this.parserOptions.cacheSize && this._cache;
2196
+ const ast = isCache
2197
+ ? this.fromCacheOrParse(expr, parserOptions)
2198
+ : parse(expr, parserOptions);
2199
+ return ast;
2200
+ }
2201
+ catch (error) {
2202
+ if (error instanceof Error) {
2203
+ throw new Error(error.message);
2204
+ }
2205
+ else {
2206
+ throw error;
2207
+ }
2208
+ }
2209
+ }
2210
+ return undefined;
2211
+ }
2212
+ //#region Private methods
2213
+ fromCacheOrParse(expr, options) {
2214
+ if (this._cache) {
2215
+ const hashKey = this._cache.getHashKey('', expr);
2216
+ let ast = this._cache.get(hashKey);
2217
+ if (!ast) {
2218
+ ast = parse(expr, options);
2219
+ this._cache.set(hashKey, ast);
2220
+ }
2221
+ return ast;
2222
+ }
2223
+ return undefined;
2185
2224
  }
2225
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
2226
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, providedIn: 'root' }); }
2186
2227
  }
2228
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, decorators: [{
2229
+ type: Injectable,
2230
+ args: [{
2231
+ providedIn: 'root'
2232
+ }]
2233
+ }], ctorParameters: () => [] });
2187
2234
 
2188
2235
  /**
2189
2236
  * Service for evaluating and parsing expressions.
@@ -2194,43 +2241,44 @@ class EvalService extends BaseEval {
2194
2241
  * @param parserService The parser service used for parsing expressions.
2195
2242
  */
2196
2243
  constructor(parserService) {
2197
- super();
2244
+ super(parserService);
2198
2245
  this.parserService = parserService;
2199
- this.parserOptions = {
2200
- ...defaultOptions$1,
2201
- ecmaVersion: 2020,
2202
- extractExpressions: false,
2203
- cacheSize: 100
2204
- };
2246
+ this.parserOptions = defaultParserOptions;
2205
2247
  }
2206
2248
  /**
2207
- * Parses the given expression into an abstract syntax tree (AST).
2208
- * @param expression The expression to parse.
2209
- * @returns The AST representing the expression.
2249
+ * Evaluates the given expression.
2250
+ * @param expression The expression to evaluate.
2251
+ * @param context The evaluation context.
2252
+ * @param options The evaluation options.
2253
+ * @returns The result of the evaluation.
2210
2254
  */
2211
- parse(expression) {
2212
- if (!expression) {
2213
- return undefined;
2214
- }
2215
- else if (typeof expression === 'string') {
2216
- const ast = this.parserService.parse(expression, this.parserOptions);
2217
- return ast;
2255
+ simpleEval(expression, context, options) {
2256
+ try {
2257
+ const ast = this.parse(expression);
2258
+ const state = this.createState(context, options);
2259
+ const value = evaluate(ast, state);
2260
+ return value;
2218
2261
  }
2219
- else {
2220
- return expression;
2262
+ catch (error) {
2263
+ if (error instanceof Error) {
2264
+ throw new Error(error.message);
2265
+ }
2266
+ else {
2267
+ throw error;
2268
+ }
2221
2269
  }
2222
2270
  }
2223
2271
  /**
2224
- * Evaluates the given expression.
2225
- * @param expression The expression to evaluate.
2226
- * @param context The evaluation context.
2227
- * @param options The evaluation options.
2272
+ * Evaluates an expression using the provided state.
2273
+ *
2274
+ * @param expression - The expression to evaluate.
2275
+ * @param state - The state object containing variables and functions used in the evaluation.
2228
2276
  * @returns The result of the evaluation.
2277
+ * @throws If an error occurs during evaluation.
2229
2278
  */
2230
- eval(expression, context, options) {
2279
+ eval(expression, state) {
2231
2280
  try {
2232
2281
  const ast = this.parse(expression);
2233
- const state = EvalState.fromContext(context, options);
2234
2282
  const value = evaluate(ast, state);
2235
2283
  return value;
2236
2284
  }
@@ -2250,10 +2298,33 @@ class EvalService extends BaseEval {
2250
2298
  * @param options The evaluation options.
2251
2299
  * @returns A promise that resolves to the result of the evaluation.
2252
2300
  */
2253
- evalAsync(expression, context, options) {
2301
+ simpleEvalAsync(expression, context, options) {
2302
+ try {
2303
+ const ast = this.parse(expression);
2304
+ const state = this.createState(context, options);
2305
+ const promise = evaluateAsync(ast, state);
2306
+ return promise;
2307
+ }
2308
+ catch (error) {
2309
+ if (error instanceof Error) {
2310
+ throw new Error(error.message);
2311
+ }
2312
+ else {
2313
+ throw error;
2314
+ }
2315
+ }
2316
+ }
2317
+ /**
2318
+ * Asynchronously evaluates an expression using the provided state.
2319
+ *
2320
+ * @param expression - The expression to evaluate. Can be a string or an AST node.
2321
+ * @param state - The evaluation state.
2322
+ * @returns A promise that resolves to the result of the evaluation.
2323
+ * @throws If an error occurs during evaluation.
2324
+ */
2325
+ evalAsync(expression, state) {
2254
2326
  try {
2255
2327
  const ast = this.parse(expression);
2256
- const state = EvalState.fromContext(context, options);
2257
2328
  const promise = evaluateAsync(ast, state);
2258
2329
  return promise;
2259
2330
  }
@@ -2282,32 +2353,10 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImpor
2282
2353
  */
2283
2354
  class CompilerService extends BaseEval {
2284
2355
  constructor(parserService, evalService) {
2285
- super();
2356
+ super(parserService);
2286
2357
  this.parserService = parserService;
2287
2358
  this.evalService = evalService;
2288
- this.parserOptions = {
2289
- ...defaultOptions$1,
2290
- ecmaVersion: 2020,
2291
- extractExpressions: false,
2292
- cacheSize: 100
2293
- };
2294
- }
2295
- /**
2296
- * Parses the given expression into an abstract syntax tree (AST).
2297
- * @param expression - The expression to parse.
2298
- * @returns The parsed AST.
2299
- */
2300
- parse(expression) {
2301
- if (!expression) {
2302
- return undefined;
2303
- }
2304
- else if (typeof expression === 'string') {
2305
- const ast = this.parserService.parse(expression, this.parserOptions);
2306
- return ast;
2307
- }
2308
- else {
2309
- return expression;
2310
- }
2359
+ this.parserOptions = defaultParserOptions;
2311
2360
  }
2312
2361
  /**
2313
2362
  * Compiles the given expression into a state callback function.
@@ -2336,10 +2385,10 @@ class CompilerService extends BaseEval {
2336
2385
  * @returns The result of the evaluation.
2337
2386
  * @throws Error if there is an error during the evaluation.
2338
2387
  */
2339
- call(fn, context, options) {
2388
+ simpleCall(fn, context, options) {
2340
2389
  if (fn) {
2341
2390
  try {
2342
- const state = EvalState.fromContext(context, options);
2391
+ const state = this.createState(context, options);
2343
2392
  const value = call(fn, state);
2344
2393
  return value;
2345
2394
  }
@@ -2355,26 +2404,26 @@ class CompilerService extends BaseEval {
2355
2404
  return undefined;
2356
2405
  }
2357
2406
  /**
2358
- * Calls the given asynchronous state callback function with the specified context and options.
2359
- * @param fn - The asynchronous state callback function to call.
2360
- * @param context - The evaluation context or context object.
2361
- * @param options - The evaluation options.
2362
- * @returns A promise that resolves to the result of the evaluation.
2363
- * @throws Error if there is an error during the evaluation.
2407
+ * Calls the provided function with the given state.
2408
+ *
2409
+ * @param fn - The function to be called.
2410
+ * @param state - The state to be passed to the function.
2411
+ * @returns The value returned by the function.
2412
+ * @throws If an error occurs during the function call.
2364
2413
  */
2365
- async callAsync(fn, context, options) {
2414
+ call(fn, state) {
2366
2415
  if (fn) {
2367
2416
  try {
2368
- const state = EvalState.fromContext(context, options);
2369
- const promise = callAsync(fn, state);
2370
- const value = await promise;
2417
+ const value = call(fn, state);
2371
2418
  return value;
2372
2419
  }
2373
2420
  catch (error) {
2374
2421
  if (error instanceof Error) {
2375
2422
  throw new Error(error.message);
2376
2423
  }
2377
- throw new Error('error in callAsync');
2424
+ else {
2425
+ throw new Error('call');
2426
+ }
2378
2427
  }
2379
2428
  }
2380
2429
  return undefined;
@@ -2398,6 +2447,55 @@ class CompilerService extends BaseEval {
2398
2447
  throw new Error('error in compileAsync');
2399
2448
  }
2400
2449
  }
2450
+ /**
2451
+ * Calls the given asynchronous state callback function with the specified context and options.
2452
+ * @param fn - The asynchronous state callback function to call.
2453
+ * @param context - The evaluation context or context object.
2454
+ * @param options - The evaluation options.
2455
+ * @returns A promise that resolves to the result of the evaluation.
2456
+ * @throws Error if there is an error during the evaluation.
2457
+ */
2458
+ async simpleCallAsync(fn, context, options) {
2459
+ if (fn) {
2460
+ try {
2461
+ const state = this.createState(context, options);
2462
+ const promise = callAsync(fn, state);
2463
+ const value = await promise;
2464
+ return value;
2465
+ }
2466
+ catch (error) {
2467
+ if (error instanceof Error) {
2468
+ throw new Error(error.message);
2469
+ }
2470
+ throw new Error('error in callAsync');
2471
+ }
2472
+ }
2473
+ return undefined;
2474
+ }
2475
+ /**
2476
+ * Calls the provided asynchronous function with the given state.
2477
+ *
2478
+ * @param fn - The asynchronous function to call.
2479
+ * @param state - The state to pass to the function.
2480
+ * @returns A promise that resolves to the value returned by the function.
2481
+ * @throws If an error occurs during the function call.
2482
+ */
2483
+ async callAsync(fn, state) {
2484
+ if (fn) {
2485
+ try {
2486
+ const promise = callAsync(fn, state);
2487
+ const value = await promise;
2488
+ return value;
2489
+ }
2490
+ catch (error) {
2491
+ if (error instanceof Error) {
2492
+ throw new Error(error.message);
2493
+ }
2494
+ throw new Error('error in callAsync');
2495
+ }
2496
+ }
2497
+ return undefined;
2498
+ }
2401
2499
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: CompilerService, deps: [{ token: ParserService }, { token: EvalService }], target: i0.ɵɵFactoryTarget.Injectable }); }
2402
2500
  static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: CompilerService, providedIn: 'root' }); }
2403
2501
  }
@@ -2417,12 +2515,10 @@ class DiscoveryService extends BaseEval {
2417
2515
  * @param parserService The parser service to be used.
2418
2516
  */
2419
2517
  constructor(parserService) {
2420
- super();
2518
+ super(parserService);
2421
2519
  this.parserService = parserService;
2422
2520
  this.parserOptions = {
2423
- ...defaultOptions$1,
2424
- ecmaVersion: 2020,
2425
- extractExpressions: false,
2521
+ ...defaultParserOptions,
2426
2522
  cacheSize: undefined
2427
2523
  };
2428
2524
  }
@@ -2449,23 +2545,6 @@ class DiscoveryService extends BaseEval {
2449
2545
  }
2450
2546
  }
2451
2547
  }
2452
- /**
2453
- * Parses the given expression into an abstract syntax tree (AST).
2454
- * @param expression The expression to parse.
2455
- * @returns The AST representing the expression.
2456
- */
2457
- parse(expression) {
2458
- if (!expression) {
2459
- return undefined;
2460
- }
2461
- else if (typeof expression === 'string') {
2462
- const ast = this.parserService.parse(expression, this.parserOptions);
2463
- return ast;
2464
- }
2465
- else {
2466
- return expression;
2467
- }
2468
- }
2469
2548
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DiscoveryService, deps: [{ token: ParserService }], target: i0.ɵɵFactoryTarget.Injectable }); }
2470
2549
  static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DiscoveryService, providedIn: 'root' }); }
2471
2550
  }
@@ -2480,5 +2559,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImpor
2480
2559
  * Generated bundle index. Do not edit.
2481
2560
  */
2482
2561
 
2483
- export { BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EvalContext, EvalResult, EvalService, EvalState, ParserService, Queue, RecursiveVisitorResultType, Registry, Scope, ScopeRegistry, Stack, call, callAsync, compile, compileAsync, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextKey, getContextValue, isRegistryContext, parse };
2562
+ export { BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EvalContext, EvalResult, EvalService, EvalState, ParserService, Queue, RecursiveVisitorResultType, Registry, Scope, ScopeRegistry, Stack, call, callAsync, compile, compileAsync, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextKey, getContextValue, isRegistryContext, parse };
2484
2563
  //# sourceMappingURL=zvenigora-ng-eval-core.mjs.map