Skip to content

Limitations

Daniel Wirtz edited this page May 26, 2019 · 14 revisions

You might have heard this already: Compiling (untyped) JavaScript to WebAssembly doesn't make sense because it would ultimately result in shipping another (slower) JavaScript runtime that runs within a JavaScript runtime.

That's completely true and therefore it's not what AssemblyScript is doing. Instead, AssemblyScript focuses on where WebAssembly excels and skips super dynamic features of JavaScript that cannot be efficiently compiled ahead of time. Technically, the AssemblyScript compiler is much more similar to a static (let's say C++) compiler than it is to a JavaScript VM. This means:

Mandatory types

  • There are no union types, any or undefined (everything must be strictly typed).

  • Nullable types must be reference types, e.g. ClassType | null, and cannot be basic types (yet).

  • Functions must have a return type annotation, avoiding unintended inference in functions with multiple returns.

  • Variables and function parameters must either have a type annotation or an initializer.

    • If the initializer is an integer literal, i32 is assumed when it fits into 32-bits, otherwise i64 is assumed.
    • If the initializer is a float literal, f64 is assumed.
    • Otherwise the initializer is evaluated to obtain its type.
  • Optional function parameters require an initializer.

Preliminary classes

At the time of this writing, classes in AssemblyScript are functional but more like C-structs with methods. There is no method overloading yet, for example, and access modifiers are not checked. However, there has been progress recently on our own minimal runtime that will, eventually, lead to a more mature set of features.

Details

  • When calling an exported variable-length arguments WebAssembly function from JS, e.g.

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

    the actual number of parameters of the call must be specified explicitly because there is no equivalent to arguments.length on the WebAssembly side. Example:

    exports.__setargc(0);
    exports.sum(); // 3
    
    exports.__setargc(1);
    exports.sum(2); // 4
    
    exports.__setargc(2);
    exports.sum(2, 3); // 5

    The AssemblyScript loader does this and other setup automatically for you.

See also: Status / Roadmap

 

Clone this wiki locally