dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const log = []; const item = new Proxy({ qty: 2 }, { get(target, key, receiver) { log.push(`get ${String(key)}`); return Reflect.get(target, key, receiver); } }); const qty = item.qty; `${log.join(', ')}: ${qty}`; """).AsString);
Unzip the example, then dotnet run.
Prints:
get qty: 2The full program
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const log = []; const { proxy, revoke } = Proxy.revocable({ qty: 2 }, { get(target, key, receiver) { log.push(`get ${String(key)}`); return Reflect.get(target, key, receiver); }, set(target, key, value, receiver) { log.push(`set ${String(key)} = ${value}`); return Reflect.set(target, key, value, receiver); } }); proxy.qty = 3; const qty = proxy.qty; log.push(`qty: ${qty}`); revoke(); try { proxy.qty; } catch (error) { log.push(`${error.name}: ${error.message}`); } log.join('\n'); """).AsString);
dotnet run prints:
set qty = 3 get qty qty: 3 TypeError: Cannot perform 'get' on a proxy that has been revoked
The full program logs a write and a read, then revokes the proxy. After revocation, a property read throws a JavaScript TypeError before the handler runs. The sample catches that error inside the script and prints its name and message.
Variations
Keep getter behavior when forwarding. The getter doubled reads this.qty. Passing receiver to Reflect.get makes that nested access go through the proxy too:
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); Console.WriteLine(engine.Evaluate(""" const target = { qty: 2, get doubled() { return this.qty * 2; } }; const log = []; const item = new Proxy(target, { get(target, key, receiver) { log.push(String(key)); return Reflect.get(target, key, receiver); } }); const value = item.doubled; `${log.join(' -> ')}: ${value}`; """).AsString);
doubled -> qty: 4Related recipes
The short answer
In OneEcma, evaluate a script that calls new Proxy(target, handler). The handler’s get method logs a read and forwards it with Reflect.get(target, key, receiver). Add a set method for writes, forwarding with Reflect.set. In these examples the target and proxy are JavaScript objects, and C# receives the log as a string. Wrapping a JavaScript object does not automatically intercept reads on a CLR object.
How do you use a JavaScript Proxy to log property access?
A handler method is called a trap. A get trap runs when code reads a property through the wrapper, and a set trap runs when code assigns through it. Log the operation before forwarding if you want to record the attempt, as the full program does for set qty = 3. Direct reads and writes on the original target do not pass through the wrapper. Hand out the proxy wherever you expect reads or writes to be logged.
Only the operations for which you provide traps get custom behavior. For example, a read of a nested object goes through the outer wrapper, but later reads on that nested object do not go through the same wrapper unless it, too, is proxied. A property log is not a copy of every change to the underlying object.
What does Reflect do in JavaScript Proxy handlers?
Reflect.get performs the normal property read with the receiver you pass. That receiver becomes this for a getter. In the variation, doubled reads this.qty, so both property names appear in the log. Passing the raw target as the receiver instead would make the nested read bypass the proxy. Forward to the trap’s target argument, not the wrapper, to avoid reentering the same trap for the same read.
Reflect.set reports whether the assignment succeeded. Return that Boolean from the trap rather than reporting success unconditionally. OneEcma’s Proxy documentation describes invariant checks: for instance, a trap cannot return a substitute value for a non-writable, non-configurable own data property.
What happens when you revoke a JavaScript Proxy?
Proxy.revocable returns a wrapper and a revoke function. Calling it disables that wrapper; it does not undo a write already forwarded to the target. Keep the function if you need to stop use of a wrapper later. The last line of the program comes from the JavaScript catch, not a get trap: the revoked wrapper throws before the read can be forwarded or logged.