servo: Merge #14840 - Implement the entry global (from servo:entry-global); r=nox

Partial fix for #10963.

Source-Repo: https://github.com/servo/servo
Source-Revision: 85d3bbd9994eedd1cca96b239b880a6c2d5aadea
This commit is contained in:
Ms2ger 2017-01-06 05:15:11 -08:00
Родитель b3d789b81f
Коммит f8d08d35f2
8 изменённых файлов: 98 добавлений и 0 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -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<Self> {
entry_global()
}
}
fn timestamp_in_ms(time: Timespec) -> u64 {

Просмотреть файл

@ -773,6 +773,10 @@ impl TestBindingMethods for TestBinding {
}
fn Panic(&self) { panic!("explicit panic from script") }
fn EntryGlobal(&self) -> Root<GlobalScope> {
GlobalScope::entry()
}
}
impl TestBinding {

Просмотреть файл

@ -522,6 +522,8 @@ interface TestBinding {
void resolvePromiseDelayed(Promise<any> p, DOMString value, unsigned long long ms);
void panic();
GlobalScope entryGlobal();
};
callback SimpleCallback = void(any value);

Просмотреть файл

@ -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()) {

Просмотреть файл

@ -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");
}