NAL has two ways of running. The interpreter walks the program directly, and it's what every game uses today. The VM runs a lowered intermediate form of the program. In the long run it should be faster, and better for hot reload. The risk with two runtimes is well known: they slowly start to disagree, and one day a game behaves differently depending on which one it's running on.
So before worrying about speed, we wanted to know whether the VM gave the same answers. Finding out properly took most of September.
First, make a run repeatable
The plan was simple. Run the same game on both runtimes with the same inputs, and compare everything, every frame. We fell at the first hurdle, because a game wouldn't even match itself from one run to the next.
Random numbers came from the thread's own unseeded generator. Now rand() uses a SplitMix64 generator that belongs to the world and gets seeded before any on_start runs. That ordering turned out to matter for other reasons too: Ember Colony's maps came out different every time until we fixed it.
The other problem was iteration order. Entities, timers and collision state were stored in hash maps, so "for each entity" could come out in a different order each run. Those maps are ordered (BTreeMap) now.
Then, make the maths match
Next up was 7 / 2. The two runtimes disagreed about whether integer maths gave integers or floats, and about what integer division even meant. Comparing snapshots is pointless while one side says 3 and the other says 3.0.
Both runtimes now use a single shared module for numbers, and its rules are part of the language. Integer maths stays integer, and overflow is an error (IntegerOverflow) instead of wrapping around. / always returns a float, so if you want whole-number division you write floor(a / b). Booleans count as integers in arithmetic, float equality allows a tiny tolerance, and dividing by zero is a DivisionByZero error. Maths functions like tan, pow and atan2 share one implementation too.
Then, make the engine calls match
The last mismatch was in how NAL talks to the engine. The interpreter used to handle things like "play this sound" or "spawn this entity" by looking up a function by name, and the VM had its own route. Now both produce the same typed host operations (HostOp) for commands like sound, spawning, scene changes, timers and the camera. Reads such as input, collisions and world lookups go through typed host queries (HostQuery). One test feeds the same program to both runtimes and checks that the commands come out identical.
The harness
With all that in place, the comparison is small. It runs two GameSessions, one on each runtime, and feeds them the same 600 frames of input. On every frame it compares the full JSON output, meaning every entity, every component, the render data and the commands. They have to match exactly.
We added one more check that turned out to be important. Both sides have to report the same number of script errors on every frame, and that number has to be zero at the end. Without it, two runtimes that failed in exactly the same way would look like a match.
The VM can't run everything yet, so the harness lowers each game as far as it can. Systems the VM supports run on the VM, and the rest fall back to the interpreter. That means we can measure how much of each game is really running on the VM. These are the current numbers:
| Game | Systems on the VM | Notes |
|---|---|---|
| simple_platformer | 9 / 9 | |
| twin_stick_shooter | 11 / 11 | |
| timer_demo | 6 / 6 | |
| simple_3d_scene | 1 / 1 | |
| platformer (3D) | 15 / 15 | |
| cube_runner | 11 / 11 | |
| orb_collector | 9 / 9 | |
| space_shooter | 17 / 17 | |
| platformer_extreme | 23 / 28 | The other 5 use plain emit statements, which the VM can't handle yet |
Our smaller targeted tests (numbers, functions, control flow, vectors, entities, input, timers and randomness, strings, loops) run entirely on the VM. The harness also found a real bug in the 3D demos. Their spinning-object systems logged UnknownField every frame, because user code couldn't see the built-in 3D components. Making those components implicitly available fixed it in both runtimes.
So why isn't the VM the default?
A table that green makes it tempting to flip the switch. We'd written down the conditions for switching ahead of time, though, and they aren't met yet.
The biggest problem is the optimizer. We wrote its passes early, in a single day back in January. When we finally looked at them closely, they produced invalid constants and assumed something about the code's structure that the earlier stage doesn't guarantee. So the VM skips the optimizer completely for now. There's also no bytecode executor for anything that talks to the engine. The VM runs the unoptimized intermediate form, and the bytecode compiler refuses any code that calls the engine. State machines, event handlers, plain emit and arrays all still fall back to the interpreter. And we haven't measured performance or tested whether hot reload keeps state.
The conformance document already said what to do in this case: "If those gates are not met, the interpreter remains the alpha runtime and the VM is labeled experimental rather than forcing an incomplete switchover." So that's what we're doing.
We're glad we wrote those conditions down before the numbers looked good. And the work to make runs repeatable has already paid off in other places. Agents' test runs can now be replayed exactly, and Ember Colony gives you the same map every time you use the same seed.