Skip to content

Commit

Permalink
Mechanically transform napi fns to return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
akonradi-signal committed Oct 23, 2024
1 parent 1efcd4e commit 25a00d9
Show file tree
Hide file tree
Showing 29 changed files with 190 additions and 221 deletions.
2 changes: 1 addition & 1 deletion crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl CallbackInfo<'_> {
ptr::null_mut(),
ptr::null_mut(),
),
sys::Status::Ok,
Ok(())
);
}

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
4 changes: 2 additions & 2 deletions crates/neon/src/sys/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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,
Ok(())
);
}

Expand All @@ -21,7 +21,7 @@ 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
Ok(())
);
len
}
12 changes: 6 additions & 6 deletions crates/neon/src/sys/arraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ 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);
if status == Err(napi::Status::PendingException) {
return Err(napi::Status::PendingException);
}

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

Ok(buf.assume_init())
}
Expand All @@ -40,7 +40,7 @@ where
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
),
napi::Status::Ok,
Ok(())
);

result.assume_init()
Expand All @@ -60,7 +60,7 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {

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

if size == 0 {
Expand All @@ -78,7 +78,7 @@ pub unsafe fn size(env: Env, buf: Local) -> usize {

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

size
Expand Down
6 changes: 3 additions & 3 deletions crates/neon/src/sys/async_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub unsafe fn schedule<I, O, D>(
Box::into_raw(data).cast(),
work,
),
napi::Status::Ok,
Ok(())
);

// Queue the work
match napi::queue_async_work(env, *work) {
napi::Status::Ok => {}
status => {
Ok(()) => {}
Err(status) => {
// If queueing failed, delete the work to prevent a leak
napi::delete_async_work(env, *work);
assert_eq!(status, napi::Status::Ok);
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
12 changes: 6 additions & 6 deletions crates/neon/src/sys/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ 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);
if status == Err(napi::Status::PendingException) {
return Err(napi::Status::PendingException);
}

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

Ok((buf.assume_init(), bytes.assume_init().cast()))
}
Expand All @@ -49,7 +49,7 @@ where
Box::into_raw(data) as *mut _,
result.as_mut_ptr(),
),
napi::Status::Ok,
Ok(())
);

result.assume_init()
Expand All @@ -69,7 +69,7 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {

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

if size == 0 {
Expand All @@ -87,7 +87,7 @@ pub unsafe fn size(env: Env, buf: Local) -> usize {

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

size
Expand Down
10 changes: 5 additions & 5 deletions crates/neon/src/sys/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub unsafe fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool {

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

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

// get_new_target is guaranteed to assign to target, so it's initialized.
let target: Local = target.assume_init();
Expand All @@ -48,7 +48,7 @@ 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);
assert_eq!(status, Ok(()));
}

/// Gets the number of arguments passed to the function.
Expand All @@ -63,7 +63,7 @@ pub unsafe fn len(env: Env, info: FunctionCallbackInfo) -> usize {
null_mut(),
null_mut(),
);
assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));
argc
}

Expand All @@ -84,7 +84,7 @@ pub unsafe fn argv(env: Env, info: FunctionCallbackInfo) -> Arguments {
null_mut(),
null_mut(),
),
napi::Status::Ok,
Ok(()),
);

// We did not allocate enough space; allocate on the heap and try again
Expand All @@ -101,7 +101,7 @@ pub unsafe fn argv(env: Env, info: FunctionCallbackInfo) -> Arguments {
null_mut(),
null_mut(),
),
napi::Status::Ok,
Ok(()),
);

// Set the size of `argv` to the number of initialized elements
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()
}
4 changes: 2 additions & 2 deletions crates/neon/src/sys/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
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);
assert_eq!(status, Ok(()));
local.assume_init()
}

Expand All @@ -26,6 +26,6 @@ pub unsafe fn new_date(env: Env, value: f64) -> Local {
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);
assert_eq!(status, Ok(()));
value
}
20 changes: 10 additions & 10 deletions crates/neon/src/sys/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub unsafe fn is_throwing(env: Env) -> bool {

let status = napi::is_exception_pending(env, b.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

b.assume_init()
}
Expand All @@ -22,7 +22,7 @@ pub unsafe fn catch_error(env: Env, error: *mut Local) -> bool {

let status = napi::get_and_clear_last_exception(env, error);

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

true
}
Expand All @@ -31,7 +31,7 @@ pub unsafe fn clear_exception(env: Env) {
let mut result = MaybeUninit::uninit();
let status = napi::is_exception_pending(env, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

if !result.assume_init() {
return;
Expand All @@ -40,20 +40,20 @@ pub unsafe fn clear_exception(env: Env) {
let mut result = MaybeUninit::uninit();
let status = napi::get_and_clear_last_exception(env, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));
}

pub unsafe fn throw(env: Env, val: Local) {
let status = napi::throw(env, val);

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));
}

pub unsafe fn new_error(env: Env, out: &mut Local, msg: Local) {
let mut result = MaybeUninit::uninit();
let status = napi::create_error(env, ptr::null_mut(), msg, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

*out = result.assume_init();
}
Expand All @@ -62,7 +62,7 @@ pub unsafe fn new_type_error(env: Env, out: &mut Local, msg: Local) {
let mut result = MaybeUninit::uninit();
let status = napi::create_type_error(env, ptr::null_mut(), msg, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

*out = result.assume_init();
}
Expand All @@ -71,7 +71,7 @@ pub unsafe fn new_range_error(env: Env, out: &mut Local, msg: Local) {
let mut result = MaybeUninit::uninit();
let status = napi::create_range_error(env, ptr::null_mut(), msg, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

*out = result.assume_init();
}
Expand All @@ -80,12 +80,12 @@ pub unsafe fn throw_error_from_utf8(env: Env, msg: *const u8, len: i32) {
let mut out = MaybeUninit::uninit();
let status = napi::create_string_utf8(env, msg as *const _, len as usize, out.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

let mut err = MaybeUninit::uninit();
let status = napi::create_error(env, ptr::null_mut(), out.assume_init(), err.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

throw(env, err.assume_init());
}
Expand Down
6 changes: 3 additions & 3 deletions crates/neon/src/sys/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub unsafe fn deref<T: 'static>(env: Env, local: Local) -> Option<*const T> {
let mut result = MaybeUninit::uninit();
let status = napi::typeof_value(env, local, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

let result = result.assume_init();

Expand All @@ -52,7 +52,7 @@ pub unsafe fn deref<T: 'static>(env: Env, local: Local) -> Option<*const T> {
let mut result = MaybeUninit::uninit();
let status = napi::get_value_external(env, local, result.as_mut_ptr());

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

let v = result.assume_init();
let v = &**v.cast_const().cast::<DebugSendWrapper<T>>() as *const T;
Expand All @@ -77,7 +77,7 @@ pub unsafe fn create<T: 'static>(env: Env, v: T, finalizer: fn(Env, T)) -> Local

// `napi_create_external` will only fail if the VM is in a throwing state
// or shutting down.
assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

let external = result.assume_init();

Expand Down
12 changes: 6 additions & 6 deletions crates/neon/src/sys/fun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ where
out.as_mut_ptr(),
);

if status == napi::Status::PendingException {
if status == Err(napi::Status::PendingException) {
drop(Box::from_raw(data));

return Err(status);
return Err(napi::Status::PendingException);
}

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

let out = out.assume_init();

Expand All @@ -54,7 +54,7 @@ where
// If adding the finalizer fails the closure will leak, but it would
// be unsafe to drop it because there's no guarantee V8 won't use the
// pointer.
assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));
}

Ok(out)
Expand All @@ -76,7 +76,7 @@ where
data.as_mut_ptr(),
);

assert_eq!(status, napi::Status::Ok);
assert_eq!(status, Ok(()));

let callback = &*data.assume_init().cast::<F>();

Expand All @@ -92,5 +92,5 @@ pub unsafe fn construct(
) -> bool {
let status = napi::new_instance(env, fun, argc, argv as *const _, out as *mut _);

status == napi::Status::Ok
status.is_ok()
}
Loading

0 comments on commit 25a00d9

Please sign in to comment.