Module neon::types

source ·
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:

flowchart LR JsValue(JsValue) JsValue-->JsObject(JsObject) click JsValue "./struct.JsValue.html" "JsValue" click JsObject "./struct.JsObject.html" "JsObject" subgraph primitives [Primitive Types] JsBoolean(JsBoolean) JsNumber(JsNumber) JsString(JsString) JsNull(JsNull) JsUndefined(JsUndefined) click JsBoolean "./struct.JsBoolean.html" "JsBoolean" click JsNumber "./struct.JsNumber.html" "JsNumber" click JsString "./struct.JsString.html" "JsString" click JsNull "./struct.JsNull.html" "JsNull" click JsUndefined "./struct.JsUndefined.html" "JsUndefined" end JsValue-->primitives

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:

flowchart LR JsObject(JsObject) click JsObject "./struct.JsObject.html" "JsObject" subgraph objects [Standard Object Types] JsFunction(JsFunction) JsArray(JsArray) JsDate(JsDate) JsError(JsError) click JsFunction "./struct.JsFunction.html" "JsFunction" click JsArray "./struct.JsArray.html" "JsArray" click JsDate "./struct.JsDate.html" "JsDate" click JsError "./struct.JsError.html" "JsError" end subgraph typedarrays [Typed Arrays] JsBuffer(JsBuffer) JsArrayBuffer(JsArrayBuffer) JsTypedArray("JsTypedArray<T>") click JsBuffer "./struct.JsBuffer.html" "JsBuffer" click JsArrayBuffer "./struct.JsArrayBuffer.html" "JsArrayBuffer" click JsTypedArray "./struct.JsTypedArray.html" "JsTypedArray" end subgraph custom [Custom Types] JsBox(JsBox) click JsBox "./struct.JsBox.html" "JsBox" end JsObject-->objects JsObject-->typedarrays JsObject-->custom

These include several categories of object types:

All object types implement the Object trait, which allows getting and setting properties of an object.

Modules

Types and traits for working with binary buffers.
Types and traits for working with JavaScript functions.

Structs

DateErrornapi-5
The Error struct for a Date
Deferred is a handle that can be used to resolve or reject a JsPromise
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.
JsDatenapi-5
A JavaScript Date object
A JS Error object.
A JavaScript function object.
JsFuturenapi-5 and futures
A Future created from a JsPromise.
The JavaScript null value.
A JavaScript number value.
A JavaScript object.
The JavaScript Promise value.
A JavaScript string primitive value.
The family of JS typed array types.
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

The error kinds corresponding to DateError

Traits

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 standard JS BigInt64Array type.
The standard JS BigUint64Array type.
The standard JS Float32Array type.
The standard JS Float64Array type.
The standard JS Int8Array type.
The standard JS Int16Array type.
The standard JS Int32Array type.
The standard JS Uint8Array type.
The standard JS Uint16Array type.
The standard JS Uint32Array type.
The result of constructing a new JsString.