Struct napi::bindgen_prelude::JsFunction
source · pub struct JsFunction(_);
Implementations§
source§impl JsFunction
impl JsFunction
See Working with JavaScript Functions.
Example:
use napi::{JsFunction, CallContext, JsNull, Result};
#[js_function(1)]
pub fn call_function(ctx: CallContext) -> Result<JsNull> {
let js_func = ctx.get::<JsFunction>(0)?;
let js_string = ctx.env.create_string("hello".as_ref())?.into_unknown()?;
js_func.call(None, &[js_string])?;
Ok(ctx.env.get_null()?)
}
sourcepub fn call<V>(&self, this: Option<&JsObject>, args: &[V]) -> Result<JsUnknown>where
V: NapiRaw,
pub fn call<V>(&self, this: Option<&JsObject>, args: &[V]) -> Result<JsUnknown>where
V: NapiRaw,
Examples found in repository?
src/js_values/global.rs (lines 14-22)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
pub fn set_interval(&self, handler: JsFunction, interval: f64) -> Result<JsTimeout> {
let func: JsFunction = self.get_named_property_unchecked("setInterval")?;
func
.call(
None,
&[
handler.into_unknown(),
unsafe { Env::from_raw(self.0.env) }
.create_double(interval)?
.into_unknown(),
],
)
.and_then(|ret| ret.try_into())
}
pub fn clear_interval(&self, timer: JsTimeout) -> Result<JsUndefined> {
let func: JsFunction = self.get_named_property_unchecked("clearInterval")?;
func
.call(None, &[timer.into_unknown()])
.and_then(|ret| ret.try_into())
}
pub fn set_timeout(&self, handler: JsFunction, interval: f64) -> Result<JsTimeout> {
let func: JsFunction = self.get_named_property_unchecked("setTimeout")?;
func
.call(
None,
&[
handler.into_unknown(),
unsafe { Env::from_raw(self.0.env) }
.create_double(interval)?
.into_unknown(),
],
)
.and_then(|ret| ret.try_into())
}
pub fn clear_timeout(&self, timer: JsTimeout) -> Result<JsUndefined> {
let func: JsFunction = self.get_named_property_unchecked("clearTimeout")?;
func
.call(None, &[timer.into_unknown()])
.and_then(|ret| ret.try_into())
}
sourcepub fn call_without_args(&self, this: Option<&JsObject>) -> Result<JsUnknown>
pub fn call_without_args(&self, this: Option<&JsObject>) -> Result<JsUnknown>
napi_call_function
The same with call
, but without arguments
sourcepub fn new_instance<V>(&self, args: &[V]) -> Result<JsObject>where
V: NapiRaw,
pub fn new_instance<V>(&self, args: &[V]) -> Result<JsObject>where
V: NapiRaw,
https://nodejs.org/api/n-api.html#n_api_napi_new_instance
This method is used to instantiate a new JavaScript
value using a given JsFunction
that represents the constructor for the object.
pub fn create_threadsafe_function<T, V, F, ES>(
&self,
max_queue_size: usize,
callback: F
) -> Result<ThreadsafeFunction<T, ES>>where
T: 'static,
V: ToNapiValue,
F: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>,
ES: T,
source§impl JsFunction
impl JsFunction
sourcepub fn into_unknown(self) -> JsUnknown
pub fn into_unknown(self) -> JsUnknown
Examples found in repository?
src/js_values/global.rs (line 17)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
pub fn set_interval(&self, handler: JsFunction, interval: f64) -> Result<JsTimeout> {
let func: JsFunction = self.get_named_property_unchecked("setInterval")?;
func
.call(
None,
&[
handler.into_unknown(),
unsafe { Env::from_raw(self.0.env) }
.create_double(interval)?
.into_unknown(),
],
)
.and_then(|ret| ret.try_into())
}
pub fn clear_interval(&self, timer: JsTimeout) -> Result<JsUndefined> {
let func: JsFunction = self.get_named_property_unchecked("clearInterval")?;
func
.call(None, &[timer.into_unknown()])
.and_then(|ret| ret.try_into())
}
pub fn set_timeout(&self, handler: JsFunction, interval: f64) -> Result<JsTimeout> {
let func: JsFunction = self.get_named_property_unchecked("setTimeout")?;
func
.call(
None,
&[
handler.into_unknown(),
unsafe { Env::from_raw(self.0.env) }
.create_double(interval)?
.into_unknown(),
],
)
.and_then(|ret| ret.try_into())
}
pub fn coerce_to_bool(self) -> Result<JsBoolean>
pub fn coerce_to_number(self) -> Result<JsNumber>
pub fn coerce_to_string(self) -> Result<JsString>
pub fn coerce_to_object(self) -> Result<JsObject>
pub fn is_date(&self) -> Result<bool>
pub fn is_promise(&self) -> Result<bool>
pub fn is_error(&self) -> Result<bool>
pub fn is_typedarray(&self) -> Result<bool>
pub fn is_dataview(&self) -> Result<bool>
pub fn is_array(&self) -> Result<bool>
pub fn is_buffer(&self) -> Result<bool>
pub fn instanceof<Constructor>(&self, constructor: Constructor) -> Result<bool>where
Constructor: NapiRaw,
Trait Implementations§
source§impl<'env> NapiRaw for &'env JsFunction
impl<'env> NapiRaw for &'env JsFunction
unsafe fn raw(&self) -> napi_value
source§impl NapiRaw for JsFunction
impl NapiRaw for JsFunction
unsafe fn raw(&self) -> napi_value
source§impl NapiValue for JsFunction
impl NapiValue for JsFunction
unsafe fn from_raw(env: napi_env, value: napi_value) -> Result<JsFunction>
unsafe fn from_raw_unchecked(env: napi_env, value: napi_value) -> JsFunction
source§impl TryFrom<JsUnknown> for JsFunction
impl TryFrom<JsUnknown> for JsFunction
source§impl TypeName for JsFunction
impl TypeName for JsFunction
source§impl ValidateNapiValue for JsFunction
impl ValidateNapiValue for JsFunction
source§unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Safety Read more