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

Expanding on Root and a potential common-case #703

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions src/handle/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ impl<T: Object> Root<T> {
/// calling one of these methods:
/// * N-API < 6, Neon will `panic` to notify of the leak
/// * N-API >= 6, Neon will drop from a global queue at a runtime cost
///
/// For example, early return with a ? or an explicit return before freeing
/// a `Root` will trigger the slow path:
/// ```
Copy link
Member

Choose a reason for hiding this comment

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

This is failing doc tests for me locally. I'm not sure why it's not failing in CI. 😕

Copy link
Author

Choose a reason for hiding this comment

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

I think I have fixed this, I wasn't even thinking about doc tests when I wrote the code.

/// # use neon::prelude::*;
/// # fn create_log_entry(a: &str, b: &str) -> Result<String, String> { unimplemented!() }
/// # fn my_neon_function(mut cx: FunctionContext) -> JsResult<JsUndefined> {
/// # let id_generator = "";
/// let callback = cx.argument::<JsFunction>(1)?.root(&mut cx);
/// let my_log = match (create_log_entry(&id_generator, "log-emitter")) {
/// Err(_err) => {
/// return cx.throw_error("Couldn't construct log");
/// },
/// Ok(log) => log,
/// };
/// # Ok(cx.undefined())
/// # }
/// ```
/// The solution in the original case for this was to bind the callback
/// after the fallible code, right before spawning an async task.
pub fn new<'a, C: Context<'a>>(cx: &mut C, value: &T) -> Self {
let env = cx.env().to_raw();
let internal = unsafe { reference::new(env, value.to_raw()) };
Expand Down