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 top of the JavaScript type hierarchy is modeled with the Neon type
JsValue. A handle to a JsValue
can refer to any JavaScript value. (For TypeScript programmers, this can be
thought of as similar to TypeScript’s unknown type.)
From there, the type hierarchy divides into object types and primitive types:
The top of the object type hierarchy is JsObject. A
handle to a JsObject can refer to any JavaScript object.
The primitive types are the built-in JavaScript datatypes that are not object
types: JsBoolean, JsNumber,
JsString, JsNull, and
JsUndefined.
§Object Types
The object type hierarchy further divides into a variety of different subtypes:
These include several categories of object types:
- Standard object types:
JsFunction,JsArray,JsDate, andJsError. - Typed arrays:
JsBuffer,JsArrayBuffer, andJsTypedArray<T>. - Custom types:
JsBox, a special Neon type that allows the creation of custom objects that own Rust data structures.
All object types implement the Object trait, which
allows getting and setting properties of an object.
Modules§
- bigint
napi-6 - Types for working with
JsBigInt. - buffer
- Types and traits for working with binary buffers.
- extract
- Traits and utilities for extract Rust data from JavaScript values.
- function
- Types and traits for working with JavaScript functions.
Structs§
- Date
Error - An error produced when constructing a date with an invalid value.
- Deferred
- A controller struct that can be used to resolve or reject a
JsPromise. - JsArray
- The type of JavaScript
Arrayobjects. - JsArray
Buffer - The type of JavaScript
ArrayBufferobjects. - JsBig
Int napi-6 - The type of JavaScript
BigIntvalues. - JsBoolean
- The type of JavaScript Boolean primitives.
- JsBox
- A JavaScript smart pointer object that owns Rust data.
- JsBuffer
- The type of Node
Bufferobjects. - JsDate
- The type of JavaScript
Dateobjects. - JsError
- The type of JavaScript
Errorobjects. - JsFunction
- The type of JavaScript
Functionobjects. - JsFuture
napi-5andfutures - A type of JavaScript
Promiseobject that acts as aFuture. - JsNull
- The type of JavaScript
nullprimitives. - JsNumber
- The type of JavaScript number primitives.
- JsObject
- The type of JavaScript objects, i.e., the root of all object types.
- JsPromise
- The type of JavaScript
Promiseobjects. - JsString
- The type of JavaScript string primitives.
- JsTyped
Array - The family of JavaScript typed array types.
- JsUndefined
- The type of JavaScript
undefinedprimitives. - JsValue
- The type of any JavaScript value, i.e., the root of all types.
- String
Overflow - An error produced when constructing a string that exceeds the limits of the runtime.
Enums§
- Date
Error Kind - The error kinds corresponding to
DateError
Traits§
- Finalize
- A trait for finalizing values owned by the main JavaScript thread.
- Value
- The trait shared by all JavaScript values.
Type Aliases§
- JsBig
Int64 Array - The type of JavaScript
BigInt64Arrayobjects. - JsBig
Uint64 Array - The type of JavaScript
BigUint64Arrayobjects. - JsFloat32
Array - The type of JavaScript
Float32Arrayobjects. - JsFloat64
Array - The type of JavaScript
Float64Arrayobjects. - JsInt8
Array - The type of JavaScript
Int8Arrayobjects. - JsInt16
Array - The type of JavaScript
Int16Arrayobjects. - JsInt32
Array - The type of JavaScript
Int32Arrayobjects. - JsUint8
Array - The type of JavaScript
Uint8Arrayobjects. - JsUint16
Array - The type of JavaScript
Uint16Arrayobjects. - JsUint32
Array - The type of JavaScript
Uint32Arrayobjects. - String
Result - The result of constructing a new
JsString.