
A WebAssembly egy vadonatúj webes technológia, hatalmas potenciállal. Jelentős hatással lesz a jövőben a webalkalmazások fejlesztésére.
De néha úgy érzem, hogy egyszerűen nem akarja, hogy megértsék ... szinte furcsán passzív-agresszív módon.
Ha megnézem a dokumentációt és a maroknyi oktatóanyagot, amelyek már odakint vannak, nem tehetek róla, de úgy érzem magam, mint egy gazda, aki esőért imádkozott, csak azért, hogy áradásba fulladjon. Műszakilag megkaptam, amit akartam ... csak nem úgy, ahogy reméltem. „Esőt akarsz ?! Ó, adok neked esőt !
A WebAssembly ugyanis sok újdonságot tesz lehetővé, és sokféle módon megvalósítható. De annyira megváltozott a februári hivatalos MVP-kiadás útján, hogy amikor először megismerkedtek vele, könnyen belefullad a részletek tengerébe.
Folytatva az eső metaforáját, ez a cikk az a kísérletem, hogy könnyű zuhannyal szolgáljak a WebAssembly bevezetésében. Nem a koncepciókat vagy az anyákat és csavarokat, hanem a tényleges megvalósítást.
Végigvezetem egy rendkívül egyszerű projekt létrehozásának és megvalósításának lépésein, ahol lehetséges, eltávolítva az összetettséget. Miután egyszer végrehajtotta, egyszerűen, sok ilyen magasabb szintű ötlet sokkal könnyebben értelmezhető.
Bontjuk le
Minden sokkal egyértelműbb lesz, ha visszalépünk, és megnézzük a WebAssembly projektben történő megvalósításának lépéseit.
Amikor először kezdi, könnyű megnézni a WebAssembly-t, és csak rengeteg lehetőséget és folyamatot látni. Ha diszkrét lépésekre bontja, akkor világos képet kaphatunk arról, hogy mi történik:
- Ír: Írj valamit (vagy használj meglévő projektet) C, C ++ vagy Rust formátumban
- Fordítás: Fordítsa le a WebAssembly-be (bináris .wasm fájlt kap)
- Belefoglalás: Vigye azt a .wasm fájlt egy projektbe
- Instantiate: Írj egy csomó aszinkron JavaScript-et, amely összeállítja a .wasm bináris fájlokat, és olyanokba állítja őket, amelyekkel a JS szépen tud játszani.
And that’s pretty much it. Granted, there are different permutations of this process, but that’s the gist of it.
Broadly speaking, it’s not all that complicated. However, it can get extremely complicated, because most of these steps allow for widely varying degrees of complexity. In each case, I’m going to err on the side of bare-bones simplicity.
For our project, we’ll be writing a simple function in C++ (don’t worry if you’re not familiar with C++, it’ll beextremely simple). The function will return the square of a given number.
Then, we’ll compile it into .wasm using an online tool (you won’t need to download or use any command line utilities). Next, we’ll instantiate it with 14 lines of JS.
When we’re done, you’ll be able to call a function written in C++ as if it were a JS function, and you’ll be amazed!
The sheer number of possibilities that this opens up are absolutely mind blowing.
Write
Let’s start with our C++ code. Remember, we won’t be using a local dev environment to write or compile this.
Instead, we’ll be using an online tool called WebAssembly Explorer. It’s kind of like CodePen for WebAssembly, and it allows you to compile your C or C++ code right in the browser and download a .wasm file all in one place.
Once you’ve opened up WebAssembly Explorer, type this C++ code into the leftmost window:
int squarer(int num) { return num * num;}
Like I said, we’re using a really simple example here. Even if you’ve never looked at C or C++ before, it’s probably not too difficult to tell what’s going on.
Compile
Next, click the button that says “compile” in the red bar above your C++ code. Here’s what you’ll see:

The middle column shows you a human-readable version of the .wasm binary that you’ve just created. This is called “WAT” or WebAssembly Text Format.
On the right is the resultant assembly code. Pretty cool.
I won’t go into much detail about either of these, but you do need to know at least a little bit about the WAT file in order to follow the next steps.
WAT exists because we humans generally have a hard time making sense of straight binary. It’s essentially a layer of abstraction that helps you understand and interact with your WebAssembly code.
In our case, what we want to understand is how our WebAssembly refers to the function that we just created. This because we’ll need to use that exact same name in our JS file later on to refer to it.
Any functions that you write in your C++ code will be available in WebAssembly as something called an “export.” We’ll talk a bit more about this later, but for now, all you need to know is that the exports are the things that you’ll be able to interact with and use.
Take a look at the WAT file and look for the word “export.” You’ll see it twice: once alongside the word memory
and again alongside the word _Z7squareri
. We don’t need to know about memory
for now, but we’re definitely interested in _Z7squareri
.
We used the function name squarer
in our C++, but now that has somehow become _z7squareri
. This can definitely be confusing the first time you see it.
As far as I can tell, the “_Z7” prefix and “i” suffix are debug markers introduced by the C++ compiler. This isn’t really important to understand in depth, though. You just need to be aware that this will happen, because you need to use this exact name in your JS file in order to call your C++ function.
Include
Now just click the “download” button at the top of the purple WAT section. You’ll get the .wasm binary file. Rename it squarer.wasm
. Then create a new directory and put your squarer.wasm
file in there, along with two other files:
index.html
(boilerplate)scripts.js
(empty for now)
Instantiate
Now for the tricky part. Or, at least, the part that caused me a lot of confusion when I first started sifting through the documentation.

Original text
Although you’ll eventually be able to include .wasm modules like a regular old ES6 module (using