r/C_Programming • u/8d8n4mbo28026ulk • Jul 05 '26
Library-fying AWK with WASM
https://napcakes.nekoweb.org/posts/libawk-wasm/
9
Upvotes
r/C_Programming • u/8d8n4mbo28026ulk • Jul 05 '26
View all comments
2
u/skeeto Jul 05 '26 edited Jul 05 '26
Two posts on hash-tries, now this fascinating piece! Your blog is off to a great start. Get that Atom feed working, please, so I can follow properly.
I, too, have been fascinated by Wasm as a way to cleanly isolate programs, but I didn't know about wasm2c. Particularly the possibility of a runtime somewhere in the sweet spot between a heavy-weight JIT (e.g. via LLVM) or a slow interpreter (e.g. wasm3). That's a neat technique.
Though of course it would be better if programs were better architected in the first place to abstract these interfaces from the get-go! I know it's a classic, but glancing at nawk, that is one badly architected program. It builds on awful libc interfaces, and has 150 global variables. Fixing all this to produce a clean, non-Wasm version of libawk would touch most lines of the program, strong evidence of its poor architecture. There are many compromises to make it fit the constraints of a 1970s computer, trade-offs that haven't made sense for some 40 years now.
Edit: That transformation looks something like this:
With just
read/writeinAwkOpsI wondered how you dealt withopen/close, but I see it got the same treatment aspopen: disabled. I know nawk doesn't bother freeing memory, which is a fine choice, and an far-too-underused approach today, but something a libawk has to address. Due to the globals, libawk must initialize a whole Wasm instance perAwkanyway, so that stops the leakage for free, too.Plus its own copy of libc, at least containing stdio and formatted I/O! Quite a bit of redundancy.
In my experience Wasm has great documentation, and the specification is quite readable. It's WASI that's a complete disaster, evidenced by the fact that no two implementations are compatible. The specification is too incomplete to be useful, and the de facto specification is the Rust-based reference implementation with an ambiguous ABI. Better to think of it not as a specification but as a piece of a particular toolchain.