Module neon::handle [−][src]
Expand description
References to garbage-collected JavaScript values.
A handle is a safe reference to a JavaScript value that is owned and managed by the JavaScript engine’s memory management system (the garbage collector).
Neon APIs that accept and return JavaScript values never use raw pointer types
(*T
) or reference types (&T
). Instead they use the
special Neon type Handle
, which encapsulates a JavaScript
Value
and ensures that Rust only maintains access to
the value while it is guaranteed to be valid.
Working with Handles
The Handle<T>
type automatically dereferences to T
(via the standard
Deref
trait), so you can call T
’s methods on a value of
type Handle<T>
. For example, we can call
JsNumber::value()
on a Handle<JsNumber>
:
let n: Handle<JsNumber> = cx.argument(0)?; let v = n.value(&mut cx); // JsNumber::value()
Example
This Neon function takes an object as its argument, extracts two properties,
width
and height
, and multiplies them together as numbers. Each JavaScript
value in the calculation is stored locally in a Handle
.
fn area(mut cx: FunctionContext) -> JsResult<JsNumber> { let rect: Handle<JsObject> = cx.argument(0)?; let width: Handle<JsNumber> = rect .get(&mut cx, "width")? .downcast_or_throw(&mut cx)?; let w: f64 = width.value(&mut cx); let height: Handle<JsNumber> = rect .get(&mut cx, "height")? .downcast_or_throw(&mut cx)?; let h: f64 = height.value(&mut cx); Ok(cx.number(w * h)) }
Structs
DowncastError | An error representing a failed downcast. |
Handle | A handle to a JavaScript value that is owned by the JavaScript engine. |
Traits
Managed | The trait of data owned by the JavaScript engine and that can only be accessed via handles. |
Type Definitions
DowncastResult | The result of a call to |