@zvenigora/ng-eval-core 0.1.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/README.md +9 -0
  2. package/esm2022/index.mjs +2 -0
  3. package/esm2022/lib/actual/services/base-eval.mjs +19 -0
  4. package/esm2022/lib/actual/services/compiler.service.mjs +142 -0
  5. package/esm2022/lib/actual/services/discovery.service.mjs +75 -0
  6. package/esm2022/lib/actual/services/eval.service.mjs +99 -0
  7. package/esm2022/lib/actual/services/index.mjs +2 -0
  8. package/esm2022/lib/actual/services/parser.service.mjs +91 -0
  9. package/esm2022/lib/actual/services/public-api.mjs +5 -0
  10. package/esm2022/lib/internal/classes/common/base-registry.mjs +119 -0
  11. package/esm2022/lib/internal/classes/common/cache.mjs +132 -0
  12. package/esm2022/lib/internal/classes/common/case-insensitive-registry.mjs +138 -0
  13. package/esm2022/lib/internal/classes/common/context.mjs +62 -0
  14. package/esm2022/lib/internal/classes/common/index.mjs +2 -0
  15. package/esm2022/lib/internal/classes/common/public-api.mjs +10 -0
  16. package/esm2022/lib/internal/classes/common/queue.mjs +42 -0
  17. package/esm2022/lib/internal/classes/common/registry.mjs +110 -0
  18. package/esm2022/lib/internal/classes/common/scope-registry.mjs +116 -0
  19. package/esm2022/lib/internal/classes/common/scope.mjs +55 -0
  20. package/esm2022/lib/internal/classes/common/stack.mjs +59 -0
  21. package/esm2022/lib/internal/classes/eval/eval-context.mjs +155 -0
  22. package/esm2022/lib/internal/classes/eval/eval-lookup.mjs +2 -0
  23. package/esm2022/lib/internal/classes/eval/eval-options.mjs +42 -0
  24. package/esm2022/lib/internal/classes/eval/eval-result.mjs +125 -0
  25. package/esm2022/lib/internal/classes/eval/eval-state.mjs +59 -0
  26. package/esm2022/lib/internal/classes/eval/index.mjs +2 -0
  27. package/esm2022/lib/internal/classes/eval/public-api.mjs +4 -0
  28. package/esm2022/lib/internal/functions/compile.mjs +41 -0
  29. package/esm2022/lib/internal/functions/evaluate.mjs +42 -0
  30. package/esm2022/lib/internal/functions/extract.mjs +20 -0
  31. package/esm2022/lib/internal/functions/index.mjs +2 -0
  32. package/esm2022/lib/internal/functions/parse.mjs +31 -0
  33. package/esm2022/lib/internal/functions/public-api.mjs +5 -0
  34. package/esm2022/lib/internal/interfaces/cache-type.mjs +2 -0
  35. package/esm2022/lib/internal/interfaces/index.mjs +2 -0
  36. package/esm2022/lib/internal/interfaces/parser-types.mjs +2 -0
  37. package/esm2022/lib/internal/interfaces/public-api.mjs +3 -0
  38. package/esm2022/lib/internal/interfaces/queue-type.mjs +2 -0
  39. package/esm2022/lib/internal/interfaces/recursive-visitors.mjs +6 -0
  40. package/esm2022/lib/internal/interfaces/registry-type.mjs +2 -0
  41. package/esm2022/lib/internal/interfaces/scope-type.mjs +2 -0
  42. package/esm2022/lib/internal/interfaces/stack-type.mjs +2 -0
  43. package/esm2022/lib/internal/public-api.mjs +3 -0
  44. package/esm2022/lib/internal/visitors/after-visitor.mjs +10 -0
  45. package/esm2022/lib/internal/visitors/array-expression.mjs +29 -0
  46. package/esm2022/lib/internal/visitors/arrow-function-expression.mjs +18 -0
  47. package/esm2022/lib/internal/visitors/assignment-expression.mjs +50 -0
  48. package/esm2022/lib/internal/visitors/await-expression.mjs +14 -0
  49. package/esm2022/lib/internal/visitors/before-visitor.mjs +10 -0
  50. package/esm2022/lib/internal/visitors/binary-expression.mjs +37 -0
  51. package/esm2022/lib/internal/visitors/call-expression.mjs +36 -0
  52. package/esm2022/lib/internal/visitors/conditional-expression.mjs +18 -0
  53. package/esm2022/lib/internal/visitors/identifier.mjs +11 -0
  54. package/esm2022/lib/internal/visitors/index.mjs +2 -0
  55. package/esm2022/lib/internal/visitors/literal.mjs +10 -0
  56. package/esm2022/lib/internal/visitors/logical-expression.mjs +26 -0
  57. package/esm2022/lib/internal/visitors/member-expression.mjs +32 -0
  58. package/esm2022/lib/internal/visitors/new-expression.mjs +21 -0
  59. package/esm2022/lib/internal/visitors/object-expression.mjs +53 -0
  60. package/esm2022/lib/internal/visitors/pattern.mjs +132 -0
  61. package/esm2022/lib/internal/visitors/public-api.mjs +24 -0
  62. package/esm2022/lib/internal/visitors/recursive-visitors.mjs +28 -0
  63. package/esm2022/lib/internal/visitors/tagged-template-expression.mjs +20 -0
  64. package/esm2022/lib/internal/visitors/template-literal.mjs +21 -0
  65. package/esm2022/lib/internal/visitors/this-expression.mjs +10 -0
  66. package/esm2022/lib/internal/visitors/unary-expression.mjs +19 -0
  67. package/esm2022/lib/internal/visitors/update-expression.mjs +26 -0
  68. package/esm2022/lib/internal/visitors/visitor-result.mjs +29 -0
  69. package/esm2022/public-api.mjs +6 -0
  70. package/esm2022/zvenigora-ng-eval-core.mjs +5 -0
  71. package/fesm2022/zvenigora-ng-eval-core.mjs +2300 -0
  72. package/fesm2022/zvenigora-ng-eval-core.mjs.map +1 -0
  73. package/index.d.ts +1 -0
  74. package/lib/actual/services/base-eval.d.ts +16 -0
  75. package/lib/actual/services/compiler.service.d.ts +57 -0
  76. package/lib/actual/services/discovery.service.d.ts +33 -0
  77. package/lib/actual/services/eval.service.d.ts +41 -0
  78. package/lib/actual/services/index.d.ts +1 -0
  79. package/lib/actual/services/parser.service.d.ts +36 -0
  80. package/lib/actual/services/public-api.d.ts +4 -0
  81. package/lib/internal/classes/common/base-registry.d.ts +78 -0
  82. package/lib/internal/classes/common/cache.d.ts +91 -0
  83. package/lib/internal/classes/common/case-insensitive-registry.d.ts +79 -0
  84. package/lib/internal/classes/common/context.d.ts +31 -0
  85. package/lib/internal/classes/common/index.d.ts +1 -0
  86. package/lib/internal/classes/common/public-api.d.ts +9 -0
  87. package/lib/internal/classes/common/queue.d.ts +33 -0
  88. package/lib/internal/classes/common/registry.d.ts +80 -0
  89. package/lib/internal/classes/common/scope-registry.d.ts +83 -0
  90. package/lib/internal/classes/common/scope.d.ts +30 -0
  91. package/lib/internal/classes/common/stack.d.ts +43 -0
  92. package/lib/internal/classes/eval/eval-context.d.ts +78 -0
  93. package/lib/internal/classes/eval/eval-lookup.d.ts +2 -0
  94. package/lib/internal/classes/eval/eval-options.d.ts +6 -0
  95. package/lib/internal/classes/eval/eval-result.d.ts +92 -0
  96. package/lib/internal/classes/eval/eval-state.d.ts +46 -0
  97. package/lib/internal/classes/eval/index.d.ts +1 -0
  98. package/lib/internal/classes/eval/public-api.d.ts +5 -0
  99. package/lib/internal/functions/compile.d.ts +33 -0
  100. package/lib/internal/functions/evaluate.d.ts +17 -0
  101. package/lib/internal/functions/extract.d.ts +10 -0
  102. package/lib/internal/functions/index.d.ts +1 -0
  103. package/lib/internal/functions/parse.d.ts +15 -0
  104. package/lib/internal/functions/public-api.d.ts +4 -0
  105. package/lib/internal/interfaces/cache-type.d.ts +6 -0
  106. package/lib/internal/interfaces/index.d.ts +1 -0
  107. package/lib/internal/interfaces/parser-types.d.ts +7 -0
  108. package/lib/internal/interfaces/public-api.d.ts +7 -0
  109. package/lib/internal/interfaces/queue-type.d.ts +6 -0
  110. package/lib/internal/interfaces/recursive-visitors.d.ts +43 -0
  111. package/lib/internal/interfaces/registry-type.d.ts +73 -0
  112. package/lib/internal/interfaces/scope-type.d.ts +15 -0
  113. package/lib/internal/interfaces/stack-type.d.ts +6 -0
  114. package/lib/internal/public-api.d.ts +2 -0
  115. package/lib/internal/visitors/after-visitor.d.ts +3 -0
  116. package/lib/internal/visitors/array-expression.d.ts +5 -0
  117. package/lib/internal/visitors/arrow-function-expression.d.ts +4 -0
  118. package/lib/internal/visitors/assignment-expression.d.ts +4 -0
  119. package/lib/internal/visitors/await-expression.d.ts +4 -0
  120. package/lib/internal/visitors/before-visitor.d.ts +3 -0
  121. package/lib/internal/visitors/binary-expression.d.ts +4 -0
  122. package/lib/internal/visitors/call-expression.d.ts +5 -0
  123. package/lib/internal/visitors/conditional-expression.d.ts +4 -0
  124. package/lib/internal/visitors/identifier.d.ts +3 -0
  125. package/lib/internal/visitors/index.d.ts +1 -0
  126. package/lib/internal/visitors/literal.d.ts +3 -0
  127. package/lib/internal/visitors/logical-expression.d.ts +4 -0
  128. package/lib/internal/visitors/member-expression.d.ts +5 -0
  129. package/lib/internal/visitors/new-expression.d.ts +5 -0
  130. package/lib/internal/visitors/object-expression.d.ts +4 -0
  131. package/lib/internal/visitors/pattern.d.ts +6 -0
  132. package/lib/internal/visitors/public-api.d.ts +23 -0
  133. package/lib/internal/visitors/recursive-visitors.d.ts +7 -0
  134. package/lib/internal/visitors/tagged-template-expression.d.ts +4 -0
  135. package/lib/internal/visitors/template-literal.d.ts +4 -0
  136. package/lib/internal/visitors/this-expression.d.ts +3 -0
  137. package/lib/internal/visitors/unary-expression.d.ts +4 -0
  138. package/lib/internal/visitors/update-expression.d.ts +4 -0
  139. package/lib/internal/visitors/visitor-result.d.ts +6 -0
  140. package/package.json +40 -0
  141. package/public-api.d.ts +5 -0
@@ -0,0 +1,2300 @@
1
+ import * as i0 from '@angular/core';
2
+ import { Injectable } from '@angular/core';
3
+ import { sha256 } from 'js-sha256';
4
+ import { parse as parse$1, defaultOptions as defaultOptions$1, version } from 'acorn';
5
+ import * as walk from 'acorn-walk';
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
+ /**
27
+ * Represents a registry that stores key-value pairs.
28
+ */
29
+ class BaseRegistry {
30
+ /**
31
+ * Creates a new Registry instance.
32
+ * @param entries - Optional array of key-value pairs to initialize the registry.
33
+ */
34
+ constructor(entries) {
35
+ this._registry = new Map();
36
+ this.type = 'BaseRegistry';
37
+ /**
38
+ * Options for the base registry.
39
+ */
40
+ this.options = {
41
+ caseInsensitive: false
42
+ };
43
+ if (entries && Array.isArray(entries)) {
44
+ for (const [key, value] of entries) {
45
+ this.set(key, value);
46
+ }
47
+ }
48
+ else if (entries && typeof entries === 'object') {
49
+ for (const [key, value] of Object.entries(entries)) {
50
+ this.set(key, value);
51
+ }
52
+ }
53
+ }
54
+ /**
55
+ * Creates a new Registry instance from an object.
56
+ * @param object - The object containing key-value pairs.
57
+ * @returns A new Registry instance.
58
+ */
59
+ static fromObject(object) {
60
+ const registry = new BaseRegistry();
61
+ for (const [key, value] of Object.entries(object)) {
62
+ registry.set(key, value);
63
+ }
64
+ return registry;
65
+ }
66
+ /**
67
+ * Retrieves the value associated with the specified key.
68
+ * @param key - The key to retrieve the value for.
69
+ * @returns The value associated with the key, or undefined if the key does not exist.
70
+ */
71
+ get(key) {
72
+ return this._registry.get(key);
73
+ }
74
+ /**
75
+ * Sets the value for the specified key.
76
+ * @param key - The key to set the value for.
77
+ * @param value - The value to set.
78
+ */
79
+ set(key, value) {
80
+ this._registry.set(key, value);
81
+ }
82
+ /**
83
+ * Checks if the registry contains the specified key.
84
+ * @param key - The key to check.
85
+ * @returns True if the registry contains the key, false otherwise.
86
+ */
87
+ has(key) {
88
+ return this._registry.has(key);
89
+ }
90
+ /**
91
+ * Deletes the key-value pair associated with the specified key.
92
+ * @param key - The key to delete.
93
+ * @returns True if the key-value pair was deleted, false if the key does not exist.
94
+ */
95
+ delete(key) {
96
+ return this._registry.delete(key);
97
+ }
98
+ /**
99
+ * Clears all key-value pairs from the registry.
100
+ */
101
+ clear() {
102
+ this._registry.clear();
103
+ }
104
+ /**
105
+ * Gets the number of key-value pairs in the registry.
106
+ */
107
+ get size() {
108
+ return this._registry.size;
109
+ }
110
+ /**
111
+ * Gets an iterator for the keys in the registry.
112
+ */
113
+ get keys() {
114
+ return this._registry.keys();
115
+ }
116
+ /**
117
+ * Gets an iterator for the values in the registry.
118
+ */
119
+ get values() {
120
+ return this._registry.values();
121
+ }
122
+ /**
123
+ * Gets an iterator for the key-value pairs in the registry.
124
+ */
125
+ get entries() {
126
+ return this._registry.entries();
127
+ }
128
+ /**
129
+ * Executes a provided function once for each key-value pair in the registry.
130
+ * @param callbackfn - The function to execute for each key-value pair.
131
+ * @param thisArg - Optional value to use as `this` when executing the callback function.
132
+ */
133
+ forEach(callbackfn, thisArg) {
134
+ this._registry.forEach(callbackfn, thisArg);
135
+ }
136
+ /**
137
+ * Returns an iterator for the entries of the registry.
138
+ * @returns An iterator that yields key-value pairs of type [TKey, TValue].
139
+ */
140
+ [Symbol.iterator]() {
141
+ return this._registry.entries();
142
+ }
143
+ }
144
+
145
+ // some code here:
146
+ // https://stackoverflow.com/questions/50019920/javascript-map-key-value-pairs-case-insensitive-search
147
+ /**
148
+ * Represents a case-insensitive registry that maps keys to values.
149
+ */
150
+ class CaseInsensitiveRegistry {
151
+ /**
152
+ * Creates a new instance of CaseInsensitiveRegistry.
153
+ * @param entries - Optional initial entries to populate the registry.
154
+ */
155
+ constructor(entries) {
156
+ this.registry = new Map();
157
+ this.keysMap = new Map();
158
+ this.type = 'CaseInsensitiveRegistry';
159
+ /**
160
+ * Options for the registry.
161
+ */
162
+ this.options = {
163
+ caseInsensitive: true
164
+ };
165
+ if (entries && Array.isArray(entries)) {
166
+ for (const [key, value] of entries) {
167
+ this.set(key, value);
168
+ }
169
+ }
170
+ else if (entries && typeof entries === 'object') {
171
+ for (const [key, value] of Object.entries(entries)) {
172
+ this.set(key, value);
173
+ }
174
+ }
175
+ }
176
+ /**
177
+ * Creates a new CaseInsensitiveRegistry from an object.
178
+ * @param object - The object containing key-value pairs.
179
+ * @returns A new CaseInsensitiveRegistry instance.
180
+ */
181
+ static fromObject(object) {
182
+ const registry = new CaseInsensitiveRegistry();
183
+ for (const [key, value] of Object.entries(object)) {
184
+ registry.set(key, value);
185
+ }
186
+ return registry;
187
+ }
188
+ /**
189
+ * Retrieves the value associated with the specified key.
190
+ * @param key - The key.
191
+ * @returns The value associated with the key, or undefined if the key is not found.
192
+ */
193
+ get(key) {
194
+ return this.registry.get(this.getKey(key));
195
+ }
196
+ /**
197
+ * Sets a key-value pair in the registry.
198
+ * @param key - The key.
199
+ * @param value - The value.
200
+ */
201
+ set(key, value) {
202
+ const newKey = this.getKey(key);
203
+ this.registry.set(newKey, value);
204
+ this.keysMap.set(newKey, key);
205
+ }
206
+ /**
207
+ * Checks if the registry contains the specified key.
208
+ * @param key - The key.
209
+ * @returns True if the key is found, false otherwise.
210
+ */
211
+ has(key) {
212
+ return this.registry.has(this.getKey(key));
213
+ }
214
+ /**
215
+ * Deletes the key-value pair associated with the specified key.
216
+ * @param key - The key.
217
+ * @returns True if the key-value pair is deleted, false if the key is not found.
218
+ */
219
+ delete(key) {
220
+ const newKey = this.getKey(key);
221
+ this.keysMap.delete(newKey);
222
+ return this.registry.delete(newKey);
223
+ }
224
+ /**
225
+ * Clears all key-value pairs from the registry.
226
+ */
227
+ clear() {
228
+ this.registry.clear();
229
+ this.keysMap.clear();
230
+ }
231
+ /**
232
+ * Gets the number of key-value pairs in the registry.
233
+ */
234
+ get size() {
235
+ return this.registry.size;
236
+ }
237
+ /**
238
+ * Gets an iterator for the keys in the registry.
239
+ */
240
+ get keys() {
241
+ return this.keysMap.values();
242
+ }
243
+ /**
244
+ * Gets an iterator for the values in the registry.
245
+ */
246
+ get values() {
247
+ return this.registry.values();
248
+ }
249
+ /**
250
+ * Gets an iterator for the key-value pairs in the registry.
251
+ */
252
+ get entries() {
253
+ const keys = this.keysMap.values();
254
+ const values = this.registry.values();
255
+ const entries = new Array;
256
+ for (let i = 0; i < this.registry.size; i++) {
257
+ entries.push([keys.next().value, values.next().value]);
258
+ }
259
+ return entries.values();
260
+ }
261
+ /**
262
+ * Executes a provided function once for each key-value pair in the registry.
263
+ * @param callbackfn - The function to execute for each key-value pair.
264
+ */
265
+ forEach(callbackfn) {
266
+ this.registry.forEach(callbackfn);
267
+ }
268
+ /**
269
+ * Returns an iterator for the entries of the registry.
270
+ * @returns An iterator that yields key-value pairs of type [string, unknown].
271
+ */
272
+ [Symbol.iterator]() {
273
+ return this.registry[Symbol.iterator]();
274
+ }
275
+ getKey(key) {
276
+ const keyLowerCase = typeof key === 'string'
277
+ ? key.toLowerCase()
278
+ : key;
279
+ return keyLowerCase;
280
+ }
281
+ }
282
+
283
+ class Registry {
284
+ /**
285
+ * Creates a new Registry instance.
286
+ * @param entries - Optional array of key-value pairs to initialize the registry.
287
+ */
288
+ constructor(entries, options = { caseInsensitive: false }) {
289
+ this.type = 'Registry';
290
+ /**
291
+ * Options for the registry.
292
+ */
293
+ this.options = {
294
+ caseInsensitive: false
295
+ };
296
+ this.options = options;
297
+ const caseInsensitive = typeof options?.['caseInsensitive'] === 'boolean' ? options['caseInsensitive'] : false;
298
+ this._registry = caseInsensitive
299
+ ? new CaseInsensitiveRegistry(entries)
300
+ : new BaseRegistry(entries);
301
+ }
302
+ /**
303
+ * Creates a new Registry instance from an object.
304
+ * @param object - The object containing key-value pairs.
305
+ * @returns A new Registry instance.
306
+ */
307
+ static fromObject(object, options = { caseInsensitive: false }) {
308
+ const entries = Object.entries(object);
309
+ const registry = new Registry(entries, options);
310
+ return registry;
311
+ }
312
+ /**
313
+ * Retrieves the value associated with the specified key.
314
+ * @param key - The key to retrieve the value for.
315
+ * @returns The value associated with the key, or undefined if the key does not exist.
316
+ */
317
+ get(key) {
318
+ return this._registry.get(key);
319
+ }
320
+ /**
321
+ * Sets the value for the specified key.
322
+ * @param key - The key to set the value for.
323
+ * @param value - The value to set.
324
+ */
325
+ set(key, value) {
326
+ this._registry.set(key, value);
327
+ }
328
+ /**
329
+ * Checks if the registry contains the specified key.
330
+ * @param key - The key to check.
331
+ * @returns True if the registry contains the key, false otherwise.
332
+ */
333
+ has(key) {
334
+ return this._registry.has(key);
335
+ }
336
+ /**
337
+ * Deletes the key-value pair associated with the specified key.
338
+ * @param key - The key to delete.
339
+ * @returns True if the key-value pair was deleted, false if the key does not exist.
340
+ */
341
+ delete(key) {
342
+ return this._registry.delete(key);
343
+ }
344
+ /**
345
+ * Clears all key-value pairs from the registry.
346
+ */
347
+ clear() {
348
+ this._registry.clear();
349
+ }
350
+ /**
351
+ * Gets the number of key-value pairs in the registry.
352
+ */
353
+ get size() {
354
+ return this._registry.size;
355
+ }
356
+ /**
357
+ * Gets an iterator for the keys in the registry.
358
+ */
359
+ get keys() {
360
+ return this._registry.keys;
361
+ }
362
+ /**
363
+ * Gets an iterator for the values in the registry.
364
+ */
365
+ get values() {
366
+ return this._registry.values;
367
+ }
368
+ /**
369
+ * Gets an iterator for the key-value pairs in the registry.
370
+ */
371
+ get entries() {
372
+ return this._registry.entries;
373
+ }
374
+ /**
375
+ * Executes a provided function once for each key-value pair in the registry.
376
+ * @param callbackfn - The function to execute for each key-value pair.
377
+ * @param thisArg - Optional value to use as `this` when executing the callback function.
378
+ */
379
+ forEach(callbackfn, thisArg) {
380
+ this._registry.forEach(callbackfn, thisArg);
381
+ }
382
+ /**
383
+ * Returns an iterator for the entries of the registry.
384
+ * @returns An iterator that yields key-value pairs of type [TKey, TValue].
385
+ */
386
+ [Symbol.iterator]() {
387
+ return this._registry.entries;
388
+ }
389
+ }
390
+
391
+ const defaultOptions = {
392
+ caseInsensitive: false,
393
+ namespace: '',
394
+ global: false,
395
+ thisArg: undefined
396
+ };
397
+ /**
398
+ * Represents a registry that stores key-value pairs within a specific scope.
399
+ * @template TKey The type of the keys in the registry.
400
+ * @template TValue The type of the values in the registry.
401
+ */
402
+ class ScopeRegistry {
403
+ /**
404
+ * Creates a new instance of ScopeRegistry.
405
+ * @param entries Optional. An array of key-value pairs to initialize the registry.
406
+ * @param options Optional. Options for the registry.
407
+ */
408
+ constructor(entries, options = defaultOptions) {
409
+ this.type = 'ScopeRegistry';
410
+ /**
411
+ * Options for the registry.
412
+ */
413
+ this.options = defaultOptions;
414
+ this.options = { ...defaultOptions, ...options };
415
+ this._registry = new Registry(entries, options);
416
+ }
417
+ /**
418
+ * Creates a new ScopeRegistry instance from an object.
419
+ * @param object The object containing key-value pairs.
420
+ * @param options Optional. Options for the registry.
421
+ * @returns A new ScopeRegistry instance.
422
+ */
423
+ static fromObject(object, options = defaultOptions) {
424
+ const registry = Registry.fromObject(object, options);
425
+ return registry;
426
+ }
427
+ /**
428
+ * Gets the value associated with the specified key.
429
+ * @param key The key to retrieve the value for.
430
+ * @returns The value associated with the key, or undefined if the key does not exist.
431
+ */
432
+ get(key) {
433
+ return this._registry.get(key);
434
+ }
435
+ /**
436
+ * Sets the value for the specified key.
437
+ * @param key The key to set the value for.
438
+ * @param value The value to set.
439
+ */
440
+ set(key, value) {
441
+ this._registry.set(key, value);
442
+ }
443
+ /**
444
+ * Checks if the registry contains the specified key.
445
+ * @param key The key to check.
446
+ * @returns True if the key exists in the registry, false otherwise.
447
+ */
448
+ has(key) {
449
+ return this._registry.has(key);
450
+ }
451
+ /**
452
+ * Deletes the key-value pair associated with the specified key.
453
+ * @param key The key to delete.
454
+ * @returns True if the key-value pair was deleted, false if the key does not exist.
455
+ */
456
+ delete(key) {
457
+ return this._registry.delete(key);
458
+ }
459
+ /**
460
+ * Clears all key-value pairs from the registry.
461
+ */
462
+ clear() {
463
+ this._registry.clear();
464
+ }
465
+ /**
466
+ * Gets the number of key-value pairs in the registry.
467
+ */
468
+ get size() {
469
+ return this._registry.size;
470
+ }
471
+ /**
472
+ * Gets an iterator for the keys in the registry.
473
+ */
474
+ get keys() {
475
+ return this._registry.keys;
476
+ }
477
+ /**
478
+ * Gets an iterator for the values in the registry.
479
+ */
480
+ get values() {
481
+ return this._registry.values;
482
+ }
483
+ /**
484
+ * Gets an iterator for the key-value pairs in the registry.
485
+ */
486
+ get entries() {
487
+ return this._registry.entries;
488
+ }
489
+ /**
490
+ * Executes a provided function once for each key-value pair in the registry.
491
+ * @param callbackfn The function to execute for each key-value pair.
492
+ * @param thisArg Optional. The value to use as `this` when executing the callback function.
493
+ */
494
+ forEach(callbackfn, thisArg) {
495
+ this._registry.forEach(callbackfn, thisArg);
496
+ }
497
+ /**
498
+ * Gets an iterator for the key-value pairs in the registry.
499
+ * @returns An iterator for the key-value pairs in the registry.
500
+ */
501
+ [Symbol.iterator]() {
502
+ return this._registry.entries;
503
+ }
504
+ }
505
+
506
+ /**
507
+ * Represents a generic queue data structure.
508
+ * @template T The type of elements stored in the queue.
509
+ */
510
+ class Queue {
511
+ /**
512
+ * Creates a new instance of the Queue class.
513
+ * @param _queue Optional initial array of elements.
514
+ */
515
+ constructor(_queue = []) {
516
+ this._queue = _queue;
517
+ }
518
+ /**
519
+ * Gets the number of elements in the queue.
520
+ */
521
+ get length() {
522
+ return this._queue.length;
523
+ }
524
+ /**
525
+ * Removes and returns the first element in the queue.
526
+ * @returns The first element in the queue, or undefined if the queue is empty.
527
+ */
528
+ dequeue() {
529
+ return this._queue.shift();
530
+ }
531
+ /**
532
+ * Adds an element to the end of the queue.
533
+ * @param value The element to add to the queue.
534
+ * @returns The new length of the queue.
535
+ */
536
+ enqueue(value) {
537
+ return this._queue.push(value);
538
+ }
539
+ /**
540
+ * Returns the first element in the queue without removing it.
541
+ * @returns The first element in the queue.
542
+ */
543
+ peek() {
544
+ return this._queue[0];
545
+ }
546
+ }
547
+
548
+ /**
549
+ * Represents a stack data structure.
550
+ * @template T The type of elements in the stack.
551
+ */
552
+ class Stack {
553
+ /**
554
+ * Creates a new instance of the Stack class.
555
+ * @param _stack An optional array to initialize the stack.
556
+ */
557
+ constructor(_stack = []) {
558
+ this._stack = _stack;
559
+ }
560
+ /**
561
+ * Gets the number of elements in the stack.
562
+ */
563
+ get length() {
564
+ return this._stack.length;
565
+ }
566
+ /**
567
+ * Returns the top element of the stack without removing it.
568
+ * @returns The top element of the stack.
569
+ */
570
+ peek() {
571
+ return this._stack[this._stack.length - 1];
572
+ }
573
+ /**
574
+ * Removes and returns the top element of the stack.
575
+ * @returns The top element of the stack, or undefined if the stack is empty.
576
+ */
577
+ pop() {
578
+ return this._stack.pop();
579
+ }
580
+ /**
581
+ * Adds an element to the top of the stack.
582
+ * @param value The element to be added to the stack.
583
+ * @returns The new length of the stack.
584
+ */
585
+ push(value) {
586
+ return this._stack.push(value);
587
+ }
588
+ /**
589
+ * Swaps the positions of the top two elements in the stack.
590
+ */
591
+ swap() {
592
+ const length = this._stack.length;
593
+ const temp = this._stack[length - 1];
594
+ this._stack[length - 1] = this._stack[length - 2];
595
+ this._stack[length - 2] = temp;
596
+ }
597
+ /**
598
+ * Returns the elements of the stack as an array in reverse order.
599
+ *
600
+ * @returns An array containing the elements of the stack in reverse order.
601
+ */
602
+ asArray() {
603
+ return [...this._stack].reverse();
604
+ }
605
+ }
606
+
607
+ /**
608
+ * Represents a scope that holds key-value pairs.
609
+ * @template TKey The type of the keys in the scope.
610
+ * @template TValue The type of the values in the scope.
611
+ */
612
+ class Scope {
613
+ /**
614
+ * Gets the registries in the scope.
615
+ */
616
+ get registries() {
617
+ return this._registries;
618
+ }
619
+ /**
620
+ * Creates a new scope.
621
+ * @param registries Optional array of registries to initialize the scope with.
622
+ */
623
+ constructor(registries) {
624
+ this._registries = [];
625
+ if (registries) {
626
+ this._registries = registries;
627
+ }
628
+ }
629
+ /**
630
+ * Adds a registry to the scope.
631
+ * @param registry The registry to add.
632
+ */
633
+ add(registry) {
634
+ this._registries.push(registry);
635
+ }
636
+ /**
637
+ * Gets the value associated with the specified key in the scope.
638
+ * @param key The key to search for.
639
+ * @param namespace Optional namespace to filter the search by.
640
+ * @returns An array containing the key and value if found, otherwise undefined.
641
+ */
642
+ get(key, namespace) {
643
+ const registries = this._registries.filter((registry) => {
644
+ if (registry.options.global) {
645
+ return true;
646
+ }
647
+ if (namespace && registry.options.namespace !== namespace) {
648
+ return false;
649
+ }
650
+ return true;
651
+ });
652
+ for (const registry of registries) {
653
+ if (registry.registry.has(key)) {
654
+ const value = registry.registry.get(key);
655
+ return [key, value];
656
+ }
657
+ }
658
+ return undefined;
659
+ }
660
+ }
661
+
662
+ /**
663
+ * Represents a cache implementation that stores key-value pairs.
664
+ * @template TValue The type of values stored in the cache.
665
+ */
666
+ class Cache {
667
+ /**
668
+ * Creates a new instance of the Cache class.
669
+ * @param _maxCacheSize The maximum size of the cache.
670
+ */
671
+ constructor(_maxCacheSize) {
672
+ this._maxCacheSize = _maxCacheSize;
673
+ this.type = 'Cache';
674
+ /**
675
+ * Options for the cache.
676
+ */
677
+ this.options = {
678
+ caseInsensitive: false
679
+ };
680
+ this._registry = new BaseRegistry();
681
+ this._keyQueue = [];
682
+ }
683
+ /**
684
+ * Gets the maximum size of the cache.
685
+ */
686
+ get maxCacheSize() {
687
+ return this._maxCacheSize;
688
+ }
689
+ /**
690
+ * Generates a hash key based on the given namespace and value.
691
+ * @param namespace The namespace for the key.
692
+ * @param value The value for the key.
693
+ * @returns The generated hash key.
694
+ */
695
+ getHashKey(namespace, value) {
696
+ const key = sha256(`${namespace}:${value}`);
697
+ return key;
698
+ }
699
+ /**
700
+ * Gets the value associated with the specified key.
701
+ * @param key The key to retrieve the value for.
702
+ * @returns The value associated with the key, or undefined if the key does not exist in the cache.
703
+ */
704
+ get(key) {
705
+ return this._registry.get(key);
706
+ }
707
+ /**
708
+ * Sets the value for the specified key in the cache.
709
+ * @param key The key to set the value for.
710
+ * @param value The value to set.
711
+ */
712
+ set(key, value) {
713
+ const index = this._keyQueue.indexOf(key);
714
+ if (index === -1) {
715
+ this._keyQueue.push(key);
716
+ }
717
+ while (this._keyQueue.length > this._maxCacheSize) {
718
+ const removeKey = this._keyQueue.shift();
719
+ if (removeKey) {
720
+ this._registry.delete(removeKey);
721
+ }
722
+ }
723
+ this._registry.set(key, value);
724
+ }
725
+ /**
726
+ * Checks if the cache contains the specified key.
727
+ * @param key The key to check.
728
+ * @returns True if the cache contains the key, false otherwise.
729
+ */
730
+ has(key) {
731
+ return this._registry.has(key);
732
+ }
733
+ /**
734
+ * Deletes the value associated with the specified key from the cache.
735
+ * @param key The key to delete.
736
+ * @returns True if the value was successfully deleted, false otherwise.
737
+ */
738
+ delete(key) {
739
+ const index = this._keyQueue.indexOf(key);
740
+ if (index > -1) {
741
+ this._keyQueue.splice(index, 1);
742
+ }
743
+ return this._registry.delete(key);
744
+ }
745
+ /**
746
+ * Clears the cache, removing all key-value pairs.
747
+ */
748
+ clear() {
749
+ this._keyQueue.length = 0;
750
+ this._registry.clear();
751
+ }
752
+ /**
753
+ * Gets the number of key-value pairs in the cache.
754
+ */
755
+ get size() {
756
+ return this._registry.size;
757
+ }
758
+ /**
759
+ * Gets an iterator for the keys in the cache.
760
+ */
761
+ get keys() {
762
+ return this._registry.keys;
763
+ }
764
+ /**
765
+ * Gets an iterator for the values in the cache.
766
+ */
767
+ get values() {
768
+ return this._registry.values;
769
+ }
770
+ /**
771
+ * Gets an iterator for the entries (key-value pairs) in the cache.
772
+ */
773
+ get entries() {
774
+ return this._registry.entries;
775
+ }
776
+ /**
777
+ * Executes a provided function once for each key-value pair in the cache.
778
+ * @param callbackfn The function to execute for each key-value pair.
779
+ * @param thisArg The value to use as `this` when executing the callback function.
780
+ */
781
+ forEach(callbackfn, thisArg) {
782
+ this._registry.forEach(callbackfn, thisArg);
783
+ }
784
+ /**
785
+ * Returns an iterator for the entries (key-value pairs) in the cache.
786
+ */
787
+ [Symbol.iterator]() {
788
+ return this._registry[Symbol.iterator]();
789
+ }
790
+ }
791
+
792
+ /**
793
+ * Converts a context object to a Context instance.
794
+ * @param context - The context object to convert.
795
+ * @param options - Optional evaluation options.
796
+ * @returns The converted Context instance.
797
+ */
798
+ const fromContext = (context, options) => {
799
+ const caseInsensitive = options?.caseInsensitive;
800
+ if (!context) {
801
+ return {};
802
+ }
803
+ else if (caseInsensitive && context instanceof Object) {
804
+ return Registry.fromObject(context, options);
805
+ }
806
+ else if (context instanceof Registry || context instanceof Object) {
807
+ return context;
808
+ }
809
+ else {
810
+ return context;
811
+ }
812
+ };
813
+ /**
814
+ * Retrieves the value associated with the specified key from the given context.
815
+ * If the context is an instance of Registry, it uses the `get` method to retrieve the value.
816
+ * If the context is an object, it accesses the value using the key as a property name.
817
+ * If the context is neither an instance of Registry nor an object, it returns undefined.
818
+ *
819
+ * @param context The context from which to retrieve the value.
820
+ * @param key The key associated with the value to retrieve.
821
+ * @returns The value associated with the specified key, or undefined if not found.
822
+ */
823
+ const getContextValue = (context, key) => {
824
+ if (context instanceof Registry) {
825
+ return context.get(key);
826
+ }
827
+ else if (context instanceof Object) {
828
+ return context[key];
829
+ }
830
+ else {
831
+ return undefined;
832
+ }
833
+ };
834
+ /**
835
+ * Sets a value in the given context object.
836
+ * If the context is an instance of Registry, the value is set using the key.
837
+ * If the context is a plain object, the value is set using the key as a property name.
838
+ * @param context - The context object.
839
+ * @param key - The key or property name.
840
+ * @param value - The value to be set.
841
+ */
842
+ const setContextValue = (context, key, value) => {
843
+ if (context instanceof Registry) {
844
+ const registry = context;
845
+ registry.set(key, value);
846
+ }
847
+ else if (context instanceof Object) {
848
+ const object = context;
849
+ object[key] = value;
850
+ }
851
+ };
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
+ /**
869
+ * @description
870
+ * Walks through the AST and extracts first expression.
871
+ * @param expr expression to parse
872
+ */
873
+ const extractExpression = (program) => {
874
+ if (program.body.length === 1) {
875
+ const expression = program.body[0];
876
+ if (expression.type === 'ExpressionStatement') {
877
+ return expression.expression;
878
+ }
879
+ }
880
+ return undefined;
881
+ };
882
+
883
+ const pushVisitorResult = (node, st, value) => {
884
+ const key = getKey(node);
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;
918
+ }
919
+ return undefined;
920
+ };
921
+
922
+ const afterVisitor = (node, st) => {
923
+ const options = st.options;
924
+ if (options?.['trackTime']) {
925
+ const t = performance.now();
926
+ console.time('after: ' + node.type);
927
+ return t;
928
+ }
929
+ return undefined;
930
+ };
931
+
932
+ const identifierVisitor = (node, st) => {
933
+ beforeVisitor(node, st);
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
+ }
1032
+ }
1033
+ return elements;
1034
+ };
1035
+
1036
+ const callExpressionVisitor = (node, st, callback) => {
1037
+ beforeVisitor(node, st);
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);
1045
+ }
1046
+ else {
1047
+ callback(node.callee, st);
1048
+ const caller = popVisitorResult(node, st);
1049
+ const value = !caller && node.optional
1050
+ ? undefined : caller.apply(st.context, args);
1051
+ pushVisitorResult(node, st, value);
1052
+ }
1053
+ afterVisitor(node, st);
1054
+ };
1055
+ const evaluateCall = (node, st, callback) => {
1056
+ callback(node.callee, st);
1057
+ const caller = popVisitorResult(node, st);
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);
1087
+ }
1088
+ const value = popVisitorResult(node, st);
1089
+ pushVisitorResult(node, st, value);
1090
+ afterVisitor(node, st);
1091
+ };
1092
+
1093
+ const unaryExpressionVisitor = (node, st, callback) => {
1094
+ beforeVisitor(node, st);
1095
+ callback(node.argument, st);
1096
+ const argument = popVisitorResult(node, st);
1097
+ const value = node.operator === '-' ? -argument :
1098
+ node.operator === '+' ? +argument :
1099
+ node.operator === '~' ? ~argument :
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;
1117
+ }
1118
+ else if (node.operator === '||' && left) {
1119
+ pushVisitorResult(node, st, true);
1120
+ afterVisitor(node, st);
1121
+ return;
1122
+ }
1123
+ callback(node.right, st);
1124
+ const right = popVisitorResult(node, st);
1125
+ const value = node.operator === '&&' ? left && right :
1126
+ node.operator === '||' ? left || right :
1127
+ undefined;
1128
+ pushVisitorResult(node, st, value);
1129
+ afterVisitor(node, st);
1130
+ };
1131
+
1132
+ const thisExpressionVisitor = (node, st) => {
1133
+ beforeVisitor(node, st);
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] ?? '';
1167
+ }
1168
+ return accumulator;
1169
+ }, '');
1170
+ pushVisitorResult(node, st, value);
1171
+ afterVisitor(node, st);
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);
1200
+ }
1201
+ }
1202
+ else {
1203
+ const pair = evalProperty(property, st, callback);
1204
+ map.set(pair.key, pair.value);
1205
+ }
1206
+ }
1207
+ const obj = Object.fromEntries(map);
1208
+ pushVisitorResult(node, st, obj);
1209
+ afterVisitor(node, st);
1210
+ };
1211
+ const evalProperty = (property, st, callback) => {
1212
+ // if (property.shorthand) {
1213
+ // callback(property.value, st);
1214
+ // const value = popVisitorResult(property, st);
1215
+ // return { key: value, value };
1216
+ // } else
1217
+ if (property.computed) {
1218
+ callback(property.key, st);
1219
+ const key = popVisitorResult(property, st);
1220
+ callback(property.value, st);
1221
+ const value = popVisitorResult(property, st);
1222
+ return { key, value };
1223
+ }
1224
+ else if (property.key.type === 'Identifier') {
1225
+ const key = property.key.name;
1226
+ callback(property.value, st);
1227
+ const value = popVisitorResult(property, st);
1228
+ return { key, value };
1229
+ }
1230
+ else if (property.key.type === 'Literal') {
1231
+ const key = property.key.value;
1232
+ callback(property.value, st);
1233
+ const value = popVisitorResult(property, st);
1234
+ return { key, value };
1235
+ }
1236
+ else {
1237
+ throw new Error(`Unsupported property key type: ${property.key.type}`);
1238
+ }
1239
+ };
1240
+
1241
+ /**
1242
+ * Returns the default visitors for the recursive traversal of an abstract syntax tree.
1243
+ * @returns The default visitors object.
1244
+ */
1245
+ const getDefaultVisitors = () => {
1246
+ const visitors = {};
1247
+ visitors['BinaryExpression'] = binaryExpressionVisitor;
1248
+ visitors['Identifier'] = identifierVisitor;
1249
+ visitors['Literal'] = literalVisitor;
1250
+ visitors['CallExpression'] = callExpressionVisitor;
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.`);
1298
+ }
1299
+ else if (node.left.type === 'Identifier') {
1300
+ const key = node.left.name;
1301
+ const value = func(left, right);
1302
+ st.context.set(key, value);
1303
+ pushVisitorResult(node, st, value);
1304
+ }
1305
+ else if (node.left.type === 'MemberExpression') {
1306
+ const [object, key,] = evaluateMember(node.left, st, callback);
1307
+ const value = func(left, right);
1308
+ object[key] = value;
1309
+ pushVisitorResult(node, st, value);
1310
+ }
1311
+ afterVisitor(node, st);
1312
+ };
1313
+
1314
+ const updateOperators = {
1315
+ '++': (value) => { return value + 1; },
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);
1325
+ }
1326
+ else if (node.argument.type === 'MemberExpression') {
1327
+ callback(node.argument, st);
1328
+ const [, property, value] = evaluateMember(node.argument, st, callback);
1329
+ const newValue = updateOperators[node.operator](value);
1330
+ st.context?.set(property, newValue);
1331
+ pushVisitorResult(node, st, node.prefix ? newValue : value);
1332
+ }
1333
+ afterVisitor(node, st);
1334
+ };
1335
+
1336
+ const arrowFunctionExpressionVisitor = (node, st, callback) => {
1337
+ beforeVisitor(node, st);
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);
1408
+ }
1409
+ return {};
1410
+ };
1411
+ const evaluateIdentifier = (pattern, st, arg) => {
1412
+ const object = {};
1413
+ object[pattern.name] = arg;
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.');
1420
+ }
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
+ /**
1585
+ * Constructs a new instance of the ParserService class.
1586
+ */
1587
+ constructor() {
1588
+ super();
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
+ }
1598
+ }
1599
+ /**
1600
+ * @description
1601
+ * Returns acorn version.
1602
+ */
1603
+ get version() {
1604
+ return version;
1605
+ }
1606
+ /**
1607
+ * Gets the parser options.
1608
+ * @returns The parser options.
1609
+ */
1610
+ get options() {
1611
+ return this.parserOptions;
1612
+ }
1613
+ /**
1614
+ * @description
1615
+ * Parses expression and returns ES6/ES2023 AST.
1616
+ * @param expr expression to parse
1617
+ * @param options optional parser options
1618
+ * @returns The parsed AST (Abstract Syntax Tree).
1619
+ */
1620
+ parse(expr, options) {
1621
+ if (expr) {
1622
+ try {
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;
1651
+ }
1652
+ return undefined;
1653
+ }
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
+ /**
1669
+ * Gets the original registry.
1670
+ */
1671
+ get original() {
1672
+ return this._original;
1673
+ }
1674
+ /**
1675
+ * Gets the scopes registry.
1676
+ */
1677
+ get priorScopes() {
1678
+ return this._priorScopes;
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 ?? {});
1723
+ }
1724
+ /**
1725
+ * Gets the value associated with the specified key from the evaluation context.
1726
+ * @param key - The key to retrieve the value for.
1727
+ * @returns The value associated with the key, or undefined if not found.
1728
+ */
1729
+ get(key) {
1730
+ for (const scope of this._scopes.asArray()) {
1731
+ const value = getContextValue(scope, key);
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;
1755
+ }
1756
+ /**
1757
+ * Retrieves the `this` value of the specified key from the evaluation context.
1758
+ * The value is searched in the original context, prior scopes, and lookup functions.
1759
+ * @param key - The key to retrieve the value for.
1760
+ * @returns The `this` value associated with the key, or undefined if not found.
1761
+ */
1762
+ getThis(key) {
1763
+ if (this._original) {
1764
+ const value = getContextValue(this._original, key);
1765
+ if (value !== undefined) {
1766
+ return this._original;
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;
1782
+ }
1783
+ /**
1784
+ * Sets a key-value pair in the context.
1785
+ * If the original context is a Registry, the key-value pair is set using the Registry's set method.
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.
1789
+ */
1790
+ set(key, value) {
1791
+ if (this._original && this._original instanceof Registry) {
1792
+ const registry = this._original;
1793
+ registry.set(key, value);
1794
+ }
1795
+ else if (this._original && this._original instanceof Object) {
1796
+ const obj = this._original;
1797
+ obj[key] = value;
1798
+ }
1799
+ }
1800
+ /**
1801
+ * Pushes a context to the scope stack.
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();
1814
+ }
1815
+ }
1816
+
1817
+ /**
1818
+ * Represents the result of an evaluation.
1819
+ */
1820
+ class EvalResult {
1821
+ /**
1822
+ * Gets the stack of evaluated values.
1823
+ */
1824
+ get stack() {
1825
+ return this._stack;
1826
+ }
1827
+ /**
1828
+ * Gets the evaluated value.
1829
+ */
1830
+ get value() {
1831
+ return this._value;
1832
+ }
1833
+ /**
1834
+ * Gets the error that occurred during evaluation.
1835
+ */
1836
+ get error() {
1837
+ return this._error;
1838
+ }
1839
+ /**
1840
+ * Gets the error message associated with the evaluation error.
1841
+ */
1842
+ get errorMessage() {
1843
+ return this._errorMessage;
1844
+ }
1845
+ /**
1846
+ * Gets whether the evaluation resulted in an error.
1847
+ */
1848
+ get isError() {
1849
+ return this._isError;
1850
+ }
1851
+ /**
1852
+ * Gets whether the evaluation was successful.
1853
+ */
1854
+ get isSuccess() {
1855
+ return this._isSuccess;
1856
+ }
1857
+ /**
1858
+ * Gets whether the evaluated value is undefined.
1859
+ */
1860
+ get isUndefined() {
1861
+ return this._isUndefined;
1862
+ }
1863
+ /**
1864
+ * Gets the trace of evaluated values.
1865
+ */
1866
+ get trace() {
1867
+ return this._trace;
1868
+ }
1869
+ /**
1870
+ * Gets the evaluation context.
1871
+ */
1872
+ get context() {
1873
+ return this._context;
1874
+ }
1875
+ /**
1876
+ * Gets the evaluation options.
1877
+ */
1878
+ get options() {
1879
+ return this._context.options;
1880
+ }
1881
+ /**
1882
+ * Gets the duration of the evaluation result.
1883
+ * @returns The duration in milliseconds.
1884
+ */
1885
+ get duration() {
1886
+ if (!this._startDate || !this._endDate) {
1887
+ return undefined;
1888
+ }
1889
+ return this._endDate - this._startDate;
1890
+ }
1891
+ /**
1892
+ * Creates a new instance of EvalResult.
1893
+ * @param trace The trace of evaluated values.
1894
+ * @param context The evaluation context.
1895
+ */
1896
+ constructor(context) {
1897
+ this._stack = new Stack();
1898
+ this._trace = new Registry;
1899
+ this._context = context;
1900
+ }
1901
+ /**
1902
+ * Starts the evaluation process.
1903
+ */
1904
+ start() {
1905
+ this._startDate = performance.now();
1906
+ }
1907
+ /**
1908
+ * Stops the evaluation and records the end date.
1909
+ */
1910
+ stop() {
1911
+ this._endDate = performance.now();
1912
+ return this.duration;
1913
+ }
1914
+ /**
1915
+ * Sets the success value of the evaluation result.
1916
+ *
1917
+ * @param value The value to set as the success value.
1918
+ * @returns void
1919
+ */
1920
+ setSuccess(value) {
1921
+ this._value = value;
1922
+ this._isSuccess = true;
1923
+ this._isError = false;
1924
+ this._isUndefined = false;
1925
+ }
1926
+ /**
1927
+ * Sets the failure state of the evaluation result.
1928
+ * @param error The error object associated with the failure.
1929
+ */
1930
+ setFailure(error) {
1931
+ this._error = error;
1932
+ if (error instanceof Error) {
1933
+ this._errorMessage = error.message;
1934
+ }
1935
+ this._isSuccess = false;
1936
+ this._isError = true;
1937
+ this._isUndefined = false;
1938
+ }
1939
+ }
1940
+
1941
+ /**
1942
+ * Represents the evaluation state, which includes the context, result, and options.
1943
+ */
1944
+ class EvalState {
1945
+ /**
1946
+ * Gets the evaluation context.
1947
+ */
1948
+ get context() {
1949
+ return this._context;
1950
+ }
1951
+ /**
1952
+ * Gets the evaluation result.
1953
+ */
1954
+ get result() {
1955
+ return this._result;
1956
+ }
1957
+ /**
1958
+ * Gets the evaluation options.
1959
+ */
1960
+ get options() {
1961
+ return this._options;
1962
+ }
1963
+ /**
1964
+ * Gets whether the evaluation is asynchronous.
1965
+ */
1966
+ get isAsync() {
1967
+ return this._isAsync;
1968
+ }
1969
+ /**
1970
+ * Represents the state of an evaluation.
1971
+ * @param context The evaluation context.
1972
+ * @param result The evaluation result.
1973
+ * @param options The evaluation options.
1974
+ * @param isAsync Indicates whether the evaluation is asynchronous.
1975
+ */
1976
+ constructor(context, result, options, isAsync) {
1977
+ this._context = context;
1978
+ this._result = result;
1979
+ this._options = options;
1980
+ this._isAsync = isAsync;
1981
+ }
1982
+ /**
1983
+ * Creates an instance of EvalState from a given context, options, and async flag.
1984
+ *
1985
+ * @param context - The evaluation context or context object.
1986
+ * @param options - The evaluation options.
1987
+ * @param isAsync - A flag indicating whether the evaluation is asynchronous.
1988
+ * @returns The created EvalState instance.
1989
+ */
1990
+ static fromContext(context, options, isAsync) {
1991
+ const ctx = EvalContext.fromContext(context, options);
1992
+ const result = new EvalResult(ctx);
1993
+ const state = new EvalState(ctx, result, options, isAsync);
1994
+ return state;
1995
+ }
1996
+ }
1997
+
1998
+ /**
1999
+ * Service for evaluating and parsing expressions.
2000
+ */
2001
+ class EvalService extends BaseEval {
2002
+ /**
2003
+ * Constructs a new instance of the EvalService class.
2004
+ * @param parserService The parser service used for parsing expressions.
2005
+ */
2006
+ constructor(parserService) {
2007
+ super();
2008
+ this.parserService = parserService;
2009
+ this.parserOptions = {
2010
+ ...defaultOptions$1,
2011
+ ecmaVersion: 2020,
2012
+ extractExpressions: false,
2013
+ cacheSize: 100
2014
+ };
2015
+ }
2016
+ /**
2017
+ * Parses the given expression into an abstract syntax tree (AST).
2018
+ * @param expression The expression to parse.
2019
+ * @returns The AST representing the expression.
2020
+ */
2021
+ parse(expression) {
2022
+ if (!expression) {
2023
+ return undefined;
2024
+ }
2025
+ else if (typeof expression === 'string') {
2026
+ const ast = this.parserService.parse(expression, this.parserOptions);
2027
+ return ast;
2028
+ }
2029
+ else {
2030
+ return expression;
2031
+ }
2032
+ }
2033
+ /**
2034
+ * Evaluates the given expression.
2035
+ * @param expression The expression to evaluate.
2036
+ * @param context The evaluation context.
2037
+ * @param options The evaluation options.
2038
+ * @returns The result of the evaluation.
2039
+ */
2040
+ eval(expression, context, options) {
2041
+ try {
2042
+ const ast = this.parse(expression);
2043
+ const state = EvalState.fromContext(context, options);
2044
+ const value = evaluate(ast, state);
2045
+ return value;
2046
+ }
2047
+ catch (error) {
2048
+ if (error instanceof Error) {
2049
+ throw new Error(error.message);
2050
+ }
2051
+ else {
2052
+ throw error;
2053
+ }
2054
+ }
2055
+ }
2056
+ /**
2057
+ * Asynchronously evaluates the given expression.
2058
+ * @param expression The expression to evaluate.
2059
+ * @param context The evaluation context.
2060
+ * @param options The evaluation options.
2061
+ * @returns A promise that resolves to the result of the evaluation.
2062
+ */
2063
+ evalAsync(expression, context, options) {
2064
+ try {
2065
+ const ast = this.parse(expression);
2066
+ const state = EvalState.fromContext(context, options);
2067
+ const promise = evaluateAsync(ast, state);
2068
+ return promise;
2069
+ }
2070
+ catch (error) {
2071
+ if (error instanceof Error) {
2072
+ throw new Error(error.message);
2073
+ }
2074
+ else {
2075
+ throw error;
2076
+ }
2077
+ }
2078
+ }
2079
+ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: EvalService, deps: [{ token: ParserService }], target: i0.ɵɵFactoryTarget.Injectable }); }
2080
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: EvalService, providedIn: 'root' }); }
2081
+ }
2082
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: EvalService, decorators: [{
2083
+ type: Injectable,
2084
+ args: [{
2085
+ providedIn: 'root'
2086
+ }]
2087
+ }], ctorParameters: () => [{ type: ParserService }] });
2088
+
2089
+ /**
2090
+ * CompilerService class that extends BaseEval.
2091
+ * Provides methods for compiling and evaluating expressions.
2092
+ */
2093
+ class CompilerService extends BaseEval {
2094
+ constructor(parserService, evalService) {
2095
+ super();
2096
+ this.parserService = parserService;
2097
+ 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
+ }
2121
+ }
2122
+ /**
2123
+ * Compiles the given expression into a state callback function.
2124
+ * @param expression - The expression to compile.
2125
+ * @returns The compiled state callback function.
2126
+ * @throws Error if there is an error during compilation.
2127
+ */
2128
+ compile(expression) {
2129
+ try {
2130
+ const ast = this.parse(expression);
2131
+ const fn = compile(ast);
2132
+ return fn;
2133
+ }
2134
+ catch (error) {
2135
+ if (error instanceof Error) {
2136
+ throw new Error(error.message);
2137
+ }
2138
+ throw new Error('error in compile');
2139
+ }
2140
+ }
2141
+ /**
2142
+ * Calls the given state callback function with the specified context and options.
2143
+ * @param fn - The state callback function to call.
2144
+ * @param context - The evaluation context or context object.
2145
+ * @param options - The evaluation options.
2146
+ * @returns The result of the evaluation.
2147
+ * @throws Error if there is an error during the evaluation.
2148
+ */
2149
+ call(fn, context, options) {
2150
+ if (fn) {
2151
+ try {
2152
+ const state = EvalState.fromContext(context, options);
2153
+ const value = call(fn, state);
2154
+ return value;
2155
+ }
2156
+ catch (error) {
2157
+ if (error instanceof Error) {
2158
+ throw new Error(error.message);
2159
+ }
2160
+ else {
2161
+ throw new Error('call');
2162
+ }
2163
+ }
2164
+ }
2165
+ return undefined;
2166
+ }
2167
+ /**
2168
+ * Calls the given asynchronous state callback function with the specified context and options.
2169
+ * @param fn - The asynchronous state callback function to call.
2170
+ * @param context - The evaluation context or context object.
2171
+ * @param options - The evaluation options.
2172
+ * @returns A promise that resolves to the result of the evaluation.
2173
+ * @throws Error if there is an error during the evaluation.
2174
+ */
2175
+ async callAsync(fn, context, options) {
2176
+ if (fn) {
2177
+ try {
2178
+ const state = EvalState.fromContext(context, options);
2179
+ const promise = callAsync(fn, state);
2180
+ const value = await promise;
2181
+ return value;
2182
+ }
2183
+ catch (error) {
2184
+ if (error instanceof Error) {
2185
+ throw new Error(error.message);
2186
+ }
2187
+ throw new Error('error in callAsync');
2188
+ }
2189
+ }
2190
+ return undefined;
2191
+ }
2192
+ /**
2193
+ * Compiles the given expression into an asynchronous state callback function.
2194
+ * @param expression - The expression to compile.
2195
+ * @returns The compiled asynchronous state callback function.
2196
+ * @throws Error if there is an error during compilation.
2197
+ */
2198
+ compileAsync(expression) {
2199
+ try {
2200
+ const ast = this.parse(expression);
2201
+ const fn = compileAsync(ast);
2202
+ return fn;
2203
+ }
2204
+ catch (error) {
2205
+ if (error instanceof Error) {
2206
+ throw new Error(error.message);
2207
+ }
2208
+ throw new Error('error in compileAsync');
2209
+ }
2210
+ }
2211
+ 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
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: CompilerService, providedIn: 'root' }); }
2213
+ }
2214
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: CompilerService, decorators: [{
2215
+ type: Injectable,
2216
+ args: [{
2217
+ providedIn: 'root'
2218
+ }]
2219
+ }], ctorParameters: () => [{ type: ParserService }, { type: EvalService }] });
2220
+
2221
+ /**
2222
+ * Service responsible for discovering nodes in an abstract syntax tree (AST).
2223
+ */
2224
+ class DiscoveryService extends BaseEval {
2225
+ /**
2226
+ * Creates a new instance of the DiscoveryService class.
2227
+ * @param parserService The parser service to be used.
2228
+ */
2229
+ constructor(parserService) {
2230
+ super();
2231
+ this.parserService = parserService;
2232
+ this.parserOptions = {
2233
+ ...defaultOptions$1,
2234
+ ecmaVersion: 2020,
2235
+ extractExpressions: false,
2236
+ cacheSize: undefined
2237
+ };
2238
+ }
2239
+ /**
2240
+ * Finds all nodes in the AST that match the given expression and search type.
2241
+ * @param expression The expression to search for. Can be a string or an AST node.
2242
+ * @param searchType The type of nodes to search for.
2243
+ * @param options Optional parser options.
2244
+ * @returns An array of nodes that match the search criteria, or undefined if no nodes are found.
2245
+ * @throws If an error occurs during the search process.
2246
+ */
2247
+ extract(expression, searchType) {
2248
+ try {
2249
+ const ast = this.parse(expression);
2250
+ const value = extract(ast, searchType);
2251
+ return value;
2252
+ }
2253
+ catch (error) {
2254
+ if (error instanceof Error) {
2255
+ throw new Error(error.message);
2256
+ }
2257
+ else {
2258
+ throw error;
2259
+ }
2260
+ }
2261
+ }
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
+ 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
+ static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DiscoveryService, providedIn: 'root' }); }
2281
+ }
2282
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DiscoveryService, decorators: [{
2283
+ type: Injectable,
2284
+ args: [{
2285
+ providedIn: 'root'
2286
+ }]
2287
+ }], ctorParameters: () => [{ type: ParserService }] });
2288
+
2289
+ var RecursiveVisitorResultType;
2290
+ (function (RecursiveVisitorResultType) {
2291
+ RecursiveVisitorResultType[RecursiveVisitorResultType["Stack"] = 0] = "Stack";
2292
+ RecursiveVisitorResultType[RecursiveVisitorResultType["Registry"] = 1] = "Registry";
2293
+ })(RecursiveVisitorResultType || (RecursiveVisitorResultType = {}));
2294
+
2295
+ /**
2296
+ * Generated bundle index. Do not edit.
2297
+ */
2298
+
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 as get, parse };
2300
+ //# sourceMappingURL=zvenigora-ng-eval-core.mjs.map