Implement the entry global.

Partial fix for #10963.
This commit is contained in:
Ms2ger 2016-12-29 13:39:12 +01:00
parent 839b7fe8ef
commit cb47a7e403
15 changed files with 156 additions and 0 deletions

View file

@ -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<AutoEntryScript>,
}
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());
}
}
}

View file

@ -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;

View file

@ -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<Vec<StackEntry>> = RefCell::new(Vec::new()));
#[allow(unrooted_must_root)]
#[derive(JSTraceable)]
struct StackEntry {
global: JS<GlobalScope>,
}
/// 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<GlobalScope> {
STACK.with(|stack| {
stack.borrow()
.last()
.map(|entry| Root::from_ref(&*entry.global))
}).unwrap()
}