r/C_Programming • • Jul 05 '26

Library-fying AWK with WASM

https://napcakes.nekoweb.org/posts/libawk-wasm/
9 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/8d8n4mbo28026ulk Jul 05 '26

Thanks for the kind words and... I'm on it!

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.

Yeah, it isn't great in that regard. However, I forgive all of this just because how easy it was to build it and play with it. And it's also a stand-alone project. It would be pretty painful to try this with, e.g. GNU awk, which is a ~800K binary -- you can imagine how that would go with WASM, LOL! I suspect BusyBox' wouldn't be easy either...

Fixing all this to produce a clean, non-Wasm version of libawk would touch most lines of the program

Indeed, if it was written differently, the crazy WASM stuff wouldn't be needed in the first place. I knew this going in, because I wanted to test whether WASM can be used in such a way.

This was me just toying around; I don't think I'll ever need to embed AWK in one of my programs. Wikipedia lists some projects with that use in mind though, so there exist options out there.

I wondered how you dealt with open/close, but I see it got the same treatment as popen: disabled

And shamelessly, I might say, haha! I wrote libawk with dedup-wawk in my mind. I knew that if I could make that work with some sane API, everything else would be a case of "just do it". nawk reads from stdin by default, so open() and friends weren't necessary for the test application.

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.

Good point. If I was serious about this, I'd reimplement the whole memory subsystem myself and most likely use an arena that would be rollbacked whenever necessary. wasm2c let's you do it -- it was just easier to use the default implementation.

In my experience Wasm has great documentation, and the specification is quite readable. It's WASI that's a complete disaster

Ah, right, here's me mixing up terms in the very paragraph I'm saying I'll mixup terms! I only skimmed through WASM's documentation, just to understand a little how it all works. It never got in my way, so I didn't spend a lot of time on it. WASI, though, is exactly what you said: a complete disaster (I'd be even more harsh).