DEV Community

JairusSW for AssemblyScript

Posted on • Edited on

What is AssemblyScript?

Alt Text
AssemblyScript — a variant of TypeScript that compiles to WebAssembly. Since it is compiled, it outperforms JavaScript in some cases. AS follows the TypeScript syntax as closely as possible and even adds more features to it. AssemblyScript allows us to write fast WebAssembly for the Web and Server without learning another language.

AssemblyScript is very easy to use. All you need to do is install it via NPM.

Try it out online: Online editor

~ npm i assemblyscript --save-dev
~ npx asinit .
~ npm i
Enter fullscreen mode Exit fullscreen mode

What did that do? First of all, it installed both the loader and the compiler. Secondly, it made a template project with an add function. Now, we just need to compile it to WebAssembly.

~ npm run asbuild
Enter fullscreen mode Exit fullscreen mode

So, if you check out the /build folder, there are the .wasm files that were built. NodeJS and JavaScript both provide a way to run WebAssembly files and AssemblyScript provides its own loader to work with the code. To start our code, we need another file

test.js

const wasmModule = require('./index')
// This works just like a normal module
console.log(wasmModule.add(2,9))
// -- 11
Enter fullscreen mode Exit fullscreen mode

Now, run it!

~ node test.js
Enter fullscreen mode Exit fullscreen mode

It should have outputted the number 11.

JavaScript code for the add function would look like this:

function add(a, b) {
    return a + b
}
module.exports = {
    add: add
}
Enter fullscreen mode Exit fullscreen mode

The AssemblyScript code looks like this:

export function add(a: i32, b: i32): i32 {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Pretty similar, right? When we compile it, we can require it just like a normal JavaScript file. Keep in mind that WebAssembly is sandboxed which means it can’t access the system, make HTTP requests, or log to the console. However, AssemblyScript supports both WASI and JS bindings (calling JS from AS).

If you have any questions or comments, feel free to comment or join the AssemblyScript Discord. Or, check out the website.😉

P.S: There is a tutorial at https://jtanaka.gitbook.io/guide/

Top comments (7)

Collapse
 
trusktr profile image
Joe Pea

Keep in mind that WebAssembly is sandboxed which means it can’t access the system, make HTTP requests, or log to the console. However, AssemblyScript supports both WASI and JS bindings (calling JS from AS).

But coming soon! How to use DOM APIs (via bindings) and to make websites written in AssemblyScript!...

Collapse
 
jairussw profile image
JairusSW

Yeah, write an article when asdom gets released!

Collapse
 
trusktr profile image
Joe Pea

It's already visible in the following repo, but as soon as it has enough bindings to be useful for a majority of cases, then we'll make an article. Working on Shadow DOM bindings next. :)

GitHub logo lume / asdom

Use DOM APIs in AssemblyScript

asdom

Use DOM APIs in AssemblyScript (TypeScript compiled to WebAssembly).

This allows us to write WebAssembly applications that can manipulate the DOM, and with potential for more speed!

Early Stages!

Work in progress (probably may always be), but right now it's early and many APIs need to be added.

Supported APIs so far

See the outline of supported APIs.

Usage

First you should get familiar with AssemblyScript and learn how to run AssemlyScript code in a browser.

The following asc compiler options are required: --exportRuntime --exportTable.

The --explicitStart option is required only if any of your ES modules use DOM APIs at their top level (read the comments in the below example).

In your JavaScript code that loads your Wasm module, import Asdom and pass its wasmImports to your Wasm module's imports. The following snippet assumes the use of native ES Modules in the browser and a…

Collapse
 
king11 profile image
Lakshya Singh

Definitely gonna give it a try :)

Collapse
 
jairussw profile image
JairusSW

If you need any help, I'm always available in the discord server (discord.com/invite/assemblyscript) as @JairusSW 😀

Collapse
 
ianwijma profile image
Ian Wijma

Quite interesting. Had no idea bout assemblyScript.

Collapse
 
jairussw profile image
JairusSW • Edited

Yeah, its a small project that started a while back. Its getting better and more powerful by the moment though. :)