Skip to content
Max Graey edited this page Sep 19, 2018 · 5 revisions

Instead of using the number type for all sorts of numeric values as in JS, AssemblyScript splits these up into more precise integer and floating point types that map directly to WebAssembly types.

Native types

  • i32/u32
    A 32-bit signed respectively unsigned integer.

  • i64/u64
    A 64-bit signed respectively unsigned integer.

  • f32
    A 32-bit float.

  • f64
    A 64-bit float.

Emulated types

  • i8/u8
    An 8-bit signed respectively unsigned integer.

  • i16/u16
    A 16-bit signed respectively unsigned integer.

  • bool
    A 1-bit unsigned integer.

Note that emulated types are implicitly sign-extended respectively masked to handle overflows. When doing lots of mathematical operations, it is recommended to use native types within the calculations instead, storing the value back to an emulated type afterwards.

Target specific types

  • isize/usize
    A 32-bit signed respectively unsigned integer when targeting 32-bit WebAssembly or a 64-bit signed respectively unsigned integer when targeting 64-bit WebAssembly. Equivalent to void*, size_t etc. in other languages.

Note that using target specific types might or might not require explicit casts depending on the target.

Note that WASM64 is a future feature πŸ¦„ and not supported anywhere yet.

Special types

  • void
    Indicates that a function does not return a value.

Range limits

The following range limits are present as global constants for convenience:

Constant Value
i8.MIN_VALUE: i8 -128
i8.MAX_VALUE: i8 127
i16.MIN_VALUE: i16 -32768
i16.MAX_VALUE: i16 32767
i32.MIN_VALUE: i32 -2147483648
i32.MAX_VALUE: i32 2147483647
i64.MIN_VALUE: i64 -9223372036854775808
i64.MAX_VALUE: i64 9223372036854775807
isize.MIN_VALUE: isize target specific: either i32.MIN_VALUE or i64.MIN_VALUE
isize.MAX_VALUE: isize target specific: either i32.MAX_VALUE or i64.MAX_VALUE
u8.MIN_VALUE: u8 0
u8.MAX_VALUE: u8 255
u16.MIN_VALUE: u16 0
u16.MAX_VALUE: u16 65535
u32.MIN_VALUE: u32 0
u32.MAX_VALUE: u32 4294967295
u64.MIN_VALUE: u64 0
u64.MAX_VALUE: u64 18446744073709551615
usize.MIN_VALUE: usize 0
usize.MAX_VALUE: usize target specific: either u32.MAX_VALUE or u64.MAX_VALUE
bool.MIN_VALUE: bool 0
bool.MAX_VALUE: bool 1
f32.MIN_VALUE: f32 -3.40282347e+38
f32.MAX_VALUE: f32 3.40282347e+38
f32.MIN_SAFE_INTEGER: f32 -16777215
f32.MAX_SAFE_INTEGER: f32 16777215
f32.EPSILON: f32 1.19209290e-07
f64.MIN_VALUE: f64 -1.7976931348623157e+308
f64.MAX_VALUE: f64 1.7976931348623157e+308
f64.MIN_SAFE_INTEGER: f64 -9007199254740991
f64.MAX_SAFE_INTEGER: f64 9007199254740991
f64.EPSILON: f64 2.2204460492503131e-16

Assignability

Assigning a value of one type to a target of another type can be performed without explicit casts where the full range of possible values can be represented in the target type, regardless of interpretation/signedness:

↱ bool i8/u8 i16/u16 i32/u32 i64/u64 f32 f64
bool βœ“ βœ“ βœ“ βœ“ βœ“ βœ“ βœ“
i8/u8 βœ“ βœ“ βœ“ βœ“ βœ“ βœ“
i16/u16 βœ“ βœ“ βœ“ βœ“ βœ“
i32/u32 βœ“ βœ“ βœ“
i64/u64 βœ“
f32 βœ“ βœ“
f64 βœ“

Note that isize/usize act just like i32/u32 if the target is WASM32 respectively as i64/u64 if the target is WASM64.

Example

var i8val  : i8  = -128;  // 0x80
var u8val  : u8  = i8val; // becomes 128 (0x80)
var i16val : i16 = i8val; // becomes -128 through sign-extension (0xFF80)
var u16val : u16 = i8val; // becomes 65408 through masking (0xFF80)
var f32val : f32 = i8val; // becomes -128.0

Comparability

Comparing two values of different types can be performed without an explicit cast under the same rules as outlined in assignability

  1. if the comparison is absolute (==, !=)
  2. if the comparison is relative (>, <, >=, <=) and both types have the same signedness

because WebAssembly has distinct operations for signed and unsigned comparisons. The comparison uses the larger type and returns a bool.

Division and remainder

Dividing or taking the remainder of dividing two values of different types can be performed without an explicit cast under the same rules as outlined in assignability

  • if both types have the same signedness

because WebAssembly has distinct operations for signed and unsigned division and remainder. The result is of the larger type.

Bit shifts

The result of a bit shift (<<, >>) is the left type, with the right type implicitly converted to the left type, performing an arithmetic shift if the left type is signed and a logical shift if the left type is unsigned.

The result of an unsigned right shift (>>>) is the left type (signedness is retained), with the right type implicitly converted to the left type, but always performing a logical shift.

If the left type is a float, an error is emitted.

Β 

Clone this wiki locally