Implement JSContext::get_from_thread#748
Conversation
servo/servo#45040 (comment) Signed-off-by: Sam <16504129+sagudev@users.noreply.github.com>
|
This is needed in particular for |
This should be used carefully. I think usage in deref will be ok as we only need &JSContext, but other places might not be so lucky. |
| /// | ||
| /// SAFETY: | ||
| /// - only one [JSContext] can be alive and it should not outlive [Runtime]. | ||
| pub unsafe fn get_from_thread() -> Option<JSContext> { |
There was a problem hiding this comment.
Marking a static function with no arguments as unsafe is interesting, because it makes me wonder what kind of safety comments we can write at the call site. Doesn't Runtime::get already uphold the safety comment for this implementation?
There was a problem hiding this comment.
Runtime::get returns raw context (ptr) so one does not need to uphold any invarinats really (but it should still not outlive runtime). Here we return safe jscontext so one needs to ensure that we uphold it's invariants (same as at it's construction).
This is unofficially perma cx. Sometimes it is needed: servo/servo#45040 (comment) and https://doc.servo.org/mozjs/rust/struct.Runtime.html#method.get is planned to be removed.