Add unrooted_must_root lint for enums and structs containing JS<T>, as well as functions with JS<T> in their parameter list

For safe wrappers over JS<T> (eg Temporary<T>) use #[allow(unrooted_must_root)].
For all other types containing a #[must_root] value, annotate the type with #[must_root] to ensure that it is never used unrooted
This commit is contained in:
Manish Goregaokar 2014-09-16 01:28:36 +05:30
parent 13ae369dec
commit 12dc54d238
22 changed files with 101 additions and 4 deletions

View file

@ -71,6 +71,7 @@ impl Str for AttrValue {
}
#[deriving(Encodable)]
#[must_root]
pub struct Attr {
reflector_: Reflector,
local_name: Atom,

View file

@ -2868,7 +2868,10 @@ class CGUnionStruct(CGThing):
enumConversions = [
" e%s(ref inner) => inner.to_jsval(cx)," % v["name"] for v in templateVars
]
return ("""pub enum %s {
# XXXManishearth The following should be #[must_root],
# however we currently allow it till #2661 is fixed
return ("""#[allow(unrooted_must_root)]
pub enum %s {
%s
}

View file

@ -34,6 +34,7 @@ pub enum GlobalRoot<'a, 'b> {
/// A traced reference to a global object, for use in fields of traced Rust
/// structures.
#[deriving(Encodable)]
#[must_root]
pub enum GlobalField {
WindowField(JS<Window>),
WorkerField(JS<WorkerGlobalScope>),

View file

@ -61,6 +61,7 @@ use std::mem;
/// Importantly, it requires explicit rooting in order to interact with the inner value.
/// Can be assigned into JS-owned member fields (i.e. `JS<T>` types) safely via the
/// `JS<T>::assign` method or `OptionalSettable::assign` (for `Option<JS<T>>` fields).
#[allow(unrooted_must_root)]
pub struct Temporary<T> {
inner: JS<T>,
/// On-stack JS pointer to assuage conservative stack scanner
@ -106,6 +107,7 @@ impl<T: Reflectable> Temporary<T> {
}
/// A rooted, JS-owned value. Must only be used as a field in other JS-owned types.
#[must_root]
pub struct JS<T> {
ptr: *const T
}

View file

@ -67,6 +67,7 @@ impl BrowserContext {
}
#[deriving(Encodable)]
#[must_root]
pub struct SessionHistoryEntry {
document: JS<Document>,
children: Vec<BrowserContext>

View file

@ -17,6 +17,7 @@ use geom::size::Size2D;
use canvas::canvas_render_task::{CanvasMsg, CanvasRenderTask, ClearRect, Close, FillRect, Recreate, StrokeRect};
#[deriving(Encodable)]
#[must_root]
pub struct CanvasRenderingContext2D {
reflector_: Reflector,
global: GlobalField,

View file

@ -69,6 +69,7 @@ pub enum IsHTMLDocument {
}
#[deriving(Encodable)]
#[must_root]
pub struct Document {
pub node: Node,
reflector_: Reflector,

View file

@ -23,6 +23,7 @@ use dom::text::Text;
use servo_util::str::DOMString;
#[deriving(Encodable)]
#[must_root]
pub struct DOMImplementation {
document: JS<Document>,
reflector_: Reflector,

View file

@ -14,6 +14,7 @@ use dom::window::Window;
use servo_util::str::DOMString;
#[deriving(Encodable)]
#[must_root]
pub struct DOMParser {
window: JS<Window>, //XXXjdm Document instead?
reflector_: Reflector

View file

@ -11,6 +11,7 @@ use dom::domrect::DOMRect;
use dom::window::Window;
#[deriving(Encodable)]
#[must_root]
pub struct DOMRectList {
reflector_: Reflector,
rects: Vec<JS<DOMRect>>,

View file

@ -17,6 +17,7 @@ use servo_util::namespace::Null;
use servo_util::str::{DOMString, HTML_SPACE_CHARACTERS};
#[deriving(Encodable)]
#[must_root]
pub struct DOMTokenList {
reflector_: Reflector,
element: JS<Element>,

View file

@ -19,12 +19,14 @@ use std::cell::RefCell;
use std::collections::hashmap::HashMap;
#[deriving(Encodable, Clone)]
#[must_root]
pub enum FormDatum {
StringData(DOMString),
FileData(JS<File>)
}
#[deriving(Encodable)]
#[must_root]
pub struct FormData {
data: Traceable<RefCell<HashMap<DOMString, Vec<FormDatum>>>>,
reflector_: Reflector,

View file

@ -30,12 +30,14 @@ impl<S: Encoder<E>, E> Encodable<S, E> for Box<CollectionFilter> {
}
#[deriving(Encodable)]
#[must_root]
pub enum CollectionTypeId {
Static(Vec<JS<Element>>),
Live(JS<Node>, Box<CollectionFilter>)
}
#[deriving(Encodable)]
#[must_root]
pub struct HTMLCollection {
collection: CollectionTypeId,
reflector_: Reflector,

View file

@ -12,6 +12,7 @@ use dom::element::Element;
use dom::window::Window;
#[deriving(Encodable)]
#[must_root]
pub struct NamedNodeMap {
reflector_: Reflector,
owner: JS<Element>,

View file

@ -850,6 +850,7 @@ impl<'a> Iterator<JSRef<'a, Node>> for TreeIterator<'a> {
}
}
#[must_root]
pub struct NodeIterator {
pub start_node: JS<Node>,
pub current_node: Option<JS<Node>>,

View file

@ -11,18 +11,21 @@ use dom::node::{Node, NodeHelpers};
use dom::window::Window;
#[deriving(Encodable)]
#[must_root]
pub enum NodeListType {
Simple(Vec<JS<Node>>),
Children(JS<Node>)
}
#[deriving(Encodable)]
#[must_root]
pub struct NodeList {
list_type: NodeListType,
reflector_: Reflector,
}
impl NodeList {
#[allow(unrooted_must_root)]
pub fn new_inherited(list_type: NodeListType) -> NodeList {
NodeList {
list_type: list_type,
@ -30,6 +33,7 @@ impl NodeList {
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &JSRef<Window>,
list_type: NodeListType) -> Temporary<NodeList> {
reflect_dom_object(box NodeList::new_inherited(list_type),

View file

@ -14,6 +14,7 @@ use time;
pub type DOMHighResTimeStamp = f64;
#[deriving(Encodable)]
#[must_root]
pub struct Performance {
reflector_: Reflector,
timing: JS<PerformanceTiming>,

View file

@ -19,6 +19,7 @@ use js::jsapi::JSContext;
use js::jsval::{JSVal, NullValue};
#[deriving(Encodable)]
#[must_root]
pub struct TestBinding {
reflector: Reflector,
global: GlobalField,

View file

@ -30,6 +30,7 @@ use std::ptr;
pub struct TrustedWorkerAddress(pub *const c_void);
#[deriving(Encodable)]
#[must_root]
pub struct Worker {
eventtarget: EventTarget,
refcount: Cell<uint>,

View file

@ -98,6 +98,7 @@ enum SyncOrAsync<'a, 'b> {
#[deriving(Encodable)]
#[must_root]
pub struct XMLHttpRequest {
eventtarget: XMLHttpRequestEventTarget,
ready_state: Traceable<Cell<XMLHttpRequestState>>,