- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 677
 
Built in functions
To provide direct (compile to opcode) access to native WebAssembly operations, the following functions plus a few TS/JS-like constants are provided in the global scope:
- 
NaN:
f32 | f64
NaN (not a number) as a 32-bit or 64-bit float depending on context. Compiles to a constant. - 
Infinity:
f32 | f64
Positive infinity as a 32-bit or 64-bit float depending on context. Compiles to a constant. 
- 
isInteger<
T>(value?:T):bool
Tests if the specified type or expression is of an integer type and not a reference. Compiles to a constant. - 
isFloat<
T>(value?:T):bool
Tests if the specified type or expression is of a float type. Compiles to a constant. - 
isSigned<
T>(value?:T):bool
Tests if the specified type or expression can represent negative numbers. Compiles to a constant. - 
isReference<
T>(value?:T):bool
Tests if the specified type or expression is of a reference type. Compiles to a constant. - 
isString<
T>(value?:T):bool
Tests if the specified type or expression can be used as a string. Compiles to a constant. - 
isArray<
T>(value?:T):bool
Tests if the specified type or expression can be used as an array. Compiles to a constant. - 
sizeof<
T>():usize
Determines the byte size of the specified core or class type. Compiles to a constant. - 
offsetof<
T>(fieldName?:string):usize
Determines the offset of the specified field within the given class type. Returns the class type's end offset if field name has been omitted. Compiles to a constant. - 
alignof<
T>():usize
Determines the alignment (log2) of the specified underlying core type. Compiles to a constant. 
Note that constantOffset arguments must be compile-time constants (const global or local). Similarly, fieldName arguments must be string literals.
Note that whenever the compiler spots a constant condition, it will automatically eliminate untaken branches and not attempt to compile them. For example, if a generic function is intended to work with both integers and strings with just a few statements differing, a compile-time type check with a constant condition on an if-then-else statement can be utilized to make it behave differently in parts depending on the actual type argument:
function doSomething<T>(a: T): T {
  if (isString<T>()) {
    ... // eliminated if T is not a string
  } else {
    ... // eliminated if T is a string
  }
}- 
isNaN<
T = f32 | f64>(value:T):bool
Tests if a 32-bit or 64-bit float isNaN. - 
isFinite<
T = f32 | f64>(value:T):bool
Tests if a 32-bit or 64-bit float is finite, that is notNaNor +/-Infinity. - 
clz<
T = i32 | i64>(value:T):T
Performs the sign-agnostic count leading zero bits operation on a 32-bit or 64-bit integer. All zero bits are considered leading if the value is zero. - 
ctz<
T = i32 | i64>(value:T):T
Performs the sign-agnostic count tailing zero bits operation on a 32-bit or 64-bit integer. All zero bits are considered trailing if the value is zero. - 
popcnt<
T = i32 | i64>(value:T):T
Performs the sign-agnostic count number of one bits operation on a 32-bit or 64-bit integer. - 
rotl<
T = i32 | i64>(value:T, shift:T):T
Performs the sign-agnostic rotate left operation on a 32-bit or 64-bit integer. - 
rotr<
T = i32 | i64>(value:T, shift:T):T
Performs the sign-agnostic rotate right operation on a 32-bit or 64-bit integer. - 
abs<
T = i32 | i64 | f32 | f64>(value:T):T
Computes the absolute value of an integer or float. - 
max<
T = i32 | i64 | f32 | f64>(left:T, right:T):T
Determines the maximum of two integers or floats. If either operand isNaN, returnsNaN. - 
min<
T = i32 | i64 | f32 | f64>(left:T, right:T):T
Determines the minimum of two integers or floats. If either operand isNaN, returnsNaN. - 
ceil<
T = f32 | f64>(value:T):T
Performs the ceiling operation on a 32-bit or 64-bit float. - 
floor<
T = f32 | f64>(value:T):T
Performs the floor operation on a 32-bit or 64-bit float. - 
copysign<
T = f32 | f64>(x:T, y:T):T
Composes a 32-bit or 64-bit float from the magnitude ofxand the sign ofy. - 
nearest<
T = f32 | f64>(value:T):T
Rounds to the nearest integer tied to even of a 32-bit or 64-bit float. - 
reinterpret<
T = i32 | i64 | f32 | f64>(value:*):T
Reinterprets the bits of the specified value as typeT. Valid reinterpretations are u32/i32 to/from f32 and u64/i64 to/from f64. - 
sqrt<
T = f32 | f64>(value:T):T
Calculates the square root of a 32-bit or 64-bit float. - 
trunc<
T = f32 | f64>(value:T):T
Rounds to the nearest integer towards zero of a 32-bit or 64-bit float. 
- 
load<
T>(ptr:usize, constantOffset?:usize):T
Loads a value of the specified type from memory. Equivalent to dereferncing a pointer in other languages. - 
store<
T>(ptr:usize, value:T, constantOffset?:usize):void
Stores a value of the specified type to memory. Equivalent to dereferencing a pointer in other languages when assigning a value. 
- 
select<
T>(ifTrue:T, ifFalse:T, condition:bool):T
Selects one of two pre-evaluated values depending on the condition. - 
unreachable():
*
Emits an unreachable operation that results in a runtime error when executed. Both a statement and an expression of any type. 
- 
current_memory():
i32
Returns the current memory size in units of pages. One page is 64kb. - 
grow_memory(value:
i32):i32
Grows linear memory by a given unsigned delta of pages. One page is 64kb. Returns the previous memory size in units of pages or-1on failure. Note that callinggrow_memorywhere a memory manager is present might break it. 
- 
parseInt(str:
string, radix?:i32):i64
Parses a string to a 64-bit integer. Returns0on invalid inputs unlikeNaNin JS. - 
parseFloat(str:
string):f64
Parses a string to a 64-bit float. ReturnsNaNon invalid inputs. - 
changetype<
T>(value:*):T
Changes the type of a value to another one. Useful for casting class instances to their pointer values and vice-versa. - 
assert<
T>(isTrueish:T, message?:string):T
Traps if the specified value is not true-ish, otherwise returns the non-nullable value. - 
unchecked(expr:
*):*
Explicitly requests no bounds checks on the provided expression. Useful for array accesses. - 
call_indirect<
T>(target:u32, ...args:*[]):T
Emits acall_indirectinstruction, calling the specified function in the function table by index with the specified arguments. Does result in a runtime error if the arguments do not match the called function.