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

The "Modules" 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 describes a module as the fundamental unit of code in Web Assembly. Within a module, functions are created and exported so they can be called by JavaScript. Function parameters are known as locals.


Transcript from the "Modules" Lesson

>> In Web Assembly the fundamental unit of code is a module. And if this looks, this has got to look a little strange to most of you because, it looks strange to me even now. Because at least no brackets and there's no parentheses or anything like that. The Web Assembly is actually a module, it's written in a format called the S-expression.

Not terribly important to know you can look up the docs on MDN and get real nerdy about it. But essentially, it's a tree and the different parentheses represent different branches on that tree. But ultimately, it comes back to the trunk, there are, trying to think of a good course, I think Bianca has a good course, on data structures.

There's a few good course on front of masters on data structures if you wanna learn more about trees and things like that. I try not to load you down, with too much information that is not, useful. I just thought you'd like to know, that this is actually a real format, it's an old format.

But that's it, back to our program. The financing code is a module, we can declare our modules using func. The func keyword, names and parameters are prefixed with a dollar sign so you know what you're reading. And more importantly here is the concept of types. Remember earlier actually, in the last section, we talked about different number types and how that's important.

It's important here because, again, we're dealing with binary streams of data that is ones and zeros and we need to know how to read that. And to know how to read that, we have to tell our Web Assembly module, what type of number we're looking at. So that if we see a string of eight zeros actually will say a string of 16 zeros.

We can either interpret that as one number, a 16 bit integer, or we can say, actually that's 2, 8 bit integers. And in the memory because it's this consistent long string of ones and zeros, we have noticed slices up, so we know how to deal with that memory so simply.

And because remember what I said earlier about different views, and the danger of like 2 different processes writing to memory, which will corrupt it because we don't know the format. It's just gibberish at that point. That's why we have types, is because we wanna be very specific about the memory we're accessing, and the type of data we're moving around.

So that's what the i32 stands for, and that's what we're gonna use for this class the entire time. We're not gonna touch floating corner 64 bit numbers. Not totally irrelevant, we're just gonna use 32 bit integers, and that's what i32 means. And we prefix the error, we set these types, so in this case, the function signature, the function is called name.

It's taking in a parameter called params, the params at 32 bit integer, and it's gonna return a 32 bit integer.

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