WebAssembly is easy – a hello world example
fodor.org
fodor.org
In fact there's quite a lot that's inaccurate with this short article. WebAssembly is not really close to Assembly. It offers quite a lot more structure, including control flow and even a concept of functions with types. Also refactoring performance critical code in WebAssembly is not the greatest idea since there's a massive interop overhead. Not to mention hand writing WASM is incredibly tedious.
Yeah, there's a good reason printing "Hello, World!" is the test. If you can't even get that far, you're going to have a hard time doing anything useful at all.
As other poster's have commented, for most languages the code for this is trivial but the importance is that it tests the entire compile/link/execute tool chain which may not be as trivial to setup.
Several tools I'm pretty deeply familiar with still make me feel dumb because they are user-hostile.
You are right. But if done judiciously, it can bring large benifits. The massive interop overhead has also shrunk quite a bit.
Here's a fun article about shrinking the overhead: https://hacks.mozilla.org/2018/10/calls-between-javascript-a... - IIRC, chrome as added similar optimizations.
And here's a fun article about gaining performance through rewriting in WebAssembly: https://hacks.mozilla.org/2018/01/oxidizing-source-maps-with...
To be fair, I've written a fair amount of Rust. I haven't really gotten the borrow checker, or at least I've only fought it once or twice, and most of my data structures don't need lifetimes. And from my experience, algorithmic things generally don't require you fight it (except graphs and linked structures), which is what I would think you'd try to optimize.
----
Edit: I agree though that it's not as easy as the author makes it out to be.
There is, writing WASM by hand will not only create an unmanageable mess but also should be strongly discouraged, WASM is supposed to be a target not a “language” like the author is mentioning.
Also, it’s doubtful WASM is actually faster than plain JavaScript for most use cases when not speaking of benchmark-optimized tests. It’s not the point of WASM.
Unfortunately, the way I see it is the ASM part in WASM is going to create a disaster where people really believe it’s an analog to physical machine code.
Even speaking of proper assembly code, it’s not advisable to write it by hand except in very rare and specific scenarios such as cryptography as it’s most of the time going to be slower than using an optimizing compiler anyway.
I assume there are several use cases where WASM is faster than JavaScript, despite all the things you mention. Some types of canvas manipulation are typically faster with WASM, for example.
Of course I wouldn't write web assembly by hand normally for most projects, but I also wouldn't create a blanket rule that it should be discouraged across the board. It's another language that goes through a compile process, just a very specialized one, like assembly.
[0]: https://webassembly.github.io/spec/core/text/index.html
(and yes I know, the addition would be constant folded, it's just an example okay)
However the compilers are much simpler than the equivalent JS compilers, because wasm doesn't need on-stack-invalidation.
It helped that the emulator was written with SDL and zlib, both of which have wasm implementations provided out of the box by Emscripten. Major props to the Emscripten team!
Emulator is running here if curious: https://city41.github.io/nggm/ https://github.com/city41/nggm
An actual `Hello World` is not too much effort to be honest (30 loc) https://gist.github.com/devsnek/18228912613de5068e448bf21a66...
(I also like the idea of edge computing with web assembly but haven't seen anything good for that either)
It's not an app but a development framework for C#. Interesting nevertheless.
Edit:
This should have been titled "hello addition from webassembly"
Are you sure you’re not confusing the code that the author uses to demonstrate running inline code from JavaScript with the wasm code itself?