Struct napi::CallContext

source ·
pub struct CallContext<'env> {
    pub env: &'env mut Env,
    pub length: usize,
    /* private fields */
}
Expand description

Function call context

Fields§

§env: &'env mut Env§length: usize

arguments.length

Implementations§

Examples found in repository?
src/env.rs (line 580)
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
          unsafe extern "C" fn trampoline<R: NapiRaw, F: Fn(CallContext<'_>) -> Result<R>>(
            raw_env: sys::napi_env,
            cb_info: sys::napi_callback_info,
          ) -> sys::napi_value {
            use ::std::panic::{self, AssertUnwindSafe};
            panic::catch_unwind(AssertUnwindSafe(|| {
              let (raw_this, ref raw_args, closure_data_ptr) = {
                let argc = {
                  let mut argc = 0;
                  let status = unsafe {
                    sys::napi_get_cb_info(
                      raw_env,
                      cb_info,
                      &mut argc,
                      ptr::null_mut(),
                      ptr::null_mut(),
                      ptr::null_mut(),
                    )
                  };
                  debug_assert!(
                    Status::from(status) == Status::Ok,
                    "napi_get_cb_info failed"
                  );
                  argc
                };
                let mut raw_args = vec![ptr::null_mut(); argc];
                let mut raw_this = ptr::null_mut();
                let mut closure_data_ptr = ptr::null_mut();

                let status = unsafe {
                  sys::napi_get_cb_info(
                    raw_env,
                    cb_info,
                    &mut { argc },
                    raw_args.as_mut_ptr(),
                    &mut raw_this,
                    &mut closure_data_ptr,
                  )
                };
                debug_assert!(
                  Status::from(status) == Status::Ok,
                  "napi_get_cb_info failed"
                );
                (raw_this, raw_args, closure_data_ptr)
              };

              let closure: &F = unsafe {
                closure_data_ptr
                  .cast::<F>()
                  .as_ref()
                  .expect("`napi_get_cb_info` should have yielded non-`NULL` assoc data")
              };
              let env = &mut unsafe { Env::from_raw(raw_env) };
              let ctx = CallContext::new(env, cb_info, raw_this, raw_args, raw_args.len());
              closure(ctx).map(|ret: R| unsafe { ret.raw() })
            }))
            .map_err(|e| {
              Error::from_reason(format!(
                "panic from Rust code: {}",
                if let Some(s) = e.downcast_ref::<String>() {
                  s
                } else if let Some(s) = e.downcast_ref::<&str>() {
                  s
                } else {
                  "<no error message>"
                },
              ))
            })
            .and_then(|v| v)
            .unwrap_or_else(|e| {
              unsafe { JsError::from(e).throw_into(raw_env) };
              ptr::null_mut()
            })
          }

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.