Download Game! Currently 108 players. Last logged in:InstantAnakinWicksiusWretched

Blitzer's Blog >> 72910

Back to blogs index
Posted: 03 Oct 2026 17:26 [ permalink ]
 // main.mjs import { Worker } from 'worker_threads'; import { analyze,
compile, StateBuffer } from './engine.mjs'; const payload = [ "let", [
["my_struct", ["record", "status", ["str", "processing"], "data", ["array"]]]
], ["do", ["try", ["for", "i", 1, 10, ["do", ["set-idx", "my_struct", "data",
["cons", "i", ["get", "my_struct", "data"]]], ["if", ["=", "i", 5], ["throw",
["str", "Database Crash!"]], null ] ] ], "err", ["set-idx", "my_struct",
"status", "err"] ], "my_struct" ] ]; console.log("Analyzing AST...");
analyze(payload); console.log("Compiling AST..."); const bytecode =
compile(payload); const sab = new SharedArrayBuffer(16); const stateBuffer =
new StateBuffer(sab); // Pass node_worker.mjs to the Worker const worker = new
Worker('./node_worker.mjs', { workerData: { bytecode, sab, gasLimit: 50000 }
}); let finalData = null; // Listen for the rich JSON payload passing from the
worker worker.on('message', msg => { finalData = msg; }); console.log("Dispatch
ing execution to Worker thread..."); const startTime = Date.now(); const
pollInterval = setInterval(() => { const state = stateBuffer.readState(); // 2
= Success, 3 = Out of Gas, 4 = Error, 5 = Interrupted if (state.status >= 2 ||
Date.now() - startTime >= 1000) { clearInterval(pollInterval);
worker.terminate(); console.log(`
Execution ended. Status Code: ${state.status}`); console.log("Final Output:",
JSON.stringify(finalData, null, 2)); } }, 10); 
/ Run node main.mjs in your terminal. You now have a cleanly decoupled system
where the business logic is totally independent of the runtime environment.