@zvenigora/ng-eval-core 0.1.102 → 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.
- package/esm2022/lib/actual/services/base-eval.mjs +32 -1
- package/esm2022/lib/actual/services/compiler.service.mjs +66 -40
- package/esm2022/lib/actual/services/discovery.service.mjs +4 -23
- package/esm2022/lib/actual/services/eval.service.mjs +53 -30
- package/esm2022/lib/actual/services/parser.service.mjs +19 -9
- package/esm2022/lib/internal/classes/common/base-registry.mjs +10 -1
- package/esm2022/lib/internal/classes/common/case-insensitive-registry.mjs +10 -1
- package/esm2022/lib/internal/classes/common/context.mjs +36 -4
- package/esm2022/lib/internal/classes/common/public-api.mjs +2 -2
- package/esm2022/lib/internal/classes/common/registry.mjs +10 -1
- package/esm2022/lib/internal/classes/eval/eval-context.mjs +30 -2
- package/esm2022/lib/internal/classes/eval/parser-options.mjs +8 -0
- package/esm2022/lib/internal/classes/eval/public-api.mjs +2 -1
- package/esm2022/lib/internal/visitors/assignment-expression.mjs +4 -2
- package/esm2022/lib/internal/visitors/identifier.mjs +24 -2
- package/esm2022/lib/internal/visitors/member-expression.mjs +27 -15
- package/esm2022/lib/internal/visitors/public-api.mjs +2 -1
- package/esm2022/lib/internal/visitors/update-expression.mjs +10 -4
- package/esm2022/lib/internal/visitors/utils.mjs +59 -0
- package/fesm2022/zvenigora-ng-eval-core.mjs +1398 -1135
- package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -1
- package/lib/actual/services/base-eval.d.ts +21 -2
- package/lib/actual/services/compiler.service.d.ts +28 -16
- package/lib/actual/services/discovery.service.d.ts +1 -7
- package/lib/actual/services/eval.service.d.ts +22 -10
- package/lib/actual/services/parser.service.d.ts +11 -2
- package/lib/internal/classes/common/base-registry.d.ts +6 -0
- package/lib/internal/classes/common/case-insensitive-registry.d.ts +6 -0
- package/lib/internal/classes/common/context.d.ts +11 -0
- package/lib/internal/classes/common/public-api.d.ts +1 -1
- package/lib/internal/classes/common/registry.d.ts +6 -0
- package/lib/internal/classes/eval/eval-context.d.ts +7 -0
- package/lib/internal/classes/eval/parser-options.d.ts +2 -0
- package/lib/internal/classes/eval/public-api.d.ts +1 -0
- package/lib/internal/visitors/public-api.d.ts +1 -0
- package/lib/internal/visitors/utils.d.ts +26 -0
- package/package.json +2 -2
|
@@ -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 {
|
|
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
|
*/
|
|
@@ -140,6 +121,15 @@ class BaseRegistry {
|
|
|
140
121
|
[Symbol.iterator]() {
|
|
141
122
|
return this._registry.entries();
|
|
142
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Converts a BaseRegistry instance to an object.
|
|
126
|
+
* @param registry The BaseRegistry instance to convert.
|
|
127
|
+
* @returns An object representing the key-value pairs in the registry.
|
|
128
|
+
*/
|
|
129
|
+
toObject() {
|
|
130
|
+
const object = Object.fromEntries(this.entries);
|
|
131
|
+
return object;
|
|
132
|
+
}
|
|
143
133
|
}
|
|
144
134
|
|
|
145
135
|
// some code here:
|
|
@@ -278,6 +268,15 @@ class CaseInsensitiveRegistry {
|
|
|
278
268
|
: key;
|
|
279
269
|
return keyLowerCase;
|
|
280
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Converts a BaseRegistry instance to an object.
|
|
273
|
+
* @param registry The BaseRegistry instance to convert.
|
|
274
|
+
* @returns An object representing the key-value pairs in the registry.
|
|
275
|
+
*/
|
|
276
|
+
toObject() {
|
|
277
|
+
const object = Object.fromEntries(this.entries);
|
|
278
|
+
return object;
|
|
279
|
+
}
|
|
281
280
|
}
|
|
282
281
|
|
|
283
282
|
class Registry {
|
|
@@ -386,6 +385,15 @@ class Registry {
|
|
|
386
385
|
[Symbol.iterator]() {
|
|
387
386
|
return this._registry.entries;
|
|
388
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* Converts a BaseRegistry instance to an object.
|
|
390
|
+
* @param registry The BaseRegistry instance to convert.
|
|
391
|
+
* @returns An object representing the key-value pairs in the registry.
|
|
392
|
+
*/
|
|
393
|
+
toObject() {
|
|
394
|
+
const object = Object.fromEntries(this.entries);
|
|
395
|
+
return object;
|
|
396
|
+
}
|
|
389
397
|
}
|
|
390
398
|
|
|
391
399
|
const defaultOptions = {
|
|
@@ -789,6 +797,69 @@ class Cache {
|
|
|
789
797
|
}
|
|
790
798
|
}
|
|
791
799
|
|
|
800
|
+
/**
|
|
801
|
+
* Retrieves the key-value pair from an object based on the provided key.
|
|
802
|
+
* @param obj - The object to search for the key-value pair.
|
|
803
|
+
* @param key - The key to search for in the object.
|
|
804
|
+
* @param caseInsesitive - Specifies whether the key comparison should be case-insensitive.
|
|
805
|
+
* @returns The key-value pair as an array, or undefined if the key is not found.
|
|
806
|
+
*/
|
|
807
|
+
const getKeyValue = (obj, key, caseInsesitive) => {
|
|
808
|
+
if (!caseInsesitive || typeof key !== 'string') {
|
|
809
|
+
const value = obj[key];
|
|
810
|
+
return [key, value];
|
|
811
|
+
}
|
|
812
|
+
if (typeof key === 'string') {
|
|
813
|
+
let currentObj = obj;
|
|
814
|
+
do {
|
|
815
|
+
const keys = Object.getOwnPropertyNames(currentObj);
|
|
816
|
+
if (Array.isArray(keys)) {
|
|
817
|
+
const foundKey = keys.find(k => equalIgnoreCase(key, k));
|
|
818
|
+
if (foundKey) {
|
|
819
|
+
const value = currentObj[foundKey];
|
|
820
|
+
return [foundKey, value];
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
} while ((currentObj = Object.getPrototypeOf(currentObj)));
|
|
824
|
+
}
|
|
825
|
+
return undefined;
|
|
826
|
+
};
|
|
827
|
+
/**
|
|
828
|
+
* Compares two values for equality, ignoring case sensitivity.
|
|
829
|
+
* @param a - The first value to compare.
|
|
830
|
+
* @param b - The second value to compare.
|
|
831
|
+
* @returns `true` if the values are equal, `false` otherwise.
|
|
832
|
+
*/
|
|
833
|
+
const equalIgnoreCase = (a, b) => {
|
|
834
|
+
if (typeof a === 'number' || typeof b === 'number') {
|
|
835
|
+
return a === b;
|
|
836
|
+
}
|
|
837
|
+
else if (typeof a === 'symbol' || typeof b === 'symbol') {
|
|
838
|
+
return a === b;
|
|
839
|
+
}
|
|
840
|
+
else if (typeof a === 'string' || typeof b === 'string') {
|
|
841
|
+
return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0;
|
|
842
|
+
}
|
|
843
|
+
return undefined;
|
|
844
|
+
};
|
|
845
|
+
/**
|
|
846
|
+
* Retrieves the value from an object, ignoring the case of the key.
|
|
847
|
+
* @param obj - The object to retrieve the value from.
|
|
848
|
+
* @param key - The key to search for, ignoring the case.
|
|
849
|
+
* @returns The value associated with the key, or undefined if the key is not found.
|
|
850
|
+
*/
|
|
851
|
+
const getValueIgnoreCase = (obj, key) => {
|
|
852
|
+
const pair = getKeyValue(obj, key, true);
|
|
853
|
+
if (pair) {
|
|
854
|
+
return pair[1];
|
|
855
|
+
}
|
|
856
|
+
return undefined;
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
function isRegistryContext(context) {
|
|
860
|
+
const value = ['Registry', 'CaseInsensitiveRegistry', 'BaseRegistry'].includes(context?.type);
|
|
861
|
+
return value;
|
|
862
|
+
}
|
|
792
863
|
/**
|
|
793
864
|
* Converts a context object to a Context instance.
|
|
794
865
|
* @param context - The context object to convert.
|
|
@@ -800,12 +871,12 @@ const fromContext = (context, options) => {
|
|
|
800
871
|
if (!context) {
|
|
801
872
|
return {};
|
|
802
873
|
}
|
|
874
|
+
else if (context instanceof Registry) {
|
|
875
|
+
return context;
|
|
876
|
+
}
|
|
803
877
|
else if (caseInsensitive && context instanceof Object) {
|
|
804
878
|
return Registry.fromObject(context, options);
|
|
805
879
|
}
|
|
806
|
-
else if (context instanceof Registry || context instanceof Object) {
|
|
807
|
-
return context;
|
|
808
|
-
}
|
|
809
880
|
else {
|
|
810
881
|
return context;
|
|
811
882
|
}
|
|
@@ -849,1151 +920,1317 @@ const setContextValue = (context, key, value) => {
|
|
|
849
920
|
object[key] = value;
|
|
850
921
|
}
|
|
851
922
|
};
|
|
852
|
-
|
|
853
|
-
/**
|
|
854
|
-
* @description
|
|
855
|
-
* Parses expression and returns ES6/ES2020 AST.
|
|
856
|
-
* @param expr expression to parse
|
|
857
|
-
* @param options parser options
|
|
858
|
-
*/
|
|
859
|
-
const parse = (expr, options) => {
|
|
860
|
-
options = { ecmaVersion: 2020, ...options };
|
|
861
|
-
const acornOptions = { ecmaVersion: 2020, ...options };
|
|
862
|
-
const program = parse$1(expr, acornOptions);
|
|
863
|
-
if (!options.extractExpressions) {
|
|
864
|
-
return program;
|
|
865
|
-
}
|
|
866
|
-
return extractExpression(program);
|
|
867
|
-
};
|
|
868
923
|
/**
|
|
869
|
-
*
|
|
870
|
-
*
|
|
871
|
-
*
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
924
|
+
* Retrieves the key from the given context object.
|
|
925
|
+
* If the context is an instance of Registry, it searches for the key in a case-insensitive manner if specified.
|
|
926
|
+
* If the context is an instance of Object, it uses the getKeyValue function to retrieve the key-value pair.
|
|
927
|
+
* @param context - The context object.
|
|
928
|
+
* @param key - The key to retrieve.
|
|
929
|
+
* @param caseInsensitive - Specifies whether the search should be case-insensitive (only applicable for Registry instances).
|
|
930
|
+
* @returns The retrieved key, or undefined if the key is not found.
|
|
931
|
+
*/
|
|
932
|
+
const getContextKey = (context, key, caseInsensitive) => {
|
|
933
|
+
if (context instanceof Registry) {
|
|
934
|
+
const keys = [...context.keys]; // : (string | number | symbol)[]
|
|
935
|
+
const foundKey = caseInsensitive && typeof key === 'string'
|
|
936
|
+
? keys.find(k => equalIgnoreCase(key, k))
|
|
937
|
+
: keys.find(k => k === key);
|
|
938
|
+
if (foundKey) {
|
|
939
|
+
return foundKey;
|
|
878
940
|
}
|
|
879
941
|
}
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
const result = st.result;
|
|
886
|
-
result.stack.push(value);
|
|
887
|
-
result.trace.set(key, value);
|
|
888
|
-
return value;
|
|
889
|
-
};
|
|
890
|
-
const popVisitorResult = (node, st) => {
|
|
891
|
-
const result = st.result;
|
|
892
|
-
const value = result.stack.pop();
|
|
893
|
-
return value;
|
|
894
|
-
};
|
|
895
|
-
const pushVisitorResultAsync = (node, st, value) => {
|
|
896
|
-
const key = getKey(node);
|
|
897
|
-
const result = st.result;
|
|
898
|
-
result.stack.push(value);
|
|
899
|
-
result.trace.set(key, value);
|
|
900
|
-
return value;
|
|
901
|
-
};
|
|
902
|
-
const popVisitorResultAsync = (node, st) => {
|
|
903
|
-
const result = st.result;
|
|
904
|
-
const value = result.stack.pop();
|
|
905
|
-
return value;
|
|
906
|
-
};
|
|
907
|
-
const getKey = (node) => {
|
|
908
|
-
const key = `${node.type}(${node.start}:${node.end})`;
|
|
909
|
-
return key;
|
|
910
|
-
};
|
|
911
|
-
|
|
912
|
-
const beforeVisitor = (node, st) => {
|
|
913
|
-
const options = st.options;
|
|
914
|
-
if (options?.['trackTime']) {
|
|
915
|
-
const t = performance.now();
|
|
916
|
-
console.time('before: ' + node.type);
|
|
917
|
-
return t;
|
|
942
|
+
else if (context instanceof Object) {
|
|
943
|
+
const pair = getKeyValue(context, key, caseInsensitive);
|
|
944
|
+
if (pair) {
|
|
945
|
+
return pair[0];
|
|
946
|
+
}
|
|
918
947
|
}
|
|
919
948
|
return undefined;
|
|
920
949
|
};
|
|
921
950
|
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
951
|
+
/**
|
|
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;
|
|
928
960
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
const context = st.context;
|
|
935
|
-
const value = context?.get(node.name);
|
|
936
|
-
pushVisitorResult(node, st, value);
|
|
937
|
-
afterVisitor(node, st);
|
|
938
|
-
};
|
|
939
|
-
|
|
940
|
-
const literalVisitor = (node, st) => {
|
|
941
|
-
beforeVisitor(node, st);
|
|
942
|
-
const value = node.value;
|
|
943
|
-
pushVisitorResult(node, st, value);
|
|
944
|
-
afterVisitor(node, st);
|
|
945
|
-
};
|
|
946
|
-
|
|
947
|
-
const binaryExpressionVisitor = (node, st, callback) => {
|
|
948
|
-
beforeVisitor(node, st);
|
|
949
|
-
callback(node.left, st);
|
|
950
|
-
const left = popVisitorResult(node, st);
|
|
951
|
-
callback(node.right, st);
|
|
952
|
-
const right = popVisitorResult(node, st);
|
|
953
|
-
const value = node.operator === '+' ? left + right :
|
|
954
|
-
node.operator === '-' ? left - right :
|
|
955
|
-
node.operator === '*' ? left * right :
|
|
956
|
-
node.operator === '/' ? left / right :
|
|
957
|
-
node.operator === '%' ? left % right :
|
|
958
|
-
node.operator === '**' ? left ** right :
|
|
959
|
-
node.operator === '<<' ? left << right :
|
|
960
|
-
node.operator === '>>' ? left >> right :
|
|
961
|
-
node.operator === '>>>' ? left >>> right :
|
|
962
|
-
node.operator === '&' ? left & right :
|
|
963
|
-
node.operator === '^' ? left ^ right :
|
|
964
|
-
node.operator === '|' ? left | right :
|
|
965
|
-
node.operator === '==' ? left == right :
|
|
966
|
-
node.operator === '!=' ? left != right :
|
|
967
|
-
node.operator === '===' ? left === right :
|
|
968
|
-
node.operator === '!==' ? left !== right :
|
|
969
|
-
node.operator === '<' ? left < right :
|
|
970
|
-
node.operator === '<=' ? left <= right :
|
|
971
|
-
node.operator === '>' ? left > right :
|
|
972
|
-
node.operator === '>=' ? left >= right :
|
|
973
|
-
node.operator === 'in' ? left in right :
|
|
974
|
-
node.operator === 'instanceof' ? left instanceof right :
|
|
975
|
-
node.operator === '??' ? left ?? right :
|
|
976
|
-
undefined;
|
|
977
|
-
pushVisitorResult(node, st, value);
|
|
978
|
-
afterVisitor(node, st);
|
|
979
|
-
};
|
|
980
|
-
|
|
981
|
-
const memberExpressionVisitor = (node, st, callback) => {
|
|
982
|
-
beforeVisitor(node, st);
|
|
983
|
-
const [, , value] = evaluateMember(node, st, callback);
|
|
984
|
-
pushVisitorResult(node, st, value);
|
|
985
|
-
afterVisitor(node, st);
|
|
986
|
-
};
|
|
987
|
-
const evaluateMember = (node, st, callback) => {
|
|
988
|
-
callback(node.object, st);
|
|
989
|
-
const obj = popVisitorResult(node, st);
|
|
990
|
-
const object = (node.optional ? (obj || {}) : obj);
|
|
991
|
-
if (node.property.type === 'Identifier') {
|
|
992
|
-
if (object === st.context) {
|
|
993
|
-
const value = st.context.get(node.property.name);
|
|
994
|
-
const thisValue = st.context.getThis(node.property.name);
|
|
995
|
-
return [thisValue ?? object, node.property.name, value];
|
|
996
|
-
}
|
|
997
|
-
else if (object[node.property.name]) {
|
|
998
|
-
const value = object[node.property.name];
|
|
999
|
-
return [object, node.property.name, value];
|
|
1000
|
-
}
|
|
1001
|
-
}
|
|
1002
|
-
callback(node.property, st);
|
|
1003
|
-
const prop = popVisitorResult(node, st);
|
|
1004
|
-
const property = typeof prop !== 'undefined' ? prop
|
|
1005
|
-
: node.property.type === 'Identifier' ? node.property.name : prop;
|
|
1006
|
-
const value = object[property];
|
|
1007
|
-
return [object, property, value];
|
|
1008
|
-
};
|
|
1009
|
-
|
|
1010
|
-
const arrayExpressionVisitor = (node, st, callback) => {
|
|
1011
|
-
beforeVisitor(node, st);
|
|
1012
|
-
const elements = evaluateArray(node, node.elements, st, callback);
|
|
1013
|
-
pushVisitorResult(node, st, elements);
|
|
1014
|
-
afterVisitor(node, st);
|
|
1015
|
-
};
|
|
1016
|
-
const evaluateArray = (node, items, st, callback) => {
|
|
1017
|
-
const elements = [];
|
|
1018
|
-
for (const element of items) {
|
|
1019
|
-
if (element === null || typeof element === 'undefined') {
|
|
1020
|
-
elements.push(element);
|
|
1021
|
-
}
|
|
1022
|
-
else if (element.type === 'SpreadElement') {
|
|
1023
|
-
callback(element, st);
|
|
1024
|
-
const values = popVisitorResult(node, st);
|
|
1025
|
-
elements.push(...values);
|
|
1026
|
-
}
|
|
1027
|
-
else {
|
|
1028
|
-
callback(element, st);
|
|
1029
|
-
const value = popVisitorResult(node, st);
|
|
1030
|
-
elements.push(value);
|
|
1031
|
-
}
|
|
961
|
+
/**
|
|
962
|
+
* Gets the scopes registry.
|
|
963
|
+
*/
|
|
964
|
+
get priorScopes() {
|
|
965
|
+
return this._priorScopes;
|
|
1032
966
|
}
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
const args = evaluateArray(node, node.arguments, st, callback);
|
|
1039
|
-
if (node.callee.type === 'MemberExpression') {
|
|
1040
|
-
const [object, , fn] = evaluateMember(node.callee, st, callback);
|
|
1041
|
-
const caller = fn;
|
|
1042
|
-
const value = !caller && node.callee.optional
|
|
1043
|
-
? undefined : caller.apply(object, args);
|
|
1044
|
-
pushVisitorResult(node, st, value);
|
|
967
|
+
/**
|
|
968
|
+
* Gets the scopes registry.
|
|
969
|
+
*/
|
|
970
|
+
get scopes() {
|
|
971
|
+
return this._scopes;
|
|
1045
972
|
}
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
pushVisitorResult(node, st, value);
|
|
973
|
+
/**
|
|
974
|
+
* Gets the lookups registry.
|
|
975
|
+
*/
|
|
976
|
+
get lookups() {
|
|
977
|
+
return this._lookups;
|
|
1052
978
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
const args = node.arguments.map((argument) => {
|
|
1059
|
-
callback(argument, st);
|
|
1060
|
-
const value = popVisitorResult(node, st);
|
|
1061
|
-
return value;
|
|
1062
|
-
});
|
|
1063
|
-
const value = caller.apply(st.context, args);
|
|
1064
|
-
return value;
|
|
1065
|
-
};
|
|
1066
|
-
|
|
1067
|
-
const awaitVisitor = (node, st, callback) => {
|
|
1068
|
-
beforeVisitor(node, st);
|
|
1069
|
-
const promise = new Promise((resolve) => {
|
|
1070
|
-
callback(node.argument, st);
|
|
1071
|
-
const value = popVisitorResult(node, st);
|
|
1072
|
-
resolve(value);
|
|
1073
|
-
});
|
|
1074
|
-
pushVisitorResult(node, st, promise);
|
|
1075
|
-
afterVisitor(node, st);
|
|
1076
|
-
};
|
|
1077
|
-
|
|
1078
|
-
const conditionalExpressionVisitor = (node, st, callback) => {
|
|
1079
|
-
beforeVisitor(node, st);
|
|
1080
|
-
callback(node.test, st);
|
|
1081
|
-
const test = popVisitorResult(node, st);
|
|
1082
|
-
if (test) {
|
|
1083
|
-
callback(node.consequent, st);
|
|
1084
|
-
}
|
|
1085
|
-
else {
|
|
1086
|
-
callback(node.alternate, st);
|
|
979
|
+
/**
|
|
980
|
+
* Gets the evaluation options.
|
|
981
|
+
*/
|
|
982
|
+
get options() {
|
|
983
|
+
return this._options;
|
|
1087
984
|
}
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
node.operator === '!' ? !argument :
|
|
1101
|
-
node.operator === 'typeof' ? typeof argument :
|
|
1102
|
-
node.operator === 'void' ? void argument :
|
|
1103
|
-
// node.operator === 'delete' ? delete argument :
|
|
1104
|
-
undefined;
|
|
1105
|
-
pushVisitorResult(node, st, value);
|
|
1106
|
-
afterVisitor(node, st);
|
|
1107
|
-
};
|
|
1108
|
-
|
|
1109
|
-
const logicalExpressionVisitor = (node, st, callback) => {
|
|
1110
|
-
beforeVisitor(node, st);
|
|
1111
|
-
callback(node.left, st);
|
|
1112
|
-
const left = popVisitorResult(node, st);
|
|
1113
|
-
if (node.operator === '&&' && !left) {
|
|
1114
|
-
pushVisitorResult(node, st, left);
|
|
1115
|
-
afterVisitor(node, st);
|
|
1116
|
-
return;
|
|
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();
|
|
1117
997
|
}
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
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;
|
|
1007
|
+
}
|
|
1008
|
+
const ctx = fromContext(context ?? {}, options);
|
|
1009
|
+
return new EvalContext(ctx, options ?? {});
|
|
1122
1010
|
}
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
const value = st.context;
|
|
1135
|
-
pushVisitorResult(node, st, value);
|
|
1136
|
-
afterVisitor(node, st);
|
|
1137
|
-
};
|
|
1138
|
-
|
|
1139
|
-
const newExpressionVisitor = (node, st, callback) => {
|
|
1140
|
-
beforeVisitor(node, st);
|
|
1141
|
-
const args = evaluateArray(node, node.arguments, st, callback);
|
|
1142
|
-
callback(node.callee, st);
|
|
1143
|
-
const constructor = popVisitorResult(node, st);
|
|
1144
|
-
const value = newFunc(constructor, ...args);
|
|
1145
|
-
pushVisitorResult(node, st, value);
|
|
1146
|
-
afterVisitor(node, st);
|
|
1147
|
-
};
|
|
1148
|
-
// based on Reflect.construct()
|
|
1149
|
-
// https://stackoverflow.com/questions/10428603/simulate-the-new-operator-in-javascript
|
|
1150
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/construct
|
|
1151
|
-
const newFunc = (constructor, ...args) => {
|
|
1152
|
-
const obj = Reflect.construct(constructor, args);
|
|
1153
|
-
return obj;
|
|
1154
|
-
};
|
|
1155
|
-
|
|
1156
|
-
const templateLiteralVisitor = (node, st, callback) => {
|
|
1157
|
-
beforeVisitor(node, st);
|
|
1158
|
-
const expressions = node.expressions.map((expression) => {
|
|
1159
|
-
callback(expression, st);
|
|
1160
|
-
const value = popVisitorResult(node, st);
|
|
1161
|
-
return value;
|
|
1162
|
-
});
|
|
1163
|
-
const value = node.quasis.reduce((accumulator, templateElement, index) => {
|
|
1164
|
-
accumulator += templateElement.value.cooked;
|
|
1165
|
-
if (!templateElement.tail) {
|
|
1166
|
-
accumulator += expressions[index] ?? '';
|
|
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
|
+
}
|
|
1167
1022
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
};
|
|
1173
|
-
|
|
1174
|
-
const taggedTemplateExpressionVisitor = (node, st, callback) => {
|
|
1175
|
-
beforeVisitor(node, st);
|
|
1176
|
-
callback(node.tag, st);
|
|
1177
|
-
const tag = popVisitorResult(node, st);
|
|
1178
|
-
const quasiExpressions = node.quasi.expressions.map((expression) => {
|
|
1179
|
-
callback(expression, st);
|
|
1180
|
-
const value = popVisitorResult(node, st);
|
|
1181
|
-
return value;
|
|
1182
|
-
});
|
|
1183
|
-
const quasi = node.quasi.quasis.map((templateElement) => {
|
|
1184
|
-
return templateElement.value.cooked;
|
|
1185
|
-
});
|
|
1186
|
-
const value = tag.apply(st.context, [quasi, ...quasiExpressions]);
|
|
1187
|
-
pushVisitorResult(node, st, value);
|
|
1188
|
-
afterVisitor(node, st);
|
|
1189
|
-
};
|
|
1190
|
-
|
|
1191
|
-
const objectExpressionVisitor = (node, st, callback) => {
|
|
1192
|
-
beforeVisitor(node, st);
|
|
1193
|
-
const map = new Map();
|
|
1194
|
-
for (const property of node.properties) {
|
|
1195
|
-
if (property.type === 'SpreadElement') {
|
|
1196
|
-
callback(property.argument, st);
|
|
1197
|
-
const value = popVisitorResult(property, st);
|
|
1198
|
-
for (const [key, val] of Object.entries(value)) {
|
|
1199
|
-
map.set(key, val);
|
|
1023
|
+
if (this._original) {
|
|
1024
|
+
const value = getContextValue(this._original, key);
|
|
1025
|
+
if (value !== undefined) {
|
|
1026
|
+
return value;
|
|
1200
1027
|
}
|
|
1201
1028
|
}
|
|
1202
|
-
|
|
1203
|
-
const
|
|
1204
|
-
|
|
1029
|
+
for (const scope of this._priorScopes) {
|
|
1030
|
+
const value = getContextValue(scope, key);
|
|
1031
|
+
if (value !== undefined) {
|
|
1032
|
+
return value;
|
|
1033
|
+
}
|
|
1205
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
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
return undefined;
|
|
1206
1042
|
}
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
const value = popVisitorResult(property, st);
|
|
1234
|
-
return { key, 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;
|
|
1235
1069
|
}
|
|
1236
|
-
|
|
1237
|
-
|
|
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;
|
|
1238
1097
|
}
|
|
1239
|
-
|
|
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
|
+
}
|
|
1114
|
+
}
|
|
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);
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* Pops a context from the scope stack.
|
|
1126
|
+
*/
|
|
1127
|
+
pop() {
|
|
1128
|
+
this._scopes.pop();
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1240
1131
|
|
|
1241
1132
|
/**
|
|
1242
|
-
*
|
|
1243
|
-
* @returns The default visitors object.
|
|
1133
|
+
* Represents the result of an evaluation.
|
|
1244
1134
|
*/
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
visitors['AwaitExpression'] = awaitVisitor;
|
|
1252
|
-
visitors['ConditionalExpression'] = conditionalExpressionVisitor;
|
|
1253
|
-
visitors['MemberExpression'] = memberExpressionVisitor;
|
|
1254
|
-
visitors['ArrayExpression'] = arrayExpressionVisitor;
|
|
1255
|
-
visitors['UnaryExpression'] = unaryExpressionVisitor;
|
|
1256
|
-
visitors['LogicalExpression'] = logicalExpressionVisitor;
|
|
1257
|
-
visitors['ThisExpression'] = thisExpressionVisitor;
|
|
1258
|
-
visitors['NewExpression'] = newExpressionVisitor;
|
|
1259
|
-
visitors['TemplateLiteral'] = templateLiteralVisitor;
|
|
1260
|
-
visitors['TaggedTemplateExpression'] = taggedTemplateExpressionVisitor;
|
|
1261
|
-
visitors['ObjectExpression'] = objectExpressionVisitor;
|
|
1262
|
-
visitors['AssignmentExpression'] = assignmentExpressionVisitor;
|
|
1263
|
-
visitors['UpdateExpression'] = updateExpressionVisitor;
|
|
1264
|
-
visitors['ArrowFunctionExpression'] = arrowFunctionExpressionVisitor;
|
|
1265
|
-
return visitors;
|
|
1266
|
-
};
|
|
1267
|
-
|
|
1268
|
-
const assignmentOperators = {
|
|
1269
|
-
'=': (left, value) => { return left = value; },
|
|
1270
|
-
'+=': (left, value) => { return left += value; },
|
|
1271
|
-
'-=': (left, value) => { return left -= value; },
|
|
1272
|
-
'*=': (left, value) => { return left *= value; },
|
|
1273
|
-
'/=': (left, value) => { return left /= value; },
|
|
1274
|
-
'%=': (left, value) => { return left %= value; },
|
|
1275
|
-
'**=': (left, value) => { return left **= value; },
|
|
1276
|
-
'<<=': (left, value) => { return left <<= value; },
|
|
1277
|
-
'>>=': (left, value) => { return left >>= value; },
|
|
1278
|
-
'>>>=': (left, value) => { return left >>>= value; },
|
|
1279
|
-
'&=': (left, value) => { return left &= value; },
|
|
1280
|
-
'^=': (left, value) => { return left ^= value; },
|
|
1281
|
-
'|=': (left, value) => { return left |= value; },
|
|
1282
|
-
'&&=': (left, value) => { return left &&= value; },
|
|
1283
|
-
'||=': (left, value) => { return left ||= value; },
|
|
1284
|
-
'??=': (left, value) => { return left ??= value; },
|
|
1285
|
-
};
|
|
1286
|
-
const assignmentExpressionVisitor = (node, st, callback) => {
|
|
1287
|
-
beforeVisitor(node, st);
|
|
1288
|
-
callback(node.left, st);
|
|
1289
|
-
const left = popVisitorResult(node, st);
|
|
1290
|
-
callback(node.right, st);
|
|
1291
|
-
const right = popVisitorResult(node, st);
|
|
1292
|
-
const func = assignmentOperators[node.operator];
|
|
1293
|
-
if (!func) {
|
|
1294
|
-
throw new Error(`Unsupported assignment operator: ${node.operator}`);
|
|
1295
|
-
}
|
|
1296
|
-
else if (!st.context) {
|
|
1297
|
-
throw new Error(`Context is not set.`);
|
|
1135
|
+
class EvalResult {
|
|
1136
|
+
/**
|
|
1137
|
+
* Gets the stack of evaluated values.
|
|
1138
|
+
*/
|
|
1139
|
+
get stack() {
|
|
1140
|
+
return this._stack;
|
|
1298
1141
|
}
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1142
|
+
/**
|
|
1143
|
+
* Gets the evaluated value.
|
|
1144
|
+
*/
|
|
1145
|
+
get value() {
|
|
1146
|
+
return this._value;
|
|
1304
1147
|
}
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1148
|
+
/**
|
|
1149
|
+
* Gets the error that occurred during evaluation.
|
|
1150
|
+
*/
|
|
1151
|
+
get error() {
|
|
1152
|
+
return this._error;
|
|
1310
1153
|
}
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
'--': (value) => { return value - 1; },
|
|
1317
|
-
};
|
|
1318
|
-
const updateExpressionVisitor = (node, st, callback) => {
|
|
1319
|
-
beforeVisitor(node, st);
|
|
1320
|
-
if (node.argument.type === 'Identifier') {
|
|
1321
|
-
const value = st.context?.get(node.argument.name);
|
|
1322
|
-
const newValue = updateOperators[node.operator](value);
|
|
1323
|
-
st.context?.set(node.argument.name, newValue);
|
|
1324
|
-
pushVisitorResult(node, st, node.prefix ? newValue : value);
|
|
1154
|
+
/**
|
|
1155
|
+
* Gets the error message associated with the evaluation error.
|
|
1156
|
+
*/
|
|
1157
|
+
get errorMessage() {
|
|
1158
|
+
return this._errorMessage;
|
|
1325
1159
|
}
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
pushVisitorResult(node, st, node.prefix ? newValue : value);
|
|
1160
|
+
/**
|
|
1161
|
+
* Gets whether the evaluation resulted in an error.
|
|
1162
|
+
*/
|
|
1163
|
+
get isError() {
|
|
1164
|
+
return this._isError;
|
|
1332
1165
|
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
const fn = (...arrowArgs) => {
|
|
1339
|
-
const newContext = evaluatePatterns(node.params, st, callback, arrowArgs);
|
|
1340
|
-
st.context?.push(newContext);
|
|
1341
|
-
const value = evaluate(node.body, st);
|
|
1342
|
-
st.context?.pop();
|
|
1343
|
-
return value;
|
|
1344
|
-
};
|
|
1345
|
-
pushVisitorResult(node, st, fn);
|
|
1346
|
-
afterVisitor(node, st);
|
|
1347
|
-
};
|
|
1348
|
-
|
|
1349
|
-
// based on evalArrowContext
|
|
1350
|
-
// https://github.com/6utt3rfly/jse-eval/blob/main/index.ts
|
|
1351
|
-
const evaluatePatterns = (patterns, st, callback, args) => {
|
|
1352
|
-
let context = {};
|
|
1353
|
-
patterns.map((pattern, i) => {
|
|
1354
|
-
switch (pattern.type) {
|
|
1355
|
-
case 'Identifier':
|
|
1356
|
-
{
|
|
1357
|
-
const object = evaluateIdentifier(pattern, st, args[i]);
|
|
1358
|
-
context = { ...context, ...object };
|
|
1359
|
-
}
|
|
1360
|
-
break;
|
|
1361
|
-
case 'MemberExpression':
|
|
1362
|
-
{
|
|
1363
|
-
const object = evaluateMemberExpression(pattern, st, callback, args[i]);
|
|
1364
|
-
context = { ...context, ...object };
|
|
1365
|
-
}
|
|
1366
|
-
break;
|
|
1367
|
-
case 'ObjectPattern':
|
|
1368
|
-
{
|
|
1369
|
-
const object = evaluateObjectPattern(pattern, st, callback, args[i]);
|
|
1370
|
-
context = { ...context, ...object };
|
|
1371
|
-
}
|
|
1372
|
-
break;
|
|
1373
|
-
case 'ArrayPattern':
|
|
1374
|
-
{
|
|
1375
|
-
const object = evaluateArrayPattern(pattern, st, callback, args[i]);
|
|
1376
|
-
context = { ...context, ...object };
|
|
1377
|
-
}
|
|
1378
|
-
break;
|
|
1379
|
-
case 'RestElement':
|
|
1380
|
-
{
|
|
1381
|
-
const object = evaluateRestElement(pattern, st, callback, args.slice(i));
|
|
1382
|
-
context = { ...context, ...object };
|
|
1383
|
-
}
|
|
1384
|
-
break;
|
|
1385
|
-
// case 'AssignmentPattern': {
|
|
1386
|
-
// const object = evaluateAssignmentPattern(pattern, st, callback, args[i]);
|
|
1387
|
-
// context = {...context, ...object};
|
|
1388
|
-
// }
|
|
1389
|
-
// break;
|
|
1390
|
-
}
|
|
1391
|
-
});
|
|
1392
|
-
return context;
|
|
1393
|
-
};
|
|
1394
|
-
const evaluatePattern = (pattern, st, callback, arg) => {
|
|
1395
|
-
switch (pattern.type) {
|
|
1396
|
-
case 'Identifier':
|
|
1397
|
-
return evaluateIdentifier(pattern, st, arg);
|
|
1398
|
-
case 'MemberExpression':
|
|
1399
|
-
return evaluateMemberExpression(pattern, st, callback, arg);
|
|
1400
|
-
case 'ObjectPattern':
|
|
1401
|
-
return evaluateObjectPattern(pattern, st, callback, arg);
|
|
1402
|
-
case 'ArrayPattern':
|
|
1403
|
-
return evaluateArrayPattern(pattern, st, callback, arg);
|
|
1404
|
-
case 'RestElement':
|
|
1405
|
-
return evaluateRestElement(pattern, st, callback, arg);
|
|
1406
|
-
// case 'AssignmentPattern':
|
|
1407
|
-
// return evaluateAssignmentPattern(pattern, st, callback, arg);
|
|
1166
|
+
/**
|
|
1167
|
+
* Gets whether the evaluation was successful.
|
|
1168
|
+
*/
|
|
1169
|
+
get isSuccess() {
|
|
1170
|
+
return this._isSuccess;
|
|
1408
1171
|
}
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
return object;
|
|
1415
|
-
};
|
|
1416
|
-
const evaluateMemberExpression = (pattern, st, callback, arg) => {
|
|
1417
|
-
if (pattern.type === 'MemberExpression') {
|
|
1418
|
-
console.log(pattern, st, callback, arg);
|
|
1419
|
-
throw new Error('evaluateMemberExpression is not implemented.');
|
|
1172
|
+
/**
|
|
1173
|
+
* Gets whether the evaluated value is undefined.
|
|
1174
|
+
*/
|
|
1175
|
+
get isUndefined() {
|
|
1176
|
+
return this._isUndefined;
|
|
1420
1177
|
}
|
|
1421
|
-
return {};
|
|
1422
|
-
};
|
|
1423
|
-
const evaluateObjectPattern = (pattern, st, callback, arg) => {
|
|
1424
|
-
let context = {};
|
|
1425
|
-
pattern.properties.map((pattern) => {
|
|
1426
|
-
switch (pattern.type) {
|
|
1427
|
-
case 'Property':
|
|
1428
|
-
{
|
|
1429
|
-
let key;
|
|
1430
|
-
if (pattern.key.type === 'Identifier') {
|
|
1431
|
-
key = pattern.key.name;
|
|
1432
|
-
}
|
|
1433
|
-
else if (pattern.key.type === 'Literal') {
|
|
1434
|
-
key = pattern.key.value;
|
|
1435
|
-
}
|
|
1436
|
-
else {
|
|
1437
|
-
callback(pattern.key, st);
|
|
1438
|
-
key = popVisitorResult(pattern, st);
|
|
1439
|
-
if (typeof key !== 'string' && typeof key !== 'number')
|
|
1440
|
-
throw new Error(`Unsupported property key type: ${pattern.key.type}`);
|
|
1441
|
-
}
|
|
1442
|
-
const ctx = arg;
|
|
1443
|
-
st.context?.push(ctx);
|
|
1444
|
-
callback(pattern.value, st);
|
|
1445
|
-
const value = popVisitorResult(pattern, st);
|
|
1446
|
-
st.context?.pop();
|
|
1447
|
-
const pair = {};
|
|
1448
|
-
pair[key] = value;
|
|
1449
|
-
context = { ...context, ...pair };
|
|
1450
|
-
}
|
|
1451
|
-
break;
|
|
1452
|
-
case 'RestElement':
|
|
1453
|
-
{
|
|
1454
|
-
const object = evaluateRestElement(pattern, st, callback, arg);
|
|
1455
|
-
context = { ...context, ...object };
|
|
1456
|
-
}
|
|
1457
|
-
break;
|
|
1458
|
-
}
|
|
1459
|
-
});
|
|
1460
|
-
return context;
|
|
1461
|
-
};
|
|
1462
|
-
const evaluateArrayPattern = (pattern, st, callback, args) => {
|
|
1463
|
-
const patterns = pattern.elements.filter(s => !!s);
|
|
1464
|
-
const object = evaluatePatterns(patterns, st, callback, args);
|
|
1465
|
-
return object;
|
|
1466
|
-
};
|
|
1467
|
-
const evaluateRestElement = (pattern, st, callback, args) => {
|
|
1468
|
-
const argument = pattern.argument;
|
|
1469
|
-
const object = evaluatePattern(argument, st, callback, args);
|
|
1470
|
-
return object;
|
|
1471
|
-
};
|
|
1472
|
-
// const evaluateAssignmentPattern = (pattern: AssignmentPattern, st: EvalState, callback: walk.WalkerCallback<EvalState>, args: unknown) => {
|
|
1473
|
-
// const left = evaluatePattern(pattern.left, st, callback, args);
|
|
1474
|
-
// callback(pattern.right, st);
|
|
1475
|
-
// const value = popVisitorResult(pattern, st);
|
|
1476
|
-
// const object = {...left};
|
|
1477
|
-
// return object;
|
|
1478
|
-
// }
|
|
1479
|
-
|
|
1480
|
-
/**
|
|
1481
|
-
* Evaluates the given node and returns the result.
|
|
1482
|
-
*
|
|
1483
|
-
* @param node The node to evaluate.
|
|
1484
|
-
* @param state The evaluation state.
|
|
1485
|
-
* @returns The result of the evaluation.
|
|
1486
|
-
*/
|
|
1487
|
-
const evaluate = (node, state) => {
|
|
1488
|
-
if (node) {
|
|
1489
|
-
state.result.start();
|
|
1490
|
-
try {
|
|
1491
|
-
const visitors = getDefaultVisitors();
|
|
1492
|
-
walk.recursive(node, state, visitors);
|
|
1493
|
-
const value = popVisitorResult(node, state);
|
|
1494
|
-
state.result.stop();
|
|
1495
|
-
state.result.setSuccess(value);
|
|
1496
|
-
return value;
|
|
1497
|
-
}
|
|
1498
|
-
catch (error) {
|
|
1499
|
-
state.result.stop();
|
|
1500
|
-
state.result.setFailure(error);
|
|
1501
|
-
throw error;
|
|
1502
|
-
}
|
|
1503
|
-
}
|
|
1504
|
-
return undefined;
|
|
1505
|
-
};
|
|
1506
|
-
/**
|
|
1507
|
-
* Asynchronously evaluates the given abstract syntax tree (AST) using the provided evaluation state.
|
|
1508
|
-
* @param ast The abstract syntax tree to evaluate.
|
|
1509
|
-
* @param state The evaluation state.
|
|
1510
|
-
* @returns A promise that resolves to the evaluated value or undefined.
|
|
1511
|
-
*/
|
|
1512
|
-
const evaluateAsync = (ast, state) => {
|
|
1513
|
-
const promise = new Promise((resolve) => {
|
|
1514
|
-
const value = evaluate(ast, state);
|
|
1515
|
-
resolve(value);
|
|
1516
|
-
});
|
|
1517
|
-
return promise;
|
|
1518
|
-
};
|
|
1519
|
-
|
|
1520
|
-
/**
|
|
1521
|
-
* Compiles the given node and returns a function that can be used to evaluate it.
|
|
1522
|
-
*
|
|
1523
|
-
* @param node The node to compile.
|
|
1524
|
-
* @returns A function that takes an `EvalState` and returns the result of evaluating the node.
|
|
1525
|
-
*/
|
|
1526
|
-
const compile = (node) => {
|
|
1527
|
-
return evaluate.bind(null, node);
|
|
1528
|
-
};
|
|
1529
|
-
/**
|
|
1530
|
-
* Compiles the given node asynchronously.
|
|
1531
|
-
*
|
|
1532
|
-
* @param node - The node to compile.
|
|
1533
|
-
* @returns A function that takes an `EvalState` object and returns a `Promise` that resolves to the compiled result.
|
|
1534
|
-
*/
|
|
1535
|
-
const compileAsync = (node) => {
|
|
1536
|
-
return evaluateAsync.bind(null, node);
|
|
1537
|
-
};
|
|
1538
|
-
/**
|
|
1539
|
-
* Calls the provided function with the given state and returns the result.
|
|
1540
|
-
*
|
|
1541
|
-
* @param fn The function to be called.
|
|
1542
|
-
* @param state The state to be passed to the function.
|
|
1543
|
-
* @returns The result of calling the function.
|
|
1544
|
-
*/
|
|
1545
|
-
const call = (fn, state) => {
|
|
1546
|
-
const value = fn(state);
|
|
1547
|
-
return value;
|
|
1548
|
-
};
|
|
1549
|
-
/**
|
|
1550
|
-
* Calls an asynchronous function with the given state and returns a promise that resolves to the result.
|
|
1551
|
-
* @param fn The asynchronous function to call.
|
|
1552
|
-
* @param state The state to pass to the function.
|
|
1553
|
-
* @returns A promise that resolves to the result of the function.
|
|
1554
|
-
*/
|
|
1555
|
-
const callAsync = (fn, state) => {
|
|
1556
|
-
const promise = fn(state);
|
|
1557
|
-
return promise;
|
|
1558
|
-
};
|
|
1559
|
-
|
|
1560
|
-
/**
|
|
1561
|
-
* Finds all nodes of a specific type in the given AST.
|
|
1562
|
-
*
|
|
1563
|
-
* @param ast The AST to search in.
|
|
1564
|
-
* @param searchType The type of nodes to search for.
|
|
1565
|
-
* @returns An array of nodes of the specified type if found, otherwise undefined.
|
|
1566
|
-
*/
|
|
1567
|
-
const extract = (node, searchType) => {
|
|
1568
|
-
if (node) {
|
|
1569
|
-
const state = [];
|
|
1570
|
-
const visitors = {
|
|
1571
|
-
[searchType]: (node) => { state.push(node); return; }
|
|
1572
|
-
};
|
|
1573
|
-
walk.simple(node, visitors);
|
|
1574
|
-
return state.length > 0 ? state : undefined;
|
|
1575
|
-
}
|
|
1576
|
-
return undefined;
|
|
1577
|
-
};
|
|
1578
|
-
|
|
1579
|
-
/**
|
|
1580
|
-
* @description
|
|
1581
|
-
* Service for parsing expressions and returning ES6/ES2020 AST.
|
|
1582
|
-
*/
|
|
1583
|
-
class ParserService extends BaseEval {
|
|
1584
1178
|
/**
|
|
1585
|
-
*
|
|
1179
|
+
* Gets the trace of evaluated values.
|
|
1586
1180
|
*/
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
this.parserOptions = {
|
|
1590
|
-
...defaultOptions$1,
|
|
1591
|
-
ecmaVersion: 2020,
|
|
1592
|
-
extractExpressions: true,
|
|
1593
|
-
cacheSize: 100
|
|
1594
|
-
};
|
|
1595
|
-
if (this.parserOptions.cacheSize) {
|
|
1596
|
-
this._cache = new Cache(this.parserOptions.cacheSize);
|
|
1597
|
-
}
|
|
1181
|
+
get trace() {
|
|
1182
|
+
return this._trace;
|
|
1598
1183
|
}
|
|
1599
1184
|
/**
|
|
1600
|
-
*
|
|
1601
|
-
* Returns acorn version.
|
|
1185
|
+
* Gets the evaluation context.
|
|
1602
1186
|
*/
|
|
1603
|
-
get
|
|
1604
|
-
return
|
|
1187
|
+
get context() {
|
|
1188
|
+
return this._context;
|
|
1605
1189
|
}
|
|
1606
1190
|
/**
|
|
1607
|
-
* Gets the
|
|
1608
|
-
* @returns The parser options.
|
|
1191
|
+
* Gets the evaluation options.
|
|
1609
1192
|
*/
|
|
1610
1193
|
get options() {
|
|
1611
|
-
return this.
|
|
1194
|
+
return this._context.options;
|
|
1612
1195
|
}
|
|
1613
1196
|
/**
|
|
1614
|
-
*
|
|
1615
|
-
*
|
|
1616
|
-
* @param expr expression to parse
|
|
1617
|
-
* @param options optional parser options
|
|
1618
|
-
* @returns The parsed AST (Abstract Syntax Tree).
|
|
1197
|
+
* Gets the duration of the evaluation result.
|
|
1198
|
+
* @returns The duration in milliseconds.
|
|
1619
1199
|
*/
|
|
1620
|
-
|
|
1621
|
-
if (
|
|
1622
|
-
|
|
1623
|
-
const parserOptions = { ...this.parserOptions, ...options };
|
|
1624
|
-
const isCache = this.parserOptions.cacheSize && this._cache;
|
|
1625
|
-
const ast = isCache
|
|
1626
|
-
? this.fromCacheOrParse(expr, parserOptions)
|
|
1627
|
-
: parse(expr, parserOptions);
|
|
1628
|
-
return ast;
|
|
1629
|
-
}
|
|
1630
|
-
catch (error) {
|
|
1631
|
-
if (error instanceof Error) {
|
|
1632
|
-
throw new Error(error.message);
|
|
1633
|
-
}
|
|
1634
|
-
else {
|
|
1635
|
-
throw error;
|
|
1636
|
-
}
|
|
1637
|
-
}
|
|
1638
|
-
}
|
|
1639
|
-
return undefined;
|
|
1640
|
-
}
|
|
1641
|
-
//#region Private methods
|
|
1642
|
-
fromCacheOrParse(expr, options) {
|
|
1643
|
-
if (this._cache) {
|
|
1644
|
-
const hashKey = this._cache.getHashKey('', expr);
|
|
1645
|
-
let ast = this._cache.get(hashKey);
|
|
1646
|
-
if (!ast) {
|
|
1647
|
-
ast = parse(expr, options);
|
|
1648
|
-
this._cache.set(hashKey, ast);
|
|
1649
|
-
}
|
|
1650
|
-
return ast;
|
|
1200
|
+
get duration() {
|
|
1201
|
+
if (!this._startDate || !this._endDate) {
|
|
1202
|
+
return undefined;
|
|
1651
1203
|
}
|
|
1652
|
-
return
|
|
1204
|
+
return this._endDate - this._startDate;
|
|
1653
1205
|
}
|
|
1654
|
-
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
1655
|
-
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, providedIn: 'root' }); }
|
|
1656
|
-
}
|
|
1657
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ParserService, decorators: [{
|
|
1658
|
-
type: Injectable,
|
|
1659
|
-
args: [{
|
|
1660
|
-
providedIn: 'root'
|
|
1661
|
-
}]
|
|
1662
|
-
}], ctorParameters: () => [] });
|
|
1663
|
-
|
|
1664
|
-
/**
|
|
1665
|
-
* Represents the evaluation context for the code execution.
|
|
1666
|
-
*/
|
|
1667
|
-
class EvalContext {
|
|
1668
1206
|
/**
|
|
1669
|
-
*
|
|
1207
|
+
* Creates a new instance of EvalResult.
|
|
1208
|
+
* @param trace The trace of evaluated values.
|
|
1209
|
+
* @param context The evaluation context.
|
|
1670
1210
|
*/
|
|
1671
|
-
|
|
1672
|
-
|
|
1211
|
+
constructor(context) {
|
|
1212
|
+
this._stack = new Stack();
|
|
1213
|
+
this._trace = new Registry;
|
|
1214
|
+
this._context = context;
|
|
1673
1215
|
}
|
|
1674
1216
|
/**
|
|
1675
|
-
*
|
|
1217
|
+
* Starts the evaluation process.
|
|
1676
1218
|
*/
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
}
|
|
1680
|
-
/**
|
|
1681
|
-
* Gets the scopes registry.
|
|
1682
|
-
*/
|
|
1683
|
-
get scopes() {
|
|
1684
|
-
return this._scopes;
|
|
1685
|
-
}
|
|
1686
|
-
/**
|
|
1687
|
-
* Gets the lookups registry.
|
|
1688
|
-
*/
|
|
1689
|
-
get lookups() {
|
|
1690
|
-
return this._lookups;
|
|
1691
|
-
}
|
|
1692
|
-
/**
|
|
1693
|
-
* Gets the evaluation options.
|
|
1694
|
-
*/
|
|
1695
|
-
get options() {
|
|
1696
|
-
return this._options;
|
|
1697
|
-
}
|
|
1698
|
-
/**
|
|
1699
|
-
* Creates a new instance of EvalContext.
|
|
1700
|
-
* @param original - The original registry or an object to create a registry from.
|
|
1701
|
-
* @param options - The evaluation options.
|
|
1702
|
-
*/
|
|
1703
|
-
constructor(original, options) {
|
|
1704
|
-
this.type = 'EvalContext';
|
|
1705
|
-
this._original = original;
|
|
1706
|
-
this._priorScopes = [];
|
|
1707
|
-
this._scopes = new Stack();
|
|
1708
|
-
this._options = options;
|
|
1709
|
-
this._lookups = new Registry();
|
|
1710
|
-
}
|
|
1711
|
-
/**
|
|
1712
|
-
* Converts the given context to an instance of EvalContext.
|
|
1713
|
-
* @param context - The context to convert.
|
|
1714
|
-
* @param options - The evaluation options.
|
|
1715
|
-
* @returns The converted EvalContext instance.
|
|
1716
|
-
*/
|
|
1717
|
-
static fromContext(context, options) {
|
|
1718
|
-
if (context instanceof EvalContext) {
|
|
1719
|
-
return context;
|
|
1720
|
-
}
|
|
1721
|
-
const ctx = fromContext(context ?? {}, options);
|
|
1722
|
-
return new EvalContext(ctx, options ?? {});
|
|
1219
|
+
start() {
|
|
1220
|
+
this._startDate = performance.now();
|
|
1723
1221
|
}
|
|
1724
1222
|
/**
|
|
1725
|
-
*
|
|
1726
|
-
* @param key - The key to retrieve the value for.
|
|
1727
|
-
* @returns The value associated with the key, or undefined if not found.
|
|
1223
|
+
* Stops the evaluation and records the end date.
|
|
1728
1224
|
*/
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
if (value !== undefined) {
|
|
1733
|
-
return value;
|
|
1734
|
-
}
|
|
1735
|
-
}
|
|
1736
|
-
if (this._original) {
|
|
1737
|
-
const value = getContextValue(this._original, key);
|
|
1738
|
-
if (value !== undefined) {
|
|
1739
|
-
return value;
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
for (const scope of this._priorScopes) {
|
|
1743
|
-
const value = getContextValue(scope, key);
|
|
1744
|
-
if (value !== undefined) {
|
|
1745
|
-
return value;
|
|
1746
|
-
}
|
|
1747
|
-
}
|
|
1748
|
-
for (const lookup of Array.from(this._lookups.values)) {
|
|
1749
|
-
const value = lookup(key, this, this._options);
|
|
1750
|
-
if (value !== undefined) {
|
|
1751
|
-
return value;
|
|
1752
|
-
}
|
|
1753
|
-
}
|
|
1754
|
-
return undefined;
|
|
1225
|
+
stop() {
|
|
1226
|
+
this._endDate = performance.now();
|
|
1227
|
+
return this.duration;
|
|
1755
1228
|
}
|
|
1756
1229
|
/**
|
|
1757
|
-
*
|
|
1758
|
-
*
|
|
1759
|
-
* @param
|
|
1760
|
-
* @returns
|
|
1230
|
+
* Sets the success value of the evaluation result.
|
|
1231
|
+
*
|
|
1232
|
+
* @param value The value to set as the success value.
|
|
1233
|
+
* @returns void
|
|
1761
1234
|
*/
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
}
|
|
1768
|
-
}
|
|
1769
|
-
for (const scope of this._priorScopes) {
|
|
1770
|
-
const value = getContextValue(this._original, key);
|
|
1771
|
-
if (value !== undefined) {
|
|
1772
|
-
return scope;
|
|
1773
|
-
}
|
|
1774
|
-
}
|
|
1775
|
-
for (const lookup of Array.from(this._lookups.values)) {
|
|
1776
|
-
const value = lookup(key, this, this._options);
|
|
1777
|
-
if (value !== undefined) {
|
|
1778
|
-
return lookup;
|
|
1779
|
-
}
|
|
1780
|
-
}
|
|
1781
|
-
return undefined;
|
|
1235
|
+
setSuccess(value) {
|
|
1236
|
+
this._value = value;
|
|
1237
|
+
this._isSuccess = true;
|
|
1238
|
+
this._isError = false;
|
|
1239
|
+
this._isUndefined = false;
|
|
1782
1240
|
}
|
|
1783
1241
|
/**
|
|
1784
|
-
* Sets
|
|
1785
|
-
*
|
|
1786
|
-
* If the original context is an Object, the key-value pair is set directly on the object.
|
|
1787
|
-
* @param key - The key of the pair.
|
|
1788
|
-
* @param value - The value of the pair.
|
|
1242
|
+
* Sets the failure state of the evaluation result.
|
|
1243
|
+
* @param error The error object associated with the failure.
|
|
1789
1244
|
*/
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
}
|
|
1795
|
-
else if (this._original && this._original instanceof Object) {
|
|
1796
|
-
const obj = this._original;
|
|
1797
|
-
obj[key] = value;
|
|
1245
|
+
setFailure(error) {
|
|
1246
|
+
this._error = error;
|
|
1247
|
+
if (error instanceof Error) {
|
|
1248
|
+
this._errorMessage = error.message;
|
|
1798
1249
|
}
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
* @param context - The context to push.
|
|
1803
|
-
* @param options - The evaluation options.
|
|
1804
|
-
*/
|
|
1805
|
-
push(context, options) {
|
|
1806
|
-
const ctx = fromContext(context, options);
|
|
1807
|
-
this._scopes.push(ctx);
|
|
1808
|
-
}
|
|
1809
|
-
/**
|
|
1810
|
-
* Pops a context from the scope stack.
|
|
1811
|
-
*/
|
|
1812
|
-
pop() {
|
|
1813
|
-
this._scopes.pop();
|
|
1250
|
+
this._isSuccess = false;
|
|
1251
|
+
this._isError = true;
|
|
1252
|
+
this._isUndefined = false;
|
|
1814
1253
|
}
|
|
1815
1254
|
}
|
|
1816
1255
|
|
|
1817
1256
|
/**
|
|
1818
|
-
* Represents the result
|
|
1257
|
+
* Represents the evaluation state, which includes the context, result, and options.
|
|
1819
1258
|
*/
|
|
1820
|
-
class
|
|
1821
|
-
/**
|
|
1822
|
-
* Gets the stack of evaluated values.
|
|
1823
|
-
*/
|
|
1824
|
-
get stack() {
|
|
1825
|
-
return this._stack;
|
|
1826
|
-
}
|
|
1259
|
+
class EvalState {
|
|
1827
1260
|
/**
|
|
1828
|
-
* Gets the
|
|
1261
|
+
* Gets the evaluation context.
|
|
1829
1262
|
*/
|
|
1830
|
-
get
|
|
1831
|
-
return this.
|
|
1263
|
+
get context() {
|
|
1264
|
+
return this._context;
|
|
1832
1265
|
}
|
|
1833
1266
|
/**
|
|
1834
|
-
* Gets the
|
|
1267
|
+
* Gets the evaluation result.
|
|
1835
1268
|
*/
|
|
1836
|
-
get
|
|
1837
|
-
return this.
|
|
1269
|
+
get result() {
|
|
1270
|
+
return this._result;
|
|
1838
1271
|
}
|
|
1839
1272
|
/**
|
|
1840
|
-
* Gets the
|
|
1273
|
+
* Gets the evaluation options.
|
|
1841
1274
|
*/
|
|
1842
|
-
get
|
|
1843
|
-
return this.
|
|
1275
|
+
get options() {
|
|
1276
|
+
return this._options;
|
|
1844
1277
|
}
|
|
1845
1278
|
/**
|
|
1846
|
-
* Gets whether the evaluation
|
|
1279
|
+
* Gets whether the evaluation is asynchronous.
|
|
1847
1280
|
*/
|
|
1848
|
-
get
|
|
1849
|
-
return this.
|
|
1281
|
+
get isAsync() {
|
|
1282
|
+
return this._isAsync;
|
|
1850
1283
|
}
|
|
1851
1284
|
/**
|
|
1852
|
-
*
|
|
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.
|
|
1853
1290
|
*/
|
|
1854
|
-
|
|
1855
|
-
|
|
1291
|
+
constructor(context, result, options, isAsync) {
|
|
1292
|
+
this._context = context;
|
|
1293
|
+
this._result = result;
|
|
1294
|
+
this._options = options;
|
|
1295
|
+
this._isAsync = isAsync;
|
|
1856
1296
|
}
|
|
1857
1297
|
/**
|
|
1858
|
-
*
|
|
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.
|
|
1859
1304
|
*/
|
|
1860
|
-
|
|
1861
|
-
|
|
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;
|
|
1862
1310
|
}
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
const defaultParserOptions = {
|
|
1314
|
+
...defaultOptions$1,
|
|
1315
|
+
ecmaVersion: 2020,
|
|
1316
|
+
extractExpressions: false,
|
|
1317
|
+
cacheSize: 100
|
|
1318
|
+
};
|
|
1319
|
+
|
|
1320
|
+
/**
|
|
1321
|
+
* Base class for evaluation.
|
|
1322
|
+
*/
|
|
1323
|
+
class BaseEval {
|
|
1324
|
+
constructor(parserService) {
|
|
1325
|
+
this.parserService = parserService;
|
|
1868
1326
|
}
|
|
1869
1327
|
/**
|
|
1870
|
-
* Gets the
|
|
1328
|
+
* Gets the parser options.
|
|
1871
1329
|
*/
|
|
1872
|
-
get
|
|
1873
|
-
return this.
|
|
1330
|
+
get parserOptions() {
|
|
1331
|
+
return this._parserOptions;
|
|
1874
1332
|
}
|
|
1875
1333
|
/**
|
|
1876
|
-
*
|
|
1334
|
+
* Sets the parser options.
|
|
1335
|
+
* @param value The parser options to set.
|
|
1877
1336
|
*/
|
|
1878
|
-
|
|
1879
|
-
|
|
1337
|
+
set parserOptions(value) {
|
|
1338
|
+
this._parserOptions = value;
|
|
1880
1339
|
}
|
|
1881
1340
|
/**
|
|
1882
|
-
*
|
|
1883
|
-
* @
|
|
1341
|
+
* Parses the given expression into an abstract syntax tree (AST).
|
|
1342
|
+
* @param expression - The expression to parse.
|
|
1343
|
+
* @returns The parsed AST.
|
|
1884
1344
|
*/
|
|
1885
|
-
|
|
1886
|
-
if (!
|
|
1345
|
+
parse(expression) {
|
|
1346
|
+
if (!expression) {
|
|
1887
1347
|
return undefined;
|
|
1888
1348
|
}
|
|
1889
|
-
|
|
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
|
+
}
|
|
1890
1356
|
}
|
|
1891
1357
|
/**
|
|
1892
|
-
* Creates
|
|
1893
|
-
* @param
|
|
1894
|
-
* @param
|
|
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.
|
|
1895
1362
|
*/
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
this._context = context;
|
|
1363
|
+
createState(context, options) {
|
|
1364
|
+
const state = EvalState.fromContext(context, options);
|
|
1365
|
+
return state;
|
|
1900
1366
|
}
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
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;
|
|
1906
1381
|
}
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
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
|
+
}
|
|
1913
1395
|
}
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
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;
|
|
1925
1434
|
}
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
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;
|
|
1444
|
+
}
|
|
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);
|
|
1463
|
+
}
|
|
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
|
+
};
|
|
1481
|
+
|
|
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;
|
|
1538
|
+
}
|
|
1539
|
+
else if (node.property.type === 'Literal') {
|
|
1540
|
+
key = node.property.value;
|
|
1541
|
+
}
|
|
1542
|
+
else {
|
|
1543
|
+
callback(node.property, st);
|
|
1544
|
+
key = popVisitorResult(node, st);
|
|
1545
|
+
}
|
|
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];
|
|
1551
|
+
}
|
|
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];
|
|
1934
1558
|
}
|
|
1935
|
-
this._isSuccess = false;
|
|
1936
|
-
this._isError = true;
|
|
1937
|
-
this._isUndefined = false;
|
|
1938
1559
|
}
|
|
1939
|
-
|
|
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
|
+
}
|
|
1585
|
+
}
|
|
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);
|
|
1598
|
+
}
|
|
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);
|
|
1605
|
+
}
|
|
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);
|
|
1637
|
+
}
|
|
1638
|
+
else {
|
|
1639
|
+
callback(node.alternate, st);
|
|
1640
|
+
}
|
|
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;
|
|
1670
|
+
}
|
|
1671
|
+
else if (node.operator === '||' && left) {
|
|
1672
|
+
pushVisitorResult(node, st, true);
|
|
1673
|
+
afterVisitor(node, st);
|
|
1674
|
+
return;
|
|
1675
|
+
}
|
|
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] ?? '';
|
|
1720
|
+
}
|
|
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;
|
|
2136
|
+
}
|
|
2137
|
+
return undefined;
|
|
2138
|
+
};
|
|
1940
2139
|
|
|
1941
2140
|
/**
|
|
1942
|
-
*
|
|
2141
|
+
* @description
|
|
2142
|
+
* Service for parsing expressions and returning ES6/ES2020 AST.
|
|
1943
2143
|
*/
|
|
1944
|
-
class
|
|
2144
|
+
class ParserService {
|
|
1945
2145
|
/**
|
|
1946
|
-
* Gets the
|
|
2146
|
+
* Gets the parser options.
|
|
1947
2147
|
*/
|
|
1948
|
-
get
|
|
1949
|
-
return this.
|
|
2148
|
+
get parserOptions() {
|
|
2149
|
+
return this._parserOptions;
|
|
1950
2150
|
}
|
|
1951
2151
|
/**
|
|
1952
|
-
*
|
|
2152
|
+
* Sets the parser options.
|
|
2153
|
+
* @param value The parser options to set.
|
|
1953
2154
|
*/
|
|
1954
|
-
|
|
1955
|
-
|
|
2155
|
+
set parserOptions(value) {
|
|
2156
|
+
this._parserOptions = value;
|
|
1956
2157
|
}
|
|
1957
2158
|
/**
|
|
1958
|
-
*
|
|
2159
|
+
* Constructs a new instance of the ParserService class.
|
|
1959
2160
|
*/
|
|
1960
|
-
|
|
1961
|
-
|
|
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
|
+
}
|
|
1962
2169
|
}
|
|
1963
2170
|
/**
|
|
1964
|
-
*
|
|
2171
|
+
* @description
|
|
2172
|
+
* Returns acorn version.
|
|
1965
2173
|
*/
|
|
1966
|
-
get
|
|
1967
|
-
return
|
|
2174
|
+
get version() {
|
|
2175
|
+
return version;
|
|
1968
2176
|
}
|
|
1969
2177
|
/**
|
|
1970
|
-
*
|
|
1971
|
-
* @
|
|
1972
|
-
* @param result The evaluation result.
|
|
1973
|
-
* @param options The evaluation options.
|
|
1974
|
-
* @param isAsync Indicates whether the evaluation is asynchronous.
|
|
2178
|
+
* Gets the parser options.
|
|
2179
|
+
* @returns The parser options.
|
|
1975
2180
|
*/
|
|
1976
|
-
|
|
1977
|
-
this.
|
|
1978
|
-
this._result = result;
|
|
1979
|
-
this._options = options;
|
|
1980
|
-
this._isAsync = isAsync;
|
|
2181
|
+
get options() {
|
|
2182
|
+
return this.parserOptions;
|
|
1981
2183
|
}
|
|
1982
2184
|
/**
|
|
1983
|
-
*
|
|
1984
|
-
*
|
|
1985
|
-
* @param
|
|
1986
|
-
* @param options
|
|
1987
|
-
* @
|
|
1988
|
-
* @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).
|
|
1989
2190
|
*/
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
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;
|
|
1995
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' }); }
|
|
1996
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: () => [] });
|
|
1997
2234
|
|
|
1998
2235
|
/**
|
|
1999
2236
|
* Service for evaluating and parsing expressions.
|
|
@@ -2004,43 +2241,44 @@ class EvalService extends BaseEval {
|
|
|
2004
2241
|
* @param parserService The parser service used for parsing expressions.
|
|
2005
2242
|
*/
|
|
2006
2243
|
constructor(parserService) {
|
|
2007
|
-
super();
|
|
2244
|
+
super(parserService);
|
|
2008
2245
|
this.parserService = parserService;
|
|
2009
|
-
this.parserOptions =
|
|
2010
|
-
...defaultOptions$1,
|
|
2011
|
-
ecmaVersion: 2020,
|
|
2012
|
-
extractExpressions: false,
|
|
2013
|
-
cacheSize: 100
|
|
2014
|
-
};
|
|
2246
|
+
this.parserOptions = defaultParserOptions;
|
|
2015
2247
|
}
|
|
2016
2248
|
/**
|
|
2017
|
-
*
|
|
2018
|
-
* @param expression The expression to
|
|
2019
|
-
* @
|
|
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.
|
|
2020
2254
|
*/
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
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;
|
|
2028
2261
|
}
|
|
2029
|
-
|
|
2030
|
-
|
|
2262
|
+
catch (error) {
|
|
2263
|
+
if (error instanceof Error) {
|
|
2264
|
+
throw new Error(error.message);
|
|
2265
|
+
}
|
|
2266
|
+
else {
|
|
2267
|
+
throw error;
|
|
2268
|
+
}
|
|
2031
2269
|
}
|
|
2032
2270
|
}
|
|
2033
2271
|
/**
|
|
2034
|
-
* Evaluates the
|
|
2035
|
-
*
|
|
2036
|
-
* @param
|
|
2037
|
-
* @param
|
|
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.
|
|
2038
2276
|
* @returns The result of the evaluation.
|
|
2277
|
+
* @throws If an error occurs during evaluation.
|
|
2039
2278
|
*/
|
|
2040
|
-
eval(expression,
|
|
2279
|
+
eval(expression, state) {
|
|
2041
2280
|
try {
|
|
2042
2281
|
const ast = this.parse(expression);
|
|
2043
|
-
const state = EvalState.fromContext(context, options);
|
|
2044
2282
|
const value = evaluate(ast, state);
|
|
2045
2283
|
return value;
|
|
2046
2284
|
}
|
|
@@ -2060,10 +2298,33 @@ class EvalService extends BaseEval {
|
|
|
2060
2298
|
* @param options The evaluation options.
|
|
2061
2299
|
* @returns A promise that resolves to the result of the evaluation.
|
|
2062
2300
|
*/
|
|
2063
|
-
|
|
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) {
|
|
2064
2326
|
try {
|
|
2065
2327
|
const ast = this.parse(expression);
|
|
2066
|
-
const state = EvalState.fromContext(context, options);
|
|
2067
2328
|
const promise = evaluateAsync(ast, state);
|
|
2068
2329
|
return promise;
|
|
2069
2330
|
}
|
|
@@ -2092,32 +2353,10 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImpor
|
|
|
2092
2353
|
*/
|
|
2093
2354
|
class CompilerService extends BaseEval {
|
|
2094
2355
|
constructor(parserService, evalService) {
|
|
2095
|
-
super();
|
|
2356
|
+
super(parserService);
|
|
2096
2357
|
this.parserService = parserService;
|
|
2097
2358
|
this.evalService = evalService;
|
|
2098
|
-
this.parserOptions =
|
|
2099
|
-
...defaultOptions$1,
|
|
2100
|
-
ecmaVersion: 2020,
|
|
2101
|
-
extractExpressions: false,
|
|
2102
|
-
cacheSize: 100
|
|
2103
|
-
};
|
|
2104
|
-
}
|
|
2105
|
-
/**
|
|
2106
|
-
* Parses the given expression into an abstract syntax tree (AST).
|
|
2107
|
-
* @param expression - The expression to parse.
|
|
2108
|
-
* @returns The parsed AST.
|
|
2109
|
-
*/
|
|
2110
|
-
parse(expression) {
|
|
2111
|
-
if (!expression) {
|
|
2112
|
-
return undefined;
|
|
2113
|
-
}
|
|
2114
|
-
else if (typeof expression === 'string') {
|
|
2115
|
-
const ast = this.parserService.parse(expression, this.parserOptions);
|
|
2116
|
-
return ast;
|
|
2117
|
-
}
|
|
2118
|
-
else {
|
|
2119
|
-
return expression;
|
|
2120
|
-
}
|
|
2359
|
+
this.parserOptions = defaultParserOptions;
|
|
2121
2360
|
}
|
|
2122
2361
|
/**
|
|
2123
2362
|
* Compiles the given expression into a state callback function.
|
|
@@ -2146,10 +2385,10 @@ class CompilerService extends BaseEval {
|
|
|
2146
2385
|
* @returns The result of the evaluation.
|
|
2147
2386
|
* @throws Error if there is an error during the evaluation.
|
|
2148
2387
|
*/
|
|
2149
|
-
|
|
2388
|
+
simpleCall(fn, context, options) {
|
|
2150
2389
|
if (fn) {
|
|
2151
2390
|
try {
|
|
2152
|
-
const state =
|
|
2391
|
+
const state = this.createState(context, options);
|
|
2153
2392
|
const value = call(fn, state);
|
|
2154
2393
|
return value;
|
|
2155
2394
|
}
|
|
@@ -2165,26 +2404,26 @@ class CompilerService extends BaseEval {
|
|
|
2165
2404
|
return undefined;
|
|
2166
2405
|
}
|
|
2167
2406
|
/**
|
|
2168
|
-
* Calls the
|
|
2169
|
-
*
|
|
2170
|
-
* @param
|
|
2171
|
-
* @param
|
|
2172
|
-
* @returns
|
|
2173
|
-
* @throws
|
|
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.
|
|
2174
2413
|
*/
|
|
2175
|
-
|
|
2414
|
+
call(fn, state) {
|
|
2176
2415
|
if (fn) {
|
|
2177
2416
|
try {
|
|
2178
|
-
const
|
|
2179
|
-
const promise = callAsync(fn, state);
|
|
2180
|
-
const value = await promise;
|
|
2417
|
+
const value = call(fn, state);
|
|
2181
2418
|
return value;
|
|
2182
2419
|
}
|
|
2183
2420
|
catch (error) {
|
|
2184
2421
|
if (error instanceof Error) {
|
|
2185
2422
|
throw new Error(error.message);
|
|
2186
2423
|
}
|
|
2187
|
-
|
|
2424
|
+
else {
|
|
2425
|
+
throw new Error('call');
|
|
2426
|
+
}
|
|
2188
2427
|
}
|
|
2189
2428
|
}
|
|
2190
2429
|
return undefined;
|
|
@@ -2208,6 +2447,55 @@ class CompilerService extends BaseEval {
|
|
|
2208
2447
|
throw new Error('error in compileAsync');
|
|
2209
2448
|
}
|
|
2210
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
|
+
}
|
|
2211
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 }); }
|
|
2212
2500
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: CompilerService, providedIn: 'root' }); }
|
|
2213
2501
|
}
|
|
@@ -2227,12 +2515,10 @@ class DiscoveryService extends BaseEval {
|
|
|
2227
2515
|
* @param parserService The parser service to be used.
|
|
2228
2516
|
*/
|
|
2229
2517
|
constructor(parserService) {
|
|
2230
|
-
super();
|
|
2518
|
+
super(parserService);
|
|
2231
2519
|
this.parserService = parserService;
|
|
2232
2520
|
this.parserOptions = {
|
|
2233
|
-
...
|
|
2234
|
-
ecmaVersion: 2020,
|
|
2235
|
-
extractExpressions: false,
|
|
2521
|
+
...defaultParserOptions,
|
|
2236
2522
|
cacheSize: undefined
|
|
2237
2523
|
};
|
|
2238
2524
|
}
|
|
@@ -2259,23 +2545,6 @@ class DiscoveryService extends BaseEval {
|
|
|
2259
2545
|
}
|
|
2260
2546
|
}
|
|
2261
2547
|
}
|
|
2262
|
-
/**
|
|
2263
|
-
* Parses the given expression into an abstract syntax tree (AST).
|
|
2264
|
-
* @param expression The expression to parse.
|
|
2265
|
-
* @returns The AST representing the expression.
|
|
2266
|
-
*/
|
|
2267
|
-
parse(expression) {
|
|
2268
|
-
if (!expression) {
|
|
2269
|
-
return undefined;
|
|
2270
|
-
}
|
|
2271
|
-
else if (typeof expression === 'string') {
|
|
2272
|
-
const ast = this.parserService.parse(expression, this.parserOptions);
|
|
2273
|
-
return ast;
|
|
2274
|
-
}
|
|
2275
|
-
else {
|
|
2276
|
-
return expression;
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
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 }); }
|
|
2280
2549
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DiscoveryService, providedIn: 'root' }); }
|
|
2281
2550
|
}
|
|
@@ -2286,15 +2555,9 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImpor
|
|
|
2286
2555
|
}]
|
|
2287
2556
|
}], ctorParameters: () => [{ type: ParserService }] });
|
|
2288
2557
|
|
|
2289
|
-
var RecursiveVisitorResultType;
|
|
2290
|
-
(function (RecursiveVisitorResultType) {
|
|
2291
|
-
RecursiveVisitorResultType[RecursiveVisitorResultType["Stack"] = 0] = "Stack";
|
|
2292
|
-
RecursiveVisitorResultType[RecursiveVisitorResultType["Registry"] = 1] = "Registry";
|
|
2293
|
-
})(RecursiveVisitorResultType || (RecursiveVisitorResultType = {}));
|
|
2294
|
-
|
|
2295
2558
|
/**
|
|
2296
2559
|
* Generated bundle index. Do not edit.
|
|
2297
2560
|
*/
|
|
2298
2561
|
|
|
2299
|
-
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, getContextValue
|
|
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 };
|
|
2300
2563
|
//# sourceMappingURL=zvenigora-ng-eval-core.mjs.map
|