Skip to content

Add AsHandleValue trait to safely convert Heap<Value> to HandleValue #592

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

Closed
wants to merge 1 commit into from

Conversation

Taym95
Copy link
Member

@Taym95 Taym95 commented Jul 12, 2025

Needed for servo/servo#37258

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
@Taym95 Taym95 requested review from jdm and gterzian July 12, 2025 17:30
@redfire75369
Copy link
Contributor

redfire75369 commented Jul 12, 2025

Heap<Value> doesn't root anything on its own. For this to be safe, it needs to be on a traced Heap<T>. So I'm pretty sure this isn't safe.

@Taym95
Copy link
Member Author

Taym95 commented Jul 12, 2025

Heap<Value> doesn't root anything on its own. For this to be safe, it needs to be on a traced Heap<T>. So I'm pretty sure this isn't safe.

So if I understand correctly, as_handle_value Is only safe If it's stored inside a structure that is traced by SpiderMonkey (e.g. inside a RootedGuard, JS::Rooted or custom traceable struct) I can rename it to AsHandleValueIfRooted with more detailed doc.

context:
We need this trait to safely convert a rooted Heap into a HandleValue<'a> without repeating unsafe { HandleValue::from_raw(self.handle()) } at every call site

@jdm
Copy link
Member

jdm commented Jul 12, 2025

I'm not a fan of this solution because it doesn't actually force the caller to use it in a safe way; it just hides an unsafe conversion.

@jdm
Copy link
Member

jdm commented Jul 12, 2025

I think if we annotate Heap with the crown::unrooted_must_root so that we can have unrooted values be compiler errors I would be willing to reevaluate this.

@jdm
Copy link
Member

jdm commented Jul 12, 2025

I would also prefer creating the rust::Handle values from the Heap values directly so the lifetime comes from the self value. Handle::from_raw is really unsafe because the lifetime has no relationship with the original source.

@sagudev
Copy link
Member

sagudev commented Jul 12, 2025

I think if we annotate Heap with the crown::unrooted_must_root so that we can have unrooted values be compiler errors I would be willing to reevaluate this.

Wouldn't that be to strict in some cases?

@jdm
Copy link
Member

jdm commented Jul 12, 2025

I think if we annotate Heap with the crown::unrooted_must_root so that we can have unrooted values be compiler errors I would be willing to reevaluate this.

Wouldn't that be to strict in some cases?

I'm having trouble coming up with those cases.

@sagudev
Copy link
Member

sagudev commented Jul 13, 2025

I would also prefer creating the rust::Handle values from the Heap values directly so the lifetime comes from the self value. Handle::from_raw is really unsafe because the lifetime has no relationship with the original source.

In this case we should put the trait in servo, as we need crown to ensure it's safety.

@Taym95
Copy link
Member Author

Taym95 commented Jul 13, 2025

I would also prefer creating the rust::Handle values from the Heap values directly so the lifetime comes from the self value. Handle::from_raw is really unsafe because the lifetime has no relationship with the original source.

I will do that in Servo.

@Taym95 Taym95 closed this Jul 13, 2025
@Taym95
Copy link
Member Author

Taym95 commented Jul 13, 2025

I think if we annotate Heap with the crown::unrooted_must_root so that we can have unrooted values be compiler errors I would be willing to reevaluate this.

sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants