This specification defines asm.js, a strict subset of Java Script that can be used as a low-level, efficient target language for compilers.
The following is a small but complete example of an module.
var heap = new Array Buffer(0x10000); // 64k heap init(heap, START, END); // fill a region with input values var fast = Geometric Mean(window, null, heap); // produce exports object linked to AOT-compiled code fast.geometric Mean(START, END); // computes geometric mean of input valuesvar bogus Global = ; var slow = Geometric Mean(bogus Global, null, heap); // produces purely-interpreted/JITted version console.log(slow.geometric Mean(START, END)); // computes bizarro-geometric mean thanks to bogus Global Validation of an module relies on a static type system that classifies and constrains the syntax.
The language provides an abstraction similar to the C/C virtual machine: a large binary heap with efficient loads and stores, integer and floating-point arithmetic, first-order function definitions, and function pointers.
The programming model is built around integer and floating-point arithmetic and a virtual heap represented as a typed array.
However, this representation creates an overlap between signed and unsigned numbers that causes an ambiguity in determining which Java Script number they represent.
For example, the bit pattern )—that is, the range of integers such that an unboxed 32-bit representation has the same value whether it is interpreted as signed or unsigned.
An This explicit directive allows Java Script engines to avoid performing pointless and potentially costly validation on other Java Script code, and to report validation errors in developer consoles only where relevant.
Because is a strict subset of Java Script, this specification only defines the validation logic—the execution semantics is simply that of Java Script.
In asm.js, the type of a variable never has a known signedness.
This allows them to be compiled as 32-bit integer registers and memory words.
However, validated is amenable to ahead-of-time (AOT) compilation.