mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Return a Temporary from *Binding::Wrap.
Returning a JS<T> is GC-unsafe. This commit also includes some cleanup around Node and Document reflection.
This commit is contained in:
parent
360d5d01d8
commit
439bc78cab
5 changed files with 16 additions and 28 deletions
|
@ -1792,7 +1792,7 @@ class CGWrapMethod(CGAbstractMethod):
|
||||||
else:
|
else:
|
||||||
args = [Argument('*mut JSContext', 'aCx'),
|
args = [Argument('*mut JSContext', 'aCx'),
|
||||||
Argument("Box<%s>" % descriptor.concreteType, 'aObject', mutable=True)]
|
Argument("Box<%s>" % descriptor.concreteType, 'aObject', mutable=True)]
|
||||||
retval = 'JS<%s>' % descriptor.concreteType
|
retval = 'Temporary<%s>' % descriptor.concreteType
|
||||||
CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args, pub=True)
|
CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args, pub=True)
|
||||||
|
|
||||||
def definition_body(self):
|
def definition_body(self):
|
||||||
|
@ -1809,7 +1809,7 @@ assert!(proto.is_not_null());
|
||||||
|
|
||||||
raw.reflector().set_jsobject(obj);
|
raw.reflector().set_jsobject(obj);
|
||||||
|
|
||||||
return raw;""" % CreateBindingJSObject(self.descriptor, "scope"))
|
Temporary::new(raw)""" % CreateBindingJSObject(self.descriptor, "scope"))
|
||||||
else:
|
else:
|
||||||
return CGGeneric("""\
|
return CGGeneric("""\
|
||||||
%s
|
%s
|
||||||
|
@ -1818,7 +1818,8 @@ with_compartment(aCx, obj, || {
|
||||||
JS_SetPrototype(aCx, obj, proto);
|
JS_SetPrototype(aCx, obj, proto);
|
||||||
});
|
});
|
||||||
raw.reflector().set_jsobject(obj);
|
raw.reflector().set_jsobject(obj);
|
||||||
return raw;""" % CreateBindingJSObject(self.descriptor))
|
|
||||||
|
Temporary::new(raw)""" % CreateBindingJSObject(self.descriptor))
|
||||||
|
|
||||||
|
|
||||||
class CGIDLInterface(CGThing):
|
class CGIDLInterface(CGThing):
|
||||||
|
|
|
@ -377,9 +377,9 @@ pub trait Reflectable {
|
||||||
pub fn reflect_dom_object<T: Reflectable>
|
pub fn reflect_dom_object<T: Reflectable>
|
||||||
(obj: Box<T>,
|
(obj: Box<T>,
|
||||||
window: &JSRef<window::Window>,
|
window: &JSRef<window::Window>,
|
||||||
wrap_fn: extern "Rust" fn(*mut JSContext, &JSRef<window::Window>, Box<T>) -> JS<T>)
|
wrap_fn: extern "Rust" fn(*mut JSContext, &JSRef<window::Window>, Box<T>) -> Temporary<T>)
|
||||||
-> Temporary<T> {
|
-> Temporary<T> {
|
||||||
Temporary::new(wrap_fn(window.deref().get_cx(), window, obj))
|
wrap_fn(window.get_cx(), window, obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(raw_pointer_deriving)]
|
#[allow(raw_pointer_deriving)]
|
||||||
|
|
|
@ -47,7 +47,6 @@ use servo_util::namespace::{Namespace, Null};
|
||||||
use servo_util::str::{DOMString, null_str_as_empty_ref};
|
use servo_util::str::{DOMString, null_str_as_empty_ref};
|
||||||
|
|
||||||
use std::collections::hashmap::HashMap;
|
use std::collections::hashmap::HashMap;
|
||||||
use js::jsapi::JSContext;
|
|
||||||
use std::ascii::StrAsciiExt;
|
use std::ascii::StrAsciiExt;
|
||||||
use std::cell::{Cell, RefCell};
|
use std::cell::{Cell, RefCell};
|
||||||
use url::{Url, from_str};
|
use url::{Url, from_str};
|
||||||
|
@ -192,20 +191,6 @@ impl<'a> DocumentHelpers for JSRef<'a, Document> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Document {
|
impl Document {
|
||||||
pub fn reflect_document(document: Box<Document>,
|
|
||||||
window: &JSRef<Window>,
|
|
||||||
wrap_fn: extern "Rust" fn(*mut JSContext, &JSRef<Window>, Box<Document>) -> JS<Document>)
|
|
||||||
-> Temporary<Document> {
|
|
||||||
assert!(document.reflector().get_jsobject().is_null());
|
|
||||||
let raw_doc = reflect_dom_object(document, window, wrap_fn).root();
|
|
||||||
assert!(raw_doc.reflector().get_jsobject().is_not_null());
|
|
||||||
|
|
||||||
let doc_alias = raw_doc.clone();
|
|
||||||
let node: &JSRef<Node> = NodeCast::from_ref(&doc_alias);
|
|
||||||
node.set_owner_doc(&*raw_doc);
|
|
||||||
Temporary::from_rooted(&*raw_doc)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_inherited(window: &JSRef<Window>,
|
pub fn new_inherited(window: &JSRef<Window>,
|
||||||
url: Option<Url>,
|
url: Option<Url>,
|
||||||
is_html_document: IsHTMLDocument,
|
is_html_document: IsHTMLDocument,
|
||||||
|
@ -243,7 +228,12 @@ impl Document {
|
||||||
|
|
||||||
pub fn new(window: &JSRef<Window>, url: Option<Url>, doctype: IsHTMLDocument, content_type: Option<DOMString>) -> Temporary<Document> {
|
pub fn new(window: &JSRef<Window>, url: Option<Url>, doctype: IsHTMLDocument, content_type: Option<DOMString>) -> Temporary<Document> {
|
||||||
let document = Document::new_inherited(window, url, doctype, content_type);
|
let document = Document::new_inherited(window, url, doctype, content_type);
|
||||||
Document::reflect_document(box document, window, DocumentBinding::Wrap)
|
let document = reflect_dom_object(box document, window,
|
||||||
|
DocumentBinding::Wrap).root();
|
||||||
|
|
||||||
|
let node: &JSRef<Node> = NodeCast::from_ref(&*document);
|
||||||
|
node.set_owner_doc(&*document);
|
||||||
|
Temporary::from_rooted(&*document)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -907,13 +907,10 @@ impl Node {
|
||||||
pub fn reflect_node<N: Reflectable+NodeBase>
|
pub fn reflect_node<N: Reflectable+NodeBase>
|
||||||
(node: Box<N>,
|
(node: Box<N>,
|
||||||
document: &JSRef<Document>,
|
document: &JSRef<Document>,
|
||||||
wrap_fn: extern "Rust" fn(*mut JSContext, &JSRef<Window>, Box<N>) -> JS<N>)
|
wrap_fn: extern "Rust" fn(*mut JSContext, &JSRef<Window>, Box<N>) -> Temporary<N>)
|
||||||
-> Temporary<N> {
|
-> Temporary<N> {
|
||||||
assert!(node.reflector().get_jsobject().is_null());
|
let window = document.window.root();
|
||||||
let window = document.deref().window.root();
|
reflect_dom_object(node, &*window, wrap_fn)
|
||||||
let node = reflect_dom_object(node, &window.root_ref(), wrap_fn).root();
|
|
||||||
assert!(node.deref().reflector().get_jsobject().is_not_null());
|
|
||||||
Temporary::from_rooted(&*node)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_inherited(type_id: NodeTypeId, doc: &JSRef<Document>) -> Node {
|
pub fn new_inherited(type_id: NodeTypeId, doc: &JSRef<Document>) -> Node {
|
||||||
|
|
|
@ -388,7 +388,7 @@ impl Window {
|
||||||
script_chan: ScriptChan,
|
script_chan: ScriptChan,
|
||||||
compositor: Box<ScriptListener>,
|
compositor: Box<ScriptListener>,
|
||||||
image_cache_task: ImageCacheTask)
|
image_cache_task: ImageCacheTask)
|
||||||
-> JS<Window> {
|
-> Temporary<Window> {
|
||||||
let win = box Window {
|
let win = box Window {
|
||||||
eventtarget: EventTarget::new_inherited(WindowTypeId),
|
eventtarget: EventTarget::new_inherited(WindowTypeId),
|
||||||
script_chan: script_chan,
|
script_chan: script_chan,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue