@zvenigora/ng-eval-core 0.10.1 → 0.11.0

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/CHANGELOG.md CHANGED
@@ -14,6 +14,91 @@ Versions 0.1.104–0.1.107 are on npm without entries here.
14
14
 
15
15
  ---
16
16
 
17
+ ## [0.11.0] - 2026-10-04
18
+
19
+ **A security fix: an expression no longer resolves a name on the prototype-pollution blocklist
20
+ against its context. Also, the member-write policy is asked about built-in methods that write,
21
+ and seventeen unused exports are removed.** A breaking minor, in three ways. An expression that
22
+ names such a key of its context now throws ([B6](../../docs/backlog-retired.md#b6)). An import of
23
+ a removed symbol breaks ([B5](../../docs/backlog-retired.md#b5)). And a context that implements
24
+ `checkMemberWrite` is asked about calls it was not asked about before
25
+ ([C4](../../docs/backlog-retired.md#c4)). The one other change to an exported symbol is an
26
+ addition, `EvalMemberWrite.method`. The `.d.ts` loses the seventeen, gains `method`, and changes
27
+ documentation comments: `checkMemberWrite` loses the paragraph that said a method call was outside
28
+ what it sees. `eval-signals` 0.4.0 builds on the change and requires this version.
29
+
30
+ ### Security
31
+ - **An expression no longer resolves a name on the prototype-pollution blocklist against its
32
+ context — [B6](../../docs/backlog-retired.md#b6).** An identifier is resolved against the
33
+ context, and for a context built from a plain object or a class instance the lookup was a bare
34
+ property access, so an identifier resolved the members the context inherits from
35
+ `Object.prototype`; so did `this.k` and `this["k"]`. The blocklist guarded member access to the
36
+ objects an expression holds, not resolution against the context, and through `Object`'s own
37
+ static functions an expression could then modify shared prototypes. Every published version
38
+ up to 0.10.0 is affected.
39
+ - **Now refused before any lookup**, with the error the member visitor throws for any other
40
+ object, `Access to dangerous property "constructor" is blocked for security reasons`:
41
+ `__proto__`, `constructor`, `prototype`, the four `__define…` and `__lookup…` accessors,
42
+ `hasOwnProperty`, `isPrototypeOf`, `propertyIsEnumerable`, `toString`, `valueOf` and
43
+ `toLocaleString`, as an identifier, as `this.k` or `this["k"]`, or as a member of an
44
+ `EvalScope` held in the context. This applies over every kind of context: a plain object, a
45
+ `Registry`, a class instance, an `EvalContext`, a prior scope or a signal context. Under
46
+ `caseInsensitive` the key the lookup matched is checked too, so `CONSTRUCTOR` over a context
47
+ that holds `constructor` is refused.
48
+ - **A context key with one of those names can no longer be read by an expression**, even where
49
+ the context holds it as a key of its own, as a `Registry` or a signal context's source can.
50
+ Rename the key. An arrow parameter or a `let` with one of these names was already refused.
51
+
52
+ ### Added
53
+ - **`EvalMemberWrite.method`**, optional: the built-in method about to make a write, named as
54
+ the specification names it, `'Array.prototype.push'` or `'Object.assign'`. Undefined for an
55
+ assignment or an update. [C4](../../docs/backlog-retired.md#c4).
56
+
57
+ ### Changed
58
+ - **A context that implements `checkMemberWrite` is also asked before a built-in method writes
59
+ into an object it is handed — [C4](../../docs/backlog-retired.md#c4).** The call visitor asks
60
+ before calling `Array`'s `copyWithin`, `fill`, `pop`, `push`, `reverse`, `shift`, `sort`,
61
+ `splice` or `unshift`, the typed arrays' `copyWithin`, `fill`, `reverse`, `set` or `sort`,
62
+ `Map`'s and `WeakMap`'s `set` and `delete`, `Map`'s and `Set`'s `clear`, `Set`'s and
63
+ `WeakSet`'s `add` and `delete`, a `Date` setter, or, when a context supplies `Object`, its
64
+ `assign`, `defineProperty`, `defineProperties`, `setPrototypeOf`, `freeze`, `seal` or
65
+ `preventExtensions`. Each is matched by identity, so a method of your own named `push` is not
66
+ asked about, and `Array.prototype.push` reached under another name is. The target is the object
67
+ the method writes into: its receiver, which is the context itself for a bare call, or, for
68
+ `Object`'s, its first argument. Reached through `Function.prototype.call`, `apply` or `bind`, a
69
+ method is asked about as a direct call of it, with the `this` they pass as its receiver
70
+ ([C5](../../docs/backlog-retired.md#c5)); `bind` is asked when it binds, so `[].push.bind(a)`
71
+ is refused before the bound function exists. `createdByEvaluation` comes from the same record
72
+ as for a member write, so `[...a].sort()` on a copy the walk made is allowed; `key` is
73
+ undefined. Throwing refuses the call, and it is not made. A context that does not implement the
74
+ method pays one field read per call, and nothing else changes for it.
75
+ - **Not asked about:** `test` and `exec` on a regex, which advance a global or sticky one's
76
+ `lastIndex`; and a method you wrote.
77
+ - `checkMemberWrite`'s JSDoc no longer says a method call is outside what it sees, and
78
+ `EvalMemberWrite`'s says what `target` and `key` are for a method call.
79
+
80
+ ### Removed
81
+ - **Seventeen exports nothing in the library used — [B5](../../docs/backlog-retired.md#b5).**
82
+ No non-spec source in the three packages referenced any of them beyond its own declaration and
83
+ the barrels. Removing an export breaks a consumer that imports it, so this is a breaking minor;
84
+ nothing else about the library changes.
85
+ - **`RecursiveVisitorState`** and **`RecursiveVisitorResult`**, `@deprecated` since 0.3.0. Use
86
+ `EvalHooks` to observe a walk, and `EvalState` and `EvalResult` for its state and result.
87
+ - **`ScopeOptions`**. Use `EvalScopeOptions`, which is what `EvalScope` takes.
88
+ - **`ScopeType`** and **`RegistryOptionType`**, which nothing implemented. No replacement.
89
+ - **`RecursiveVisitorOptions`**, **`RecursiveVisitorContext`**,
90
+ **`RecursiveVisitorResultType`**, **`RecursiveVisitorStackResult`**,
91
+ **`RecursiveVisitorRegistryResult`**, **`RecursiveVisitor`**, **`RecursiveAggregateVisitor`**
92
+ and **`AggregateType`**, the rest of the visitor vocabulary those two belonged to. No
93
+ replacement: the visitors are not public.
94
+ - **`Queue`** and **`QueueType`**. No replacement; an array's `push` and `shift` do the same.
95
+ - **`getContextKey`**. No replacement. `EvalContext.getKey` answers the question for a whole
96
+ context, and unlike this function it answers `undefined` for a key nothing holds.
97
+ - **`isRegistryContext`**. No replacement; `context instanceof Registry` is the test the
98
+ library itself uses.
99
+
100
+ ---
101
+
17
102
  ## [0.10.1] - 2026-10-05
18
103
 
19
104
  **A security fix: an expression no longer resolves a name on the prototype-pollution blocklist
@@ -47,7 +132,7 @@ their peer ranges.
47
132
 
48
133
  ---
49
134
 
50
- ## [0.10.0] - 2026-10-03
135
+ ## [0.10.0] - 2026-10-04
51
136
 
52
137
  **An opt-in member-write policy on `EvalContext`, and three fixes.** Nothing changes for a context
53
138
  that does not opt in, and every change to an exported symbol is an addition: `EvalContext` gains
@@ -400,48 +400,6 @@ class Registry {
400
400
  }
401
401
  }
402
402
 
403
- /**
404
- * Represents a generic queue data structure.
405
- * @template T The type of elements stored in the queue.
406
- */
407
- class Queue {
408
- /**
409
- * Creates a new instance of the Queue class.
410
- * @param _queue Optional initial array of elements.
411
- */
412
- constructor(_queue = []) {
413
- this._queue = _queue;
414
- }
415
- /**
416
- * Gets the number of elements in the queue.
417
- */
418
- get length() {
419
- return this._queue.length;
420
- }
421
- /**
422
- * Removes and returns the first element in the queue.
423
- * @returns The first element in the queue, or undefined if the queue is empty.
424
- */
425
- dequeue() {
426
- return this._queue.shift();
427
- }
428
- /**
429
- * Adds an element to the end of the queue.
430
- * @param value The element to add to the queue.
431
- * @returns The new length of the queue.
432
- */
433
- enqueue(value) {
434
- return this._queue.push(value);
435
- }
436
- /**
437
- * Returns the first element in the queue without removing it.
438
- * @returns The first element in the queue.
439
- */
440
- peek() {
441
- return this._queue[0];
442
- }
443
- }
444
-
445
403
  /**
446
404
  * Represents a stack data structure with proper memory management.
447
405
  * @template T The type of elements in the stack.
@@ -675,84 +633,6 @@ class Cache {
675
633
  }
676
634
  }
677
635
 
678
- /**
679
- * Strips the `ParenthesizedExpression` wrappers acorn keeps around a node
680
- * under `preserveParens`, so `(a)` and `((a))` are handled as `a`. A walk
681
- * passes through them without help, since `acorn-walk`'s base visitor does;
682
- * only a visitor that branches on a child's *type* has to unwrap it first.
683
- * @param node - The node, possibly parenthesised.
684
- * @returns The innermost node that is not a `ParenthesizedExpression`.
685
- */
686
- const unwrapParentheses = (node) => {
687
- let inner = node;
688
- while (inner.type === 'ParenthesizedExpression') {
689
- inner = inner.expression;
690
- }
691
- return inner;
692
- };
693
- /**
694
- * Retrieves the key-value pair from an object based on the provided key.
695
- * @param obj - The object to search for the key-value pair.
696
- * @param key - The key to search for in the object.
697
- * @param caseInsesitive - Specifies whether the key comparison should be case-insensitive.
698
- * @returns The key-value pair as an array, or undefined if the key is not found.
699
- */
700
- const getKeyValue = (obj, key, caseInsesitive) => {
701
- if (!caseInsesitive || typeof key !== 'string') {
702
- const value = obj[key];
703
- return [key, value];
704
- }
705
- if (typeof key === 'string') {
706
- let currentObj = obj;
707
- do {
708
- const keys = Object.getOwnPropertyNames(currentObj);
709
- if (Array.isArray(keys)) {
710
- const foundKey = keys.find(k => equalIgnoreCase(key, k));
711
- if (foundKey) {
712
- const value = currentObj[foundKey];
713
- return [foundKey, value];
714
- }
715
- }
716
- } while ((currentObj = Object.getPrototypeOf(currentObj)));
717
- }
718
- return undefined;
719
- };
720
- /**
721
- * Compares two values for equality, ignoring case sensitivity.
722
- * @param a - The first value to compare.
723
- * @param b - The second value to compare.
724
- * @returns `true` if the values are equal, `false` otherwise.
725
- */
726
- const equalIgnoreCase = (a, b) => {
727
- if (typeof a === 'number' || typeof b === 'number') {
728
- return a === b;
729
- }
730
- else if (typeof a === 'symbol' || typeof b === 'symbol') {
731
- return a === b;
732
- }
733
- else if (typeof a === 'string' || typeof b === 'string') {
734
- return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0;
735
- }
736
- return undefined;
737
- };
738
- /**
739
- * Retrieves the value from an object, ignoring the case of the key.
740
- * @param obj - The object to retrieve the value from.
741
- * @param key - The key to search for, ignoring the case.
742
- * @returns The value associated with the key, or undefined if the key is not found.
743
- */
744
- const getValueIgnoreCase = (obj, key) => {
745
- const pair = getKeyValue(obj, key, true);
746
- if (pair) {
747
- return pair[1];
748
- }
749
- return undefined;
750
- };
751
-
752
- function isRegistryContext(context) {
753
- const value = ['Registry', 'CaseInsensitiveRegistry', 'BaseRegistry'].includes(context?.type);
754
- return value;
755
- }
756
636
  /**
757
637
  * Converts a context object to a Context instance.
758
638
  * @param context - The context object to convert.
@@ -813,33 +693,6 @@ const setContextValue = (context, key, value) => {
813
693
  object[key] = value;
814
694
  }
815
695
  };
816
- /**
817
- * Retrieves the key from the given context object.
818
- * If the context is an instance of Registry, it searches for the key in a case-insensitive manner if specified.
819
- * If the context is an instance of Object, it uses the getKeyValue function to retrieve the key-value pair.
820
- * @param context - The context object.
821
- * @param key - The key to retrieve.
822
- * @param caseInsensitive - Specifies whether the search should be case-insensitive (only applicable for Registry instances).
823
- * @returns The retrieved key, or undefined if the key is not found.
824
- */
825
- const getContextKey = (context, key, caseInsensitive) => {
826
- if (context instanceof Registry) {
827
- const keys = [...context.keys]; // : (string | number | symbol)[]
828
- const foundKey = caseInsensitive && typeof key === 'string'
829
- ? keys.find(k => equalIgnoreCase(key, k))
830
- : keys.find(k => k === key);
831
- if (foundKey) {
832
- return foundKey;
833
- }
834
- }
835
- else if (context instanceof Object) {
836
- const pair = getKeyValue(context, key, caseInsensitive);
837
- if (pair) {
838
- return pair[0];
839
- }
840
- }
841
- return undefined;
842
- };
843
696
 
844
697
  /**
845
698
  * The two push helpers each carry their own trace-bound call rather than
@@ -922,11 +775,79 @@ const afterVisitor = (node, st) => {
922
775
  st.hooks.dispatch('after', node, st, value);
923
776
  };
924
777
 
925
- var RecursiveVisitorResultType;
926
- (function (RecursiveVisitorResultType) {
927
- RecursiveVisitorResultType[RecursiveVisitorResultType["Stack"] = 0] = "Stack";
928
- RecursiveVisitorResultType[RecursiveVisitorResultType["Registry"] = 1] = "Registry";
929
- })(RecursiveVisitorResultType || (RecursiveVisitorResultType = {}));
778
+ /**
779
+ * Strips the `ParenthesizedExpression` wrappers acorn keeps around a node
780
+ * under `preserveParens`, so `(a)` and `((a))` are handled as `a`. A walk
781
+ * passes through them without help, since `acorn-walk`'s base visitor does;
782
+ * only a visitor that branches on a child's *type* has to unwrap it first.
783
+ * @param node - The node, possibly parenthesised.
784
+ * @returns The innermost node that is not a `ParenthesizedExpression`.
785
+ */
786
+ const unwrapParentheses = (node) => {
787
+ let inner = node;
788
+ while (inner.type === 'ParenthesizedExpression') {
789
+ inner = inner.expression;
790
+ }
791
+ return inner;
792
+ };
793
+ /**
794
+ * Retrieves the key-value pair from an object based on the provided key.
795
+ * @param obj - The object to search for the key-value pair.
796
+ * @param key - The key to search for in the object.
797
+ * @param caseInsesitive - Specifies whether the key comparison should be case-insensitive.
798
+ * @returns The key-value pair as an array, or undefined if the key is not found.
799
+ */
800
+ const getKeyValue = (obj, key, caseInsesitive) => {
801
+ if (!caseInsesitive || typeof key !== 'string') {
802
+ const value = obj[key];
803
+ return [key, value];
804
+ }
805
+ if (typeof key === 'string') {
806
+ let currentObj = obj;
807
+ do {
808
+ const keys = Object.getOwnPropertyNames(currentObj);
809
+ if (Array.isArray(keys)) {
810
+ const foundKey = keys.find(k => equalIgnoreCase(key, k));
811
+ if (foundKey) {
812
+ const value = currentObj[foundKey];
813
+ return [foundKey, value];
814
+ }
815
+ }
816
+ } while ((currentObj = Object.getPrototypeOf(currentObj)));
817
+ }
818
+ return undefined;
819
+ };
820
+ /**
821
+ * Compares two values for equality, ignoring case sensitivity.
822
+ * @param a - The first value to compare.
823
+ * @param b - The second value to compare.
824
+ * @returns `true` if the values are equal, `false` otherwise.
825
+ */
826
+ const equalIgnoreCase = (a, b) => {
827
+ if (typeof a === 'number' || typeof b === 'number') {
828
+ return a === b;
829
+ }
830
+ else if (typeof a === 'symbol' || typeof b === 'symbol') {
831
+ return a === b;
832
+ }
833
+ else if (typeof a === 'string' || typeof b === 'string') {
834
+ return a.localeCompare(b, 'en', { sensitivity: 'base' }) === 0;
835
+ }
836
+ return undefined;
837
+ };
838
+ /**
839
+ * Retrieves the value from an object, ignoring the case of the key.
840
+ * @param obj - The object to retrieve the value from.
841
+ * @param key - The key to search for, ignoring the case.
842
+ * @returns The value associated with the key, or undefined if the key is not found.
843
+ */
844
+ const getValueIgnoreCase = (obj, key) => {
845
+ const pair = getKeyValue(obj, key, true);
846
+ if (pair) {
847
+ return pair[1];
848
+ }
849
+ return undefined;
850
+ };
930
851
 
931
852
  /**
932
853
  * Prototype Pollution Prevention Utilities
@@ -1235,7 +1156,7 @@ const emitRead = (node, st, value) => {
1235
1156
  *
1236
1157
  * An identifier is resolved against the context, and `EvalContext.get` reads a
1237
1158
  * plain object there - the original, or a scope - with a bare property access.
1238
- * So up to 0.10.0 a name on the blocklist resolved to what the context inherits
1159
+ * So up to 0.10.x a name on the blocklist resolved to what the context inherits
1239
1160
  * from `Object.prototype` (`docs/backlog-retired.md` B6): the blocklist guarded
1240
1161
  * member access to the objects an expression holds, and the context is not one
1241
1162
  * of them. Every kind of context is refused alike, including those - a
@@ -1706,7 +1627,7 @@ const evaluateMember = (node, st, callback) => {
1706
1627
  // `this.k` resolves `k` against the context, as an identifier does, so it
1707
1628
  // is refused as one is (`identifier.ts`, `docs/backlog-retired.md` B6): a
1708
1629
  // blocklisted key before the lookup, and under `caseInsensitive` a
1709
- // blocklisted key the lookup matched. Up to 0.10.0 neither was checked,
1630
+ // blocklisted key the lookup matched. Up to 0.10.x neither was checked,
1710
1631
  // and `this.constructor` over a plain object was `Object`.
1711
1632
  if (isDangerousProperty(key)) {
1712
1633
  throw new Error(`Access to dangerous property "${String(key)}" is blocked for security reasons`);
@@ -1901,6 +1822,146 @@ const evaluateArray = (node, items, st, callback) => {
1901
1822
  return elements;
1902
1823
  };
1903
1824
 
1825
+ /**
1826
+ * Whether `value` can be held in a `WeakSet` - an object or a function.
1827
+ */
1828
+ const isObjectLike = (value) => (typeof value === 'object' && value !== null) || typeof value === 'function';
1829
+ /**
1830
+ * Records `value` as created by the walk, unless it is a primitive.
1831
+ *
1832
+ * For the one recording site whose value may be a primitive: an array or
1833
+ * parameter rest is `args.slice(i)`, which is a string when the destructured
1834
+ * source is one (`let [h, ...t] = "abc"`), and `WeakSet.add` throws on a
1835
+ * primitive. The literal, object-rest and arrow-function sites always create
1836
+ * an object and add directly.
1837
+ *
1838
+ * @param created - `st.createdObjects`, already read and found set by the caller.
1839
+ * @param value - The value the walk created.
1840
+ */
1841
+ const recordCreated = (created, value) => {
1842
+ if (isObjectLike(value)) {
1843
+ created.add(value);
1844
+ }
1845
+ };
1846
+ /**
1847
+ * Asks the context whether a member write may happen, through
1848
+ * `EvalContext.checkMemberWrite`. A refusal is a throw, and it propagates from
1849
+ * here unchanged: nothing has been written yet.
1850
+ *
1851
+ * Called by the member branches of `assignment-expression.ts` and
1852
+ * `update-expression.ts`, immediately before `safeSetProperty`, and only after
1853
+ * the caller has read `st.createdObjects` and found it set. That read is the
1854
+ * whole of the default path's cost: a state whose context has no policy holds
1855
+ * no set, so neither visitor reaches this call.
1856
+ *
1857
+ * @param st - The state of the walk making the write.
1858
+ * @param created - `st.createdObjects`, already read by the caller.
1859
+ * @param target - The object about to receive the write.
1860
+ * @param key - The property about to be written.
1861
+ */
1862
+ const consultMemberWrite = (st, created, target, key) => {
1863
+ st.context?.checkMemberWrite?.({
1864
+ target,
1865
+ key,
1866
+ createdByEvaluation: isObjectLike(target) && created.has(target),
1867
+ });
1868
+ };
1869
+ /**
1870
+ * The built-ins that write into their receiver or their first argument, keyed
1871
+ * by the function itself. Identity is the point: a method of the caller's own
1872
+ * named `push` is not one of these, and `Array.prototype.push` reached under
1873
+ * another name is.
1874
+ *
1875
+ * Read through property descriptors, since `%TypedArray%.prototype` carries
1876
+ * getters that throw when read off the prototype itself. Built once, from this
1877
+ * realm's built-ins: an array made in another realm - an iframe - holds that
1878
+ * realm's `push`, which is not in here.
1879
+ */
1880
+ const MUTATING_BUILT_INS = (() => {
1881
+ const table = new Map();
1882
+ const add = (owner, ownerName, names, writes) => {
1883
+ for (const name of names) {
1884
+ const fn = Object.getOwnPropertyDescriptor(owner, name)?.value;
1885
+ if (typeof fn === 'function') {
1886
+ table.set(fn, { name: `${ownerName}.${name}`, writes });
1887
+ }
1888
+ }
1889
+ };
1890
+ add(Array.prototype, 'Array.prototype', ['copyWithin', 'fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], 'receiver');
1891
+ add(Object.getPrototypeOf(Int8Array.prototype), '%TypedArray%.prototype', ['copyWithin', 'fill', 'reverse', 'set', 'sort'], 'receiver');
1892
+ add(Map.prototype, 'Map.prototype', ['set', 'delete', 'clear'], 'receiver');
1893
+ add(Set.prototype, 'Set.prototype', ['add', 'delete', 'clear'], 'receiver');
1894
+ add(WeakMap.prototype, 'WeakMap.prototype', ['set', 'delete'], 'receiver');
1895
+ add(WeakSet.prototype, 'WeakSet.prototype', ['add', 'delete'], 'receiver');
1896
+ add(Date.prototype, 'Date.prototype', Object.getOwnPropertyNames(Date.prototype).filter((name) => name.startsWith('set')), 'receiver');
1897
+ add(Object, 'Object', ['assign', 'defineProperty', 'defineProperties', 'setPrototypeOf', 'freeze', 'seal', 'preventExtensions'], 'first argument');
1898
+ return table;
1899
+ })();
1900
+ /**
1901
+ * `Function.prototype`'s three ways to run a function with a `this` the
1902
+ * caller chooses. Read once, from this realm, as the table above is.
1903
+ */
1904
+ const FUNCTION_CALL = Function.prototype.call;
1905
+ const FUNCTION_APPLY = Function.prototype.apply;
1906
+ const FUNCTION_BIND = Function.prototype.bind;
1907
+ /**
1908
+ * The arguments `Function.prototype.apply` passes on: its second argument, read
1909
+ * as an array-like, as `apply` reads it. Nothing for anything else - `apply`
1910
+ * passes none for `null` or `undefined`, and throws for a primitive.
1911
+ */
1912
+ const argumentsOf = (list) => isObjectLike(list) ? Array.prototype.slice.call(list) : [];
1913
+ /**
1914
+ * Asks the context whether a call may run, when the function is a built-in
1915
+ * that writes into an object it is handed, through
1916
+ * `EvalContext.checkMemberWrite` with `method` naming it. Any other function is
1917
+ * not asked about. A refusal is a throw, and it propagates from here
1918
+ * unchanged: the call has not been made.
1919
+ *
1920
+ * Reached through `Function.prototype.call`, `apply` or `bind`, the function
1921
+ * the visitor calls is one of those three and not the built-in, so it is asked
1922
+ * about as a direct call of the function they run: its receiver is the `this`
1923
+ * they pass, and its arguments are the rest, or `apply`'s list. Followed
1924
+ * through `call.call`, `call.apply` and the like, since each hop is one more of
1925
+ * the three. `bind` makes a new function, which no table could recognise when
1926
+ * it is called later, so it is asked at bind time, about the bound `this` and
1927
+ * arguments: binding a built-in to an object the walk did not create is
1928
+ * refused before the bound function exists (`docs/backlog-retired.md` C5).
1929
+ *
1930
+ * Called by `call-expression.ts` immediately before the call, and only after
1931
+ * it has read `st.createdObjects` and found it set, as for a member write.
1932
+ *
1933
+ * @param st - The state of the walk making the call.
1934
+ * @param created - `st.createdObjects`, already read by the caller.
1935
+ * @param fn - The function about to be called.
1936
+ * @param receiver - The `this` it is about to be called with.
1937
+ * @param args - The arguments it is about to be called with.
1938
+ */
1939
+ const consultMethodWrite = (st, created, fn, receiver, args) => {
1940
+ if (fn === FUNCTION_CALL) {
1941
+ consultMethodWrite(st, created, receiver, args[0], args.slice(1));
1942
+ return;
1943
+ }
1944
+ if (fn === FUNCTION_APPLY) {
1945
+ consultMethodWrite(st, created, receiver, args[0], argumentsOf(args[1]));
1946
+ return;
1947
+ }
1948
+ if (fn === FUNCTION_BIND) {
1949
+ consultMethodWrite(st, created, receiver, args[0], args.slice(1));
1950
+ return;
1951
+ }
1952
+ const builtIn = MUTATING_BUILT_INS.get(fn);
1953
+ if (!builtIn) {
1954
+ return;
1955
+ }
1956
+ const target = builtIn.writes === 'receiver' ? receiver : args[0];
1957
+ st.context?.checkMemberWrite?.({
1958
+ target,
1959
+ key: undefined,
1960
+ createdByEvaluation: isObjectLike(target) && created.has(target),
1961
+ method: builtIn.name,
1962
+ });
1963
+ };
1964
+
1904
1965
  /**
1905
1966
  * Security: List of dangerous functions that should not be callable
1906
1967
  */
@@ -2035,6 +2096,12 @@ const scopeReceiver = (st, object) => {
2035
2096
  };
2036
2097
  /**
2037
2098
  * Enhanced call expression visitor with security checks
2099
+ *
2100
+ * A context with a member-write policy is asked, before the call, about a
2101
+ * built-in that writes into its receiver or its first argument - see
2102
+ * `consultMethodWrite`. The receiver asked about is the one the call is made
2103
+ * with: the member's object, a prior scope's `thisArg`, or the context itself
2104
+ * for a bare call. A context without a policy pays one field read per call.
2038
2105
  */
2039
2106
  const callExpressionVisitor = (node, st, callback) => {
2040
2107
  beforeVisitor(node, st);
@@ -2042,7 +2109,12 @@ const callExpressionVisitor = (node, st, callback) => {
2042
2109
  if (node.callee.type === 'MemberExpression') {
2043
2110
  const [object, propertyName, fn] = evaluateMember(node.callee, st, callback);
2044
2111
  const functionName = typeof propertyName === 'string' ? propertyName : undefined;
2045
- const value = safeCall(fn, scopeReceiver(st, object), args, node.callee.optional, functionName);
2112
+ const receiver = scopeReceiver(st, object);
2113
+ const created = st.createdObjects;
2114
+ if (created) {
2115
+ consultMethodWrite(st, created, fn, receiver, args);
2116
+ }
2117
+ const value = safeCall(fn, receiver, args, node.callee.optional, functionName);
2046
2118
  pushVisitorResult(node, st, value);
2047
2119
  }
2048
2120
  else {
@@ -2053,6 +2125,10 @@ const callExpressionVisitor = (node, st, callback) => {
2053
2125
  if (node.callee.type === 'Identifier') {
2054
2126
  functionName = node.callee.name;
2055
2127
  }
2128
+ const created = st.createdObjects;
2129
+ if (created) {
2130
+ consultMethodWrite(st, created, caller, st.context, args);
2131
+ }
2056
2132
  const value = safeCall(caller, st.context, args, node.optional, functionName);
2057
2133
  pushVisitorResult(node, st, value);
2058
2134
  }
@@ -2453,51 +2529,6 @@ const getDefaultVisitors = () => {
2453
2529
  return visitors;
2454
2530
  };
2455
2531
 
2456
- /**
2457
- * Whether `value` can be held in a `WeakSet` - an object or a function.
2458
- */
2459
- const isObjectLike = (value) => (typeof value === 'object' && value !== null) || typeof value === 'function';
2460
- /**
2461
- * Records `value` as created by the walk, unless it is a primitive.
2462
- *
2463
- * For the one recording site whose value may be a primitive: an array or
2464
- * parameter rest is `args.slice(i)`, which is a string when the destructured
2465
- * source is one (`let [h, ...t] = "abc"`), and `WeakSet.add` throws on a
2466
- * primitive. The literal, object-rest and arrow-function sites always create
2467
- * an object and add directly.
2468
- *
2469
- * @param created - `st.createdObjects`, already read and found set by the caller.
2470
- * @param value - The value the walk created.
2471
- */
2472
- const recordCreated = (created, value) => {
2473
- if (isObjectLike(value)) {
2474
- created.add(value);
2475
- }
2476
- };
2477
- /**
2478
- * Asks the context whether a member write may happen, through
2479
- * `EvalContext.checkMemberWrite`. A refusal is a throw, and it propagates from
2480
- * here unchanged: nothing has been written yet.
2481
- *
2482
- * Called by the member branches of `assignment-expression.ts` and
2483
- * `update-expression.ts`, immediately before `safeSetProperty`, and only after
2484
- * the caller has read `st.createdObjects` and found it set. That read is the
2485
- * whole of the default path's cost: a state whose context has no policy holds
2486
- * no set, so neither visitor reaches this call.
2487
- *
2488
- * @param st - The state of the walk making the write.
2489
- * @param created - `st.createdObjects`, already read by the caller.
2490
- * @param target - The object about to receive the write.
2491
- * @param key - The property about to be written.
2492
- */
2493
- const consultMemberWrite = (st, created, target, key) => {
2494
- st.context?.checkMemberWrite?.({
2495
- target,
2496
- key,
2497
- createdByEvaluation: isObjectLike(target) && created.has(target),
2498
- });
2499
- };
2500
-
2501
2532
  // based on evalArrowContext
2502
2533
  // https://github.com/6utt3rfly/jse-eval/blob/main/index.ts
2503
2534
  /**
@@ -3875,9 +3906,8 @@ class EvalScope {
3875
3906
  * Whether a context holds a key at all, regardless of the value bound to it.
3876
3907
  *
3877
3908
  * There is no `getContextValue` counterpart for this: that helper cannot
3878
- * distinguish an absent key from one bound to `undefined`, and `getContextKey`
3879
- * with `caseInsensitive: false` reports every key as present for a plain
3880
- * object. Own properties only - the scopes this is used on are object literals
3909
+ * distinguish an absent key from one bound to `undefined`. Own properties
3910
+ * only - the scopes this is used on are object literals
3881
3911
  * built by `evaluatePatterns`, and inherited names are not bindings.
3882
3912
  */
3883
3913
  const hasContextKey = (context, key) => {
@@ -6261,5 +6291,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.2.1", ngImpor
6261
6291
  * Generated bundle index. Do not edit.
6262
6292
  */
6263
6293
 
6264
- export { ASYNC_HOOK_MESSAGE, BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EMPTY_COMPLETION, EvalContext, EvalHooks, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Queue, RecursiveVisitorResultType, Registry, Stack, call, callAsync, compile, compileAsync, createDependencyTracker, createTimingHook, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextKey, getContextValue, isRegistryContext, parse };
6294
+ export { ASYNC_HOOK_MESSAGE, BaseRegistry, Cache, CaseInsensitiveRegistry, CompilerService, DiscoveryService, EMPTY_COMPLETION, EvalContext, EvalHooks, EvalResult, EvalScope, EvalService, EvalState, EvalTrace, ParserService, Registry, Stack, call, callAsync, compile, compileAsync, createDependencyTracker, createTimingHook, defaultParserOptions, evaluate, evaluateAsync, extract, extractExpression, fromContext, getContextValue, parse };
6265
6295
  //# sourceMappingURL=zvenigora-ng-eval-core.mjs.map