DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
504 stars 63 forks source link

SelfMethod not accepting mutable reference in 0.3.1 #174

Closed richarddd closed 1 year ago

richarddd commented 1 year ago

Hi,

I'm trying to migrate my project from 0.1.7 to 0.3.1.

Since Func::from(Method(...) no longer accepting self functions with mutable references what can i use instead? We have SelfMethod::from() but that requires reference to be none mutable. The only example i see in tests is to use another implementation for methods that mutates like:

impl<'js> Class<'js, A> {
    pub fn add(self, val: Persistent<Class<'static, A>>) {
        self.borrow().refs.borrow_mut().insert(val);
    }

    pub fn rm(self, val: Persistent<Class<'static, A>>) {
        self.borrow().refs.borrow_mut().remove(&val);
    }
}

...and then define the props like this:

proto.set("add", Func::from(Method(Class::<A>::add)))?;
proto.set("rm", Func::from(Method(Class::<A>::rm)))?;

Is there another (less verbose) way?

DelSkayn commented 1 year ago

Between 0.1.7 and 0.3.1 I have found a lot of soundness problems with the library which I had to remove.

Currently there is no way to get a mutable reference to ClassDef object. It used to be possible because we allowed you to take mutable references to the classdef from any class instance. This could result in aliasing mutable references which is unsound.

SelfMethod is somewhat of a hack to make references to classes work again after #167

What you are doing with RefCells is currently the best way to do that.

I am working on redesigning the class interface in the class_redesign branch to make this less verbose. It will internally use a RefCell like structure to allow mutable references.

DelSkayn commented 1 year ago

You might want to wait on rewriting using the newer version cause the library is currently in a bit of an awkward place cause of all the functionality that had to be scrapped due to unsoundness.