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();

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> {

The JavaScript Type Hierarchy

The Neon type hierarchy, described in detail below.

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’s unknown type.)
  • JsObject: This is the top of the object type hierarchy. Object types all implement the Object trait, which allows getting and setting properties.
  • Primitive types: These are the built-in JavaScript datatypes that are not object types: JsNumber, JsBoolean, JsString, JsNull, and JsUndefined.


Types and traits for working with JavaScript functions.



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.


A JavaScript Date object

A JS Error object.

A JavaScript function object.

The JavaScript null value.

A JavaScript number value.

A JavaScript object.


The JavaScript Promise value.

A JavaScript string primitive value.

The standard JS TypedArray type.

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.


The error kinds corresponding to DateError


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.