Check out a free preview of the full Web Assembly (Wasm) course

The "Writing AssemblyScript" Lesson is part of the full, Web Assembly (Wasm) course featured in this preview video. Here's what you'd learn in this lesson:

Jem writes a minusOne function using AssemblyScript. Function parameters and return values are typed. When the project is built, the wat and wasm files are generated in the build directory.

Preview
Close

Transcript from the "Writing AssemblyScript" Lesson

[00:00:00]
>> Let's write some assembly scripts, and important to note about these notes that I'm working off of. I made a link back to the repo and the repo should have the completed lesson for every step that we go through. That way, it's easy for me to keep going and keep going.

[00:00:21]
And I build up like something by the end it's gonna get a little complex. And I don't want people to become intimidated by missing a step. And then you realize you missed a step somewhere and like none of it works or you're getting a weird error or things like that.

[00:00:35]
I really wanna follow the blessed path, the paved road for this, so I've created a. A series of exercises that are all represent different steps of what we're gonna take. So if you're ever confused and lost, you will see this working repo, you can always go back there.

[00:00:50]
Clone it and you'll jump right back to where we are, however, as I said before, you learn better when you code yourself. Don't copy and paste, if you can, try to copy paste, try to code things out directly. Build out those neuromuscular pathways by typing and let it run through your arms to your brain and let it absorb.

[00:01:13]
All right, so let's write some assembly script. Now we're gonna bring back that n minus one example because it's pretty simple. So we're gonna just delete this and we're gonna start off. Now we're back to familiar syntax, we're back to JavaScript or TypeScript in this instance. So things should be a little bit easier to grasp, but let's right actually, we'll start off with a function.

[00:01:44]
Nothing new here, we'll call it n minus one, actually, sorry, I'm writing JavaScript. Actually, I'll write this in JavaScript first and then we'll convert it to assembly scripts. So we can see kind of that the mental model, so minus one is just gonna return and minus one. I know groundbreaking stuff here, it's a simple example, but let's write this in assembly scripts.

[00:02:17]
And in this case, that just means adding the type references to the variables and the function that we're using. So add a type, we're gonna say, n is always gonna be a 32-bit integer, and this function is always gonna return a 32 bit integer. So if you've never dealt with Typescript before, it looks a little strange.

[00:02:48]
And it almost seems like you're not writing JavaScript as much because you have these weird characters that aren't official. Li part of the ACMA script standard. However, because assembly or web assembly script compiles to web assembly. And we need to know that specific type of number we're dealing with.

[00:03:06]
Remember the outcodes earlier and how every type of number has an outcode associated with it? We have to tell AssemblyScript what type of out codes we're gonna use, so it knows how to call them in WebAssembly. Remember, this isn't JavaScript. [LAUGH] We get nothing for free, well, we get very little for free now.

[00:03:24]
We have to be explicit for every single step, implicit computing is it works things like SQL statements. That's implicit you're giving a set of directions, or even HTML, you're saying a set of directions that you want the machine to follow. Versus explicit programming, you're saying do this. If you say, here's a pro tip, go on Twitter right now and be like, hey, what do you think difference we employ a declarative.

[00:03:56]
And imperative programming is and then you'll watch that erupt into this giant flame or people to argue about imperative versus declarative. Doesn't really matter to me. All I care about is specificity because we need the specificity to execute our low level programming that is WebAssembly. All right, sorry for that rants, I just, I had to do it, next up is tabs versus spaces.

[00:04:24]
We'll argue about that later, all right, so we've now written a bit of assembly script. Now what's left to do is compile it, so I'm gonna run NPM run, ASMR, build, and you don't have to, but you know, it's nice to see it. Let us look at our did I not save, it's let's make sure I don't see anything in here optimize that What is wrong?

[00:04:58]
Easy mistake, so I wrote this function in assembly script, but I never exported it. And because assembly script uses a form of it uses ahead of time compilation, similar to C or Java or anything like that. And any strongly typed language is gonna use ahead of time compilation.

[00:05:19]
So it catches those type errors before they happen, assembly script does the same thing, it compiles ahead of time. So if there's a function that we write, but it's never accessed, it actually tree shakes those out and it won't actually. It won't create it, and won't use it because it's not being called to why include unnecessary code into your file.

[00:05:38]
So we looked at that optimize that lot, and the untouched that what are functions that in there. Why because I forgot to export it, so let's export that function, and then now let's rerun that build. And now we see the minus one function, and if we look carefully, that looks exactly like the example we wrote earlier.

[00:06:06]
So now we know the code we wrote in web assembly, last time was good and valid. It's been compiled down to WebAssembly from assembly scripts. And again, I really love this for learning if you're trying to figure out well how to if statements work. How to for loops, work from assembly script into web assembly.

[00:06:26]
Writing the code out and compiling it and seeing what the compiler is doing is such a valuable learning tool. So I'm probably gonna keep referring back to these watch files, just because it's really interesting to know what's going on under the hood.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now