Zig and WASM(observablehq.com)
observablehq.com
Zig and WASM
https://observablehq.com/@yurivish/zig-wasm
66 コメント
Hi Charles, this is my fault for missing the attribution – I'm very sorry.
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.
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.
No problem Yuri, thanks for the credit and the apology! I do recommend writing articles based on doing the thing from scratch yourself though as you can write something more nuanced and interesting that way.
First reaction: chill, it’s a super basic example…
Looking closer to it: boy he didn’t even care to change the log message.
Thanks for the example meheleventyone!
Looking closer to it: boy he didn’t even care to change the log message.
Thanks for the example meheleventyone!
This is actually the second time this really basic example has been ripped off by someone else for internet clout.
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.
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.
That sucks. I'm happy to flag this article, as it's rather light on content and stealing the little content it has is not cool. Recommend others do the same.
I've been working with Zig lately (after 2 years of rust), and I have to say that the lack of borrow checker allows a lot of flexibility. You might argue it is dangerous, but in some case it's ok.
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.
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.
For a somewhat more complete example which runs in browsers, check out my little pacman.zig toy project [0]. This is cheating a bit by using the Emscripten SDK as 'sysroot' (needed for the Emscripten headers to provide the glue to web APIs), and the Emscripten linker to create the additional html and js output file, but the actual code is all compiled with the Zig compiler to WASM, emcc is only used for linking.
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
[2] https://github.com/floooh/sokol-zig
[3] https://github.com/schellingb/wajic
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
[2] https://github.com/floooh/sokol-zig
[3] https://github.com/schellingb/wajic
Zig is probably the most common tool to compile C/C++ to WebAssembly outside the browser, but the Zig language itself has first-class support for WebAssembly/WASI.
The resulting modules are small and very optimized.
The resulting modules are small and very optimized.
The most common tool is Emscriptem, even the Rust toolchain depends on it.
Rusts wasm32-wasi and wasm32-unknown-unknown targets don't depend on Emscripten and they've been the preferred ways of targeting wasm for years now.
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.
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.
wasm32-unknown-emscripten is useful if you want to build a Rust program using SDL2 and GL for the Web, for example. But yes, as you said, it's much less used. Most Rust code on the Web is not a port of existing applications (which is what Emscripten focuses on), unlike C++.
Pity that not all tutorials have been updated, as I recently hit that bump, unfortunely I don't have the tutorial link any longer.
If you're targeting browsers then the Rust WASM book goes over using wasm32-unknown-unknown with wasm_bindgen to auto-generate the JS glue
https://rustwasm.github.io/docs/book/
If you're targeting some WASI-based platform then it's more straightforward, since no JS glue is needed. CF have a quick example
https://blog.cloudflare.com/announcing-wasi-on-workers/
https://rustwasm.github.io/docs/book/
If you're targeting some WASI-based platform then it's more straightforward, since no JS glue is needed. CF have a quick example
https://blog.cloudflare.com/announcing-wasi-on-workers/
Thanks for the links.
For modules to be run in a web browser, yes.
Not outside the browser, with WASI instead of a Javascript context.
Not outside the browser, with WASI instead of a Javascript context.
A rounding error in market share.
Whenever there is a post relating to Zig, I can always rely on you to post negative comments for no real reason. I can understand not liking the language, but this is about the C/C++ compilation toolchain, which uses clang under the hood. Even if you think Zig is the worst language in the world, you can still use the toolchain without having to deal with the language at all.
I guess my fundamental question is, what does your comment add to the conversation? It seems like you're not even disagreeing with the fundamental premise; you just felt the need to diminish Zig simply because someone tried to acknowledge that it was useful for something.
I guess my fundamental question is, what does your comment add to the conversation? It seems like you're not even disagreeing with the fundamental premise; you just felt the need to diminish Zig simply because someone tried to acknowledge that it was useful for something.
Had the OP not stated that "Zig is probably the most common tool to compile C/C++ to WebAssembly outside the browser,...", I wouldn't have said anything.
You say it yourself, this is about the C and C++ toolchain offered by clang, ergo not Zig.
I only reply on hype regarding Zig, like the previous remark, or it being "safe", when it is as safe as Modula-2 already was in 1978.
Which while miles beyond C, it wasn't without flaws.
As for what adds to the conversation, I guess taming a bit the hype, regardless of the language.
If you are so attentive, you will have remarked that I spare no language, all get their share.
Finally, if you don't like my comments, page down is your friend.
You say it yourself, this is about the C and C++ toolchain offered by clang, ergo not Zig.
I only reply on hype regarding Zig, like the previous remark, or it being "safe", when it is as safe as Modula-2 already was in 1978.
Which while miles beyond C, it wasn't without flaws.
As for what adds to the conversation, I guess taming a bit the hype, regardless of the language.
If you are so attentive, you will have remarked that I spare no language, all get their share.
Finally, if you don't like my comments, page down is your friend.
What do you mean by “outside the browser”?
I am also trying to learn wasm via the zig route:
* 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
* 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
And just now console.log() :)
https://github.com/daneelsan/zig-wasm-logger
Excited for zig, waiting for a stable net module in standard library
What do you want from a net module? We're having an ok time using the built-in io_uring support in the stdlib, but there's not yet a generic event loop that abstracts over epoll/io_uring/select/kqueue/whatever Windows has.
I would like to have basically everything in the Go `net`, `io` and `ioutil` modules (especially functions to work with streams like io.Copy)
Using Wasi (opening/reading/writing files) via zig: https://christophvoigt.com/zig-file-open-in-wasi-via-preopen...
Application error: a client-side exception has occurred
Somewhat related, how does Zig compare to Rust? I've been learning Rust but also saw Zig gaining traction. I believe the JS runtime Bun is written in Zig which was on HN last week.
It tends to break down along the same lines as the C vs. C++ debate from 15 years ago.
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
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
ArrayBoundCheck(12)
https://github.com/meheleventyone/zig-wasm-test
Thanks for the credit yurivish! :(