Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make build() fn's closure return a Result #1064

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 10 additions & 4 deletions crates/neon/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ pub trait Object: Value {
cx: &mut C,
key: K,
) -> NeonResult<Handle<'a, JsValue>> {
build(cx.env(), |out| unsafe {
key.get_from(cx, out, self.to_local())
build(cx.env(), move || unsafe {
let mut out: raw::Local = std::ptr::null_mut();
key.get_from(cx, &mut out, self.to_local())
.then_some(out)
.ok_or(Throw::new())
})
}

Expand All @@ -183,8 +186,11 @@ pub trait Object: Value {
fn get_own_property_names<'a, C: Context<'a>>(&self, cx: &mut C) -> JsResult<'a, JsArray> {
let env = cx.env();

build(cx.env(), |out| unsafe {
sys::object::get_own_property_names(out, env.to_raw(), self.to_local())
build(env, move || unsafe {
let mut out: raw::Local = std::ptr::null_mut();
sys::object::get_own_property_names(&mut out, env.to_raw(), self.to_local())
.then_some(out)
.ok_or(Throw::new())
})
}

Expand Down
12 changes: 8 additions & 4 deletions crates/neon/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
use crate::{
context::Context,
handle::Handle,
result::JsResult,
result::{JsResult, Throw},
sys::raw,
types::{build, private::ValueInternal, JsString, JsValue},
};

pub fn eval<'a, 'b, C: Context<'a>>(
cx: &mut C,
script: Handle<'b, JsString>,
) -> JsResult<'a, JsValue> {
let env = cx.env().to_raw();
build(cx.env(), |out| unsafe {
crate::sys::string::run_script(out, env, script.to_local())
let env = cx.env();
build(env, move || unsafe {
let mut out: raw::Local = std::ptr::null_mut();
crate::sys::string::run_script(&mut out, env.to_raw(), script.to_local())
.then_some(out)
.ok_or(Throw::new())
})
}
28 changes: 18 additions & 10 deletions crates/neon/src/types_impl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ impl JsError {
cx: &mut C,
msg: S,
) -> NeonResult<Handle<'a, JsError>> {
let env = cx.env();
let msg = cx.string(msg.as_ref());
build(cx.env(), |out| unsafe {
sys::error::new_error(cx.env().to_raw(), out, msg.to_local());
true
})

let mut out = std::ptr::null_mut();
let value = unsafe {
sys::error::new_error(env.to_raw(), &mut out, msg.to_local());
Self::from_local(env, out)
};
Ok(Handle::new_internal(value))
}

/// Creates an instance of the [`TypeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypeError) class.
Expand All @@ -91,10 +95,12 @@ impl JsError {
cx: &mut C,
msg: S,
) -> NeonResult<Handle<'a, JsError>> {
let env = cx.env();
let msg = cx.string(msg.as_ref());
build(cx.env(), |out| unsafe {
sys::error::new_type_error(cx.env().to_raw(), out, msg.to_local());
true
build(env, || unsafe {
let mut out = std::ptr::null_mut();
sys::error::new_type_error(env.to_raw(), &mut out, msg.to_local());
Ok(out)
})
}

Expand All @@ -105,10 +111,12 @@ impl JsError {
cx: &mut C,
msg: S,
) -> NeonResult<Handle<'a, JsError>> {
let env = cx.env();
let msg = cx.string(msg.as_ref());
build(cx.env(), |out| unsafe {
sys::error::new_range_error(cx.env().to_raw(), out, msg.to_local());
true
build(env, move || unsafe {
let mut out: raw::Local = std::ptr::null_mut();
sys::error::new_range_error(env.to_raw(), &mut out, msg.to_local());
Ok(out)
})
}
}
Expand Down
52 changes: 33 additions & 19 deletions crates/neon/src/types_impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,13 @@ pub use self::promise::JsFuture;

// This should be considered deprecated and will be removed:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was noted; I hadn't seen a plan for removal and went for a safe refactor that enabled what I really wanted: infallible JsError::error. Is that end goal even possible and, if so, is it worth blocking on the larger cleanup for?

// https://github.com/neon-bindings/neon/issues/983
pub(crate) fn build<'a, T: Value, F: FnOnce(&mut raw::Local) -> bool>(
pub(crate) fn build<'a, T: Value, E, F: FnOnce() -> Result<raw::Local, E>>(
env: Env,
init: F,
) -> JsResult<'a, T> {
unsafe {
let mut local: raw::Local = std::mem::zeroed();
if init(&mut local) {
Ok(Handle::new_internal(T::from_local(env, local)))
} else {
Err(Throw::new())
}
}
) -> Result<Handle<'a, T>, E> {
let local = init()?;
let value = unsafe { T::from_local(env, local) };
Ok(Handle::new_internal(value))
}

impl<T: Value> SuperType<T> for JsValue {
Expand All @@ -89,8 +84,11 @@ impl<T: Object> SuperType<T> for JsObject {
pub trait Value: ValueInternal {
fn to_string<'cx, C: Context<'cx>>(&self, cx: &mut C) -> JsResult<'cx, JsString> {
let env = cx.env();
build(env, |out| unsafe {
sys::convert::to_string(out, env.to_raw(), self.to_local())
build(env, || unsafe {
let mut out = std::ptr::null_mut();
sys::convert::to_string(&mut out, env.to_raw(), self.to_local())
.then_some(out)
.ok_or(Throw::new())
})
}

Expand Down Expand Up @@ -1169,9 +1167,20 @@ impl JsFunction {
AS: AsRef<[Handle<'b, JsValue>]>,
{
let (argc, argv) = unsafe { prepare_call(cx, args.as_ref()) }?;
let env = cx.env().to_raw();
build(cx.env(), |out| unsafe {
sys::fun::call(out, env, self.to_local(), this.to_local(), argc, argv)
let env = cx.env();
build(env, move || unsafe {
let mut out: raw::Local = std::ptr::null_mut();

sys::fun::call(
&mut out,
env.to_raw(),
self.to_local(),
this.to_local(),
argc,
argv,
)
.then_some(out)
.ok_or(Throw::new())
})
}

Expand Down Expand Up @@ -1204,10 +1213,15 @@ impl JsFunction {
AS: AsRef<[Handle<'b, JsValue>]>,
{
let (argc, argv) = unsafe { prepare_call(cx, args.as_ref()) }?;
let env = cx.env().to_raw();
build(cx.env(), |out| unsafe {
sys::fun::construct(out, env, self.to_local(), argc, argv)
})
let env = cx.env();
{
build(env, move || unsafe {
let mut out: raw::Local = std::ptr::null_mut();
sys::fun::construct(&mut out, env.to_raw(), self.to_local(), argc, argv)
.then_some(out)
.ok_or(Throw::new())
})
}
}
}

Expand Down