Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/nodex-api/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,19 @@ impl NapiEnv {
/// napi_set_instance_data(). If no data is set, the call will succeed and data will be set to
/// NULL.
#[inline]
pub fn get_instance_data<T>(&self) -> NapiResult<Option<&mut T>> {
pub fn get_instance_data<T>(&self) -> NapiResult<Option<&T>> {
let data = napi_call!(=napi_get_instance_data, *self) as *mut T;
if data.is_null() {
Ok(None)
} else {
unsafe { Ok(Some(&*(data as *const T))) }
}
}

#[cfg(feature = "v6")]
#[inline]
/// Mutable access variant of instance data; use when data must be updated in place.
pub fn get_instance_data_mut<T>(&mut self) -> NapiResult<Option<&mut T>> {
let data = napi_call!(=napi_get_instance_data, *self) as *mut T;
if data.is_null() {
Ok(None)
Expand Down
3 changes: 2 additions & 1 deletion crates/nodex-api/src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ macro_rules! napi_call {
unsafe {
let mut result = std::mem::MaybeUninit::uninit();
let status = $crate::api::$napi($($args),+, result.as_mut_ptr());
(status, result.assume_init())
let value = status.ok().then(|| result.assume_init());
(status, value)
}
};

Expand Down
2 changes: 1 addition & 1 deletion crates/nodex-api/src/reference.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{api, prelude::*};

#[derive(Clone, Debug)]
#[derive(Debug)]
pub struct NapiRef(NapiEnv, napi_ref);

impl NapiRef {
Expand Down
8 changes: 7 additions & 1 deletion crates/nodex-api/src/value/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ impl<T> JsExternal<T> {
}

/// Access the underlaying data.
pub fn get(&self) -> NapiResult<&mut T> {
pub fn get(&self) -> NapiResult<&T> {
let ext = napi_call!(=napi_get_value_external, self.env(), self.raw());
unsafe { Ok(&*(ext as *const T)) }
}

/// Mutable access to the underlying external data.
pub fn get_mut(&mut self) -> NapiResult<&mut T> {
let ext = napi_call!(=napi_get_value_external, self.env(), self.raw());
unsafe { Ok(&mut *(ext as *mut T)) }
}
Expand Down
20 changes: 18 additions & 2 deletions crates/nodex-api/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,26 @@ pub trait NapiValueT: NapiValueCheck + Sized {
///
/// NB: if a there is no wrap or the wrap is just removed by NapiValue::remove_wrap, return
/// None.
fn unwrap<T>(&self) -> NapiResult<Option<&mut T>> {
fn unwrap<T>(&self) -> NapiResult<Option<&T>> {
let (status, value) = napi_call!(?napi_unwrap, self.env(), self.raw());
match status {
NapiStatus::Ok => unsafe { Ok(Some(&mut *(value as *mut T))) },
NapiStatus::Ok => match value {
Some(value) => unsafe { Ok(Some(&*(value as *const T))) },
None => Ok(None),
},
NapiStatus::InvalidArg => Ok(None),
err => Err(err),
}
}

/// Mutable variant of unwrap.
fn unwrap_mut<T>(&mut self) -> NapiResult<Option<&mut T>> {
let (status, value) = napi_call!(?napi_unwrap, self.env(), self.raw());
match status {
NapiStatus::Ok => match value {
Some(value) => unsafe { Ok(Some(&mut *(value as *mut T))) },
None => Ok(None),
},
NapiStatus::InvalidArg => Ok(None),
err => Err(err),
}
Expand Down
6 changes: 4 additions & 2 deletions crates/nodex-api/src/work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ impl<T> NapiAsyncWork<T> {
unsafe {
let mut pair: Box<(
Box<dyn FnMut(&mut T)>,
Box<dyn FnMut(NapiEnv, NapiStatus, T)>,
Box<dyn FnMut(NapiEnv, NapiStatus, T) -> NapiResult<()>>,
T,
)> = Box::from_raw(data as _);
let mut complete = pair.1;
complete(env, status, pair.2);
if let Err(err) = complete(env, status, pair.2) {
log::error!("NapiAsyncWork::complete: {}", err);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nodex::prelude::*;

nodex::napi_module!(init);

fn init(env: NapiEnv, mut exports: JsObject) -> NapiResult<()> {
fn init(mut env: NapiEnv, mut exports: JsObject) -> NapiResult<()> {
nodex::napi_guard!(env.napi_version()?);

let mut obj = env.object()?;
Expand Down Expand Up @@ -203,7 +203,7 @@ fn init(env: NapiEnv, mut exports: JsObject) -> NapiResult<()> {

let value = env.get_instance_data::<usize>()?;
println!("get instance data: {:?}", value);
if let Ok(Some(data)) = env.get_instance_data::<usize>() {
if let Ok(Some(data)) = env.get_instance_data_mut::<usize>() {
*data = 200;
}
let value = env.get_instance_data::<usize>()?;
Expand Down
Loading