What is AssemblyScript?

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…


This content originally appeared on DEV Community and was authored by Jairus Tanaka

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

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

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

Now, run it!

~ node test.js

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
}

The AssemblyScript code looks like this:

export function add(a: i32, b: i32): i32 {
    return a + b;
}

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/


This content originally appeared on DEV Community and was authored by Jairus Tanaka


Print Share Comment Cite Upload Translate Updates
APA

Jairus Tanaka | Sciencx (2021-07-12T18:34:40+00:00) What is AssemblyScript?. Retrieved from https://www.scien.cx/2021/07/12/what-is-assemblyscript/

MLA
" » What is AssemblyScript?." Jairus Tanaka | Sciencx - Monday July 12, 2021, https://www.scien.cx/2021/07/12/what-is-assemblyscript/
HARVARD
Jairus Tanaka | Sciencx Monday July 12, 2021 » What is AssemblyScript?., viewed ,<https://www.scien.cx/2021/07/12/what-is-assemblyscript/>
VANCOUVER
Jairus Tanaka | Sciencx - » What is AssemblyScript?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/12/what-is-assemblyscript/
CHICAGO
" » What is AssemblyScript?." Jairus Tanaka | Sciencx - Accessed . https://www.scien.cx/2021/07/12/what-is-assemblyscript/
IEEE
" » What is AssemblyScript?." Jairus Tanaka | Sciencx [Online]. Available: https://www.scien.cx/2021/07/12/what-is-assemblyscript/. [Accessed: ]
rf:citation
» What is AssemblyScript? | Jairus Tanaka | Sciencx | https://www.scien.cx/2021/07/12/what-is-assemblyscript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.