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 http://mozilla.org/MPL/2.0/. */
4
5 use dom::bindings::root::{Dom, DomRoot};
6 use dom::bindings::trace::JSTraceable;
7 use dom::globalscope::GlobalScope;
8 use js::jsapi::GetScriptedCallerGlobal;
9 use js::jsapi::HideScriptedCaller;
10 use js::jsapi::JSTracer;
11 use js::jsapi::UnhideScriptedCaller;
12 use js::rust::Runtime;
13 use std::cell::RefCell;
14 use std::thread;
15
16 thread_local!(static STACK: RefCell<Vec<StackEntry>> = RefCell::new(Vec::new()));
17
18 #[derive(Debug, Eq, JSTraceable, PartialEq)]
19 enum StackEntryKind {
20 Incumbent,
21 Entry,
22 }
23
24 #[allow(unrooted_must_root)]
25 #[derive(JSTraceable)]
26 struct StackEntry {
27 global: Dom<GlobalScope>,
28 kind: StackEntryKind,
29 }
30
31 /// Traces the script settings stack.
trace(tracer: *mut JSTracer)32 pub unsafe fn trace(tracer: *mut JSTracer) {
33 STACK.with(|stack| {
34 stack.borrow().trace(tracer);
35 })
36 }
37
is_execution_stack_empty() -> bool38 pub fn is_execution_stack_empty() -> bool {
39 STACK.with(|stack| {
40 stack.borrow().is_empty()
41 })
42 }
43
44 /// RAII struct that pushes and pops entries from the script settings stack.
45 pub struct AutoEntryScript {
46 global: DomRoot<GlobalScope>,
47 }
48
49 impl AutoEntryScript {
50 /// <https://html.spec.whatwg.org/multipage/#prepare-to-run-script>
new(global: &GlobalScope) -> Self51 pub fn new(global: &GlobalScope) -> Self {
52 STACK.with(|stack| {
53 trace!("Prepare to run script with {:p}", global);
54 let mut stack = stack.borrow_mut();
55 stack.push(StackEntry {
56 global: Dom::from_ref(global),
57 kind: StackEntryKind::Entry,
58 });
59 AutoEntryScript {
60 global: DomRoot::from_ref(global),
61 }
62 })
63 }
64 }
65
66 impl Drop for AutoEntryScript {
67 /// <https://html.spec.whatwg.org/multipage/#clean-up-after-running-script>
drop(&mut self)68 fn drop(&mut self) {
69 STACK.with(|stack| {
70 let mut stack = stack.borrow_mut();
71 let entry = stack.pop().unwrap();
72 assert_eq!(&*entry.global as *const GlobalScope,
73 &*self.global as *const GlobalScope,
74 "Dropped AutoEntryScript out of order.");
75 assert_eq!(entry.kind, StackEntryKind::Entry);
76 trace!("Clean up after running script with {:p}", &*entry.global);
77 });
78
79 // Step 5
80 if !thread::panicking() && incumbent_global().is_none() {
81 self.global.perform_a_microtask_checkpoint();
82 }
83 }
84 }
85
86 /// Returns the ["entry"] global object.
87 ///
88 /// ["entry"]: https://html.spec.whatwg.org/multipage/#entry
entry_global() -> DomRoot<GlobalScope>89 pub fn entry_global() -> DomRoot<GlobalScope> {
90 STACK.with(|stack| {
91 stack.borrow()
92 .iter()
93 .rev()
94 .find(|entry| entry.kind == StackEntryKind::Entry)
95 .map(|entry| DomRoot::from_ref(&*entry.global))
96 }).unwrap()
97 }
98
99 /// RAII struct that pushes and pops entries from the script settings stack.
100 pub struct AutoIncumbentScript {
101 global: usize,
102 }
103
104 impl AutoIncumbentScript {
105 /// <https://html.spec.whatwg.org/multipage/#prepare-to-run-a-callback>
new(global: &GlobalScope) -> Self106 pub fn new(global: &GlobalScope) -> Self {
107 // Step 2-3.
108 unsafe {
109 let cx = Runtime::get();
110 assert!(!cx.is_null());
111 HideScriptedCaller(cx);
112 }
113 STACK.with(|stack| {
114 trace!("Prepare to run a callback with {:p}", global);
115 // Step 1.
116 let mut stack = stack.borrow_mut();
117 stack.push(StackEntry {
118 global: Dom::from_ref(global),
119 kind: StackEntryKind::Incumbent,
120 });
121 AutoIncumbentScript {
122 global: global as *const _ as usize,
123 }
124 })
125 }
126 }
127
128 impl Drop for AutoIncumbentScript {
129 /// <https://html.spec.whatwg.org/multipage/#clean-up-after-running-a-callback>
drop(&mut self)130 fn drop(&mut self) {
131 STACK.with(|stack| {
132 // Step 4.
133 let mut stack = stack.borrow_mut();
134 let entry = stack.pop().unwrap();
135 // Step 3.
136 assert_eq!(&*entry.global as *const GlobalScope as usize,
137 self.global,
138 "Dropped AutoIncumbentScript out of order.");
139 assert_eq!(entry.kind, StackEntryKind::Incumbent);
140 trace!("Clean up after running a callback with {:p}", &*entry.global);
141 });
142 unsafe {
143 // Step 1-2.
144 let cx = Runtime::get();
145 assert!(!cx.is_null());
146 UnhideScriptedCaller(cx);
147 }
148 }
149 }
150
151 /// Returns the ["incumbent"] global object.
152 ///
153 /// ["incumbent"]: https://html.spec.whatwg.org/multipage/#incumbent
incumbent_global() -> Option<DomRoot<GlobalScope>>154 pub fn incumbent_global() -> Option<DomRoot<GlobalScope>> {
155 // https://html.spec.whatwg.org/multipage/#incumbent-settings-object
156
157 // Step 1, 3: See what the JS engine has to say. If we've got a scripted
158 // caller override in place, the JS engine will lie to us and pretend that
159 // there's nothing on the JS stack, which will cause us to check the
160 // incumbent script stack below.
161 unsafe {
162 let cx = Runtime::get();
163 assert!(!cx.is_null());
164 let global = GetScriptedCallerGlobal(cx);
165 if !global.is_null() {
166 return Some(GlobalScope::from_object(global));
167 }
168 }
169
170 // Step 2: nothing from the JS engine. Let's use whatever's on the explicit stack.
171 STACK.with(|stack| {
172 stack.borrow()
173 .last()
174 .map(|entry| DomRoot::from_ref(&*entry.global))
175 })
176 }
177