neon::context

Struct FunctionContext

Source
pub struct FunctionContext<'cx> { /* private fields */ }
Expand description

An execution context of a function call.

The type parameter T is the type of the this-binding.

Implementations§

Source§

impl<'cx> FunctionContext<'cx>

Source

pub fn kind(&self) -> CallKind

Indicates whether the function was called with new.

Source

pub fn len(&self) -> usize

Indicates the number of arguments that were passed to the function.

Source

pub fn is_empty(&self) -> bool

Indicates if no arguments were passed to the function.

Source

pub fn argument_opt(&mut self, i: usize) -> Option<Handle<'cx, JsValue>>

Produces the ith argument, or None if i is greater than or equal to self.len().

Source

pub fn argument<V: Value>(&mut self, i: usize) -> JsResult<'cx, V>

Produces the ith argument and casts it to the type V, or throws an exception if i is greater than or equal to self.len() or cannot be cast to V.

Source

pub fn this<T: Value>(&mut self) -> JsResult<'cx, T>

Produces a handle to the this-binding and attempts to downcast as a specific type. Equivalent to calling cx.this_value().downcast_or_throw(&mut cx).

Throws an exception if the value is a different type.

Source

pub fn this_value(&mut self) -> Handle<'cx, JsValue>

Produces a handle to the function’s this-binding.

Source

pub fn args<T>(&mut self) -> NeonResult<T>
where T: FromArgs<'cx>,

Extract Rust data from the JavaScript arguments.

This is frequently more efficient and ergonomic than getting arguments individually. See the extract module documentation for more examples.

fn add(mut cx: FunctionContext) -> JsResult<JsNumber> {
    let (a, b): (f64, f64) = cx.args()?;

    Ok(cx.number(a + b))
}
Source

pub fn args_opt<T>(&mut self) -> NeonResult<Option<T>>
where T: FromArgs<'cx>,

Extract Rust data from the JavaScript arguments.

Similar to FunctionContext::args, but does not throw a JavaScript exception on errors. Useful for function overloading.

fn combine(mut cx: FunctionContext) -> JsResult<JsValue> {
    if let Some((a, b)) = cx.args_opt::<(f64, f64)>()? {
        return Ok(cx.number(a + b).upcast());
    }

    let (a, b): (String, String) = cx.args()?;

    Ok(cx.string(a + &b).upcast())
}

Trait Implementations§

Source§

impl<'cx> Context<'cx> for FunctionContext<'cx>

Source§

fn lock<'b>(&'b mut self) -> Lock<'b, Self>
where 'a: 'b,

Lock the JavaScript engine, returning an RAII guard that keeps the lock active as long as the guard is alive. Read more
Source§

fn execute_scoped<'b, T, F>(&mut self, f: F) -> T
where F: FnOnce(Cx<'b>) -> T, 'a: 'b,

Executes a computation in a new memory management scope. Read more
Source§

fn compute_scoped<'b, V, F>(&mut self, f: F) -> JsResult<'a, V>
where V: Value, F: FnOnce(Cx<'b>) -> JsResult<'b, V>, 'a: 'b,

Executes a computation in a new memory management scope and computes a single result value that outlives the computation. Read more
Source§

fn try_catch<T, F>(&mut self, f: F) -> Result<T, Handle<'a, JsValue>>
where F: FnOnce(&mut Self) -> NeonResult<T>,

Source§

fn boolean(&mut self, b: bool) -> Handle<'a, JsBoolean>

Convenience method for creating a JsBoolean value.
Source§

fn number<T: Into<f64>>(&mut self, x: T) -> Handle<'a, JsNumber>

Convenience method for creating a JsNumber value.
Source§

fn string<S: AsRef<str>>(&mut self, s: S) -> Handle<'a, JsString>

Convenience method for creating a JsString value. Read more
Source§

fn try_string<S: AsRef<str>>(&mut self, s: S) -> StringResult<'a>

Convenience method for creating a JsString value. Read more
Source§

fn null(&mut self) -> Handle<'a, JsNull>

Convenience method for creating a JsNull value.
Source§

fn undefined(&mut self) -> Handle<'a, JsUndefined>

Convenience method for creating a JsUndefined value.
Source§

fn empty_object(&mut self) -> Handle<'a, JsObject>

Convenience method for creating an empty JsObject value.
Source§

fn empty_array(&mut self) -> Handle<'a, JsArray>

Convenience method for creating an empty JsArray value.
Source§

fn array_buffer(&mut self, size: usize) -> JsResult<'a, JsArrayBuffer>

Convenience method for creating an empty JsArrayBuffer value.
Source§

fn buffer(&mut self, size: usize) -> JsResult<'a, JsBuffer>

Convenience method for creating an empty JsBuffer value.
Source§

fn date( &mut self, value: impl Into<f64>, ) -> Result<Handle<'a, JsDate>, DateError>

Available on crate feature napi-5 only.
Convenience method for creating a JsDate value.
Source§

fn global<T: Value>(&mut self, name: &str) -> JsResult<'a, T>

Convenience method for looking up a global property by name. Read more
Source§

fn global_object(&mut self) -> Handle<'a, JsObject>

Produces a handle to the JavaScript global object.
Source§

fn throw<T: Value, U>(&mut self, v: Handle<'_, T>) -> NeonResult<U>

Throws a JS value.
Source§

fn error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>

Creates a direct instance of the Error class.
Source§

fn type_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>

Creates an instance of the TypeError class.
Source§

fn range_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>

Creates an instance of the RangeError class.
Source§

fn throw_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>

Throws a direct instance of the Error class.
Source§

fn throw_type_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>

Throws an instance of the TypeError class.
Source§

fn throw_range_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>

Throws an instance of the RangeError class.
Source§

fn boxed<U: Finalize + 'static>(&mut self, v: U) -> Handle<'a, JsBox<U>>

Convenience method for wrapping a value in a JsBox. Read more
Source§

fn channel(&mut self) -> Channel

Available on crate feature napi-4 only.
Returns an unbounded channel for scheduling events to be executed on the JavaScript thread. Read more
Source§

fn promise(&mut self) -> (Deferred, Handle<'a, JsPromise>)

Creates a Deferred and JsPromise pair. The Deferred handle can be used to resolve or reject the JsPromise. Read more
Source§

fn task<'cx, O, E>(&'cx mut self, execute: E) -> TaskBuilder<'cx, Self, E>
where O: Send + 'static, E: FnOnce() -> O + Send + 'static, 'a: 'cx,

Creates a TaskBuilder which can be used to schedule the execute callback to asynchronously execute on the Node worker pool. Read more
Source§

fn to_raw(&self) -> Env

Available on crate feature sys only.
Gets the raw sys::Env for usage with Node-API.
Source§

impl<'cx> Deref for FunctionContext<'cx>

Source§

type Target = Cx<'cx>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'cx> DerefMut for FunctionContext<'cx>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'cx> From<FunctionContext<'cx>> for Cx<'cx>

Source§

fn from(cx: FunctionContext<'cx>) -> Self

Converts to this type from the input type.
Source§

impl<'cx> UnwindSafe for FunctionContext<'cx>

Auto Trait Implementations§

§

impl<'cx> Freeze for FunctionContext<'cx>

§

impl<'cx> RefUnwindSafe for FunctionContext<'cx>

§

impl<'cx> !Send for FunctionContext<'cx>

§

impl<'cx> !Sync for FunctionContext<'cx>

§

impl<'cx> Unpin for FunctionContext<'cx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.