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 1 commit
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
15 changes: 15 additions & 0 deletions src/handle/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ impl<T: Object> Root<T> {
/// The caller _must_ ensure `Root::into_inner` or `Root::drop` is called
/// to properly dispose of the `Root<T>`. If the value is dropped without
/// calling one of these methods, it will *panic*.
///
/// Be careful that you aren't short-circuiting with an early return before
Copy link
Member

Choose a reason for hiding this comment

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

Since this communicates the same as above, can it be reworded to present it as an example?

For example, early return with a ? may cause a function to return before freeing a Root. ...

Copy link
Author

Choose a reason for hiding this comment

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

I've attempted a rewording along the lines suggested here.

/// your Root value gets into_inner'd or dropped. The following will cause
/// a runtime panic when your error case gets triggered:
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 no longer strictly accurate after merging #700

Copy link
Author

Choose a reason for hiding this comment

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

I've changed it to delineate panic prior to 0.8, slow path otherwise.

/// ```
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.

/// 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,
/// };
/// ```
/// 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