Započnite s WebAssembly - koristeći samo 14 redaka JavaScript-a

WebAssembly je potpuno nova web tehnologija s ogromnim potencijalom. To će imati značajan utjecaj na razvoj web aplikacija u budućnosti.

Ali, ponekad osjećam da to jednostavno ne želi biti shvaćeno ... gotovo na neobično pasivno-agresivan način.

Kad pogledam dokumentaciju i pregršt tutorijala koji su već vani, ne mogu se ne osjećati poput farmera koji je molio za kišu, samo da bi se utopio u poplavi. Tehnički sam dobio ono što sam želio ... samo ne onako kako sam se nadao. “Želiš kišu ?! Oh, dat ću ti kišu ! "

To je zato što WebAssembly omogućuje toliko novih stvari i može se implementirati na toliko različitih načina. No, toliko se promijenio na putu do službenog izdanja MVP-a u veljači, da je kad tek započnete učiti o tome lako utonuti u more detalja.

Nastavljajući metaforu o kiši, ovaj je članak moj pokušaj pružiti lagani pljusak uvoda u WebAssembly. Ne koncepti ili matice, već stvarna provedba.

Provest ću vas kroz korake za stvaranje i provedbu izuzetno jednostavnog projekta, uklanjajući složenost gdje god je to moguće. Nakon što ste je jednom implementirali, ma koliko jednostavno, puno je ideja s više razine puno lakše razumjeti.

Razdvojimo to

Sve će biti puno jasnije ako se odmaknemo i pogledamo popis koraka koji su uključeni u implementaciju WebAssembly-a u projekt.

Kada prvi put započinjete, lako je pogledati WebAssembly i vidjeti samo gomilu opcija i procesa. Raščlamba na diskretne korake pomoći će nam da dobijemo jasnu sliku o tome što se događa:

  1. Pisanje: Napišite nešto (ili upotrijebite postojeći projekt) na C, C ++ ili Rust
  2. Sastavi : Sastavite ga u WebAssembly (dajući vam binarnu .wasm datoteku)
  3. Uključi: Uključi tu .wasm datoteku u projekt
  4. Instanciraj: Napišite hrpu asinkronog JavaScript-a koji će kompajlirati binarni .wasm i instancirati ga u nešto s čime se JS može lijepo igrati.

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