90 points · by yurivish · 4y ago · 62 comments
https://github.com/meheleventyone/zig-wasm-test
Thanks for the credit yurivish! :(
I've just added a credit to you and your repository (see the second sentence).
I had put together this minimal example based on your repository together with a StackOverflow answer containing the build command (https://stackoverflow.com/questions/68476647/errors-with-com...).
Being just a single file with a simple build command it seemed like a minimal advancement on the state of the art, so I quickly decided to publish, and did not appropriately credit the original as I should have. I hope you can accept my apology – this was an honest mistake.
Looking closer to it: boy he didn’t even care to change the log message.
Thanks for the example meheleventyone!
It used to be a bit more complex and fiddly which is why I made the repo initially to gather some disparate info and my own contribution into one place. Now this is something you can put together with zero experience with Zig in a spare half an hour. Which really is as it should be with first-class support for WASM. I mostly maintain this because it's nice to have.
I think Zig complements very well rust for some applications. And it is very exciting to have both. This WASM example shows how simple and straightforward it is.
I really hope that they are not going "to fight", by that I mean the communities and not the languages themselves. I've already seen some people being "religious" about one or the other (HN included), even insults. We should be very happy to have new tools to build better softwares and not fight.
I wish the very best to both communities and languages.
The interesting part is that the platform abstraction is provided by the sokol C headers [1], with auto-generated Zig bindings [2]. It's interesting because the C headers use "Emscripten magic" (mainly embedding Javascript snippets in the C sources via the EM_JS() macro), and the Zig compiler is able to compile this (when it has access to the Emscripten headers).
It would be nice if the "Emscripten platform" could get the same type of cross-compilation support as the desktop platforms eventually, but apart from bundling the Emscripten headers, this would also require to implement some of the "Emscripten magic" in the Zig linker.
Maybe projects like WaJIC [3] can help with this (this basically implements the "Emscripten magic" of embedding Javascript snippets in C/C++ source code, but without Emscripten (only the wasm-opt tool is needed AFAIK).
Anyway... it's a lot of fun to tinker around with this stuff in Zig :)
[0] https://github.com/floooh/pacman.zig
[1] https://github.com/floooh/sokol
The resulting modules are small and very optimized.
There is a wasm32-unknown-emscripten target but I think that's basically on life support if not outright deprecated. It was only really a stop-gap until native tooling like wasm_bindgen got fleshed out.
* A minimal example showing how HTML5's canvas, wasm memory and zig can interact: https://github.com/daneelsan/minimal-zig-wasm-canvas
* This mini game shows how Zig, WASM, Javascript and HTML5 canvas can interact: https://github.com/daneelsan/Dodgeballz
* WEFX is a simple graphics drawing package using Zig, WASM, and an HTML canvas: https://github.com/daneelsan/zig-wefx
People who are willing to accept a lot of complexity in order to solve certain problems or gain expressiveness tend to prefer Rust. It's a complex language with a rough learning curve and not-great compile times, but it does help solve some really difficult problems.
People who prefer simpler, more straightforwards languages (like C and Go) tend to prefer Zig. The core of the language is much smaller and more C-like, the (new) compiler is really fast and uses less memory in comparison to Rust or C++. The selection of features it does provide is very tasteful and clever in a way that has been really impressive. They do a huge amount with very little.
e.g.
* macros are avoided entirely by using compile time code evaluation and dead code elimination
* "generics" are implemented by using types as function arguments (no special syntax to learn) and compile time code evaluation