NEAR Blockchain


Lesson 1

What is AssemblyScript?

AssemblyScript compiles a strict variant of Typescript.


The term strict refers to a programming language that allows only strict functions - which are functions whose parameters must be evaluated completely before one can call them.  They always evaluate their arguments whereas a non-strict function might not evaluate some of its arguments.  Also known as an eager programming language.

TypeScript is a typed superset of JavaScript.  That means it extends JavaScript by adding types which saves you time by catching errors and providing fixes before you run your code.  This leads to better documentation and type inference means its optional to write types (don't have to put in your code until you want more safety).  Because TypeScript knows JavaScript, it can usually generate types for you in many cases - just by creating a variable and assigning it a value.

AssemblyScript only has limited type inference because the type of each expression must be known in advance.  That means variable and parameter declarations must either have their type annotated or have an initializer.  Functions also have to be annotated with a return type to help the compiler make the right decisions.  Basically, that means explicity saying what types things are like so:

let x: i32 = 0

export function makeSomething(thing: string) : Array<i32> {}

More on TypeScript

TypeScript is an open-source language from Microsoft.  It provides everything that JavaScript does with a type system on top of it.  The main benefit of TypeScript is that it can highlight unexpected behaviour in your code, lowering the change of bugs.  It does this by ensuring you consistently assign things like language primitives such as string, number, or object.

AssemblyScript compiles to WebAssembly.  WebAssembly (WASM) is a binary instruction format for a stack-based virtual machine.  WASM provides two main benefits:

  1. It's binary format can be natively decoded much faster than JavaScript can be parsed (up to 20x faster).  This leads to better user experience, particularly on mobile.
  2. It avoids constraints imposed by ahead of time compilation and good performance on engines without asm.js optimizations - as it is a new standard that makes it much easier to add the features required to reach native levels of performance.

AssemblyScript uses Binaryen to do the compilation to WASM.  Binaryen is a compiler and toolchain infrastructure for WASM written in C++, making the compilation easy, fast, and effective.

Whereas JavaScript parses at execution (analyzes and converts a program into an internal format that a runtime environment such as a browser can run), AssemblyScript does all this (compiles) ahead of time - producing really fast WASM binaries (programs).

AssemblyScript hasn't been around all that long (approx three years ago - late 2017/early 2018).  It's open-source and it still evolves and grows (as does WASM) thanks to the dedication and commitment of many talented developers.  So, there are still quirks to be aware of and some things won't get implemented until they become features in WASM.  

Even though it's still evolving, you can still do some pretty wonderful and complex things with it.  It is suitable as a general-purpose higher-level language for WASM today and it's Vital Point AI's preferred language for NEAR Protocol contract development.

This course will thus teach AssemblyScript in the context of building NEAR smart contracts.  Lets get to it.