Live data from Hacker News

Build Your Own WebAssembly Compiler

infoq.com

1–10 of 20 posts

Re: Build Your Own WebAssembly Compiler

#2
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

Re: Build Your Own WebAssembly Compiler

#3
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

Re: Build Your Own WebAssembly Compiler

#6

Are there any fast ways to run wasm on systems that forbid creating new executable pages? A high performance interpreter?

What system do you mean? iOS supports wasm AFAIK.

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.

Re: Build Your Own WebAssembly Compiler

#7

Earlier quoted context omitted.

What system do you mean? iOS supports wasm AFAIK.

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.

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.

Re: Build Your Own WebAssembly Compiler

#9

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 w…

I found this one pretty interesting. https://maierfelix.github.io/mini-c/

Re: Build Your Own WebAssembly Compiler

#10

Earlier quoted context omitted.

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.

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.

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.

Post reply on HN