NHacker Next on Rails
  • top
  • new
  • best
  • ask
  • show
  • jobs
login
GuidelinesFAQListsAPISecurityLegalApply to YCContact

Hacker News on Rails, powered by React on Rails Pro. Open the source on GitHub by ShakaCode.

Commit 06fc0df

Rendered at 09:05:34 GMT+0000 (Coordinated Universal Time) with Rails + React on Rails RSC.

Wax: A User-Friendly Syntax for WebAssembly (tarides.com)

203 points by theanonymousone 17h ago | 48 comments

sheept 17h ago
Not super related, but I love that the text representation of wasm is named WAT, it feels like a callback to the infamous Javascript talk, Wat[0]

[0]: https://www.destroyallsoftware.com/talks/wat

nkjoep 16h ago
Watman.
pcwalton 15h ago
Reminds me a bit of GCC's GIMPLE. For example: https://gcc.gnu.org/onlinedocs/gcc-4.2.3/gccint/GIMPLE-Examp...

Unlike LLVM IR, which has a wildly different syntax from any other language, GCC's SSA IR was designed to look like a subset of C. There are some obvious benefits of doing it that way. Wasm can be even closer to a high level language because it has high level control flow...

rzzzt 13h ago
asm.js was exactly this: https://en.wikipedia.org/wiki/Asm.js
gavinray 16h ago
This is essentially just Rust with the compile target set to WASM, though?

WASM Text (WAT) is not really meant to be human-authored, it's an IR representation, like LLVM IR.

The one genuine use I could see out of this is the ability to go backwards, from WAT -> High-level language

  > "Compilers usually run one way. The most interesting engineering in Wax is in the reverse arrow."
masklinn 16h ago
> This is essentially just Rust with the compile target set to WASM, though?

It's not rust at all, it's a rust-like representation of wasm bytecode.

> WASM Text (WAT) is not really meant to be human-authored, it's an IR representation, like LLVM IR.

Which is exactly why TFA wrote an alternate representation, they're writing the runtime for a wasm target so they specifically want to craft their bytecode, and WAT is not a great thing to write.

Onavo 15h ago
Tbh I don't understand why people don't like the intermediate representation. It's ridiculously high level compared to say x64 or ARM or even RISC V assembly. It's a simple lisp that most high schoolers who have taken basic programming can read it without issue.

I can't think of a single other production level VM with such an easy to read decompiled form.

cestith 13h ago
People who hate Lisp or hate S-expressions generally won't like WAT. That's what I see in Wax - a Rust lover wants something more Rust-like than Lisp-like.

Lisps are really handy, though, because it's basically textual representation of an AST. Code is data, data is code, and a tree walker with minimal lexing and parsing can interpret the thing through (eval ), compile it, or make transformations to the tree. There's not much hidden semantics in special syntax.

masklinn 13h ago
Except this lisp does not come with quoting or eval, and if you want to parse it you’ll probably have an easier time with the bytecode itself.
koolala 10h ago
IR needs a massive compiler to use.
LeFantome 15h ago
CIL
ethin 10h ago
Maybe people don't like it because it's LISPy and practically unreadable?
xienze 11h ago
Well the problem with an alternate syntax like this is that it's not really 1:1 with WAT so it ends up being hard for you easily figure out how to derive an exact WAT form from your starting language. WAX wouldn't eliminate this problem.

What I've been doing is making a simple library that can build WASM instructions in memory which closely follows the syntax and semantics, and using my programming language of choice as basically a preprocessor that provides high level abstractions over raw WAT/WASM. That method is VERY amenable to optimization, either by hand or via LLM. Because you can run your code generation, figure out how the bytecode should change, and work backwards. And if you take care while writing it, it can be very easy to follow the code.

masklinn 11h ago
> Well the problem with an alternate syntax like this is that it's not really 1:1 with WAT

The author claims it is so and wax can be converted back and forth with wat or wasm bytecode. Do you have evidence to the contrary?

> What I've been doing is making a simple library that can build WASM instructions in memory which closely follows the syntax and semantics, and using my programming language of choice as basically a preprocessor that provides high level abstractions over raw WAT/WASM.

That is a completely different purpose unrelated to the goals of wax.

xienze 11h ago
> Do you have evidence to the contrary?

What I mean is, you can't effectively steer a high level language to produce some exact sequence of bytecode. It's not "1:1" with what you end up with, it's the nature of the beast. To get that, you have to write in WAT or a higher level WAT generator that you CAN steer.

As a trivial example, `a = 1; a = a + 1` (note: I'm not using this exact language, just speaking in generalities). Will the compiler produce the naive bytecode:

```

(local $a i32)

i32.const 1

local.set $a

local.get $a

i32.const 1

i32.add

local.set $a

```

Or will it perform constant folding and give you:

```

(local $a i32)

i32.const 2

local.set $a

```

Maybe, maybe not. Who knows! Now that's a trivial example obviously, but there's plenty of places where there are optimizations you'd LIKE to happen but have no control over. Which no doubt WAX is susceptible to.

> That is a completely different purpose unrelated to the goals of wax.

But it's the only way to truly shape the end-result bytecode into EXACTLY what you want when starting from a higher level language.

masklinn 14m ago
> What I mean is

wrong, because you apparently refuse to read

> you can't effectively steer a high level language

wax is not a high level language

> It's not "1:1" with what you end up with, it's the nature of the beast.

Which would be relevant if you were looking at the correct page in the bestiary.

> note: I'm not using this exact language, just speaking in generalities

Which is a bad idea, given this is a about a specific language, which is not an HLL, which is designed for the explicit and specific purpose of round-tripping through the bytecode and text representations, and which has a god damn live playground.

> Maybe, maybe not. Who knows!

The language spec: https://ocsigen.org/wax/correspondence/round_trip.html

> there's plenty of places where there are optimizations you'd LIKE to happen

Not for wax. Because that's not the point. Which you keep missing.

> Which no doubt WAX is susceptible to.

gods below...

> when starting from a higher level language.

And again that's where you're way off base, on a different continent entirely, despite having been told that directly.

dapperdrake 14h ago
For tinkering with assembly programming style development wat and wat2wasm are excellent. Shame that wasm doesn’t really have sin(), cos(), and tan(). Makes tinkering with gui code that much harder. And having to manually link something like a math library is annoying for one-off experiments.

Calling the external JavaScript Math functions adds an incredible amount of latency as far as I recall. Has that been fixed for Math functions in the mean time?

ncurses1010 4h ago
I tried it on a slightly outdated version of node, either 22 or 24 and found that there was some latency but it wasn't terrible. One thing to keep in mind though is if you're repeatedly calling an imported function then it gets less bad (maybe some differences between the first tier wasm engine and the second)
davexunit 7h ago
I disagree with the implication that s-expressions are not user friendly. It's really an excellent feature that I wish the Wasm group stuck with for other things like WIT but alas. In Hoot, a Scheme to Wasm compiler, we take great advantage of WAT. We can make abstractions that generate WAT and splat it into our handwritten Wasm runtime library with quasiquote/unquote. We also have a macro for splicing inline Wasm into Scheme programs.
koolala 12h ago
Curious to see what its SIMD syntax looks like. Was this article / language LLM written? It's great Wax compiles easy in the browser.
vertex_wharf 15h ago
Reverse direction is the only use-case I have. Previously I’ve used seqtk (via biowasm) in the browser to demo how to calculate GC content for a stats class. It works, but it’s a bit opaque in a way it wouldn’t be if I wrote the code in Python (can’t read the code before running it). Could use Wax for this (though need to check how good it is at decompiling modules compiled from C first).
julesrms 16h ago
Very nice.

Having done a bit of staring-at-WASM while building a compiler, I never stopped being annoyed by the question of who came up with that terrible parentheses syntax. I could never see how it made any sense for any use-case.

Obviously the very top-level parser for the parenthesis stuff is simpler than a parser for a more complex syntax like Wax's. But that is a tiny part of the parser's job, and the savings are marginal. Just because you start by parsing the text into a simple node tree, you still have to check that tree against the implicit syntax that you're expecting it to conform to, and a lot of that checking could have been avoided by pushing it into the actual syntax of a nicer language. They already have a binary format, so the whole point of this text format is to be human-readable, which it absolutely isn't. So the compromise it makes to make the parser very slightly simpler just seems mad to me. Anyway, end of rant..

hnlmorg 16h ago
WAT is just Sexpr isn’t it?

There’s a lot of people, myself included, who quite like s-expressions. And it’s not all that surprising that WAT is LISPy given JavaScript was originally intended to be a LISP too.

masklinn 16h ago
It's just sexpr but it's a rough sexpr since it's an exact translation of the bytecode:

    (func $add (export "add") (param $x i32) (param $y i32) (result i32)
      (i32.add (local.get $x) (local.get $y)))
In clojure using native annotations, that would be along the lines of

    (defn add ^int [^int x ^int y]
      (+ x y))
yawaramin 14h ago
WAT was never intended to be a high-level language like Clojure, it's a text representation of WebAssembly.
masklinn 14h ago
Yes?

The point is that while "WAT is just sexpr" it's not exactly a great lisp, or experience.

yawaramin 1h ago
Yes?

My point is that it was never designed to be. So measuring it against that standard doesn't make sense.

gavmor 12h ago
Yeah, "user-friendly" here just means non-homoiconic, Algol-style.
boxed 15h ago
> given JavaScript was originally intended to be a LISP too.

Isn't that basically just apocryphal handwaving? Javascript doesn't look like a lisp in any way, not syntactically obviously, but not behaviorally either. To say that he aimed for a lisp with C ish syntax either means he failed extremely bad, or that he thought lisp was shit.

masklinn 15h ago
It's a touch more complicated but according to Brendan Eich[0]

- he was originally hired to "do scheme", management believed they needed to embed a programming language and he was sold / believed that was an option

- by the time he got moved to the client and could work on scripting, Netscape was trying to get Java in Navigator (for the browser components), and management mandated a java-ish language for frontend scripting

- Eich got first-class functions from Scheme, and Self prototypes as an implementation detail to get an object model, as well as java's Date API and the primitive/object split

[0] https://brendaneich.com/2008/04/popularity/

masklinn 16h ago
As the essay notes, wat was mostly designed as a pretty printer, not necessarily a pretty one but one which would be clear to the compiler writer, such that they could cross-check and debug their codegen.
saghm 15h ago
I've been toying with using WASM as a way to expose custom scripting to expose to users, and because the product I'm working on already leans heavily into providing an agentic interface (which is both legitimately useful and sometimes a bit much...), and I've found that the agent seems pretty capable of writing valid WAT that fits the conventions described by the documentation that gets automatically fed into its context. It might be nice to have something more readable for me when trying to inspect what it creates, but on the other hand, apparently one of the product managers trying it out already decided to have his own agent just manually write JavaScript for him to look at and then map it to and from the WAT, which I'm not really convinced is a good experience for us to propose to customers, but I guess it works?
pyrolistical 9h ago
That’s neat

I wonder if I could use wax to transpile back to zig, which is comparable to use friendliness of wax

kccqzy 16h ago
This reminded me of a JIT project where pieces of code were directly written in LLVM IR. This is of course a terribly masochistic way of programming.

It took a build system expert in that project to finally allow the build system to express the concept of compiling C++ to Bitcode which is then used by JIT.

__s 14h ago
I revived an old project of mine: luwa https://github.com/serprex/luwa

Lua macroassembler for webassembly used to implement a Lua runtime. Now with microcode to synthesize interpreter eval & jit

gagan2020 14h ago
In the AI disruptive wave, old "User-Friendly Syntax" pitch is soothing.
asibahi 15h ago
Holy crap this is almost exactly what I was trying to go for in my own language.

https://ar-ms.me/projects/quince/

I must check this in depth and steal ideas.

Defletter 10h ago
It continues to baffle me why so many languages these days seem to loathe the return statement.
fhcbix 10h ago
Sir this is a We-bassembly text representation, not a high-level programming language.
munificent 9h ago
It's because they loathe statements. Once you get rid of those, then the only reason to have return is as an early exit.
samatman 14h ago
I know this is focusing on the wrong thing but, here we go

Wouldn't it be nicer to compare the string four bytes at a time for all but the tail?

And then compare the tail by comparing the last four bytes, and ignoring the "extra work" because there's no condition that way?

svachalek 9h ago
Yes that should be approximately 4x faster...
citizenpaul 11h ago
> a Rust-like syntax for WebAssembly

Why? Don't people generally consider Rust syntax difficult to learn? I'm jesting slightly I love Rust. Just saying it kinda has a ring of hey this is hard so I replaced it with this other hard thing as an abstraction layer to the hard thing.

XKCD there are now 17 competing standards is whats in my head.

koolala 4h ago
Wasm uses data types very strictly
boguscoder 16h ago
Good idea but AI bleeds through text so much it’s repelling.
deklesen 16h ago
Who the hell needs user-friendly syntax anymore? Sorry, but, ...
spoiler 16h ago
If you mean AI use, then AIs will benefit from it as well, like from multiple angles, direct and indirect. It can also inspire other ideas or tooling around it, move the whole ecosystem forward if it gains traction.

It can be a great learning tool or aid, like if you're trying to deepen you comprehension of WASM, being able to toggle between multiple formats (one of which is relatively familiar to most Devs) can help speed that up by a lot.

Lots of reasons to make things user friendly

connicpu 16h ago
Even if you're someone who never even looks at the code their LLM spits out, all the big LLMs are trained on way more rust code than lisps, so it stands to reason they will likely do better with a rust-like syntax.