HN Theater @HNTheaterMonth

The best talks and videos of Hacker News.

Hacker News Comments on
Build Your Own WebAssembly Compiler

Colin Eberhardt · InfoQ · 144 HN points · 0 HN comments
HN Theater has aggregated all Hacker News stories and comments that mention Colin Eberhardt's video "Build Your Own WebAssembly Compiler".
Watch on InfoQ [↗]
InfoQ Summary
Colin Eberhardt looks at some of the internals of WebAssembly, explores how it works “under the hood”, and looks at how to create a (simple) compiler that targets this runtime.
HN Theater Rankings

Hacker News Stories and Comments

All the comments and stories posted to Hacker News that reference this video.
Dec 27, 2019 · 144 points, 19 comments · submitted by simonpure
catpolice
Last year I decided to write a WASM-targeting compiler in Javascript, completely by hand. The idea was to compile a vaguely C-like toy language to WASM without any external dependencies.

Part of the motivation was that while I've written many simple parsers, I mostly used parser generators with BNF grammars and I didn't feel like I had a good sense of how the code I ended up generating actually worked for something with complex syntax and semantics. I felt like I was writing specs for a parser, rather than writing a parser. And I didn't have a huge amount of experience with code generation.

My toy language has vaguely C-like syntax with block scope and infix operators with various precedences, so it was a bit more complicated than JSON, but I ended up using something like Pratt parsing/Precedence Climbing (see https://www.oilshell.org/blog/2017/03/31.html) and wrote the whole thing in a way that's - hopefully - pretty easy to read for folks interested in wrapping their head around parsing complex syntax (e.g. with scope and name resolution). The lexer, parser and language definition ended up being about 1000 lines of JS (counting some pretty extensive comments).

Code generation is pretty straightforward once you have an AST.

Any JS programmers that are interested in really getting into the nitty-gritty of writing your own parser/compiler should check it out. The source is here: https://github.com/j-s-n/WebBS.

If you want to play around with the language and inspect the generated ASTs and WASM bytecode, there's an interactive IDE with example code here: https://j-s-n.github.io/WebBS/index.html#splash

vchak1
I found this one pretty interesting. https://maierfelix.github.io/mini-c/
ridiculous_fish
Are there any fast ways to run wasm on systems that forbid creating new executable pages? A high performance interpreter?
vbezhenar
What system do you mean? iOS supports wasm AFAIK.
jimmcslim
Mobile Safari may support WASM, but I think the GP is suggesting that a native iOS app would not be permitted to load, compile, and execute a WebAssembly module.
vbezhenar
I might be wrong, but I think that one should be able to utilize JavaScript engine within native iOS app which will use fast implementation.
nickmain
JavaScriptCore and JS running in a web page using WKWebView are both running in the same app process and do not support WASM since they cannot generate code (cannot write to executable memory pages).

SFSafariViewController does support WASM by executing in a separate more privileged process but interaction with the app is very limited.

saagarjha
WKWebView does as well because it too runs out-of-process. You can load WebAssembly into it.
throwGuardian
But WKWebView is equivalent to running inside a browser, with no system access.

If JSCore allowed WASM JIT-ing, one could easily port WASI to iOS for full system access, and update iOS apps by downloading new wasm versions. Of course Apple won't do that citing __Insert__App__Store__excuse__here, but everyone knows it's because they want that sweet 30% cut, and that can only be effectively enforced with a tight chokehold on updates

ridiculous_fish
Not iOS but it's an interesting thought experiment: if you wanted to build a fast WASM implementation for iOS, how would yo do it?
e_y_
If your WASM code doesn't change (isn't being downloaded at runtime from the Internet), then you could ahead-of-time compile it to native machine code (ARM/ARM64) and link it into your app.

It doesn't look like anyone's tried to do it for iOS but InNative has a compiler for Windows/Linux.

maxgraey
check this one: https://github.com/wasm3/wasm3
ridiculous_fish
Nice find, only 4.5x slower is nothing to sneeze at. I doubt that WASM is optimal for interpretation and you could probably gain another 2x through a bytecode compilation target optimized for interpreting.
vshymanskyy
Actually, it's not interpreted directly. Wasm is converted to a very interpreter-friendly representation.
Aardappel
Documentation on how Lobster's Wasm-backend works, which contains plenty of useful info for anyone making their own backend: http://aardappel.github.io/lobster/implementation_wasm.html
pjmlp
Cool, thanks for sharing.
ryohey
Here’s my wasm parser/runtime written in TypeScript, for instance. https://github.com/ryohey/ts-wasm-runtime
6nf
My self hosted compiler: https://github.com/PierreRossouw/wats
thomasballinger
Here's Colin's blog post with code and diagrams inline: https://blog.scottlogic.com/2019/05/17/webassembly-compiler....

I found Colin's posts real helpful when writing some WebAssembly by hand for the first few days of Advent of Code: https://observablehq.com/collection/@ballingt/advent-of-wasm

HN Theater is an independent project and is not operated by Y Combinator or any of the video hosting platforms linked to on this site.
~ yaj@
;laksdfhjdhksalkfj more things
yahnd.com ~ Privacy Policy ~
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.