Skip to content

Implement JSContext::get_from_thread#748

Merged
sagudev merged 3 commits into
mainfrom
sagudev-patch-6
Jun 2, 2026
Merged

Implement JSContext::get_from_thread#748
sagudev merged 3 commits into
mainfrom
sagudev-patch-6

Conversation

@sagudev

@sagudev sagudev commented May 21, 2026

Copy link
Copy Markdown
Member

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.

sagudev added 3 commits May 21, 2026 07:34
servo/servo#45040 (comment)

Signed-off-by: Sam <16504129+sagudev@users.noreply.github.com>
.
Signed-off-by: Sam <16504129+sagudev@users.noreply.github.com>
.
Signed-off-by: Sam <16504129+sagudev@users.noreply.github.com>
@Gae24

Gae24 commented May 28, 2026

Copy link
Copy Markdown
Contributor

This is needed in particular for DOMString, there are three calls to Runtime::get that are probably tricky to remove, e.g one is called from Deref trait.

@sagudev sagudev marked this pull request as ready for review May 29, 2026 03:48
@sagudev

sagudev commented May 29, 2026

Copy link
Copy Markdown
Member Author

This is needed in particular for DOMString, there are three calls to Runtime::get that are probably tricky to remove, e.g one is called from Deref trait.

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.

Comment thread mozjs/src/context.rs
///
/// SAFETY:
/// - only one [JSContext] can be alive and it should not outlive [Runtime].
pub unsafe fn get_from_thread() -> Option<JSContext> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@sagudev sagudev added this pull request to the merge queue Jun 2, 2026
Merged via the queue into main with commit 568c412 Jun 2, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants