Module neon::types [−][src]
Expand description
Representations of JavaScript’s core builtin types.
Modeling JavaScript Types
All JavaScript values in Neon implement the abstract Value
trait, which
is the most generic way to work with JavaScript values. Neon provides a
number of types that implement this trait, each representing a particular
type of JavaScript value.
By convention, JavaScript types in Neon have the prefix Js
in their name,
such as JsNumber
(for the JavaScript number
type) or JsFunction
(for the JavaScript
function
type).
Handles and Casts
Access to JavaScript values in Neon works through handles,
which ensure the safe interoperation between Rust and the JavaScript garbage
collector. This means, for example, a Rust variable that stores a JavaScript string
will have the type Handle<JsString>
rather than JsString
.
Neon types model the JavaScript type hierarchy through the use of casts.
The Handle::upcast()
method safely converts
a handle to a JavaScript value of one type into a handle to a value of its
supertype. For example, it’s safe to treat a JsArray
as a JsObject
, so you can do an “upcast” and it will
never fail:
fn as_object(array: Handle<JsArray>) -> Handle<JsObject> {
let object: Handle<JsObject> = array.upcast();
object
}
Unlike upcasts, the Handle::downcast()
method
requires a runtime check to test a value’s type at runtime, so it can fail with
a DowncastError
:
fn as_array<'a>(
cx: &mut impl Context<'a>,
object: Handle<'a, JsObject>
) -> JsResult<'a, JsArray> {
object.downcast(cx).or_throw(cx)
}
The JavaScript Type Hierarchy
The JavaScript type hierarchy includes:
JsValue
: This is the top of the type hierarchy, and can refer to any JavaScript value. (For TypeScript programmers, this can be thought of as similar to TypeScript’sunknown
type.)JsObject
: This is the top of the object type hierarchy. Object types all implement theObject
trait, which allows getting and setting properties.- Standard object types:
JsFunction
,JsArray
,JsDate
, andJsError
. - Typed arrays:
JsBuffer
andJsArrayBuffer
. - Custom types:
JsBox
, a special Neon type that allows the creation of custom objects that own Rust data structures.
- Standard object types:
- Primitive types: These are the built-in JavaScript datatypes that are not
object types:
JsNumber
,JsBoolean
,JsString
,JsNull
, andJsUndefined
.
Structs
A reference to the internal backing buffer data of a Buffer
or ArrayBuffer
object, which can be accessed via the Borrow
and BorrowMut
traits.
napi-5
The Error struct for a Date
A JavaScript array object, i.e. a value for which Array.isArray
would return true
.
The standard JS ArrayBuffer
type.
A JavaScript boolean primitive value.
A smart pointer for Rust data managed by the JavaScript engine.
The Node Buffer
type.
napi-5
A JavaScript Date object
A JS Error
object.
A JavaScript function object.
The JavaScript null
value.
A JavaScript number value.
A JavaScript object.
A JavaScript string primitive value.
The JavaScript undefined
value.
A JavaScript value of any type.
An error produced when constructing a string that exceeds the JS engine’s maximum string size.
Enums
napi-5
The error kinds corresponding to DateError
Traits
The trait for element types by which a buffer’s binary data can be indexed.
Finalize is executed on the main JavaScript thread and executed immediately
before garbage collection.
Values contained by a JsBox
must implement Finalize
.
The trait shared by all JavaScript values.
Type Definitions
The result of constructing a new JsString
.