Make reflect_dom_object take a &GlobalScope

This commit is contained in:
Anthony Ramine 2016-09-27 13:16:41 +02:00
parent 093b189b48
commit fcb59d3057
132 changed files with 488 additions and 407 deletions

View file

@ -1927,8 +1927,10 @@ class CGImports(CGWrapper):
if t in dictionaries or t in enums:
continue
if t.isInterface() or t.isNamespace():
descriptor = descriptorProvider.getDescriptor(getIdentifier(t).name)
extras += [descriptor.path]
name = getIdentifier(t).name
descriptor = descriptorProvider.getDescriptor(name)
if name != 'GlobalScope':
extras += [descriptor.path]
parentName = descriptor.getParentName()
if parentName:
descriptor = descriptorProvider.getDescriptor(parentName)
@ -2523,7 +2525,8 @@ class CGWrapMethod(CGAbstractMethod):
def __init__(self, descriptor):
assert not descriptor.interface.isCallback()
assert not descriptor.isGlobal()
args = [Argument('*mut JSContext', 'cx'), Argument('GlobalRef', 'scope'),
args = [Argument('*mut JSContext', 'cx'),
Argument('&GlobalScope', 'scope'),
Argument("Box<%s>" % descriptor.concreteType, 'object')]
retval = 'Root<%s>' % descriptor.concreteType
CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args,
@ -5594,6 +5597,7 @@ def generate_imports(config, cgthings, descriptors, callbacks=None, dictionaries
'dom::bindings::weakref::WeakBox',
'dom::bindings::weakref::WeakReferenceable',
'dom::browsingcontext::BrowsingContext',
'dom::globalscope::GlobalScope',
'mem::heap_size_of_raw_self_and_children',
'libc',
'util::prefs::PREFS',

View file

@ -127,7 +127,7 @@ pub unsafe fn throw_dom_exception(cx: *mut JSContext, global: GlobalRef, result:
};
assert!(!JS_IsExceptionPending(cx));
let exception = DOMException::new(global, code);
let exception = DOMException::new(global.as_global_scope(), code);
rooted!(in(cx) let mut thrown = UndefinedValue());
exception.to_jsval(cx, thrown.handle_mut());
JS_SetPendingException(cx, thrown.handle());

View file

@ -11,9 +11,11 @@ use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::conversions::root_from_object;
use dom::bindings::error::{ErrorInfo, report_pending_exception};
use dom::bindings::inheritance::Castable;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflectable, Reflector};
use dom::console::TimerSet;
use dom::globalscope::GlobalScope;
use dom::window;
use dom::workerglobalscope::WorkerGlobalScope;
use ipc_channel::ipc::IpcSender;
@ -55,6 +57,14 @@ pub enum GlobalRoot {
}
impl<'a> GlobalRef<'a> {
/// Returns that `GlobalRef` as a `GlobalScope` referengce.
pub fn as_global_scope(&self) -> &GlobalScope {
match *self {
GlobalRef::Window(window) => window.upcast(),
GlobalRef::Worker(worker) => worker.upcast(),
}
}
/// Get the `JSContext` for the `JSRuntime` associated with the thread
/// this global object is on.
pub fn get_cx(&self) -> *mut JSContext {

View file

@ -10,10 +10,10 @@ use core::nonzero::NonZero;
use dom::bindings::codegen::Bindings::IterableIteratorBinding::IterableKeyAndValueResult;
use dom::bindings::codegen::Bindings::IterableIteratorBinding::IterableKeyOrValueResult;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root};
use dom::bindings::reflector::{Reflector, Reflectable, MutReflectable, reflect_dom_object};
use dom::bindings::trace::JSTraceable;
use dom::globalscope::GlobalScope;
use js::conversions::ToJSValConvertible;
use js::jsapi::{JSContext, JSObject, MutableHandleValue, MutableHandleObject, HandleValue};
use js::jsval::UndefinedValue;
@ -85,7 +85,7 @@ impl<T: Reflectable + JSTraceable + Iterable> IterableIterator<T> {
/// Create a new iterator instance for the provided iterable DOM interface.
pub fn new(iterable: &T,
type_: IteratorType,
wrap: fn(*mut JSContext, GlobalRef, Box<IterableIterator<T>>)
wrap: fn(*mut JSContext, &GlobalScope, Box<IterableIterator<T>>)
-> Root<Self>) -> Root<Self> {
let iterator = box IterableIterator {
reflector: Reflector::new(),
@ -94,7 +94,7 @@ impl<T: Reflectable + JSTraceable + Iterable> IterableIterator<T> {
index: Cell::new(0),
};
let global = iterable.global();
reflect_dom_object(iterator, global.r(), wrap)
reflect_dom_object(iterator, global.r().as_global_scope(), wrap)
}
/// Return the next value from the iterable object.

View file

@ -4,19 +4,25 @@
//! The `Reflector` struct.
use dom::bindings::global::{GlobalRef, GlobalRoot, global_root_from_reflector};
use dom::bindings::conversions::DerivedFrom;
use dom::bindings::global::{GlobalRoot, global_root_from_reflector};
use dom::bindings::js::Root;
use dom::globalscope::GlobalScope;
use js::jsapi::{HandleObject, JSContext, JSObject};
use std::cell::UnsafeCell;
use std::ptr;
/// Create the reflector for a new DOM object and yield ownership to the
/// reflector.
pub fn reflect_dom_object<T: Reflectable>(obj: Box<T>,
global: GlobalRef,
wrap_fn: fn(*mut JSContext, GlobalRef, Box<T>) -> Root<T>)
-> Root<T> {
wrap_fn(global.get_cx(), global, obj)
pub fn reflect_dom_object<T, U>(
obj: Box<T>,
global: &U,
wrap_fn: fn(*mut JSContext, &GlobalScope, Box<T>) -> Root<T>)
-> Root<T>
where T: Reflectable, U: DerivedFrom<GlobalScope>
{
let global_scope = global.upcast();
wrap_fn(global_scope.get_cx(), global_scope, obj)
}
/// A struct to store a reference to the reflector of a DOM object.