mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Move more bindings code to script_bindings (#35578)
* Move JSContext wrapper to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Move webidl constant bindings to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Move CanGc to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Move Dom<T> and Root<T> types to script_bindings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Formatting. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Extra docs for new traits. Signed-off-by: Josh Matthews <josh@joshmatthews.net> * Fix clippy warnings. Signed-off-by: Josh Matthews <josh@joshmatthews.net> --------- Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
parent
54286229ea
commit
35f21e426b
23 changed files with 788 additions and 641 deletions
|
@ -3,6 +3,35 @@
|
|||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
|
||||
use js::jsapi::JSContext as RawJSContext;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(transparent)]
|
||||
pub struct JSContext(*mut RawJSContext);
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
impl JSContext {
|
||||
/// Create a new [`JSContext`] object from the given raw pointer.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// The `RawJSContext` argument must point to a valid `RawJSContext` in memory.
|
||||
pub unsafe fn from_ptr(raw_js_context: *mut RawJSContext) -> Self {
|
||||
JSContext(raw_js_context)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
impl Deref for JSContext {
|
||||
type Target = *mut RawJSContext;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
thread_local!(
|
||||
static THREAD_ACTIVE: Cell<bool> = const { Cell::new(true) };
|
||||
|
@ -15,3 +44,19 @@ pub fn runtime_is_alive() -> bool {
|
|||
pub fn mark_runtime_dead() {
|
||||
THREAD_ACTIVE.with(|t| t.set(false));
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
/// A compile-time marker that there are operations that could trigger a JS garbage collection
|
||||
/// operation within the current stack frame. It is trivially copyable, so it should be passed
|
||||
/// as a function argument and reused when calling other functions whenever possible. Since it
|
||||
/// is only meaningful within the current stack frame, it is impossible to move it to a different
|
||||
/// thread or into a task that will execute asynchronously.
|
||||
pub struct CanGc(PhantomData<*mut ()>);
|
||||
|
||||
impl CanGc {
|
||||
/// Create a new CanGc value, representing that a GC operation is possible within the
|
||||
/// current stack frame.
|
||||
pub fn note() -> CanGc {
|
||||
CanGc(PhantomData)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue