@zvenigora/ng-eval-core 0.1.105 → 0.1.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/esm2022/lib/actual/services/eval.service.mjs +13 -1
  2. package/esm2022/lib/internal/classes/common/public-api.mjs +1 -3
  3. package/esm2022/lib/internal/classes/eval/eval-context.mjs +24 -6
  4. package/esm2022/lib/internal/classes/eval/eval-lookup.mjs +1 -1
  5. package/esm2022/lib/internal/classes/eval/eval-result.mjs +19 -3
  6. package/esm2022/lib/internal/classes/eval/eval-scope.mjs +86 -0
  7. package/esm2022/lib/internal/classes/eval/eval-state.mjs +1 -1
  8. package/esm2022/lib/internal/classes/eval/eval-trace.mjs +22 -0
  9. package/esm2022/lib/internal/classes/eval/public-api.mjs +3 -1
  10. package/esm2022/lib/internal/interfaces/public-api.mjs +1 -1
  11. package/esm2022/lib/internal/interfaces/registry-type.mjs +1 -1
  12. package/esm2022/lib/internal/visitors/member-expression.mjs +7 -1
  13. package/esm2022/lib/internal/visitors/visitor-result.mjs +3 -9
  14. package/fesm2022/zvenigora-ng-eval-core.mjs +351 -371
  15. package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
  16. package/lib/internal/classes/common/public-api.d.ts +0 -2
  17. package/lib/internal/classes/eval/eval-context.d.ts +11 -3
  18. package/lib/internal/classes/eval/eval-lookup.d.ts +1 -1
  19. package/lib/internal/classes/eval/eval-result.d.ts +16 -3
  20. package/lib/internal/classes/eval/eval-scope.d.ts +51 -0
  21. package/lib/internal/classes/eval/eval-state.d.ts +4 -4
  22. package/lib/internal/classes/eval/eval-trace.d.ts +20 -0
  23. package/lib/internal/classes/eval/public-api.d.ts +2 -0
  24. package/lib/internal/interfaces/public-api.d.ts +1 -1
  25. package/lib/internal/interfaces/registry-type.d.ts +0 -6
  26. package/package.json +1 -1
  27. package/esm2022/lib/internal/classes/common/scope-registry.mjs +0 -116
  28. package/esm2022/lib/internal/classes/common/scope.mjs +0 -55
  29. package/lib/internal/classes/common/scope-registry.d.ts +0 -83
  30. package/lib/internal/classes/common/scope.d.ts +0 -30
@@ -1,7 +1,7 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { Injectable } from '@angular/core';
3
3
  import { sha256 } from 'js-sha256';
4
- import { defaultOptions as defaultOptions$1, parse as parse$1, version } from 'acorn';
4
+ import { defaultOptions, parse as parse$1, version } from 'acorn';
5
5
  import * as walk from 'acorn-walk';
6
6
 
7
7
  /**
@@ -396,121 +396,6 @@ class Registry {
396
396
  }
397
397
  }
398
398
 
399
- const defaultOptions = {
400
- caseInsensitive: false,
401
- namespace: '',
402
- global: false,
403
- thisArg: undefined
404
- };
405
- /**
406
- * Represents a registry that stores key-value pairs within a specific scope.
407
- * @template TKey The type of the keys in the registry.
408
- * @template TValue The type of the values in the registry.
409
- */
410
- class ScopeRegistry {
411
- /**
412
- * Creates a new instance of ScopeRegistry.
413
- * @param entries Optional. An array of key-value pairs to initialize the registry.
414
- * @param options Optional. Options for the registry.
415
- */
416
- constructor(entries, options = defaultOptions) {
417
- this.type = 'ScopeRegistry';
418
- /**
419
- * Options for the registry.
420
- */
421
- this.options = defaultOptions;
422
- this.options = { ...defaultOptions, ...options };
423
- this._registry = new Registry(entries, options);
424
- }
425
- /**
426
- * Creates a new ScopeRegistry instance from an object.
427
- * @param object The object containing key-value pairs.
428
- * @param options Optional. Options for the registry.
429
- * @returns A new ScopeRegistry instance.
430
- */
431
- static fromObject(object, options = defaultOptions) {
432
- const registry = Registry.fromObject(object, options);
433
- return registry;
434
- }
435
- /**
436
- * Gets the value associated with the specified key.
437
- * @param key The key to retrieve the value for.
438
- * @returns The value associated with the key, or undefined if the key does not exist.
439
- */
440
- get(key) {
441
- return this._registry.get(key);
442
- }
443
- /**
444
- * Sets the value for the specified key.
445
- * @param key The key to set the value for.
446
- * @param value The value to set.
447
- */
448
- set(key, value) {
449
- this._registry.set(key, value);
450
- }
451
- /**
452
- * Checks if the registry contains the specified key.
453
- * @param key The key to check.
454
- * @returns True if the key exists in the registry, false otherwise.
455
- */
456
- has(key) {
457
- return this._registry.has(key);
458
- }
459
- /**
460
- * Deletes the key-value pair associated with the specified key.
461
- * @param key The key to delete.
462
- * @returns True if the key-value pair was deleted, false if the key does not exist.
463
- */
464
- delete(key) {
465
- return this._registry.delete(key);
466
- }
467
- /**
468
- * Clears all key-value pairs from the registry.
469
- */
470
- clear() {
471
- this._registry.clear();
472
- }
473
- /**
474
- * Gets the number of key-value pairs in the registry.
475
- */
476
- get size() {
477
- return this._registry.size;
478
- }
479
- /**
480
- * Gets an iterator for the keys in the registry.
481
- */
482
- get keys() {
483
- return this._registry.keys;
484
- }
485
- /**
486
- * Gets an iterator for the values in the registry.
487
- */
488
- get values() {
489
- return this._registry.values;
490
- }
491
- /**
492
- * Gets an iterator for the key-value pairs in the registry.
493
- */
494
- get entries() {
495
- return this._registry.entries;
496
- }
497
- /**
498
- * Executes a provided function once for each key-value pair in the registry.
499
- * @param callbackfn The function to execute for each key-value pair.
500
- * @param thisArg Optional. The value to use as `this` when executing the callback function.
501
- */
502
- forEach(callbackfn, thisArg) {
503
- this._registry.forEach(callbackfn, thisArg);
504
- }
505
- /**
506
- * Gets an iterator for the key-value pairs in the registry.
507
- * @returns An iterator for the key-value pairs in the registry.
508
- */
509
- [Symbol.iterator]() {
510
- return this._registry.entries;
511
- }
512
- }
513
-
514
399
  /**
515
400
  * Represents a generic queue data structure.
516
401
  * @template T The type of elements stored in the queue.
@@ -612,61 +497,6 @@ class Stack {
612
497
  }
613
498
  }
614
499
 
615
- /**
616
- * Represents a scope that holds key-value pairs.
617
- * @template TKey The type of the keys in the scope.
618
- * @template TValue The type of the values in the scope.
619
- */
620
- class Scope {
621
- /**
622
- * Gets the registries in the scope.
623
- */
624
- get registries() {
625
- return this._registries;
626
- }
627
- /**
628
- * Creates a new scope.
629
- * @param registries Optional array of registries to initialize the scope with.
630
- */
631
- constructor(registries) {
632
- this._registries = [];
633
- if (registries) {
634
- this._registries = registries;
635
- }
636
- }
637
- /**
638
- * Adds a registry to the scope.
639
- * @param registry The registry to add.
640
- */
641
- add(registry) {
642
- this._registries.push(registry);
643
- }
644
- /**
645
- * Gets the value associated with the specified key in the scope.
646
- * @param key The key to search for.
647
- * @param namespace Optional namespace to filter the search by.
648
- * @returns An array containing the key and value if found, otherwise undefined.
649
- */
650
- get(key, namespace) {
651
- const registries = this._registries.filter((registry) => {
652
- if (registry.options.global) {
653
- return true;
654
- }
655
- if (namespace && registry.options.namespace !== namespace) {
656
- return false;
657
- }
658
- return true;
659
- });
660
- for (const registry of registries) {
661
- if (registry.registry.has(key)) {
662
- const value = registry.registry.get(key);
663
- return [key, value];
664
- }
665
- }
666
- return undefined;
667
- }
668
- }
669
-
670
500
  /**
671
501
  * Represents a cache implementation that stores key-value pairs.
672
502
  * @template TValue The type of values stored in the cache.
@@ -993,7 +823,7 @@ class EvalContext {
993
823
  this._priorScopes = [];
994
824
  this._scopes = new Stack();
995
825
  this._options = options;
996
- this._lookups = new Registry();
826
+ this._lookups = [];
997
827
  }
998
828
  /**
999
829
  * Converts the given context to an instance of EvalContext.
@@ -1027,12 +857,12 @@ class EvalContext {
1027
857
  }
1028
858
  }
1029
859
  for (const scope of this._priorScopes) {
1030
- const value = getContextValue(scope, key);
860
+ const value = scope.get(key);
1031
861
  if (value !== undefined) {
1032
862
  return value;
1033
863
  }
1034
864
  }
1035
- for (const lookup of Array.from(this._lookups.values)) {
865
+ for (const lookup of this._lookups) {
1036
866
  const value = lookup(key, this, this._options);
1037
867
  if (value !== undefined) {
1038
868
  return value;
@@ -1059,7 +889,7 @@ class EvalContext {
1059
889
  return scope;
1060
890
  }
1061
891
  }
1062
- for (const lookup of Array.from(this._lookups.values)) {
892
+ for (const lookup of this._lookups) {
1063
893
  const value = lookup(key, this, this._options);
1064
894
  if (value !== undefined) {
1065
895
  return lookup;
@@ -1088,7 +918,7 @@ class EvalContext {
1088
918
  }
1089
919
  }
1090
920
  for (const scope of this._priorScopes) {
1091
- const foundKey = getContextKey(scope, key, caseInsensitive);
921
+ const foundKey = getContextKey(scope.context, key, caseInsensitive);
1092
922
  if (foundKey !== undefined) {
1093
923
  return foundKey;
1094
924
  }
@@ -1127,6 +957,46 @@ class EvalContext {
1127
957
  pop() {
1128
958
  this._scopes.pop();
1129
959
  }
960
+ /**
961
+ * Converts the EvalContext instance to an object representation.
962
+ * If the original value is an instance of Registry, it converts it to an object using the toObject method of the registry.
963
+ * If the original value is an object, it returns the original object.
964
+ * @returns The object representation of the EvalContext instance.
965
+ */
966
+ toObject() {
967
+ if (this._original && this._original instanceof Registry) {
968
+ const registry = this._original;
969
+ const object = registry.toObject();
970
+ return object;
971
+ }
972
+ else if (this._original && this._original instanceof Object) {
973
+ const obj = this._original;
974
+ return obj;
975
+ }
976
+ return undefined;
977
+ }
978
+ }
979
+
980
+ /**
981
+ * Represents a trace of evaluation for a specific code execution.
982
+ */
983
+ class EvalTrace extends Array {
984
+ /**
985
+ * Adds a new trace item to the evaluation trace.
986
+ * @param node - The AST node associated with the trace item.
987
+ * @param value - The value of the evaluated expression.
988
+ * @param expression - The expression being evaluated.
989
+ */
990
+ add(node, value, expression) {
991
+ const item = {
992
+ type: node.type,
993
+ value
994
+ };
995
+ if (expression) {
996
+ item.expression = expression.substring(node.start, node.end);
997
+ }
998
+ this.push(item);
999
+ }
1130
1000
  }
1131
1001
 
1132
1002
  /**
@@ -1203,6 +1073,21 @@ class EvalResult {
1203
1073
  }
1204
1074
  return this._endDate - this._startDate;
1205
1075
  }
1076
+ /**
1077
+ * Gets the expression associated with the evaluation result.
1078
+ * @returns The expression as a string, or undefined if no expression is associated.
1079
+ */
1080
+ get expression() {
1081
+ return this._expression;
1082
+ }
1083
+ /**
1084
+ * Sets the expression for the evaluation result.
1085
+ *
1086
+ * @param value - The expression to be set.
1087
+ */
1088
+ set expression(value) {
1089
+ this._expression = value;
1090
+ }
1206
1091
  /**
1207
1092
  * Creates a new instance of EvalResult.
1208
1093
  * @param trace The trace of evaluated values.
@@ -1210,7 +1095,7 @@ class EvalResult {
1210
1095
  */
1211
1096
  constructor(context) {
1212
1097
  this._stack = new Stack();
1213
- this._trace = new Registry;
1098
+ this._trace = new EvalTrace();
1214
1099
  this._context = context;
1215
1100
  }
1216
1101
  /**
@@ -1311,96 +1196,16 @@ class EvalState {
1311
1196
  }
1312
1197
 
1313
1198
  const defaultParserOptions = {
1314
- ...defaultOptions$1,
1199
+ ...defaultOptions,
1315
1200
  ecmaVersion: 2020,
1316
1201
  extractExpressions: false,
1317
1202
  cacheSize: 100
1318
1203
  };
1319
1204
 
1320
- /**
1321
- * Base class for evaluation.
1322
- */
1323
- class BaseEval {
1324
- constructor(parserService) {
1325
- this.parserService = parserService;
1326
- }
1327
- /**
1328
- * Gets the parser options.
1329
- */
1330
- get parserOptions() {
1331
- return this._parserOptions;
1332
- }
1333
- /**
1334
- * Sets the parser options.
1335
- * @param value The parser options to set.
1336
- */
1337
- set parserOptions(value) {
1338
- this._parserOptions = value;
1339
- }
1340
- /**
1341
- * Parses the given expression into an abstract syntax tree (AST).
1342
- * @param expression - The expression to parse.
1343
- * @returns The parsed AST.
1344
- */
1345
- parse(expression) {
1346
- if (!expression) {
1347
- return undefined;
1348
- }
1349
- else if (typeof expression === 'string') {
1350
- const ast = this.parserService.parse(expression, this.parserOptions);
1351
- return ast;
1352
- }
1353
- else {
1354
- return expression;
1355
- }
1356
- }
1357
- /**
1358
- * Creates an instance of EvalState.
1359
- * @param context - The evaluation context or context object.
1360
- * @param options - The evaluation options.
1361
- * @returns The created EvalState instance.
1362
- */
1363
- createState(context, options) {
1364
- const state = EvalState.fromContext(context, options);
1365
- return state;
1366
- }
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;
1381
- }
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;
1394
- }
1395
- }
1396
- return undefined;
1397
- };
1398
-
1399
1205
  const pushVisitorResult = (node, st, value) => {
1400
- const key = getKey(node);
1401
1206
  const result = st.result;
1402
1207
  result.stack.push(value);
1403
- result.trace.set(key, value);
1208
+ result.trace.add(node, value, st.result.expression);
1404
1209
  return value;
1405
1210
  };
1406
1211
  const popVisitorResult = (node, st) => {
@@ -1409,10 +1214,9 @@ const popVisitorResult = (node, st) => {
1409
1214
  return value;
1410
1215
  };
1411
1216
  const pushVisitorResultAsync = (node, st, value) => {
1412
- const key = getKey(node);
1413
1217
  const result = st.result;
1414
1218
  result.stack.push(value);
1415
- result.trace.set(key, value);
1219
+ result.trace.add(node, value, st.result.expression);
1416
1220
  return value;
1417
1221
  };
1418
1222
  const popVisitorResultAsync = (node, st) => {
@@ -1420,10 +1224,6 @@ const popVisitorResultAsync = (node, st) => {
1420
1224
  const value = result.stack.pop();
1421
1225
  return value;
1422
1226
  };
1423
- const getKey = (node) => {
1424
- const key = `${node.type}(${node.start}:${node.end})`;
1425
- return key;
1426
- };
1427
1227
 
1428
1228
  const beforeVisitor = (node, st) => {
1429
1229
  const options = st.options;
@@ -1549,6 +1349,11 @@ const evaluateMember = (node, st, callback) => {
1549
1349
  const thisValue = st.context.getThis(key);
1550
1350
  return [thisValue ?? object, key, value];
1551
1351
  }
1352
+ else if (object instanceof EvalScope) {
1353
+ const value = object.get(key);
1354
+ const thisValue = object;
1355
+ return [thisValue ?? object, key, value];
1356
+ }
1552
1357
  else {
1553
1358
  const caseInsesitive = !!(st.options?.caseInsensitive);
1554
1359
  const pair = getKeyValue(object, key, caseInsesitive);
@@ -1894,24 +1699,153 @@ const updateExpressionVisitor = (node, st, callback) => {
1894
1699
  afterVisitor(node, st);
1895
1700
  };
1896
1701
 
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);
1702
+ /**
1703
+ * @description
1704
+ * Parses expression and returns ES6/ES2020 AST.
1705
+ * @param expr expression to parse
1706
+ * @param options parser options
1707
+ */
1708
+ const parse = (expr, options) => {
1709
+ options = { ecmaVersion: 2020, ...options };
1710
+ const acornOptions = { ecmaVersion: 2020, ...options };
1711
+ const program = parse$1(expr, acornOptions);
1712
+ if (!options.extractExpressions) {
1713
+ return program;
1714
+ }
1715
+ return extractExpression(program);
1908
1716
  };
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) => {
1717
+ /**
1718
+ * @description
1719
+ * Walks through the AST and extracts first expression.
1720
+ * @param expr expression to parse
1721
+ */
1722
+ const extractExpression = (program) => {
1723
+ if (program.body.length === 1) {
1724
+ const expression = program.body[0];
1725
+ if (expression.type === 'ExpressionStatement') {
1726
+ return expression.expression;
1727
+ }
1728
+ }
1729
+ return undefined;
1730
+ };
1731
+
1732
+ /**
1733
+ * Evaluates the given node and returns the result.
1734
+ *
1735
+ * @param node The node to evaluate.
1736
+ * @param state The evaluation state.
1737
+ * @returns The result of the evaluation.
1738
+ */
1739
+ const evaluate = (node, state) => {
1740
+ if (node) {
1741
+ state.result.start();
1742
+ try {
1743
+ const visitors = getDefaultVisitors();
1744
+ walk.recursive(node, state, visitors);
1745
+ const value = popVisitorResult(node, state);
1746
+ state.result.stop();
1747
+ state.result.setSuccess(value);
1748
+ return value;
1749
+ }
1750
+ catch (error) {
1751
+ state.result.stop();
1752
+ state.result.setFailure(error);
1753
+ throw error;
1754
+ }
1755
+ }
1756
+ return undefined;
1757
+ };
1758
+ /**
1759
+ * Asynchronously evaluates the given abstract syntax tree (AST) using the provided evaluation state.
1760
+ * @param ast The abstract syntax tree to evaluate.
1761
+ * @param state The evaluation state.
1762
+ * @returns A promise that resolves to the evaluated value or undefined.
1763
+ */
1764
+ const evaluateAsync = (ast, state) => {
1765
+ const promise = new Promise((resolve) => {
1766
+ const value = evaluate(ast, state);
1767
+ resolve(value);
1768
+ });
1769
+ return promise;
1770
+ };
1771
+
1772
+ /**
1773
+ * Compiles the given node and returns a function that can be used to evaluate it.
1774
+ *
1775
+ * @param node The node to compile.
1776
+ * @returns A function that takes an `EvalState` and returns the result of evaluating the node.
1777
+ */
1778
+ const compile = (node) => {
1779
+ return evaluate.bind(null, node);
1780
+ };
1781
+ /**
1782
+ * Compiles the given node asynchronously.
1783
+ *
1784
+ * @param node - The node to compile.
1785
+ * @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
1786
+ */
1787
+ const compileAsync = (node) => {
1788
+ return evaluateAsync.bind(null, node);
1789
+ };
1790
+ /**
1791
+ * Calls the provided function with the given state and returns the result.
1792
+ *
1793
+ * @param fn The function to be called.
1794
+ * @param state The state to be passed to the function.
1795
+ * @returns The result of calling the function.
1796
+ */
1797
+ const call = (fn, state) => {
1798
+ const value = fn(state);
1799
+ return value;
1800
+ };
1801
+ /**
1802
+ * Calls an asynchronous function with the given state and returns a promise that resolves to the result.
1803
+ * @param fn The asynchronous function to call.
1804
+ * @param state The state to pass to the function.
1805
+ * @returns A promise that resolves to the result of the function.
1806
+ */
1807
+ const callAsync = (fn, state) => {
1808
+ const promise = fn(state);
1809
+ return promise;
1810
+ };
1811
+
1812
+ /**
1813
+ * Finds all nodes of a specific type in the given AST.
1814
+ *
1815
+ * @param ast The AST to search in.
1816
+ * @param searchType The type of nodes to search for.
1817
+ * @returns An array of nodes of the specified type if found, otherwise undefined.
1818
+ */
1819
+ const extract = (node, searchType) => {
1820
+ if (node) {
1821
+ const state = [];
1822
+ const visitors = {
1823
+ [searchType]: (node) => { state.push(node); return; }
1824
+ };
1825
+ walk.simple(node, visitors);
1826
+ return state.length > 0 ? state : undefined;
1827
+ }
1828
+ return undefined;
1829
+ };
1830
+
1831
+ const arrowFunctionExpressionVisitor = (node, st, callback) => {
1832
+ beforeVisitor(node, st);
1833
+ const fn = (...arrowArgs) => {
1834
+ const newContext = evaluatePatterns(node.params, st, callback, arrowArgs);
1835
+ st.context?.push(newContext);
1836
+ const value = evaluate(node.body, st);
1837
+ st.context?.pop();
1838
+ return value;
1839
+ };
1840
+ pushVisitorResult(node, st, fn);
1841
+ afterVisitor(node, st);
1842
+ };
1843
+
1844
+ // based on evalArrowContext
1845
+ // https://github.com/6utt3rfly/jse-eval/blob/main/index.ts
1846
+ const evaluatePatterns = (patterns, st, callback, args) => {
1847
+ let context = {};
1848
+ patterns.map((pattern, i) => {
1915
1849
  switch (pattern.type) {
1916
1850
  case 'Identifier':
1917
1851
  {
@@ -2038,104 +1972,138 @@ const evaluateRestElement = (pattern, st, callback, args) => {
2038
1972
  // return object;
2039
1973
  // }
2040
1974
 
1975
+ const defaultEvalScopeOptions = {
1976
+ global: false,
1977
+ caseInsensitive: false,
1978
+ namespace: undefined,
1979
+ thisArg: undefined
1980
+ };
2041
1981
  /**
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.
1982
+ * Represents an evaluation scope.
2047
1983
  */
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);
1984
+ class EvalScope {
1985
+ /**
1986
+ * Gets the context of the evaluation scope.
1987
+ * @returns The context object.
1988
+ */
1989
+ get context() {
1990
+ return this._context;
1991
+ }
1992
+ /**
1993
+ * Gets the options for evaluating the scope.
1994
+ * @returns The evaluation options.
1995
+ */
1996
+ get options() {
1997
+ return this._options;
1998
+ }
1999
+ /**
2000
+ * Creates a new instance of EvalScope.
2001
+ * @param context - The context of the evaluation scope.
2002
+ * @param options - The options for evaluating the scope.
2003
+ */
2004
+ constructor(context, options) {
2005
+ this.type = 'EvalScope';
2006
+ this._context = context ?? {};
2007
+ this._options = { ...defaultEvalScopeOptions, ...options ?? {} };
2008
+ }
2009
+ /**
2010
+ * Creates an instance of EvalScope from the given context and options.
2011
+ *
2012
+ * @param context - The context object.
2013
+ * @param options - The options object.
2014
+ * @returns An instance of EvalScope.
2015
+ */
2016
+ static fromObject(context, options) {
2017
+ return new EvalScope(context, options);
2018
+ }
2019
+ /**
2020
+ * Gets the value of the specified key.
2021
+ * @param key - The key to retrieve the value for.
2022
+ * @returns The value of the specified key, or undefined if the key is not found.
2023
+ */
2024
+ get(key) {
2025
+ const { caseInsensitive, namespace, global } = this._options;
2026
+ const { context } = this;
2027
+ if (typeof key === 'string' && namespace) {
2028
+ const found = caseInsensitive
2029
+ ? equalIgnoreCase(key, namespace)
2030
+ : key === namespace;
2031
+ if (found) {
2032
+ return this.context;
2033
+ }
2034
+ }
2035
+ if (global) {
2036
+ const value = getContextValue(context, key);
2057
2037
  return value;
2058
2038
  }
2059
- catch (error) {
2060
- state.result.stop();
2061
- state.result.setFailure(error);
2062
- throw error;
2039
+ return undefined;
2040
+ }
2041
+ /**
2042
+ * Sets the value of the specified key.
2043
+ * @param key - The key to set the value for.
2044
+ * @param value - The value to set.
2045
+ */
2046
+ set(key, value) {
2047
+ const { context } = this;
2048
+ if (context && context instanceof Registry) {
2049
+ const registry = context;
2050
+ registry.set(key, value);
2051
+ }
2052
+ else if (context && context instanceof Object) {
2053
+ const obj = context;
2054
+ obj[key] = value;
2063
2055
  }
2064
2056
  }
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
- };
2057
+ }
2120
2058
 
2121
2059
  /**
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.
2060
+ * Base class for evaluation.
2127
2061
  */
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;
2062
+ class BaseEval {
2063
+ constructor(parserService) {
2064
+ this.parserService = parserService;
2136
2065
  }
2137
- return undefined;
2138
- };
2066
+ /**
2067
+ * Gets the parser options.
2068
+ */
2069
+ get parserOptions() {
2070
+ return this._parserOptions;
2071
+ }
2072
+ /**
2073
+ * Sets the parser options.
2074
+ * @param value The parser options to set.
2075
+ */
2076
+ set parserOptions(value) {
2077
+ this._parserOptions = value;
2078
+ }
2079
+ /**
2080
+ * Parses the given expression into an abstract syntax tree (AST).
2081
+ * @param expression - The expression to parse.
2082
+ * @returns The parsed AST.
2083
+ */
2084
+ parse(expression) {
2085
+ if (!expression) {
2086
+ return undefined;
2087
+ }
2088
+ else if (typeof expression === 'string') {
2089
+ const ast = this.parserService.parse(expression, this.parserOptions);
2090
+ return ast;
2091
+ }
2092
+ else {
2093
+ return expression;
2094
+ }
2095
+ }
2096
+ /**
2097
+ * Creates an instance of EvalState.
2098
+ * @param context - The evaluation context or context object.
2099
+ * @param options - The evaluation options.
2100
+ * @returns The created EvalState instance.
2101
+ */
2102
+ createState(context, options) {
2103
+ const state = EvalState.fromContext(context, options);
2104
+ return state;
2105
+ }
2106
+ }
2139
2107
 
2140
2108
  /**
2141
2109
  * @description
@@ -2256,6 +2224,9 @@ class EvalService extends BaseEval {
2256
2224
  try {
2257
2225
  const ast = this.parse(expression);
2258
2226
  const state = this.createState(context, options);
2227
+ if (state?.result && typeof expression === 'string') {
2228
+ state.result.expression = expression;
2229
+ }
2259
2230
  const value = evaluate(ast, state);
2260
2231
  return value;
2261
2232
  }
@@ -2279,6 +2250,9 @@ class EvalService extends BaseEval {
2279
2250
  eval(expression, state) {
2280
2251
  try {
2281
2252
  const ast = this.parse(expression);
2253
+ if (state?.result && typeof expression === 'string') {
2254
+ state.result.expression = expression;
2255
+ }
2282
2256
  const value = evaluate(ast, state);
2283
2257
  return value;
2284
2258
  }
@@ -2302,6 +2276,9 @@ class EvalService extends BaseEval {
2302
2276
  try {
2303
2277
  const ast = this.parse(expression);
2304
2278
  const state = this.createState(context, options);
2279
+ if (state?.result && typeof expression === 'string') {
2280
+ state.result.expression = expression;
2281
+ }
2305
2282
  const promise = evaluateAsync(ast, state);
2306
2283
  return promise;
2307
2284
  }
@@ -2325,6 +2302,9 @@ class EvalService extends BaseEval {
2325
2302
  evalAsync(expression, state) {
2326
2303
  try {
2327
2304
  const ast = this.parse(expression);
2305
+ if (state?.result && typeof expression === 'string') {
2306
+ state.result.expression = expression;
2307
+ }
2328
2308
  const promise = evaluateAsync(ast, state);
2329
2309
  return promise;
2330
2310
  }
@@ -2559,5 +2539,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImpor
2559
2539
  * Generated bundle index. Do not edit.
2560
2540
  */
2561
2541
 
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 };
2542
+ export { BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EvalContext, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Queue, RecursiveVisitorResultType, Registry, Stack, call, callAsync, compile, compileAsync, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextKey, getContextValue, isRegistryContext, parse };
2563
2543
  //# sourceMappingURL=zvenigora-ng-eval-core.mjs.map