<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>webassembly — Gerrit Niezen</title>
    <link>https://gerritniezen.com/tag:webassembly</link>
    <description>Maker of open-source software and hardware.</description>
    <pubDate>Mon, 16 Nov 2026 00:37:08 +0000</pubDate>
    <image>
      <url>https://i.snap.as/aMPXpIot.png</url>
      <title>webassembly — Gerrit Niezen</title>
      <link>https://gerritniezen.com/tag:webassembly</link>
    </image>
    <item>
      <title>Zig and WebAssembly in the browser</title>
      <link>https://gerritniezen.com/zig-and-webassembly-in-the-browser?pk_campaign=rss-feed</link>
      <description>I&#39;m starting to learn the Zig programming language, and wanted to see how easy it is to load it in the browser with WebAssembly. I noticed that the official documentation only includes a Node.js example, so I thought I&#39;d write up an example for the browser here.</description>
      <content:encoded><![CDATA[<p>I&#39;m starting to learn the Zig programming language, and wanted to see how easy it is to load it in the browser with WebAssembly. I noticed that the official documentation <a href="https://ziglang.org/documentation/master/#toc-Freestanding">only includes a Node.js example</a>, so I thought I&#39;d write up an example for the browser here.</p>



<p>The <code>math.zig</code> file looks the same as the official example:</p>

<pre><code class="language-zig">extern fn print(i32) void;

export fn add(a: i32, b: i32) void {
    print(a + b);
}
</code></pre>

<p>Essentially we&#39;re exporting the <code>add()</code> function written in Zig, and using a function called <code>print()</code> that we have to define in JavaScript.</p>

<p>To compile it, run</p>

<pre><code class="language-sh">zig build-lib math.zig -target wasm32-freestanding -dynamic -rdynamic
</code></pre>

<p>The JavaScript file <code>index.js</code> looks like this:</p>

<pre><code class="language-js">const imports = {
  env: {
    print: (result) =&gt; { 
      document.getElementById(&#39;text&#39;).innerHTML = `The result is ${result}`;
    }
  }
};

try {
  const response = await fetch(&#34;math.wasm&#34;);
  const bytes = await response.arrayBuffer();
  const result = await WebAssembly.instantiate(bytes, imports);
  const add = result.instance.exports.add;
  add(1, 2);
} catch (err) {
  console.log(err);
}
</code></pre>

<p>In short, we can use the built-in <code>fetch()</code> to get the <code>math.wasm</code> file we compiled in the previous step. We then instantiate the WebAssembly code, and we can use the <code>add()</code> function that we wrote in Zig. The <code>print()</code> function gets defined as part of the <code>imports</code> object.</p>

<p>All that&#39;s left is to create a simple <code>index.html</code>:</p>

<pre><code class="language-html">&lt;!doctype html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Zig in the browser&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  	&lt;div id=&#34;text&#34;&gt;&lt;/div&gt;
    &lt;script type=&#34;module&#34; src=&#34;index.js&#34;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Take note that we&#39;re defining <code>index.js</code> as a module, so that we can use top-level <code>await</code> in JavaScript.</p>

<p>The easiest way to start up a simple web server to run the code is</p>

<pre><code class="language-sh">python -m http.server
</code></pre>

<p>which you can then access in your browser by going to <code>http://localhost:8000</code>.</p>

<p>Thoughts? <a href="https://remark.as/p/gerritniezen.com/zig-and-webassembly-in-the-browser">Discuss...</a> if you have a write.as account.</p>

<p><a href="https://gerritniezen.com/tag:zig" class="hashtag"><span>#</span><span class="p-category">zig</span></a> <a href="https://gerritniezen.com/tag:webassembly" class="hashtag"><span>#</span><span class="p-category">webassembly</span></a></p>

<p><i>Comment on this post</i>
<div id="cusdis_thread"></div></p>
]]></content:encoded>
      <guid>https://gerritniezen.com/zig-and-webassembly-in-the-browser</guid>
      <pubDate>Wed, 12 Jul 2023 13:31:08 +0000</pubDate>
    </item>
  </channel>
</rss>