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

Structs

BinaryData

A reference to the internal backing buffer data of a Buffer or ArrayBuffer object, which can be accessed via the Borrow and BorrowMut traits.

DateError

The Error struct for a Date

JsArray

A JavaScript array object, i.e. a value for which Array.isArray would return true.

JsArrayBuffer

The standard JS ArrayBuffer type.

JsBoolean

A JavaScript boolean primitive value.

JsBox

A smart pointer for Rust data managed by the JavaScript engine.

JsBuffer

The Node Buffer type.

JsDate

A JavaScript Date object

JsError

A JS Error object.

JsFunction

A JavaScript function object.

JsNull

The JavaScript null value.

JsNumber

A JavaScript number value.

JsObject

A JavaScript object.

JsString

A JavaScript string primitive value.

JsUndefined

The JavaScript undefined value.

JsValue

A JavaScript value of any type.

StringOverflow

An error produced when constructing a string that exceeds the JS engine’s maximum string size.

Enums

DateErrorKind

The error kinds corresponding to DateError

Traits

BinaryViewType

The trait for element types by which a buffer’s binary data can be indexed.

Value

The trait shared by all JavaScript values.

Type Definitions

StringResult

The result of constructing a new JsString.