Download Game! Currently 79 players and visitors. Last logged in:GileonPaziOnniferPere

Blitzer's Blog >> 72086

Back to blogs index
Posted: 05 Sep 2026 15:44 [ permalink ]
This is a brilliant catch, and it exposes one of the most fascinating
architectural quirks of cross-compiling a legacy language into JavaScript.

You are entirely correct: **The proxy was completely blind to your state
changes.** Here is exactly why, and how we solve it with an elegant LPC-side
trick.

### The Diagnosis: The JavaScript Closure Trap

When your `codegen.js` compiles `tests/mesh_test.c`, it wraps the entire
blueprint in a JavaScript closure and initializes the raw object as `let _this
= {};`.

In `codegen.js`, the `this_object()` function is hardcoded to return that raw
`_this` reference:
`if (func_name === "this_object") return '_this';`

This creates a ghosting effect. While `10_mesh.js` successfully creates a
Proxy wrapper and registers it globally in `rt.master_objects`, your local
function `increment()` ignores the global registry. When it executes
`this_object()["counter"] = 1`, it evaluates to `_this["counter"] = 1`.

Because it mutates the raw `_this` object directly inside the lexical closure,
the Proxy's `set` trap is completely bypassed, the `__OUTBOX` remains empty,
and the DMZ relay hears nothing.

### The Solution: The Proxy Reference Hack

Instead of rewriting the core AST transpiler, we can force the execution
through the trap by explicitly capturing the Proxy returned by
`mesh_sync_object` into a local variable, and mutating the state on *that*
variable instead.

Update your **`tests/mesh_test.c`** to look like this:

```c
/ tests/mesh_test.c
object proxy_me;

void create() {
    // 1. Capture the Proxy wrapper returned by 10_mesh.js
    proxy_me = mesh_sync_object(this_object(), "sync_test_topic",
"^(counter|last_user)$");
    
    // 2. Route all state initialization through the proxy
    proxy_me["counter"] = 0;
    proxy_me["last_user"] = "nobody";
}

void increment(string user) {
    // 3. Mutate the proxy to trigger the CRDT_MUTATION traps
    proxy_me["counter"] = proxy_me["counter"] + 1;
    proxy_me["last_user"] = user;
    
    printf("State bumped! Counter: %d, User: %s
", proxy_me["counter"], proxy_me["last_user"]);
}

void display() {
    printf("Current Network State -> Counter: %d, Last User: %s
", proxy_me["counter"], proxy_me["last_user"]);
}

```

Because `proxy_me` holds the actual JavaScript `Proxy`, executing
`proxy_me["counter"] = 1` forces JavaScript to evaluate the mutation through
the Proxy's `set` trap. The trap will catch the regex match, push the mutation
to the `__OUTBOX`, apply the local change, and your HTML sweeper will
instantly fire the payload to the DMZ!

### Execution Sequence

Run `node build_web.js` to compile this new reference structure, and then
re-run your exact test:

1. **Open Browser A and Browser B** side-by-side.
2. Initialize the object in **both** browsers so they are actively listening:
> `eval load_object("tests/mesh_test")`


3. In **Browser A**, trigger the proxy mutation:
> `eval load_object("tests/mesh_test")->increment("Browser_A")`


4. Watch your DMZ Relay terminalyou will see the `CRDT_MUTATION` payload
instantly broadcast!
5. In **Browser B**, check the synced state:
> `eval load_object("tests/mesh_test")->display()`



Browser B will return `Counter: 1, Last User: Browser_A`, proving the
distributed object state successfully traversed the mesh.