Skip to content

Commit

Permalink
Merge pull request #1065 from akonradi-signal/neon-runtime-check-status
Browse files Browse the repository at this point in the history
Return Results from native function wrappers
  • Loading branch information
dherman authored Nov 4, 2024
2 parents 7dec390 + ad2cb8f commit 323450a
Show file tree
Hide file tree
Showing 29 changed files with 280 additions and 433 deletions.
20 changes: 9 additions & 11 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,15 @@ impl CallbackInfo<'_> {
// * Node-API fills empty slots with `undefined`
// * `Handle` and `JsValue` are transparent wrappers around a raw pointer
unsafe {
assert_eq!(
sys::get_cb_info(
cx.env().to_raw(),
self.info,
&mut argc,
argv.as_mut_ptr().cast(),
ptr::null_mut(),
ptr::null_mut(),
),
sys::Status::Ok,
);
sys::get_cb_info(
cx.env().to_raw(),
self.info,
&mut argc,
argv.as_mut_ptr().cast(),
ptr::null_mut(),
ptr::null_mut(),
)
.unwrap();
}

// Empty values will be filled with `undefined`
Expand Down
8 changes: 4 additions & 4 deletions crates/neon/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ pub trait Object: Value {
let obj = self.to_local();
unsafe {
match sys::object::freeze(env, obj) {
sys::Status::Ok => Ok(self),
sys::Status::PendingException => Err(Throw::new()),
Ok(()) => Ok(self),
Err(sys::Status::PendingException) => Err(Throw::new()),
_ => cx.throw_type_error("object cannot be frozen"),
}
}
Expand All @@ -359,8 +359,8 @@ pub trait Object: Value {
let obj = self.to_local();
unsafe {
match sys::object::seal(env, obj) {
sys::Status::Ok => Ok(self),
sys::Status::PendingException => Err(Throw::new()),
Ok(()) => Ok(self),
Err(sys::Status::PendingException) => Err(Throw::new()),
_ => cx.throw_type_error("object cannot be sealed"),
}
}
Expand Down
10 changes: 2 additions & 8 deletions crates/neon/src/sys/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use super::{
};

pub unsafe fn new(out: &mut Local, env: Env, length: usize) {
assert_eq!(
napi::create_array_with_length(env, length, out as *mut _),
napi::Status::Ok,
);
napi::create_array_with_length(env, length, out as *mut _).unwrap();
}

/// Gets the length of a `napi_value` containing a JavaScript Array.
Expand All @@ -19,9 +16,6 @@ pub unsafe fn new(out: &mut Local, env: Env, length: usize) {
/// exception.
pub unsafe fn len(env: Env, array: Local) -> u32 {
let mut len = 0;
assert_eq!(
napi::get_array_length(env, array, &mut len as *mut _),
napi::Status::Ok
);
napi::get_array_length(env, array, &mut len as *mut _).unwrap();
len
}
39 changes: 15 additions & 24 deletions crates/neon/src/sys/arraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ pub unsafe fn new(env: Env, len: usize) -> Result<Local, napi::Status> {
let mut buf = MaybeUninit::uninit();
let status = napi::create_arraybuffer(env, len, null_mut(), buf.as_mut_ptr());

if status == napi::Status::PendingException {
return Err(status);
}

assert_eq!(status, napi::Status::Ok);
match status {
Err(err @ napi::Status::PendingException) => return Err(err),
status => status.unwrap(),
};

Ok(buf.assume_init())
}
Expand All @@ -31,17 +30,15 @@ where
let length = buf.len();
let mut result = MaybeUninit::uninit();

assert_eq!(
napi::create_external_arraybuffer(
env,
buf.as_mut_ptr() as *mut _,
length,
Some(drop_external::<T>),
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
),
napi::Status::Ok,
);
napi::create_external_arraybuffer(
env,
buf.as_mut_ptr() as *mut _,
length,
Some(drop_external::<T>),
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
)
.unwrap();

result.assume_init()
}
Expand All @@ -58,10 +55,7 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {
let mut data = MaybeUninit::uninit();
let mut size = 0usize;

assert_eq!(
napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _),
napi::Status::Ok,
);
napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap();

if size == 0 {
return &mut [];
Expand All @@ -76,10 +70,7 @@ pub unsafe fn size(env: Env, buf: Local) -> usize {
let mut data = MaybeUninit::uninit();
let mut size = 0usize;

assert_eq!(
napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _),
napi::Status::Ok,
);
napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap();

size
}
26 changes: 12 additions & 14 deletions crates/neon/src/sys/async_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,24 @@ pub unsafe fn schedule<I, O, D>(
let work = &mut data.work as *mut _;

// Create the `async_work`
assert_eq!(
napi::create_async_work(
env,
ptr::null_mut(),
super::string(env, "neon_async_work"),
Some(call_execute::<I, O, D>),
Some(call_complete::<I, O, D>),
Box::into_raw(data).cast(),
work,
),
napi::Status::Ok,
);
napi::create_async_work(
env,
ptr::null_mut(),
super::string(env, "neon_async_work"),
Some(call_execute::<I, O, D>),
Some(call_complete::<I, O, D>),
Box::into_raw(data).cast(),
work,
)
.unwrap();

// Queue the work
match napi::queue_async_work(env, *work) {
napi::Status::Ok => {}
Ok(()) => {}
status => {
// If queueing failed, delete the work to prevent a leak
napi::delete_async_work(env, *work);
assert_eq!(status, napi::Status::Ok);
status.unwrap()
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/neon/src/sys/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ macro_rules! generate {
napi_name!($name),
")",
)]
pub unsafe fn $name($($param: $ptype,)*)$( -> $rtype)* {
(NAPI.$name)($($param,)*)
pub unsafe fn $name($($param: $ptype,)*)$( -> ::core::result::Result<(), $rtype>)* {
#[allow(unused)]
let r = (NAPI.$name)($($param,)*);
$(match r {
<$rtype>::Ok => Ok(()),
status => Err(status)
})*
}
)*
};
Expand Down
39 changes: 15 additions & 24 deletions crates/neon/src/sys/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ pub unsafe fn uninitialized(env: Env, len: usize) -> Result<(Local, *mut u8), na
let mut bytes = MaybeUninit::uninit();
let status = napi::create_buffer(env, len, bytes.as_mut_ptr(), buf.as_mut_ptr());

if status == napi::Status::PendingException {
return Err(status);
}

assert_eq!(status, napi::Status::Ok);
match status {
Err(err @ napi::Status::PendingException) => return Err(err),
status => status.unwrap(),
};

Ok((buf.assume_init(), bytes.assume_init().cast()))
}
Expand All @@ -40,17 +39,15 @@ where
let length = buf.len();
let mut result = MaybeUninit::uninit();

assert_eq!(
napi::create_external_buffer(
env,
length,
buf.as_mut_ptr() as *mut _,
Some(drop_external::<T>),
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
),
napi::Status::Ok,
);
napi::create_external_buffer(
env,
length,
buf.as_mut_ptr() as *mut _,
Some(drop_external::<T>),
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
)
.unwrap();

result.assume_init()
}
Expand All @@ -67,10 +64,7 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {
let mut data = MaybeUninit::uninit();
let mut size = 0usize;

assert_eq!(
napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _),
napi::Status::Ok,
);
napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap();

if size == 0 {
return &mut [];
Expand All @@ -85,10 +79,7 @@ pub unsafe fn size(env: Env, buf: Local) -> usize {
let mut data = MaybeUninit::uninit();
let mut size = 0usize;

assert_eq!(
napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _),
napi::Status::Ok,
);
napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _).unwrap();

size
}
53 changes: 23 additions & 30 deletions crates/neon/src/sys/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ impl Arguments {
pub unsafe fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool {
let mut target: MaybeUninit<Local> = MaybeUninit::zeroed();

let status = napi::get_new_target(env, info, target.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
napi::get_new_target(env, info, target.as_mut_ptr()).unwrap();

// get_new_target is guaranteed to assign to target, so it's initialized.
let target: Local = target.assume_init();
Expand All @@ -47,23 +45,22 @@ pub unsafe fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool {
}

pub unsafe fn this(env: Env, info: FunctionCallbackInfo, out: &mut Local) {
let status = napi::get_cb_info(env, info, null_mut(), null_mut(), out as *mut _, null_mut());
assert_eq!(status, napi::Status::Ok);
napi::get_cb_info(env, info, null_mut(), null_mut(), out as *mut _, null_mut()).unwrap();
}

/// Gets the number of arguments passed to the function.
// TODO: Remove this when `FunctionContext` is refactored to get call info upfront.
pub unsafe fn len(env: Env, info: FunctionCallbackInfo) -> usize {
let mut argc = 0usize;
let status = napi::get_cb_info(
napi::get_cb_info(
env,
info,
&mut argc as *mut _,
null_mut(),
null_mut(),
null_mut(),
);
assert_eq!(status, napi::Status::Ok);
)
.unwrap();
argc
}

Expand All @@ -75,34 +72,30 @@ pub unsafe fn argv(env: Env, info: FunctionCallbackInfo) -> Arguments {
// Starts as the size allocated; after `get_cb_info` it is the number of arguments
let mut argc = ARGV_SIZE;

assert_eq!(
napi::get_cb_info(
env,
info,
&mut argc as *mut _,
argv.as_mut_ptr().cast(),
null_mut(),
null_mut(),
),
napi::Status::Ok,
);
napi::get_cb_info(
env,
info,
&mut argc as *mut _,
argv.as_mut_ptr().cast(),
null_mut(),
null_mut(),
)
.unwrap();

// We did not allocate enough space; allocate on the heap and try again
let argv = if argc > ARGV_SIZE {
// We know exactly how much space to reserve
let mut argv = Vec::with_capacity(argc);

assert_eq!(
napi::get_cb_info(
env,
info,
&mut argc as *mut _,
argv.as_mut_ptr(),
null_mut(),
null_mut(),
),
napi::Status::Ok,
);
napi::get_cb_info(
env,
info,
&mut argc as *mut _,
argv.as_mut_ptr(),
null_mut(),
null_mut(),
)
.unwrap();

// Set the size of `argv` to the number of initialized elements
argv.set_len(argc);
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/sys/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ use super::{
pub unsafe fn to_string(out: &mut Local, env: Env, value: Local) -> bool {
let status = napi::coerce_to_string(env, value, out as *mut _);

status == napi::Status::Ok
status.is_ok()
}
6 changes: 2 additions & 4 deletions crates/neon/src/sys/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use super::{
/// `env` is a raw pointer. Please ensure it points to a napi_env that is valid for the current context.
pub unsafe fn new_date(env: Env, value: f64) -> Local {
let mut local = MaybeUninit::zeroed();
let status = napi::create_date(env, value, local.as_mut_ptr());
assert_eq!(status, napi::Status::Ok);
napi::create_date(env, value, local.as_mut_ptr()).unwrap();
local.assume_init()
}

Expand All @@ -25,7 +24,6 @@ pub unsafe fn new_date(env: Env, value: f64) -> Local {
/// `Local` must be an NAPI value associated with the given `Env`
pub unsafe fn value(env: Env, p: Local) -> f64 {
let mut value = 0.0;
let status = napi::get_date_value(env, p, &mut value as *mut _);
assert_eq!(status, napi::Status::Ok);
napi::get_date_value(env, p, &mut value as *mut _).unwrap();
value
}
Loading

0 comments on commit 323450a

Please sign in to comment.