TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: WebAssembly from the Ground Up – learn WASM by building a compiler

7 点作者 pdubroy大约 2 个月前
Hi HN!<p>We (pdubroy &amp; marianoguerra) just launched an online book called WebAssembly from the Ground Up. It&#x27;s an online book to learn Wasm by building a simple compiler in JavaScript.<p>This is the book we wish we&#x27;d had 3 years ago. Unlike many WebAssembly resources that focus on use cases and tooling, we wanted a deep dive into how Wasm actually works.<p>We focus on the core of WebAssembly: the module format and the instruction set. We think the low-level details — the &quot;virtual ISA&quot; — are the most interesting part, and we had the crazy idea that writing a compiler is the best way to learn it.<p>Over the course of the book, you&#x27;ll build up two things:<p>1. An &quot;assembler library&quot;, that can be used to produce WebAssembly modules. Here&#x27;s an example:<p><pre><code> const mod = module([ typesec([functype([], [])]), funcsec([typeidx(0)]), exportsec([export_(&#x27;main&#x27;, exportdesc.func(0))]), codesec([code(func([], [instr.end]))]), ]); return Uint8Array.from(mod.flat(Infinity)); </code></pre> 2. A very simple compiler for a language called Wafer, which looks like this:<p><pre><code> extern func setPixel(x, y, r, g, b, a); func draw(width, height, t) { let y = 0; while y &lt; height { let x = 0; while x &lt; width { let r = t; let g = x; let b = y; let a = 255; setPixel(x, y, r, g, b, a); x := x + 1; } y := y + 1; } 0 } </code></pre> In case you&#x27;re not a fan of JavaScript, we&#x27;ve already heard of readers who&#x27;ve worked through the book in F# and Haskell. :-D<p>You can check out a sample here: <a href="https:&#x2F;&#x2F;wasmgroundup.com&#x2F;book&#x2F;contents-sample&#x2F;" rel="nofollow">https:&#x2F;&#x2F;wasmgroundup.com&#x2F;book&#x2F;contents-sample&#x2F;</a> — we&#x27;d love to hear what you think! There&#x27;s also a 25% discount for HN readers — just use the code HNWASM25 at checkout.

1 comment

marianoguerra大约 2 个月前
Hi! I&#x27;m the book Co-author, here&#x27;s an experiment that didn&#x27;t end up (yet?) in the book: an interactive binary operation inspector:<p>short demo: <a href="https:&#x2F;&#x2F;x.com&#x2F;warianoguerra&#x2F;status&#x2F;1759988555869266329" rel="nofollow">https:&#x2F;&#x2F;x.com&#x2F;warianoguerra&#x2F;status&#x2F;1759988555869266329</a><p>longer writing: <a href="https:&#x2F;&#x2F;marianoguerra.org&#x2F;msite&#x2F;binary-tetris&#x2F;" rel="nofollow">https:&#x2F;&#x2F;marianoguerra.org&#x2F;msite&#x2F;binary-tetris&#x2F;</a>