<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>naftpink - rust</title>
    <link>https://naft.pink</link>
    <description>Notes tagged rust on naftpink.</description>
    <language>en</language>
    <atom:link href="https://naft.pink/blog/tags/rust/feed.xml" rel="self" type="application/xml" />
    <item>
      <title>writing a scripting VM in rust</title>
      <link>https://naft.pink/blog/writing-a-vm-in-rust</link>
      <guid isPermaLink="true">https://naft.pink/blog/writing-a-vm-in-rust</guid>
      <pubDate>Thu, 18 Jun 2026 00:00:00 GMT</pubDate>
      <description>Learning Rust by porting my game&apos;s scripting VM out of C++: a stack-based bytecode interpreter and a small compiler for gameplay scripts.</description>
      <content:encoded><![CDATA[

<p>I&#x27;m building a game in Unreal, and gameplay logic in C++ means a rebuild every
time I change a number. That loop is slow enough that I stop experimenting,
which is the worst thing a tool can do to you.</p>
<p>The fix is scripts. The excuse is that I wanted to learn Rust properly, so I&#x27;m
writing the whole thing myself: a small language, a compiler, and a VM to run
the bytecode.</p>
<h2 id="it-started-in-c">it started in C++</h2>
<p>The first version was C++, which made sense at the time - the game is already
C++, so the VM could just reach into game state directly. It worked. It was also
the kind of code where a crash three frames later was caused by something I did
three functions ago.</p>
<p>Rewriting it in Rust was supposed to be a learning exercise. It turned into the
version I actually use.</p>
<h2 id="stack-based-for-now">stack-based, for now</h2>
<p>It&#x27;s a stack VM. Every tutorial starts with a stack VM, and after the port I
never had a good enough reason to move off it - the interpreter is small, the
compiler is almost trivially simple, and none of my scripts are hot enough for
dispatch count to be the thing that hurts.</p>
<figure data-rehype-pretty-code-figure=""><pre tabindex="0" data-language="rust" data-theme="rose-pine-moon"><code data-language="rust" data-theme="rose-pine-moon"><span data-line=""><span style="color:#3E8FB0">enum</span><span style="color:#9CCFD8"> Op</span><span style="color:#908CAA"> {</span></span>
<span data-line=""><span style="color:#EA9A97">    LoadConst</span><span style="color:#908CAA">(</span><span style="color:#9CCFD8">u16</span><span style="color:#908CAA">),</span></span>
<span data-line=""><span style="color:#9CCFD8">    Add</span><span style="color:#908CAA">,</span></span>
<span data-line=""><span style="color:#EA9A97">    JumpIfFalse</span><span style="color:#908CAA">(</span><span style="color:#9CCFD8">u32</span><span style="color:#908CAA">),</span></span>
<span data-line=""><span style="color:#9CCFD8">    Call</span><span style="color:#908CAA"> {</span><span style="color:#E0DEF4;font-style:italic"> func</span><span style="color:#3E8FB0">:</span><span style="color:#9CCFD8"> u16</span><span style="color:#908CAA">,</span><span style="color:#E0DEF4;font-style:italic"> argc</span><span style="color:#3E8FB0">:</span><span style="color:#9CCFD8"> u8</span><span style="color:#908CAA"> },</span></span>
<span data-line=""><span style="color:#9CCFD8">    Return</span><span style="color:#908CAA">,</span></span>
<span data-line=""><span style="color:#908CAA">}</span></span></code></pre></figure>
<p>The cost is obvious when you read the bytecode: <code>push</code>, <code>push</code>, <code>add</code>, <code>store</code>
for what is conceptually one operation. Registers are where I want to take it -
wider instructions, far fewer dispatches per line of script - but that means
writing a register allocator, and I&#x27;d rather have hot reload working first.</p>
<h2 id="what-rust-actually-taught-me">what rust actually taught me</h2>
<p>The borrow checker is the reason this works. The C++ version did the obvious
thing: hand the VM a pointer to the host game state and let it write wherever.
Rust refused to let me port that straight across, so I ended up with an explicit
host interface - the script asks, the host answers, nothing reaches across.</p>
<p>That boundary is the good part of the design and I didn&#x27;t come up with it. The
compiler forced it on me.</p>
<h2 id="still-missing">still missing</h2>
<ul>
<li>Garbage collection. Strings currently live forever. I&#x27;m going to pretend that&#x27;s a feature until it isn&#x27;t.</li>
<li>Debug info. A runtime error gives you a bytecode offset, which is useless. Line numbers next.</li>
<li>Hot reload, the whole reason for the project. Swapping a function&#x27;s bytecode mid-frame without invalidating live call frames is the interesting problem, and I haven&#x27;t solved it yet.</li>
</ul>
<p>Gets rewritten roughly every other month. That&#x27;s fine - the rewrites are where
the learning is.</p>]]></content:encoded>
      <category>rust</category>
      <category>gamedev</category>
    </item>
  </channel>
</rss>
