Library core

This is the core library, which means you usually don't need to import it.

The Toit core library.
This library is automatically imported into every Toit program.

Exported interfaces

A container specialized for bytes.

A collection of elements.

A task.

Exported classes

A Boolean value.

A double-ended queue.

A Duration, capturing relative times.

The class of false.

A 64 bit floating point value.

A map that uses object identity instead of the == operator to test equality of keys. This map still uses the hash-code method on keys (see Map). There is no identity hash code operation on arbitrary classes in Toit.

A set that uses object identity instead of the == operator to test equality of elements. This set still uses the hash-code method on elements (see Set). There is no identity hash code operation on arbitrary classes in Toit.

A 64 bit integer.

A lambda, or closure.

A linear collection of objects.

A map from key objects to values.

A number.

A base class for all classes.

Simplistic profiler based on bytecode invocation counts.

A set of keys.

A Unicode text object.

A wall clock time.

A decomposed view of a Time object.

The class of true.

Exported globals

Assertion failed error.
Thrown when an assertion fails both by the 'assert:' language construct and by other assertion checking libraries.

Deprecated. Use system.BITS-PER-BYTE instead.

Deprecated. Use system.BITS-PER-WORD instead.

Deprecated. Use system.BYTES-PER-WORD instead.

Cancelled error.
Thrown when a task has been cancelled.

Deadline exceeded error.
Thrown when a with-timeout times out.

The number of bytes per kilobyte.

Deprecated: Use system.LINE-TERMINATOR instead.

The number of bytes per megabyte.

Deprecated: Use system.PLATFORM-FREERTOS instead.

Deprecated: Use system.PLATFORM-LINUX instead.

Deprecated: Use system.PLATFORM-MACOS instead.

Deprecated: Use system.PLATFORM-WINDOWS instead.

Deprecated: Use system.STATS-INDEX-ALLOCATED-MEMORY instead.

Deprecated: Use system.STATS-INDEX-BYTES-ALLOCATED-IN-OBJECT-HEAP instead.

Deprecated: Use system.STATS-INDEX-FULL-COMPACTING-GC-COUNT instead.

Deprecated: Use system.STATS-INDEX-FULL-GC-COUNT instead.

Deprecated: Use system.STATS-INDEX-GC-COUNT instead.

Deprecated: Use system.STATS-INDEX-GROUP-ID instead.

Deprecated: Use system.STATS-INDEX-PROCESS-ID instead.

Deprecated: Use system.STATS-INDEX-PROCESS-MESSAGE-COUNT instead.

Deprecated: Use system.STATS-INDEX-RESERVED-MEMORY instead.

Deprecated: Use system.STATS-INDEX-SYSTEM-FREE-MEMORY instead.

Deprecated: Use system.STATS-INDEX-SYSTEM-LARGEST-FREE instead.

Exported functions

__entry__main task/any -> none

__entry__spawn task/any -> none

__entry__task lambda/any -> none

add-finalizer object/any lambda/any -> none
Registers the given lambda as a finalizer for the object.
Calls the finalizer if all references to the object are lost. (See limitations below).
Errors
It is an error to assign a finalizer to a smi or an instance that already has a finalizer (see remove-finalizer).
It is also an error to assign null as a finalizer.
Warning
Misuse of this API can lead to undefined behavior that is hard to debug.
Advanced
Finalizers are not automatically called when a program exits. This is also true for objects that weren't reachable anymore before the program exited.
An arbitrary amount of time may pass from the object becomes unreachable and the finalizer is called.

Returns information about who build this application snapshot.
Deprecated. Use system.app-sdk-info instead.

Returns the Toit SDK version that generated this application snapshot.
Deprecated. Use system.app-sdk-version instead.

catch --trace/bool=false --unwind/bool=false [block] -> any
If an exception is thrown during the block call, then the trace is printed if the trace is true (and the trace can otherwise be printed).
If an exception is thrown during the block call, then unwinding continues if the unwind is true.

catch --trace/bool=false [--unwind] [block] -> any
If an exception is thrown during the block call, then the trace is printed if the trace is true (and the trace can otherwise be printed).

catch [--trace] --unwind/bool=false [block] -> any
If an exception is thrown during the block call, then unwinding continues if the unwind is true.

catch [--trace] [--unwind] [block] -> any
Catches exceptions thrown in the given block.
Returns null if the call to the block completes without exception.
Returns the thrown exception if an exception is thrown during the call of the block and unwind returns a falsy value (see below).
The trace block decides whether the trace should be printed in case an exception has been thrown. The trace block is called with the thrown exception and the trace (trace.call exception trace) and should return a boolean. If the trace call returns true, then the trace is printed. However, the trace can only be printed if there is a trace and this task hasn't been cancelled.
The unwind block decides whether unwinding should continue in case of a caught exception. The unwind block is called with the thrown exception and the trace (unwind.call exception trace) and should return a boolean value. If unwind call returns true, then unwinding continues.

Returns the number of initial zeros in binary representation of the argument.
The argument is treated as an unsigned 64 bit number. Thus it returns 0 if given a negative input.
Deprecated. Use int.count-leading-zeros instead.

Creates an off-heap byte array with the given size.
Off-heap byte arrays are preferred when transferring data between applications.

critical-do --respect-deadline/bool=true [block] -> none
Enters and calls the given critical block.
Within block, the current task won't be interrupted by cancellation exceptions. Instead such exceptions will be delayed until the block is left. The critical block can be interrupted by a timeout (see with-timeout) if respect-deadline is true.

debug object/any -> none
Prints the given object for debugging.
Does not yield the currently running task.

debug -> none
Prints an empty line for debugging.
Does not yield the currently running task.

exit status/int -> none
Exits the VM with the given status.
Argument $status
0 signals a successful exit. All other statuses are error codes.

Deprecated: Use system.gc-count instead.

hex-char-to-value char/int [--on-error] -> int
Deprecated. Use hex-char-to-value char [--if-error] instead.

hex-char-to-value char/int [--if-error] -> int
Converts a hex digit character char in the ranges '0'-'9', 'a'-'f', or 'A'-'F'.
Returns the value between 0 and 15.
Calls the block if-error on invalid input and returns its return value if any.

Converts a hex digit character in the ranges '0'-'9', 'a'-'f', or 'A'-'F'.
Returns the value between 0 and 15.
The input must be a valid hex digit character.

hex-digit char/int [error-block] -> int
Deprecated. Use hex-char-to-value instead.

hex-digit char/int -> int
Deprecated. Use hex-char-to-value instead.

identical x/any y/any -> bool
Whether x and y are identical objects.
Every object is identical to itself.
For numbers, two objects are furthermore identical if they have the same numeric value. Contrary to == two numbers must be of the same type, and have the same bit-pattern. As such, -0.0 == 0.0, but not identical -0.0 0.0.
Two strings are identical if they contain the same characters. For example, we have identical "tw" + "in" "twin".
For floats, two NaN's (not-a-number) are identical when they have the same bits. For example, we have identical float.NAN float.NAN, but not identical float.NAN (float.from-bits float.NAN.bits + 1). This is unlike == where NaN's are never equal, so float.NAN == float.NAN is always false.

Returns the max of a and b.
Returns a if a and b are equal.
Returns float.NAN if either is float.NAN.
Returns the greater element, according to Comparable.compare-to, otherwise.

Returns the min of a and b.
Returns a if a and b are equal.
Returns float.NAN if either is float.NAN.
Returns the smaller element, according to Comparable.compare-to, otherwise.

Deprecated: Use system.platform instead.

print message/any -> none
Prints the message.
The resulting message is stringified using Object.stringify.

print -> none
Prints an empty line.
This function is generally used to improve the output of the console output, but may have no effect on other receivers of the print message.

process-stats --gc/bool=false list/List=(List system.STATS-LIST-SIZE_) -> List
Deprecated: Use system.process-stats instead.

process-stats --gc/bool=false group/any id/any list/List=(List system.STATS-LIST-SIZE_) -> List
Deprecated: Use system.process-stats instead.

Returns the name of the toit file, image, or snapshot that the current program was run from. May return null if this information is not available.
Deprecated. Use system.program-name instead.

random -> any
Returns a random number in the range [0..0xFFF_FFFF] (inclusive).
The returned number is the result of a PRNG (pseudo random number generator). The seed of the PRNG can be changed by calling set-random-seed.

random n/int -> any
Returns a random number in the range [0..n[ (n exclusive).
The returned number is the result of a PRNG (pseudo random number generator). The seed of the PRNG can be changed by calling set-random-seed.

random start/int end/int -> any
Returns a pseudo-random number from start to end - 1.

remove-finalizer object/any -> bool
Unregisters the finalizer registered for object.
Returns whether the object had a finalizer.

rethrow exception/any trace/ByteArray -> none
Rethrows the given exception along with the trace.
Works like throw except it uses the given trace rather than generating one at the throw point.
Used to rethrow a caught exception (see catch [--trace] [block]) as though it wasn't caught.

round-down value/int divisor/int -> int
Rounds a non-negative value down to the previous multiple of the divisor.
Examples

round-down 27 10  // => 20
round-down 7 2    // => 6
round-down -13 10 // => OUT_OF_RANGE error

round-up value/int divisor/int -> int
Rounds a non-negative value up to the next multiple of the divisor.
Examples

round-up 17 10  // => 20
round-up 7 2    // => 8
round-up -13 10 // => OUT_OF_RANGE error

set-random-seed seed/Data -> none
Seeds the random number generator with the seed.
Currently only the first 16 bytes of the seed are used.

set-timezone rules/string -> any
Stores the given rules in the TZ environment variable and calls tzset, thus activating it.
Valid TZ values can be easily obtained by looking at the last line of the zoneinfo files on Linux machines:

tail -n1 /usr/share/zoneinfo/Europe/Copenhagen
Examples

set-timezone "CET-1CEST,M3.5.0,M10.5.0/3"  // Central European Timezone (as of 2024).
set-timezone "PST8PDT,M3.2.0,M11.1.0"  // Pacific Time (as of 2024).

sleep duration/Duration -> none
Makes the current task sleep for the duration.

sleep --ms/int -> none
Makes the current task sleep for the given ms of milliseconds.

spawn lambda/Lambda --priority/int=null -> Process
Spawns a new process that starts executing the given lambda.
The new process does not share any memory with the spawning process. If the lambda captures variables, those are copied to the new process.
May throw if the captured variables can't be serialized.

Returns the object for the current task.
Deprecated: Use Task.current instead.

task code/Lambda --name/string="User task" --background/bool=null -> Task
Creates a new user task.
Calls code in a new task.
If the background flag is set, then the new task will not block termination. The background task flag is passed on to sub-tasks.

throw exception/any -> none
Throws the given exception.
Unwinds the stack of this task calling associated finally-blocks. By default, a task with an exception will print the stack trace and terminate the task.
The exception can also be caught with a catch [block].

Converts a number between 0 and 15 to a lower case hex digit.

Converts a number between 0 and 15 to an upper case hex digit.

unreachable -> none
Marks code point as unreachable.
Must not be reached in the program.

utf-8-bytes char/any -> any

Returns information about who built this virtual machine.
Deprecated. Use system.vm-sdk-info instead.

Returns the Toit SDK model that this virtual machine has been built from.
Deprecated. Use system.vm-sdk-model instead.

Returns the Toit SDK version that this virtual machine has been built from.
Deprecated. Use system.vm-sdk-version instead.

with-timeout timeout/Duration [block] -> any
Calls the given block but throws an exception if the timeout is exceeded.
If timeout is null, calls the block without a timeout.

with-timeout --ms/int [block] -> any
Calls the given block but throws an exception if a timeout of ms milliseconds is exceeded.

with-timeout --us/int [block] -> any
Calls the given block but throws an exception if a timeout of us microseconds is exceeded.

write-utf-8-to-byte-array byte-array/any position/any char/any -> any

yield -> none
Voluntarily yields control of the CPU to other tasks.
Advanced
The Toit programming language is cooperatively scheduled, so it is important to place yields in long running loops if other tasks should get an opportunity to run.