Unsafe code
Unsafe functions
Keen provides some safety guarantees:
- Every value is what the type says it is.
- No value is
null. - Immutable objects never change.
- Non-
globalfunctions don't have global side effects. (See Global side effects.) - Non-
globalfunctions behave the same on all targets.
Some functions may break these guarantees, so they are marked unsafe.
Some common unsafe functions are in keen/unsafe.
unsafe is a spec (see Specs),
so any function can be marked unsafe with the syntax for using a spec.
An unsafe function can call any other unsafe function.
You can mark your own functions unsafe for any reason.
For example, a function that exposes implementation details of a library could be marked unsafe
even if the library is written using only safe code.
Using unsafe functions safely
There would be no point to unsafe functions if there weren't some way to use them safely.
How to do so depends on the function, so read the documentation carefully.
To call an unsafe function from a non-unsafe function, wrap the call in a trusted expression.
The above calls unsafe functions safely:
uninitialized-bufferisunsafein general because it returns uninitialized memory.
(That means a default value such asnull, but that is still unsafe by Keen's standards.)
It's safe in this case because all buffer elements are initialized before using them.cast-immutableisunsafein general because it treats abufferas an immutablearray(without copying).
That's unsafe because immutable data should never change.
It's safe in this case because there is no remaining reference to the buffer.
The argument to trusted can be an indented block:
Some important unsafe functions
gc-safe-value returns an arbitrary constant value for a type.
This is useful to overwrite unused elements of a buffer so that they don't hold on to memory.
reference-equal returns true if two references point to the same place in memory.
This can vary depending on how well Keen optimizes constants.
reinterpret-cast treats any type as any other type.
This can lead to bytecode validation errors given a Java target,
or unpredictable behavior when the target is JavaScript.