mozjs icon indicating copy to clipboard operation
mozjs copied to clipboard

Documentation for Heap::handle is unclear

Open jdm opened this issue 11 months ago • 2 comments

https://github.com/servo/mozjs/blob/d6c3bd9a9a86a53c627355241b015fa7768ed688/mozjs-sys/src/jsgc.rs#L333-L348

I no longer remember when "if you know what you're doing applies", but we have a lot of usages in Servo which makes me nervous.

jdm avatar Dec 14 '24 08:12 jdm

IIUC this is only safe when done on RootedGuard<Heap<T>> (or similar, something that ensures that heap is rooted), so we could just do

impl RootedGuard<Heap<T>> {
pub fn handle(&self) -> JS::Handle<T> { 
         JS::Handle::from_marked_location(self.ptr.get() as *const _) 
     } 
}

sagudev avatar Dec 14 '24 08:12 sagudev

From my understanding, it's also safe if you have any Heap<T>, as long as it's traced, eg via a custom trace op or a global tracer.

redfire75369 avatar Dec 14 '24 14:12 redfire75369