<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>zig &amp;mdash; Gerrit Niezen</title>
    <link>https://gerritniezen.com/tag:zig</link>
    <description>Maker of open-source software and hardware.</description>
    <pubDate>Wed, 29 Apr 2026 15:26:39 +0000</pubDate>
    <image>
      <url>https://i.snap.as/aMPXpIot.png</url>
      <title>zig &amp;mdash; Gerrit Niezen</title>
      <link>https://gerritniezen.com/tag:zig</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>&lt;![CDATA[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.&#xA;&#xA;!--more--&#xA;&#xA;The math.zig file looks the same as the official example:&#xA;&#xA;extern fn print(i32) void;&#xA;&#xA;export fn add(a: i32, b: i32) void {&#xA;    print(a + b);&#xA;}&#xA;&#xA;Essentially we&#39;re exporting the add() function written in Zig, and using a function called print() that we have to define in JavaScript.&#xA;&#xA;To compile it, run&#xA;&#xA;zig build-lib math.zig -target wasm32-freestanding -dynamic -rdynamic&#xA;&#xA;The JavaScript file index.js looks like this:&#xA;&#xA;const imports = {&#xA;  env: {&#xA;    print: (result) =  { &#xA;      document.getElementById(&#39;text&#39;).innerHTML = The result is ${result};&#xA;    }&#xA;  }&#xA;};&#xA;&#xA;try {&#xA;  const response = await fetch(&#34;math.wasm&#34;);&#xA;  const bytes = await response.arrayBuffer();&#xA;  const result = await WebAssembly.instantiate(bytes, imports);&#xA;  const add = result.instance.exports.add;&#xA;  add(1, 2);&#xA;} catch (err) {&#xA;  console.log(err);&#xA;}&#xA;&#xA;In short, we can use the built-in fetch() to get the math.wasm file we compiled in the previous step. We then instantiate the WebAssembly code, and we can use the add() function that we wrote in Zig. The print() function gets defined as part of the imports object.&#xA;&#xA;All that&#39;s left is to create a simple index.html:&#xA;&#xA;!doctype html&#xA;html&#xA;  head&#xA;    titleZig in the browser/title&#xA;  /head&#xA;  body&#xA;  &#x9;div id=&#34;text&#34;/div&#xA;    script type=&#34;module&#34; src=&#34;index.js&#34;/script&#xA;  /body&#xA;/html&#xA;&#xA;Take note that we&#39;re defining index.js as a module, so that we can use top-level await in JavaScript.&#xA;&#xA;The easiest way to start up a simple web server to run the code is&#xA;&#xA;python -m http.server&#xA;&#xA;which you can then access in your browser by going to http://localhost:8000.&#xA;&#xA;Thoughts? a href=&#34;https://remark.as/p/gerritniezen.com/zig-and-webassembly-in-the-browser&#34;Discuss.../a if you have a write.as account.&#xA;&#xA;#zig #webassembly&#xA;&#xA;iComment on this post/i&#xD;&#xA;div id=&#34;cusdis_thread&#34;/div]]&gt;</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" 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>