script_bindings/
script_runtime.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6use std::marker::PhantomData;
7use std::ops::Deref;
8
9use js::context::JSContext as SafeJSContext;
10use js::jsapi::JSContext as RawJSContext;
11use js::realm::{AutoRealm, CurrentRealm};
12
13#[derive(Clone, Copy)]
14#[repr(transparent)]
15pub struct JSContext(*mut RawJSContext);
16
17impl From<&mut SafeJSContext> for JSContext {
18    fn from(safe_cx: &mut SafeJSContext) -> Self {
19        unsafe { JSContext(safe_cx.raw_cx()) }
20    }
21}
22
23impl<'a> From<&mut CurrentRealm<'a>> for JSContext {
24    fn from(safe_cx: &mut CurrentRealm<'a>) -> Self {
25        unsafe { JSContext(safe_cx.raw_cx()) }
26    }
27}
28
29impl<'a> From<&mut AutoRealm<'a>> for JSContext {
30    fn from(safe_cx: &mut AutoRealm<'a>) -> Self {
31        unsafe { JSContext(safe_cx.raw_cx()) }
32    }
33}
34
35#[expect(unsafe_code)]
36impl JSContext {
37    /// Create a new [`JSContext`] object from the given raw pointer.
38    ///
39    /// # Safety
40    ///
41    /// The `RawJSContext` argument must point to a valid `RawJSContext` in memory.
42    pub unsafe fn from_ptr(raw_js_context: *mut RawJSContext) -> Self {
43        JSContext(raw_js_context)
44    }
45
46    /// For compatibility with [js::context::JSContext]
47    pub fn raw_cx(&self) -> *mut RawJSContext {
48        self.0
49    }
50
51    /// For compatibility with [js::context::JSContext]
52    pub fn raw_cx_no_gc(&self) -> *mut RawJSContext {
53        self.0
54    }
55}
56
57impl Deref for JSContext {
58    type Target = *mut RawJSContext;
59
60    fn deref(&self) -> &Self::Target {
61        &self.0
62    }
63}
64
65thread_local!(
66    static THREAD_ACTIVE: Cell<bool> = const { Cell::new(true) };
67);
68
69pub fn runtime_is_alive() -> bool {
70    THREAD_ACTIVE.with(|t| t.get())
71}
72
73pub fn mark_runtime_dead() {
74    THREAD_ACTIVE.with(|t| t.set(false));
75}
76
77/// Get the current JSContext for the running thread.
78///
79/// ## Safety
80/// Using this function is unsafe because no other JSContext may be constructed apart from initial ones,
81/// but because we are still working on passing down &mut SafeJSContext references,
82/// this function is provided as temporary workaround/placeholder.
83///
84/// As such all it's usages will need to be eventually replaced with proper &mut SafeJSContext references.
85pub unsafe fn temp_cx() -> SafeJSContext {
86    unsafe { SafeJSContext::from_ptr(js::rust::Runtime::get().unwrap()) }
87}
88
89#[derive(Clone, Copy, Debug)]
90/// A compile-time marker that there are operations that could trigger a JS garbage collection
91/// operation within the current stack frame. It is trivially copyable, so it should be passed
92/// as a function argument and reused when calling other functions whenever possible. Since it
93/// is only meaningful within the current stack frame, it is impossible to move it to a different
94/// thread or into a task that will execute asynchronously.
95pub struct CanGc(PhantomData<*mut ()>);
96
97impl CanGc {
98    /// Create a new CanGc value, representing that a GC operation is possible within the
99    /// current stack frame.
100    pub fn note() -> CanGc {
101        CanGc(PhantomData)
102    }
103
104    /// &mut SafeJSContext is always an indication that GC is possible.
105    pub fn from_cx(_cx: &mut SafeJSContext) -> CanGc {
106        CanGc::note()
107    }
108}