diff --git a/components/script/dom/bindings/callback.rs b/components/script/dom/bindings/callback.rs index 30a6dfd5a2d..efe1b8a5681 100644 --- a/components/script/dom/bindings/callback.rs +++ b/components/script/dom/bindings/callback.rs @@ -7,6 +7,7 @@ use dom::bindings::error::{Error, Fallible, report_pending_exception}; use dom::bindings::js::Root; use dom::bindings::reflector::DomObject; +use dom::bindings::settings_stack::AutoEntryScript; use dom::globalscope::GlobalScope; use js::jsapi::{Heap, MutableHandleObject}; use js::jsapi::{IsCallable, JSContext, JSObject, JS_WrapObject}; @@ -16,6 +17,7 @@ use js::jsapi::JS_GetProperty; use js::jsval::{JSVal, UndefinedValue}; use std::default::Default; use std::ffi::CString; +use std::mem::drop; use std::ptr; use std::rc::Rc; @@ -156,6 +158,9 @@ pub struct CallSetup { old_compartment: *mut JSCompartment, /// The exception handling used for the call. handling: ExceptionHandling, + /// https://heycam.github.io/webidl/#es-invoking-callback-functions + /// steps 8 and 18.2. + entry_script: Option, } impl CallSetup { @@ -167,11 +172,13 @@ impl CallSetup { let global = unsafe { GlobalScope::from_object(callback.callback()) }; let cx = global.get_cx(); + let aes = AutoEntryScript::new(&global); CallSetup { exception_global: global, cx: cx, old_compartment: unsafe { JS_EnterCompartment(cx, callback.callback()) }, handling: handling, + entry_script: Some(aes), } } @@ -190,6 +197,7 @@ impl Drop for CallSetup { self.exception_global.reflector().get_jsobject().get()); report_pending_exception(self.cx, true); } + drop(self.entry_script.take().unwrap()); } } } diff --git a/components/script/dom/bindings/mod.rs b/components/script/dom/bindings/mod.rs index 725cdbb4a8b..5d0cd864dad 100644 --- a/components/script/dom/bindings/mod.rs +++ b/components/script/dom/bindings/mod.rs @@ -149,6 +149,7 @@ pub mod num; pub mod proxyhandler; pub mod refcounted; pub mod reflector; +pub mod settings_stack; pub mod str; pub mod structuredclone; pub mod trace; diff --git a/components/script/dom/bindings/settings_stack.rs b/components/script/dom/bindings/settings_stack.rs new file mode 100644 index 00000000000..2b24800cebc --- /dev/null +++ b/components/script/dom/bindings/settings_stack.rs @@ -0,0 +1,70 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use dom::bindings::js::{JS, Root}; +use dom::bindings::trace::JSTraceable; +use dom::globalscope::GlobalScope; +use js::jsapi::JSTracer; +use std::cell::RefCell; + +thread_local!(static STACK: RefCell> = RefCell::new(Vec::new())); + +#[allow(unrooted_must_root)] +#[derive(JSTraceable)] +struct StackEntry { + global: JS, +} + +/// Traces the script settings stack. +pub unsafe fn trace(tracer: *mut JSTracer) { + STACK.with(|stack| { + stack.borrow().trace(tracer); + }) +} + +/// RAII struct that pushes and pops entries from the script settings stack. +pub struct AutoEntryScript { + global: *const GlobalScope, +} + +impl AutoEntryScript { + /// https://html.spec.whatwg.org/multipage/#prepare-to-run-script + pub fn new(global: &GlobalScope) -> Self { + STACK.with(|stack| { + trace!("Prepare to run script with {:p}", global); + let mut stack = stack.borrow_mut(); + stack.push(StackEntry { + global: JS::from_ref(global), + }); + AutoEntryScript { + global: global as *const _, + } + }) + } +} + +impl Drop for AutoEntryScript { + /// https://html.spec.whatwg.org/multipage/#clean-up-after-running-script + fn drop(&mut self) { + STACK.with(|stack| { + let mut stack = stack.borrow_mut(); + let entry = stack.pop().unwrap(); + assert_eq!(&*entry.global as *const GlobalScope, + self.global, + "Dropped AutoEntryScript out of order."); + trace!("Clean up after running script with {:p}", self.global); + }) + } +} + +/// Returns the ["entry"] global object. +/// +/// ["entry"]: https://html.spec.whatwg.org/multipage/#entry +pub fn entry_global() -> Root { + STACK.with(|stack| { + stack.borrow() + .last() + .map(|entry| Root::from_ref(&*entry.global)) + }).unwrap() +} diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index d0976ec302c..fca829c9904 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -10,6 +10,7 @@ use dom::bindings::error::{ErrorInfo, report_pending_exception}; use dom::bindings::inheritance::Castable; use dom::bindings::js::{MutNullableJS, Root}; use dom::bindings::reflector::DomObject; +use dom::bindings::settings_stack::{AutoEntryScript, entry_global}; use dom::bindings::str::DOMString; use dom::crypto::Crypto; use dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope; @@ -365,6 +366,7 @@ impl GlobalScope { let filename = CString::new(filename).unwrap(); let _ac = JSAutoCompartment::new(cx, globalhandle.get()); + let _aes = AutoEntryScript::new(self); let options = CompileOptionsWrapper::new(cx, filename.as_ptr(), 1); unsafe { if !Evaluate2(cx, options.ptr, code.as_ptr(), @@ -519,6 +521,13 @@ impl GlobalScope { global_scope_from_global(global) } } + + /// Returns the ["entry"] global object. + /// + /// ["entry"]: https://html.spec.whatwg.org/multipage/#entry + pub fn entry() -> Root { + entry_global() + } } fn timestamp_in_ms(time: Timespec) -> u64 { diff --git a/components/script/dom/testbinding.rs b/components/script/dom/testbinding.rs index 9f65d04bbfa..e556e541f60 100644 --- a/components/script/dom/testbinding.rs +++ b/components/script/dom/testbinding.rs @@ -773,6 +773,10 @@ impl TestBindingMethods for TestBinding { } fn Panic(&self) { panic!("explicit panic from script") } + + fn EntryGlobal(&self) -> Root { + GlobalScope::entry() + } } impl TestBinding { diff --git a/components/script/dom/webidls/TestBinding.webidl b/components/script/dom/webidls/TestBinding.webidl index c3274057a40..acf5697ce3c 100644 --- a/components/script/dom/webidls/TestBinding.webidl +++ b/components/script/dom/webidls/TestBinding.webidl @@ -522,6 +522,8 @@ interface TestBinding { void resolvePromiseDelayed(Promise p, DOMString value, unsigned long long ms); void panic(); + + GlobalScope entryGlobal(); }; callback SimpleCallback = void(any value); diff --git a/components/script/dom/workerglobalscope.rs b/components/script/dom/workerglobalscope.rs index 221d60c1570..6e9b919d98c 100644 --- a/components/script/dom/workerglobalscope.rs +++ b/components/script/dom/workerglobalscope.rs @@ -13,6 +13,7 @@ use dom::bindings::inheritance::Castable; use dom::bindings::js::{MutNullableJS, Root}; use dom::bindings::refcounted::Trusted; use dom::bindings::reflector::DomObject; +use dom::bindings::settings_stack::AutoEntryScript; use dom::bindings::str::DOMString; use dom::crypto::Crypto; use dom::dedicatedworkerglobalscope::DedicatedWorkerGlobalScope; @@ -331,6 +332,7 @@ impl WorkerGlobalScopeMethods for WorkerGlobalScope { impl WorkerGlobalScope { #[allow(unsafe_code)] pub fn execute_script(&self, source: DOMString) { + let _aes = AutoEntryScript::new(self.upcast()); rooted!(in(self.runtime.cx()) let mut rval = UndefinedValue()); match self.runtime.evaluate_script( self.reflector().get_jsobject(), &source, self.worker_url.as_str(), 1, rval.handle_mut()) { diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index a21f96c5890..bd8874dc475 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -10,6 +10,7 @@ use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::PromiseBinding::PromiseJobCallback; use dom::bindings::js::{Root, RootCollection, RootCollectionPtr, trace_roots}; use dom::bindings::refcounted::{LiveDOMReferences, trace_refcounted_objects}; +use dom::bindings::settings_stack; use dom::bindings::trace::{JSTraceable, trace_traceables}; use dom::bindings::utils::DOM_CALLBACKS; use dom::globalscope::GlobalScope; @@ -474,6 +475,7 @@ unsafe extern fn trace_rust_roots(tr: *mut JSTracer, _data: *mut os::raw::c_void trace_thread(tr); trace_traceables(tr); trace_roots(tr); + settings_stack::trace(tr); debug!("done custom root handler"); } diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index 4f7b9c4ec43..fe7e479282c 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -8408,6 +8408,18 @@ "url": "/_mozilla/mozilla/global.html" } ], + "mozilla/globals/entry.html": [ + { + "path": "mozilla/globals/entry.html", + "url": "/_mozilla/mozilla/globals/entry.html" + } + ], + "mozilla/globals/entry.worker.js": [ + { + "path": "mozilla/globals/entry.worker.js", + "url": "/_mozilla/mozilla/globals/entry.worker.html" + } + ], "mozilla/hit_test_nested_sc.html": [ { "path": "mozilla/hit_test_nested_sc.html", diff --git a/tests/wpt/mozilla/meta/mozilla/globals/entry.html.ini b/tests/wpt/mozilla/meta/mozilla/globals/entry.html.ini new file mode 100644 index 00000000000..52b122bc295 --- /dev/null +++ b/tests/wpt/mozilla/meta/mozilla/globals/entry.html.ini @@ -0,0 +1,3 @@ +[entry.html] + type: testharness + prefs: [dom.testbinding.enabled:true] diff --git a/tests/wpt/mozilla/meta/mozilla/globals/entry.worker.js.ini b/tests/wpt/mozilla/meta/mozilla/globals/entry.worker.js.ini new file mode 100644 index 00000000000..2205984347e --- /dev/null +++ b/tests/wpt/mozilla/meta/mozilla/globals/entry.worker.js.ini @@ -0,0 +1,3 @@ +[entry.worker.html] + type: testharness + prefs: [dom.testbinding.enabled:true] diff --git a/tests/wpt/mozilla/tests/mozilla/globals/empty.html b/tests/wpt/mozilla/tests/mozilla/globals/empty.html new file mode 100644 index 00000000000..2e54da0668a --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/globals/empty.html @@ -0,0 +1,2 @@ + +Empty page diff --git a/tests/wpt/mozilla/tests/mozilla/globals/entry.html b/tests/wpt/mozilla/tests/mozilla/globals/entry.html new file mode 100644 index 00000000000..bc896a2e0a9 --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/globals/entry.html @@ -0,0 +1,16 @@ + + +Entry page + + + + diff --git a/tests/wpt/mozilla/tests/mozilla/globals/entry.worker.js b/tests/wpt/mozilla/tests/mozilla/globals/entry.worker.js new file mode 100644 index 00000000000..f83e7a1e16b --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/globals/entry.worker.js @@ -0,0 +1,9 @@ +importScripts("/resources/testharness.js"); + +test(function() { + var entry = (new TestBinding()).entryGlobal(); + assert_equals(entry, self); +}); + + +done(); diff --git a/tests/wpt/mozilla/tests/mozilla/globals/incumbent.html b/tests/wpt/mozilla/tests/mozilla/globals/incumbent.html new file mode 100644 index 00000000000..54e28d4ef3e --- /dev/null +++ b/tests/wpt/mozilla/tests/mozilla/globals/incumbent.html @@ -0,0 +1,13 @@ + +Incumbent page + + + +