ducc icon indicating copy to clipboard operation
ducc copied to clipboard

Idea: object (and function?) handle

Open wyozi opened this issue 5 years ago • 0 comments

Hi! While I've gotten used to storing everything in ducc.globals(), I still get a bit annoyed at the whole retrieve everything from globals every time. For instance, for many calls crossing the Rust<->JS boundary I'll have to do something like ducc.globals().get("namespace").get("library").get("_refs").get(index).

However, I got an idea for a potential way to kind of store references to Duktape for faster and nicer retrieval, without sacrificing any safety guarantees. Basically, it'd be a handle type looking something like this:

#[derive(Clone)]
struct ObjectHandle {
  rc: Rc<ffi::duk_uarridx_t>
}
impl ObjectHandle {
  pub fn get_object(ducc: &Ducc) -> Object {
    // return object from heap stash
  } 
}

and API for retrieving an ObjectHandle from an Object somewhere.

Some practical concerns would be making sure that GC doesn't remove our object when we hold a handle (but is able to if we don't), which depends on Duktape's support for it. One scary bit is Drop for ObjectHandle; how can we inform Duktape that it's okay to GC the referenced object without access to &Ducc? Maybe a drop_object_reference(&Ducc) that must be called before Drop or it will panic.

The benefits of this approach would be the ability to keep a reference certain objects without caring about what is happening in the JS world, and I think the ergonomics of using it are better than the global referencing as well. If this works, it could be extended to FunctionHandle etc as well.

Does this sound like a viable/safe idea? If it turns out well, maybe the same approach would work with Mini-V8 as well.

wyozi avatar Jul 28 '20 14:07 wyozi