Open thefakeplace opened 1 month ago
This is indeed by design. The lifetime of objects on the JavaScript side can't be known in advance so an object must have ownership of it's rust value.
With ent.position
you probably want the same object to be returned every time, one solution is to store the object you want to return. So:
struct Entity<'js>{
#[qjs(get,set)]
position: Class<'js, Vec3>,
}
You can also solve this some other ways but ultimately you will have to use either some sort of shared ownership, with Rc
, Arc
, or rquickjs::Class which is kinda like a Rc<RefCell<T>>
.
Imagine the following scenario:
if you run the following JS:
You can see that
ent.position
is never updated, becauserquickjs
clones it before passing the value to the runtime, like you can see in the generated code to create theEntity
class.I know this is probably by design, because any kind of pointer could be invalidated on the native side accidentally and then (presumably) some kind of segfault.
However, it's unintuitive and I'm looking for a solution, even if it means pinning memory on the native side.
Any ideas?