Struct napi::JsFunction

source ·
pub struct JsFunction(_);

Implementations§

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()?)
}
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())
  }

napi_call_function The same with call, but without arguments

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.

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())
  }

Trait Implementations§

The type returned in the event of a conversion error.
Performs the conversion.
Safety Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.