-
-
Notifications
You must be signed in to change notification settings - Fork 670
Limitations
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:
-
There are no union types,
any
orundefined
(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, otherwisei64
is assumed. - If the initializer is a float literal,
f64
is assumed. - Otherwise the initializer is evaluated to obtain its type.
- If the initializer is an integer literal,
-
Optional function parameters require an initializer.
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.
-
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