Open srijs opened 7 years ago
I've spent quite some time this forenoon contemplating just this. I've implemented a fix for the Drop
scenario recently, but it did increase the complexity of the implementation, and whilst something similar would be possible to implement for the clone scenario, I feel the solution would be too grotty.
The most prominent alternative I've come up with so far is using a default context, created during the Runtime
instantiation. This may work thanks to the fact that objects recently became shared between contexts in ChakraCore. Therefore one context may decrement the reference count of values constructed using other contexts.
In fact, I'm not quite sure of the list of things not shared between contexts.
This would solve these problems, though some issues would persist in a multi-threaded environment, since Runtime
is Send
. Values created on a runtime's previous thread would no longer have any default Context
to rely upon.
And I agree that introducing additional lifetime dependencies (i.e Value → ContextGuard
) is preferred to be avoided.
With a detailed response from a ChakraCore team member I believe I am confident enough to start implementing the default context approach. This would alleviate several of the aforementioned issues, and would allow the API to be simplified. Though the safety risks associated with multi-threading would remain.
Problem:
The
JsAddRef
andJsRelease
APIs require a current context to be set in order to work. However, they are being exposed through theClone
andDrop
traits that don't require aContextGuard
to be present whenclone
is called, or the value is dropped.This results in the following panics, when a value is cloned and/or dropped w/o a current context:
I'm struggling to think of a way to expose a safe API through clone/drop, tbh. Especially
drop
seems to be problematic, because Rust offers no way to control under which circumstances a value might be dropped.A possible way to fix it could be to tie the lifetime of a Context or Value to the lifetime of the ContextGuard, which would require the value to be dropped before the context guard is dropped, i.e. before the current context is unset, and would require the context guard to be alive in order to clone a value.
The downside is that this complicates storing contexts or values in Rust data structures, but it's the only possible solution I can think of at this point.