script_bindings/
settings_stack.rs1use std::marker::PhantomData;
6use std::thread;
7
8use js::jsapi::{HideScriptedCaller, UnhideScriptedCaller};
9use js::rust::Runtime;
10
11use crate::DomTypes;
12use crate::interfaces::{DomHelpers, GlobalScopeHelpers};
13use crate::root::{Dom, DomRoot};
14use crate::script_runtime::CanGc;
15
16#[derive(Debug, Eq, JSTraceable, PartialEq)]
17pub enum StackEntryKind {
18 Incumbent,
19 Entry,
20}
21
22#[cfg_attr(crown, allow(crown::unrooted_must_root))]
23#[derive(JSTraceable)]
24pub struct StackEntry<D: DomTypes> {
25 pub global: Dom<D::GlobalScope>,
26 pub kind: StackEntryKind,
27}
28
29pub struct GenericAutoEntryScript<D: DomTypes> {
31 global: DomRoot<D::GlobalScope>,
32 #[cfg(feature = "tracing")]
33 #[allow(dead_code)]
34 span: tracing::span::EnteredSpan,
35}
36
37impl<D: DomTypes> GenericAutoEntryScript<D> {
38 pub fn new(global: &D::GlobalScope) -> Self {
40 let settings_stack = <D as DomHelpers<D>>::settings_stack();
41 settings_stack.with(|stack| {
42 trace!("Prepare to run script with {:p}", global);
43 let mut stack = stack.borrow_mut();
44 stack.push(StackEntry {
45 global: Dom::from_ref(global),
46 kind: StackEntryKind::Entry,
47 });
48 Self {
49 global: DomRoot::from_ref(global),
50 #[cfg(feature = "tracing")]
51 span: tracing::info_span!(
52 "ScriptEvaluate",
53 servo_profiling = true,
54 url = global.get_url().to_string(),
55 )
56 .entered(),
57 }
58 })
59 }
60}
61
62impl<D: DomTypes> Drop for GenericAutoEntryScript<D> {
63 fn drop(&mut self) {
65 let settings_stack = <D as DomHelpers<D>>::settings_stack();
66 settings_stack.with(|stack| {
67 let mut stack = stack.borrow_mut();
68 let entry = stack.pop().unwrap();
69 assert_eq!(
70 &*entry.global as *const D::GlobalScope, &*self.global as *const D::GlobalScope,
71 "Dropped AutoEntryScript out of order."
72 );
73 assert_eq!(entry.kind, StackEntryKind::Entry);
74 trace!("Clean up after running script with {:p}", &*entry.global);
75 });
76
77 if !thread::panicking() && D::GlobalScope::incumbent().is_none() {
79 self.global.perform_a_microtask_checkpoint(CanGc::note());
80 }
81 }
82}
83
84pub struct GenericAutoIncumbentScript<D: DomTypes> {
86 global: usize,
87 _marker: PhantomData<D>,
88}
89
90impl<D: DomTypes> GenericAutoIncumbentScript<D> {
91 pub fn new(global: &D::GlobalScope) -> Self {
93 unsafe {
95 let cx =
96 Runtime::get().expect("Creating a new incumbent script after runtime shutdown");
97 HideScriptedCaller(cx.as_ptr());
98 }
99 let settings_stack = <D as DomHelpers<D>>::settings_stack();
100 settings_stack.with(|stack| {
101 trace!("Prepare to run a callback with {:p}", global);
102 let mut stack = stack.borrow_mut();
104 stack.push(StackEntry {
105 global: Dom::from_ref(global),
106 kind: StackEntryKind::Incumbent,
107 });
108 Self {
109 global: global as *const _ as usize,
110 _marker: PhantomData,
111 }
112 })
113 }
114}
115
116impl<D: DomTypes> Drop for GenericAutoIncumbentScript<D> {
117 fn drop(&mut self) {
119 let settings_stack = <D as DomHelpers<D>>::settings_stack();
120 settings_stack.with(|stack| {
121 let mut stack = stack.borrow_mut();
123 let entry = stack.pop().unwrap();
124 assert_eq!(
126 &*entry.global as *const D::GlobalScope as usize, self.global,
127 "Dropped AutoIncumbentScript out of order."
128 );
129 assert_eq!(entry.kind, StackEntryKind::Incumbent);
130 trace!(
131 "Clean up after running a callback with {:p}",
132 &*entry.global
133 );
134 });
135 unsafe {
136 if let Some(cx) = Runtime::get() {
138 UnhideScriptedCaller(cx.as_ptr());
139 }
140 }
141 }
142}