Trait neon::context::Context [−][src]
pub trait Context<'a>: ContextInternal<'a> {}Show methods
fn lock(&self) -> Lock<'_> { ... } fn borrow<'c, V, T, F>(&self, v: &'c Handle<'_, V>, f: F) -> T
where
V: Value,
&'c V: Borrow,
F: for<'b> FnOnce(Ref<'b, <&'c V as Borrow>::Target>) -> T, { ... } fn borrow_mut<'c, V, T, F>(&self, v: &'c mut Handle<'_, V>, f: F) -> T
where
V: Value,
&'c mut V: BorrowMut,
F: for<'b> FnOnce(RefMut<'b, <&'c mut V as Borrow>::Target>) -> T, { ... } fn execute_scoped<T, F>(&self, f: F) -> T
where
F: for<'b> FnOnce(ExecuteContext<'b>) -> T, { ... } fn compute_scoped<V, F>(&self, f: F) -> JsResult<'a, V>
where
V: Value,
F: for<'b, 'c> FnOnce(ComputeContext<'b, 'c>) -> JsResult<'b, V>, { ... } fn try_catch<T, F>(&mut self, f: F) -> Result<T, Handle<'a, JsValue>>
where
F: FnOnce(&mut Self) -> NeonResult<T>, { ... } fn boolean(&mut self, b: bool) -> Handle<'a, JsBoolean> { ... } fn number<T: Into<f64>>(&mut self, x: T) -> Handle<'a, JsNumber> { ... } fn string<S: AsRef<str>>(&mut self, s: S) -> Handle<'a, JsString> { ... } fn try_string<S: AsRef<str>>(&mut self, s: S) -> StringResult<'a> { ... } fn null(&mut self) -> Handle<'a, JsNull> { ... } fn undefined(&mut self) -> Handle<'a, JsUndefined> { ... } fn empty_object(&mut self) -> Handle<'a, JsObject> { ... } fn empty_array(&mut self) -> Handle<'a, JsArray> { ... } fn array_buffer(&mut self, size: u32) -> JsResult<'a, JsArrayBuffer> { ... } fn buffer(&mut self, size: u32) -> JsResult<'a, JsBuffer> { ... } fn date(
&mut self,
value: impl Into<f64>
) -> Result<Handle<'a, JsDate>, DateError> { ... } fn global(&mut self) -> Handle<'a, JsObject> { ... } fn throw<T: Value, U>(&mut self, v: Handle<'_, T>) -> NeonResult<U> { ... } fn error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError> { ... } fn type_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError> { ... } fn range_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError> { ... } fn throw_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T> { ... } fn throw_type_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T> { ... } fn throw_range_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T> { ... } fn boxed<U: Finalize + Send + 'static>(
&mut self,
v: U
) -> Handle<'a, JsBox<U>> { ... } fn queue(&mut self) -> EventQueue { ... }
Expand description
An execution context, which represents the current state of a thread of execution in the JavaScript engine.
All interaction with the JavaScript engine in Neon code is mediated through instances of this trait.
A context has a lifetime 'a
, which ensures the safety of handles managed by the JS garbage collector. All handles created during the lifetime of a context are kept alive for that duration and cannot outlive the context.
Provided methods
fn lock(&self) -> Lock<'_>
[src]
Expand description
Lock the JavaScript engine, returning an RAII guard that keeps the lock active as long as the guard is alive.
If this is not the currently active context (for example, if it was used to spawn a scoped context with execute_scoped
or compute_scoped
), this method will panic.
fn borrow<'c, V, T, F>(&self, v: &'c Handle<'_, V>, f: F) -> T where
V: Value,
&'c V: Borrow,
F: for<'b> FnOnce(Ref<'b, <&'c V as Borrow>::Target>) -> T,
[src]
V: Value,
&'c V: Borrow,
F: for<'b> FnOnce(Ref<'b, <&'c V as Borrow>::Target>) -> T,
Expand description
Convenience method for locking the JavaScript engine and borrowing a single JS value’s internals.
Example:
let b: Handle<JsArrayBuffer> = cx.argument(0)?; let x: u32 = cx.borrow(&b, |data| { data.as_slice()[0] }); let n: Handle<JsNumber> = cx.number(x);
Note: the borrowed value is required to be a reference to a handle instead of a handle
as a workaround for a Rust compiler bug.
We may be able to generalize this compatibly in the future when the Rust bug is fixed,
but while the extra &
is a small ergonomics regression, this API is still a nice
convenience.
fn borrow_mut<'c, V, T, F>(&self, v: &'c mut Handle<'_, V>, f: F) -> T where
V: Value,
&'c mut V: BorrowMut,
F: for<'b> FnOnce(RefMut<'b, <&'c mut V as Borrow>::Target>) -> T,
[src]
V: Value,
&'c mut V: BorrowMut,
F: for<'b> FnOnce(RefMut<'b, <&'c mut V as Borrow>::Target>) -> T,
Expand description
Convenience method for locking the JavaScript engine and mutably borrowing a single JS value’s internals.
Example:
let mut b: Handle<JsArrayBuffer> = cx.argument(0)?; cx.borrow_mut(&mut b, |data| { let slice = data.as_mut_slice::<u32>(); slice[0] += 1; });
Note: the borrowed value is required to be a reference to a handle instead of a handle
as a workaround for a Rust compiler bug.
We may be able to generalize this compatibly in the future when the Rust bug is fixed,
but while the extra &mut
is a small ergonomics regression, this API is still a nice
convenience.
fn execute_scoped<T, F>(&self, f: F) -> T where
F: for<'b> FnOnce(ExecuteContext<'b>) -> T,
[src]
F: for<'b> FnOnce(ExecuteContext<'b>) -> T,
Expand description
Executes a computation in a new memory management scope.
Handles created in the new scope are kept alive only for the duration of the computation and cannot escape.
This method can be useful for limiting the life of temporary values created during long-running computations, to prevent leaks.
fn compute_scoped<V, F>(&self, f: F) -> JsResult<'a, V> where
V: Value,
F: for<'b, 'c> FnOnce(ComputeContext<'b, 'c>) -> JsResult<'b, V>,
[src]
V: Value,
F: for<'b, 'c> FnOnce(ComputeContext<'b, 'c>) -> JsResult<'b, V>,
Expand description
Executes a computation in a new memory management scope and computes a single result value that outlives the computation.
Handles created in the new scope are kept alive only for the duration of the computation and cannot escape, with the exception of the result value, which is rooted in the outer context.
This method can be useful for limiting the life of temporary values created during long-running computations, to prevent leaks.
fn try_catch<T, F>(&mut self, f: F) -> Result<T, Handle<'a, JsValue>> where
F: FnOnce(&mut Self) -> NeonResult<T>,
[src]
F: FnOnce(&mut Self) -> NeonResult<T>,
fn boolean(&mut self, b: bool) -> Handle<'a, JsBoolean>
[src]
Expand description
Convenience method for creating a JsBoolean
value.
fn number<T: Into<f64>>(&mut self, x: T) -> Handle<'a, JsNumber>
[src]
Expand description
Convenience method for creating a JsNumber
value.
fn string<S: AsRef<str>>(&mut self, s: S) -> Handle<'a, JsString>
[src]
Expand description
Convenience method for creating a JsString
value.
If the string exceeds the limits of the JS engine, this method panics.
fn try_string<S: AsRef<str>>(&mut self, s: S) -> StringResult<'a>
[src]
Expand description
Convenience method for creating a JsString
value.
If the string exceeds the limits of the JS engine, this method returns an Err
value.
fn null(&mut self) -> Handle<'a, JsNull>
[src]
Expand description
Convenience method for creating a JsNull
value.
fn undefined(&mut self) -> Handle<'a, JsUndefined>
[src]
Expand description
Convenience method for creating a JsUndefined
value.
fn empty_object(&mut self) -> Handle<'a, JsObject>
[src]
Expand description
Convenience method for creating an empty JsObject
value.
fn empty_array(&mut self) -> Handle<'a, JsArray>
[src]
Expand description
Convenience method for creating an empty JsArray
value.
fn array_buffer(&mut self, size: u32) -> JsResult<'a, JsArrayBuffer>
[src]
Expand description
Convenience method for creating an empty JsArrayBuffer
value.
fn buffer(&mut self, size: u32) -> JsResult<'a, JsBuffer>
[src]
Expand description
Convenience method for creating an empty JsBuffer
value.
fn date(
&mut self,
value: impl Into<f64>
) -> Result<Handle<'a, JsDate>, DateError>
[src]
&mut self,
value: impl Into<f64>
) -> Result<Handle<'a, JsDate>, DateError>
Expand description
Convenience method for creating a JsDate
value.
fn global(&mut self) -> Handle<'a, JsObject>
[src]
Expand description
Produces a handle to the JavaScript global object.
fn throw<T: Value, U>(&mut self, v: Handle<'_, T>) -> NeonResult<U>
[src]
Expand description
Throws a JS value.
fn error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
[src]
Expand description
Creates a direct instance of the Error
class.
fn type_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
[src]
Expand description
Creates an instance of the TypeError
class.
fn range_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
[src]
Expand description
Creates an instance of the RangeError
class.
fn throw_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>
[src]
Expand description
Throws a direct instance of the Error
class.
fn throw_type_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>
[src]
Expand description
Throws an instance of the TypeError
class.
fn throw_range_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>
[src]
Expand description
Throws an instance of the RangeError
class.
fn boxed<U: Finalize + Send + 'static>(&mut self, v: U) -> Handle<'a, JsBox<U>>
[src]
Expand description
Convenience method for wrapping a value in a JsBox
.
Example:
struct Point(usize, usize); impl Finalize for Point {} fn my_neon_function(mut cx: FunctionContext) -> JsResult<JsBox<Point>> { let point = cx.boxed(Point(0, 1)); Ok(point) }
fn queue(&mut self) -> EventQueue
[src]
Expand description
Creates an unbounded queue of events to be executed on a JavaScript thread