Kom godt i gang med WebAssembly - brug kun 14 linjer JavaScript

WebAssembly er en helt ny webteknologi med massivt potentiale. Det vil have en betydelig indflydelse på, hvordan webapplikationer udvikles i fremtiden.

Men nogle gange har jeg lyst til, at det bare ikke vil forstås ... næsten på en underlig passiv-aggressiv måde.

Når jeg ser på dokumentationen og den håndfulde tutorials, der allerede er derude, kan jeg ikke lade være med at føle mig som en landmand, der bad om regn, kun for at drukne i en oversvømmelse. Jeg fik teknisk det, jeg ønskede ... bare ikke på den måde, jeg havde håbet på. “Du vil have regn ?! Åh, jeg giver dig regn ! ”

Dette skyldes, at WebAssembly muliggør så mange nye ting og kan implementeres på så mange forskellige måder. Men det har ændret sig så meget undervejs til dets officielle MVP-udgivelse i februar, at når du først kommer i gang med at lære om det, er det let at drukne i et hav af detaljer.

Fortsat regnmetaforen er denne artikel mit forsøg på at give et let brusebad af en introduktion til WebAssembly. Ikke begreberne eller møtrikkerne, men den faktiske implementering.

Jeg leder dig gennem trinnene for at oprette og implementere et ekstremt simpelt projekt, hvor det er muligt at fjerne kompleksitet. Når du har implementeret det en gang, men ganske enkelt, er mange af disse ideer på højere niveau meget lettere at give mening.

Lad os nedbryde det

Alt bliver meget klarere, hvis vi træder tilbage og ser på en liste over de trin, der er involveret i implementering af WebAssembly i et projekt.

Når du først kommer i gang, er det let at se på WebAssembly og bare se en lang række muligheder og processer. At opdele det i diskrete trin hjælper os med at få et klart billede af, hvad der foregår:

  1. Skriv: Skriv noget (eller brug et eksisterende projekt) i C, C ++ eller Rust
  2. Kompilér: Kompiler det til WebAssembly (giver dig en binær .wasm-fil)
  3. Inkluder: Få den .wasm-fil ind i et projekt
  4. Instantiate: Skriv en masse asynkron JavaScript, der vil kompilere .wasm binær og instantiere den til noget, som JS kan lege pænt med.

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.

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