9 ms·
Zig and WASM
- svnpenn 4y agoApplication error: a client-side exception has occurred
- hl05 4y agoExcited for zig, waiting for a stable net module in standard library
- anonymoushn 4y agoWhat 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.
- xmorse 4y agoI would like to have basically everything in the Go `net`, `io` and `ioutil` modules (especially functions to work with streams like io.Copy)
- jedisct1 4y agoZig 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.
- dave84 4y agoWhat do you mean by “outside the browser”?
- clpm4j 4y agohttps://www.infoq.com/presentations/wasm-server-api/ https://www.infoq.com/presentations/wasm-server-api/
- syrusakbary 4y agoShameless plug :P https://www.youtube.com/watch?v=OHmycSgFAUs https://www.youtube.com/watch?v=OHmycSgFAUs
- pjmlp 4y agoThe most common tool is Emscriptem, even the Rust toolchain depends on it.
- jsheard 4y agoRusts 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.
- azakai 4y agowasm32-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++.
- pjmlp 4y agoPity that not all tutorials have been updated, as I recently hit that bump, unfortunely I don't have the tutorial link any longer.
- jsheard 4y agoIf 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/ 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://blog.cloudflare.com/announcing-wasi-on-workers/
- pjmlp 4y agoThanks for the links.
- ArrayBoundCheck 4y ago
- turnsout 4y agoTell that to Figma
- azakai 4y agoPartial list of companies using wasm, in addition to Figma: * Adobe * Microsoft * Google * Unity * AutoDesk * Zoom * Snapchat (PlayCanvas) * Twitch and many others, and that's just on the C/C++ side, and just on the Web.
- zaphar 4y agoI use it for some personal stuff because I like using non-javascript languages in the browser. For that reason I don't consider it useless at all.
- cdelsolar 4y agoWe use WASM to provide in-browser Scrabble-like analysis at https://woogles.io https://woogles.io at the end of games. The original analyzer was built in Rust and the speed is close to native.
- ArrayBoundCheck 4y ago> the speed is close to native The numbers I seen suggest wasm is 3-5x slower then native which isn't bad IMO. JavaScript is also 3-5x slower than native. Is JS close to native speed? I don't think you can say it about one but not the other
- azakai 4y agoThat estimate is way off. Wasm overhead is somewhere in the range of 50% (1.5x slower) to 14% (1.14x slower). Sources: * https://www.usenix.org/system/files/atc19-jangda.pdf https://www.usenix.org/system/files/atc19-jangda.pdf * https://kripken.github.io/blog/wasm/2020/07/27/wasmboxc.html https://kripken.github.io/blog/wasm/2020/07/27/wasmboxc.html It is true you can find a specific benchmark where wasm is 3-5x slower, say if the original uses highly-tuned x86 SIMD or relaxed atomics (or if the wasm version has SIMD or threads turned off entirely). But in general, the overhead is much lower.
- kuon 4y agoI'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.
- flohofwoe 4y agoFor 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 https://github.com/floooh/pacman.zig [1] https://github.com/floooh/sokol https://github.com/floooh/sokol [2] https://github.com/floooh/sokol-zig https://github.com/floooh/sokol-zig [3] https://github.com/schellingb/wajic https://github.com/schellingb/wajic
- meheleventyone 4y agoWow, this just straight rips off my (not very great) simple example I've been maintaining for a while: https://github.com/meheleventyone/zig-wasm-test https://github.com/meheleventyone/zig-wasm-test Thanks for the credit yurivish! :(
- voigt 4y agoFirst 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!
- meheleventyone 4y agoThis 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.
- mattgreenrocks 4y agoThat 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.
- yurivish 4y agoHi 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-compiling-zig-to-wasm32-freestanding 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.
- voigt 4y agoUsing Wasi (opening/reading/writing files) via zig: https://christophvoigt.com/zig-file-open-in-wasi-via-preopen/ https://christophvoigt.com/zig-file-open-in-wasi-via-preopen...
- cercatrova 4y agoSomewhat 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.
- dralley 4y agoIt 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
- daneelsan 4y agoI 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 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 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 https://github.com/daneelsan/zig-wefx
- daneelsan 4y agoAnd just now console.log() :) https://github.com/daneelsan/zig-wasm-logger https://github.com/daneelsan/zig-wasm-logger