-
Notifications
You must be signed in to change notification settings - Fork 3
Description
A major problem with the current GC is that the vm has no way of telling that a variable in a native function is holding a reference to a JavaScript object. If a GC cycle happens to trigger (e.g. due to calling a JS function), the object referenced by the variable is (mistakenly) deallocated.
Fixing this likely requires major changes wrt how values are represented all over the vm and how native code works with these.
One possible way to fix this is to have a separate vector of "external references" (i.e. objects referenced by bindings on the native stack). The current vm actually already has something similar, but it requires manually adding values to it. It would be nice if we could make this sort of pattern a compile error (pseudocode):
fn native_function() {
let o = create_object();
js_function(); // calls some JavaScript function, GC triggers in here, `o` is not marked and gets deallocated
print(o); // using deallocated object
}We could perhaps make some kind of macro that also adds the assigned value to the external refs vector, used like so:
fn native_function() {
letroot!(o = create_object()); // allocate object AND add to vector of external refs
js_function(); // GC triggers, but `o` is in external refs vector and is not deallocated
print(o); // ok
}