
WebAssembly is easy – a hello world example - dorin131
https://fodor.org/blog/webassembly-hello-world/
======
_hardwaregeek
This reminds me of the classic owl meme. Draw two circles, then draw the rest
of the damn owl. Writing a hello world does not make WebAssembly easy. I can
write a hello world in Rust or in Haskell trivially. That doesn't mean I
understand borrow checking or monads. Besides the author doesn't even go over
printing hello world.

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.

~~~
sp332
_There is a good reason why we are not attempting to print a string._

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.

~~~
everdev
Is that really a good "test"? Printing a string isn't hard in any non-hobby
language.

~~~
kragen
It's hard until you have the toolchain and I/O working. That's what it's a
test of. The rest of WebAssembly doesn't seem like an obstacle at all to me.
For me the big unknown is how to get a wasm program running _at all_ and
communicating with something on my screen.

------
Eikon
> Now that you know how you can easily create and use WebAssembly code, there
> is nothing stopping you from refactoring performance critical parts of your
> code using WASM.

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.

~~~
acbart
I thought that one of the major points of WASM was that it would be fast? The
first advantage listed on the official WebAssembly website is "Efficient and
fast" [0]. But I suppose you wrote "the point" \- it's just one of several
points. But ideally, it should run at "native speed", right?

[0] [https://webassembly.org/](https://webassembly.org/)

~~~
snek
It is designed to be able to skip the "tier up JIT" stage that JS code has to
go through, where JS code is profiled at runtime to decide what should be
optimized, what the types are, etc. `1 + 1` in JS and `(i32.add (i32.const 1)
(i32.const 1))` should still result in the same cpu code, the only difference
is that it takes longer for the JS code to get there (and there's no guarantee
that it happens to the JS code).

(and yes I know, the addition would be constant folded, it's just an example
okay)

~~~
sstangl
In Firefox, Wasm still uses a tiered JIT. Like JS, there is a single-pass JIT
and a JIT that does more aggressive optimizations (mostly: good regalloc).

However the compilers are much simpler than the equivalent JS compilers,
because wasm doesn't need on-stack-invalidation.

------
yajjackson
This post is funny. “Web assembly is super duper easy! let’s do a hello world
example! But also let’s not do a hello world examples because strings don’t
exist in WASM and that would be kind of confusing!”

------
vhakulinen
Would be worth mentioning that the inline-webassembly npm package is created
by the author and quite recently.

~~~
lopmotr
Wow. If you need to use a special library made by the author, then what's the
point of a hello world? You're not learning how to get the basic things
running.

~~~
slenk
You should look over the article a little more carefully. The special library
is only if you want to inline WASM in javascript. You can still write and use
WASM without it

------
city41
I'm not sure if I could say wasm is easy, but I was really impressed with how
easy it was to get an emulator written in C running in the browser with
Emscripten. It pretty much just worked and only took a couple hours to have it
working well. I started with zero wasm/Emscripten experience.

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://city41.github.io/nggm/)
[https://github.com/city41/nggm](https://github.com/city41/nggm)

------
snek
This `iw` module is a pretty cool toy, but it has to compile wasm text format
to wasm bytecode at runtime, every time.

An actual `Hello World` is not too much effort to be honest (30 loc)
[https://gist.github.com/devsnek/18228912613de5068e448bf21a66...](https://gist.github.com/devsnek/18228912613de5068e448bf21a66f2d2)

------
bordercases
WASM is fast in execution but the time spent getting data in and out of the
memory allocated by WASM so that it can do direct memory management dominates
the performance gains as a slowdown.

[https://blog.sqreen.com/webassembly-
performance/](https://blog.sqreen.com/webassembly-performance/)

------
rb808
I really like the idea of web assembly but I haven't seen any examples of it
being done well. Even this page I can't see the app running. Does anyone have
any website that shows a useful web assembly running?

(I also like the idea of edge computing with web assembly but haven't seen
anything good for that either)

~~~
sawyerjhood
I think Figma is a good example of an app written in WASM:
[https://www.figma.com/blog/webassembly-cut-figmas-load-
time-...](https://www.figma.com/blog/webassembly-cut-figmas-load-time-by-3x/)

~~~
rb808
I have to sign up to see it work?
[https://www.figma.com/](https://www.figma.com/)

~~~
sawyerjhood
Sadly yes, I forgot because I was already signed into my account

------
newnewpdro
Well that's the first time I've seen a claimed hello world example that didn't
even manage to print the damn string somewhere.

Edit:

This should have been titled "hello addition from webassembly"

------
flowerlad
The hard part of Web Assembly is getting data in and out. Since you can write
in C and compile to WebAssembly you don’t have to learn WebAssembly. But even
C doesn’t make data exchange easy. You basically pass a large array of bytes
in out instead of proper high level data structures. This is what makes
WebAssembly hard.

------
Illniyar
The first thing that come to mind when seeing that code is - that's a really
verbose syntax, especially for a low level language.

~~~
leshow
But low level languages usually are more verbose. High level languages can
afford to be more terse. The WASM code looks very similar to actual x86
assembly to me, and with a name like 'web assembly', that seems right.

------
svnpenn
Why does that look like Lisp?

~~~
rightbyte
Becouse it's written in s-expressions. You can do sequencial code too.

------
LessDmesg
Lisp syntax was a bad idea.

