WebAssembly from the Ground Up – learn WASM by building a compiler
57 points by marianoguerra
57 points by marianoguerra
Hi!
We (pdubroy & marianoguerra) just launched an online book called WebAssembly from the Ground Up. It’s an online book to learn Wasm by building a simple compiler in JavaScript.
This is the book we wish we’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.
We focus on the core of WebAssembly: the module format and the instruction set. We think the low-level details — the “virtual ISA” — are the most interesting part, and we had the crazy idea that writing a compiler is the best way to learn it.
Over the course of the book, you’ll build up two things:
1: An “assembler library”, that can be used to produce WebAssembly modules. Here’s an example:
const mod = module([
typesec([functype([], [])]),
funcsec([typeidx(0)]),
exportsec([export_('main', exportdesc.func(0))]),
codesec([code(func([], [instr.end]))]),
]);
return Uint8Array.from(mod.flat(Infinity));
2: A very simple compiler for a language called Wafer, which looks like this:
extern func setPixel(x, y, r, g, b, a);
func draw(width, height, t) {
let y = 0;
while y < height {
let x = 0;
while x < 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
}
In case you’re not a fan of JavaScript, we’ve already heard of readers who’ve worked through the book in F# and Haskell. :-D
You can check out a sample here: https://wasmgroundup.com/book/contents-sample/ — we’d love to hear what you think! There’s also a 25% discount for Lobsters readers — just use the code LOBSTERS25 at checkout.
The PDF version of the site/book has some issues. Code splitting across pages, poor line spacing between the end of code samples and text from the next paragraph, copying the code does not save newlines either and this differs from the website.
Also, maybe I’m a stickler for media, but if I get a book I expect there to be an “end” and not a code sample section named “exports” 😅
I’d very much like to see at least the page breaks improved. 🙂
We are still working on the pdf, it wasn’t planned for the initial release but we got it to a point where it’s useful for people that prefer to read on an e-reader so we decided to release it too.
Regarding the exports subsection, maybe the code there influnced us too much 😄. The main objective of that section is to export the things that the next chapter is going to import, hence the name.
The first section of most chapters is setup instead of begin/start 😄
Gotcha! Looking forward to improvements then. I tend to read books like this on a tablet 😅
The main objective of that section is to export the things that the next chapter is going to import, hence the name.
I was actually referring to the final page in the PDF! The book just shows an “exports” printout and then just… ends 😔
Thanks for the feedback!
Also, maybe I’m a stickler for media, but if I get a book I expect there to be an “end” and not a code sample section named “exports” 😅
Can you clarify what you feel is missing here? Are you referring to the Conclusion of the book, or to the way that the individual chapters end?
Specifically the very last page of the PDF is the last part of “Odds and Ends”. The very last code sample just… cuts onto a new page (and in a way where the link to stackblitz is the only link available), and then it just… ends. The acknowledgements section that follows on the site is missing, for example 😅
Ok, got it, thanks! Good point about the Acknowledgements being missing, that’s just a bug/oversight.
On the same topic, I noticed the book has no hyphenation. Was that on purpose?
Do you mean why don’t we use full justification?
If so, no, it wasn’t really a conscious decision. It’s rare to use full justification on the web, and this was designed first and foremost to be read online.
And tbh, I had never noticed that many technical books use full justification! I will certainly give it a try in the PDF at least.
So this is an ad for a paid book?
oh, the absolute horror of a six year old user of this site talking about their passion project in a way that allows a casually interested programmer to dig into this topic more.
In defense of the parent, not a huge contributor in that 6 years, and submits their own stuff with high probability. So… I’m kinda with Wilhelm, even if the book looks neat.
a comment similar to this one is why I stopped submitting a while ago, since then I only lurk.
I went to check, here’s a quick summary of my submissions:
second time I get this type of feedback here, it seems I can’t come here to share the technical stuff I do, even when the majority is done in the open and for free.
it seems I can’t come here to share the technical stuff I do,
Correct, if that’s your only interaction with the site. To quote https://lobste.rs/about:
Self-promotion: It’s great to have authors participate in the community, but not to exploit it as a write-only tool for product announcements or driving traffic to their work. As a rule of thumb, self-promo should be less than a quarter of one’s stories and comments.
It seems 5 years ago you wrote a few comments that were not about your own work, and that’s been it.
This softly imposed balance seems like it just results in contrived responses to satisfy the constraints. It’s like those subreddits where you want your writing critiqued, but you have to link to two instances of you critiquing other work first. What happens is that you get low quality empty feedback to satisfy the metric of what counts as contributing. I think there’s nothing wrong with people sharing their work and having that be their main form of contribution. This site has an upvoting and flagging functionality and is invite-based, let that be the arbiter of what is worthy of attention and what is not.
Creating and sharing prototypes, open source and technical demos and blogposts is the main way I can contribute.
Going to lurk again then.
There’s no need to just lurk! Post some comments and enter into discussions! If you want your stuff to show up organically, it will if you engage. You obviously have opinions and experiences that are valuable to the community. Share them! Submit the things you’re reading on your way to new work.
One issue I find with the culture here is that (a) lobsters is a really nice aggregator and is mostly sufficient for my reading needs and (b) I also have original stuff to I would like to share. If the content is all either stuff already on lobsters or something I created how can I post anything?
My point is a little extreme; I have very occassionally cross posted things I found on the orange site, and I do participate in comments. But I literally haven’t bothered write some articles because the only way they would get found is if I posted them here, and every time I read a thread exactly like this one I get a little bit sadder about it.
@marianoguerra - I think this is pretty cool, thank you for sharing :)
same, I use lobsters, HN and newsletters to find things, I see them after the fact, I almost never read or participate in the comments, I also don’t feel like commenting a lot.
Being forced to submit stuff and comment when I don’t feel like it to meet a quota feels like gaming the system more than what I actually do. But someone showed the rules in a comment and it seems I either game the metrics or I don’t participate at all. I’m not going to game the metrics.
I’m going to go out on a limb and suggest you ignore the rules and their pedants, and just post your work, which I think is really valuable, whenever you feel it’s ready to share. Frankly, these rules have never been consistently enforced. As long as you’re not acting like a self-promoting huckster, nobody’s going to kick you out or delete your stories or anything like that.
But I literally haven’t bothered write some articles because the only way they would get found is if I posted them here, and every time I read a thread exactly like this one I get a little bit sadder about it.
This perspective is baffling to me. Write because you have something to say, not because promoting it will lead to “fame.”
If someone writes something, they have an audience in mind they want to reach, if that audience is well represented in lobsters it makes no sense to discourage sharing it here. The person may as well write it and delete it if sharing is seen as a bad thing.
I get that the bootstrapping problem for getting your work seen is difficult, but Lobsters is just not a self promotion platform. That’s what the community has decided over its lifetime.
Users who actively engage in comments and submit stories are welcome to occasionally self promote. You can certainly turn a comment into a blog post and post back to the thread “I’ve expanded on this in this other piece.” Maybe someone will “promote” it to its own post then, if it provides a unique enough perspective, etc.
You will find that engagement here leads to views on your profile linked sites, and maybe even RSS subscribers! The more you contribute, the more likely people notice, agree with your perspective and want to surface your other stuff.
I am not a mod, and I am not trying to police you or anyone else. I am merely suggesting that the community has some guidelines and you appear to not be following them.
Take it how you will.
Fame - god no.
I get the “writing for yourself” thing. I have a bunch of markdown on my computer; it’s a normal part of research and figuring things out. There’s plenty of things to say.
I also teach (occassionally at a local university). I takes time to prepare material. I find it worthwhile because its nice to see the class learn, discuss with students, help with difficulties, etc.
Turning any of my learnings into a digestible article and then finding a way to publish it will take time and energy. If its not going to be read, or discussable on a forum, then I am going to spend my limited spare time doing something else.
You can read the first 3 chapters online or as a PDF for free, use the resulting code and the library we extracted from the book (all MIT licensed, linked in a another comment).
You can also read the docs from https://ohmjs.org/ which covers part of the content in the other chapters and was created by one of the book’s co-authors (pdubroy).
In this post you can learn how to implement arithmetic operations: https://wasmgroundup.com/blog/wasm-compiler-in-a-tweet
It’s a challenge to balance the need to avoid too much promotional material versus wanting to know what folks are working on. This reminds me of the book Polarity Management. Neither extreme end is ideal. What we want is a good balance between the two.
The library is here: @wasmgroundup/emit
you can play with the code right in the browser here: stackblitz.com/github/wasmgroundup/code
The code repo is here: https://github.com/Wasmgroundup/code