-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Problem:
The JsAddRef and JsRelease APIs require a current context to be set in order to work. However, they are being exposed through the Clone and Drop traits that don't require a ContextGuard to be present when clone 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:
thread '<unnamed>' panicked at 'Call to 'JsAddRef(value, ::std::ptr::null_mut())' failed with: NoCurrentContext', /
Users/sam/.cargo/git/checkouts/chakracore-rs-259abda0f25cff98/cbe5ca1/src/value/function.rs:158
thread '<unnamed>' panicked at 'Call to 'unsafe { JsRelease(self.as_raw(), &mut count) }' failed with: NoCurrentContext', /Users/sam/.cargo/git/checkouts/chakracore-rs-259abda0f25cff98/cbe5ca1/src/value/function.rs:158
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.