Vhai Execution
While the Vhai canvas operates strictly in the ChaosNexus Forge frontend, the heavy lifting happens behind the scenes in the ChaosNexus Anvil backend. This document covers the low-level details of how visual scripts are processed and executed by the runtime.
Execution Model
ChaosNexus Anvil uses Rhai as its underlying scripting engine. Vhai does not invent a new virtual machine, interpret a custom byte-code format, or compile to a proprietary language. Instead, Vhai is simply a visual abstraction layer on top of Rhai.
When you create a Vhai script, the ChaosNexus Forge frontend traverses the node graph topologically, tracing the white Exec wires from events to leaf nodes. It serializes this execution path directly into standard, human-readable Rhai source code.
VM Internals
When a Vhai script is saved, ChaosNexus Anvil receives the raw Rhai source code via IPC and compiles it directly into an Abstract Syntax Tree (AST).
Because the visual nodes map 1:1 with native Rhai constructs (such as if, while, and native API calls), the resulting AST is identical to one generated by a human writing raw code.
This architecture provides immense benefits:
- Zero Overhead: Visual scripts run at the exact same speed as handwritten scripts.
- Native Debugging: Any errors or stack traces thrown by the engine reference the exact line numbers in the generated source, which ChaosNexus Forge cleanly maps back to the offending visual node.
- Engine Decoupling: The ChaosNexus Anvil backend has zero knowledge of visual node coordinates, wires, or canvas meta-data. It simply executes the code it is given.
Assembly Line Compilation
Pending or quarantined plugins distributed by third-parties may not always include visual layout metadata (.canvas.json).
When ChaosNexus Forge inspects a pending plugin that lacks visual layout data, it performs an Assembly Line Compilation natively. The IDE backend reads the raw Rhai AST and autonomously lays out a left-to-right visual graph representation of the code, saving it alongside the script.
Because the generated visual canvas is fully editable, this enables users to not only visually audit unknown code before approving it into their workspace (fulfilling our zero-trust security paradigm), but also to seamlessly modify arbitrary third-party scripts visually and compile those visual changes back into valid Rhai code.