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>
impl<'cx> FunctionContext<'cx>
Sourcepub fn argument_opt(&mut self, i: usize) -> Option<Handle<'cx, JsValue>>
pub fn argument_opt(&mut self, i: usize) -> Option<Handle<'cx, JsValue>>
Produces the i
th argument, or None
if i
is greater than or equal to self.len()
.
Sourcepub fn argument<V: Value>(&mut self, i: usize) -> JsResult<'cx, V>
pub fn argument<V: Value>(&mut self, i: usize) -> JsResult<'cx, V>
Produces the i
th 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
.
Sourcepub fn this<T: Value>(&mut self) -> JsResult<'cx, T>
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.
Sourcepub fn this_value(&mut self) -> Handle<'cx, JsValue>
pub fn this_value(&mut self) -> Handle<'cx, JsValue>
Produces a handle to the function’s this
-binding.
Sourcepub fn args<T>(&mut self) -> NeonResult<T>where
T: FromArgs<'cx>,
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))
}
Sourcepub fn args_opt<T>(&mut self) -> NeonResult<Option<T>>where
T: FromArgs<'cx>,
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>
impl<'cx> Context<'cx> for FunctionContext<'cx>
Source§fn lock<'b>(&'b mut self) -> Lock<'b, Self>where
'a: 'b,
fn lock<'b>(&'b mut self) -> Lock<'b, Self>where
'a: 'b,
Source§fn execute_scoped<'b, T, F>(&mut self, f: F) -> T
fn execute_scoped<'b, T, F>(&mut self, f: F) -> T
Source§fn compute_scoped<'b, V, F>(&mut self, f: F) -> JsResult<'a, V>
fn compute_scoped<'b, V, F>(&mut self, f: F) -> JsResult<'a, V>
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>
fn boolean(&mut self, b: bool) -> Handle<'a, JsBoolean>
JsBoolean
value.Source§fn number<T: Into<f64>>(&mut self, x: T) -> Handle<'a, JsNumber>
fn number<T: Into<f64>>(&mut self, x: T) -> Handle<'a, JsNumber>
JsNumber
value.Source§fn string<S: AsRef<str>>(&mut self, s: S) -> Handle<'a, JsString>
fn string<S: AsRef<str>>(&mut self, s: S) -> Handle<'a, JsString>
JsString
value. Read moreSource§fn try_string<S: AsRef<str>>(&mut self, s: S) -> StringResult<'a>
fn try_string<S: AsRef<str>>(&mut self, s: S) -> StringResult<'a>
JsString
value. Read moreSource§fn undefined(&mut self) -> Handle<'a, JsUndefined>
fn undefined(&mut self) -> Handle<'a, JsUndefined>
JsUndefined
value.Source§fn empty_object(&mut self) -> Handle<'a, JsObject>
fn empty_object(&mut self) -> Handle<'a, JsObject>
JsObject
value.Source§fn empty_array(&mut self) -> Handle<'a, JsArray>
fn empty_array(&mut self) -> Handle<'a, JsArray>
JsArray
value.Source§fn array_buffer(&mut self, size: usize) -> JsResult<'a, JsArrayBuffer>
fn array_buffer(&mut self, size: usize) -> JsResult<'a, JsArrayBuffer>
JsArrayBuffer
value.Source§fn buffer(&mut self, size: usize) -> JsResult<'a, JsBuffer>
fn buffer(&mut self, size: usize) -> JsResult<'a, JsBuffer>
JsBuffer
value.Source§fn date(
&mut self,
value: impl Into<f64>,
) -> Result<Handle<'a, JsDate>, DateError>
fn date( &mut self, value: impl Into<f64>, ) -> Result<Handle<'a, JsDate>, DateError>
napi-5
only.JsDate
value.Source§fn global<T: Value>(&mut self, name: &str) -> JsResult<'a, T>
fn global<T: Value>(&mut self, name: &str) -> JsResult<'a, T>
Source§fn global_object(&mut self) -> Handle<'a, JsObject>
fn global_object(&mut self) -> Handle<'a, JsObject>
Source§fn error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
fn error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
Error
class.Source§fn type_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
fn type_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
TypeError
class.Source§fn range_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
fn range_error<S: AsRef<str>>(&mut self, msg: S) -> JsResult<'a, JsError>
RangeError
class.Source§fn throw_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>
fn throw_error<S: AsRef<str>, T>(&mut self, msg: S) -> NeonResult<T>
Error
class.Source§fn throw_type_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>
TypeError
class.Source§fn throw_range_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>
RangeError
class.Source§fn boxed<U: Finalize + 'static>(&mut self, v: U) -> Handle<'a, JsBox<U>>
fn boxed<U: Finalize + 'static>(&mut self, v: U) -> Handle<'a, JsBox<U>>
JsBox
. Read moreSource§fn channel(&mut self) -> Channel
fn channel(&mut self) -> Channel
napi-4
only.Source§fn task<'cx, O, E>(&'cx mut self, execute: E) -> TaskBuilder<'cx, Self, E>
fn task<'cx, O, E>(&'cx mut self, execute: E) -> TaskBuilder<'cx, Self, E>
TaskBuilder
which can be used to schedule the execute
callback to asynchronously execute on the
Node worker pool. Read moreSource§impl<'cx> Deref for FunctionContext<'cx>
impl<'cx> Deref for FunctionContext<'cx>
Source§impl<'cx> DerefMut for FunctionContext<'cx>
impl<'cx> DerefMut for FunctionContext<'cx>
Source§impl<'cx> From<FunctionContext<'cx>> for Cx<'cx>
impl<'cx> From<FunctionContext<'cx>> for Cx<'cx>
Source§fn from(cx: FunctionContext<'cx>) -> Self
fn from(cx: FunctionContext<'cx>) -> Self
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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