Because our VM is built natively on Node.js, we can achieve this transparently
using JavaScript's native Proxy object. A Proxy wraps the target object and
can silently intercept and redefine fundamental operations, like every
property assignment (set operation), without the underlying logic ever knowing
it is being watched.
Here is the exact architectural blueprint to build this out:
1. The mesh_sync_object EFUN
When an LPC object invokes mesh_sync_object(this_object(), "game_scores",
"^score_.*"), the EFUN performs three actions:
Topic Registration: It binds the object's __filename to the "game_scores"
topic in a global routing registry.
Regex Compilation: It converts the "^score_.*" string into a native JavaScript
RegExp.
Proxy Injection: It wraps the object's underlying context in the Proxy.
2. The Transparent Mutation Interceptor
Whenever the LPC code does score_blue = 5;, the AST generates _this.score_blue
= 5;. The proxy intercepts this operation automatically:
..
3. The Synchronization Lifecycle (Chicken & Egg)
To completely solve the initialization problem when Node B and Node C boot up
simultaneously:
The Subscribe Request: mesh_receive_distributed_objects("game_scores")
registers the host to listen for that topic.
The Intent Broadcast: The Node.js host immediately broadcasts a { type:
"mesh_join", topic: "game_scores" } payload to the network.
The State Dump: Any node currently hosting an active record for that topic
sees the join request and replies with a complete { type: "mesh_state", data:
{...} } snapshot.
The VM Ingestion: The transport layer routes this payload into rt.__INBOX.
During the VM's message processing tick, it unpacks the state and maps it
directly onto the local _this object using Reflect.set to bypass the Proxy's
trap, guaranteeing you do not trigger an infinite echo loop.
By passing the regex dynamically, you could even write a network_manager.c
daemon in LPC that dynamically changes which variables sync based on current
server load or game state!
How would you like to handle the actual physical transport layer for the
Node.js instances outside the VM? Should we implement a centralized local
Redis pub/sub queue, MQTT, or a direct peer-to-peer WebSocket mesh between the
hosts?
==
/ Inside efuns.d/10_mesh.js
mesh_sync_object: (ob, topic, pattern) => {
const regex = new RegExp(pattern);
// Wrap the original object to trap mutations
const proxiedOb = new Proxy(ob, {
set(target, prop, value) {
// Perform the actual assignment safely using Reflect
const success = Reflect.set(target, prop, value);
// If it matches the wildcard, push to the mesh outbox
if (success && typeof prop === 'string' && regex.test(prop)) {
ctx.rt.__OUTBOX.push({
topic: topic,
source: target.__filename,
property: prop,
value: value,
timestamp: Date.now()
});
}
return success; // Return true to satisfy Proxy invariants
}
});
// Replace the raw object reference with the Proxy in the master pool
ctx.rt.master_objects[ob.__filename] = proxiedOb;
return 1;
}
==