> Here it is. > > ~~There's two major things that are unfinished here:~~ > - ~~Dealing with the unroot_must_root lint. I'm not sure about the value of this lint with the new rooting API.~~ Done. > - ~~Updating the Cargo.locks to point to the new SM and SM binding.~~ Done. > > I also included my fixes for the rust update, but these will disappear in a rebase. A rust update is necessary to support calling `Drop` on `Heap<T>` correctly when `Heap<T>` is inside a `Rc<T>`. Otherwise `&self` points to the wrong location. > > Incremental GC is disabled here. I'm not sure how to deal with the incremental barriers so that's left for later. > > Generational GC works. SM doesn't work without it. > > The biggest change here is to the rooting API. `Root` was made movable, and `Temporary` and `JSRef` was removed. Movable `Root`s means there's no need for `Temporary`, and `JSRef`s aren't needed generally since it can be assumed that being able to obtain a reference to a dom object means it's already rooted. References have their lifetime bound to the Roots that provided them. DOM objects that haven't passed through `reflect_dom_object` don't need to be rooted, and DOM objects that have passed through `reflect_dom_object` can't be obtained without being rooted through `native_from_reflector_jsmanaged` or `JS::<T>::root()`. > > Support for `Heap<T>` ended up messier than I expected. It's split into two commits, but only because it's a bit difficult to fold them together. Supporting `Heap<T>` properly requires that that `Heap::<T>::set()` be called on something that won't move. I removed the Copy and Clone trait from `Heap<T>` so `Cell` can't hold `Heap<T>` - only `UnsafeCell` can hold it. > > `CallbackObject` is a bit tricky - I moved all callbacks into `Rc<T>` in order to make sure that the pointer inside to a `*mut JSObject` doesn't move. This is necessary for supporting `Heap<T>`. > > `RootedCollectionSet` is very general purpose now. Anything with `JSTraceable` can be rooted by `RootedCollectionSet`/`RootedTraceable`. Right now, `RootedTraceable` is only used to hold down dom objects before they're fully attached to their reflector. I had to make a custom mechanism to dispatch the trace call - couldn't figure out how to get trait objects working for this case. > > This has been tested with the following zeal settings: > > GC after every allocation > JS_GC_ZEAL=2,1 > > GC after every 100 allocations (important for catching use-after-free bugs) > JS_GC_ZEAL=2,100 > > Verify pre barriers > JS_GC_ZEAL=4,1 > > Verify post barriers > JS_GC_ZEAL=11,1 Source-Repo: https://github.com/servo/servo Source-Revision: e7808c526c348fea5e3b48af70b7f1a066652097
202 lines
8.0 KiB
Rust
202 lines
8.0 KiB
Rust
/* 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 devtools_traits::{CachedConsoleMessage, CachedConsoleMessageTypes, PAGE_ERROR, CONSOLE_API};
|
|
use devtools_traits::{EvaluateJSReply, NodeInfo, Modification, TimelineMarker, TimelineMarkerType};
|
|
use dom::bindings::conversions::FromJSValConvertible;
|
|
use dom::bindings::conversions::StringificationBehavior;
|
|
use dom::bindings::js::Root;
|
|
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
|
|
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
|
use dom::bindings::codegen::Bindings::DOMRectBinding::{DOMRectMethods};
|
|
use dom::bindings::codegen::Bindings::ElementBinding::{ElementMethods};
|
|
use dom::node::{Node, NodeHelpers};
|
|
use dom::window::{WindowHelpers, ScriptHelpers};
|
|
use dom::document::DocumentHelpers;
|
|
use page::{IterablePage, Page};
|
|
use msg::constellation_msg::PipelineId;
|
|
use script_task::{get_page, ScriptTask};
|
|
use js::jsapi::RootedValue;
|
|
use js::jsval::UndefinedValue;
|
|
|
|
use std::sync::mpsc::Sender;
|
|
use std::rc::Rc;
|
|
|
|
|
|
pub fn handle_evaluate_js(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<EvaluateJSReply>){
|
|
let page = get_page(&*page, pipeline);
|
|
let window = page.window();
|
|
let cx = window.r().get_cx();
|
|
let mut rval = RootedValue::new(cx, UndefinedValue());
|
|
window.r().evaluate_js_on_global_with_result(&eval, rval.handle_mut());
|
|
|
|
reply.send(if rval.ptr.is_undefined() {
|
|
EvaluateJSReply::VoidValue
|
|
} else if rval.ptr.is_boolean() {
|
|
EvaluateJSReply::BooleanValue(rval.ptr.to_boolean())
|
|
} else if rval.ptr.is_double() {
|
|
EvaluateJSReply::NumberValue(FromJSValConvertible::from_jsval(cx, rval.handle(), ()).unwrap())
|
|
} else if rval.ptr.is_string() {
|
|
//FIXME: use jsstring_to_str when jsval grows to_jsstring
|
|
EvaluateJSReply::StringValue(
|
|
FromJSValConvertible::from_jsval(cx, rval.handle(), StringificationBehavior::Default).unwrap())
|
|
} else if rval.ptr.is_null() {
|
|
EvaluateJSReply::NullValue
|
|
} else {
|
|
//FIXME: jsvals don't have an is_int32/is_number yet
|
|
assert!(rval.ptr.is_object());
|
|
panic!("object values unimplemented")
|
|
}).unwrap();
|
|
}
|
|
|
|
pub fn handle_get_root_node(page: &Rc<Page>, pipeline: PipelineId, reply: Sender<NodeInfo>) {
|
|
let page = get_page(&*page, pipeline);
|
|
let document = page.document();
|
|
|
|
let node = NodeCast::from_ref(document.r());
|
|
reply.send(node.summarize()).unwrap();
|
|
}
|
|
|
|
pub fn handle_get_document_element(page: &Rc<Page>, pipeline: PipelineId, reply: Sender<NodeInfo>) {
|
|
let page = get_page(&*page, pipeline);
|
|
let document = page.document();
|
|
let document_element = document.r().GetDocumentElement().unwrap();
|
|
|
|
let node = NodeCast::from_ref(document_element.r());
|
|
reply.send(node.summarize()).unwrap();
|
|
}
|
|
|
|
fn find_node_by_unique_id(page: &Rc<Page>, pipeline: PipelineId, node_id: String) -> Root<Node> {
|
|
let page = get_page(&*page, pipeline);
|
|
let document = page.document();
|
|
let node = NodeCast::from_ref(document.r());
|
|
|
|
for candidate in node.traverse_preorder() {
|
|
if candidate.r().get_unique_id() == node_id {
|
|
return candidate;
|
|
}
|
|
}
|
|
|
|
panic!("couldn't find node with unique id {}", node_id)
|
|
}
|
|
|
|
pub fn handle_get_children(page: &Rc<Page>, pipeline: PipelineId, node_id: String, reply: Sender<Vec<NodeInfo>>) {
|
|
let parent = find_node_by_unique_id(&*page, pipeline, node_id);
|
|
let children = parent.r().children().map(|child| {
|
|
child.r().summarize()
|
|
}).collect();
|
|
reply.send(children).unwrap();
|
|
}
|
|
|
|
pub fn handle_get_layout(page: &Rc<Page>, pipeline: PipelineId, node_id: String, reply: Sender<(f32, f32)>) {
|
|
let node = find_node_by_unique_id(&*page, pipeline, node_id);
|
|
let elem = ElementCast::to_ref(node.r()).expect("should be getting layout of element");
|
|
let rect = elem.GetBoundingClientRect();
|
|
let width = *rect.r().Width();
|
|
let height = *rect.r().Height();
|
|
reply.send((width, height)).unwrap();
|
|
}
|
|
|
|
pub fn handle_get_cached_messages(_pipeline_id: PipelineId,
|
|
message_types: CachedConsoleMessageTypes,
|
|
reply: Sender<Vec<CachedConsoleMessage>>) {
|
|
//TODO: check the messageTypes against a global Cache for console messages and page exceptions
|
|
let mut messages = Vec::new();
|
|
if message_types.contains(PAGE_ERROR) {
|
|
//TODO: do for real
|
|
messages.push(CachedConsoleMessage::ConsoleAPI {
|
|
__type__: "consoleAPICall".to_owned(),
|
|
level: "error".to_owned(),
|
|
filename: "http://localhost/~mihai/mozilla/test.html".to_owned(),
|
|
lineNumber: 0,
|
|
functionName: String::new(),
|
|
timeStamp: 0,
|
|
private: false,
|
|
arguments: Vec::new(),
|
|
})
|
|
}
|
|
if message_types.contains(CONSOLE_API) {
|
|
//TODO: make script error reporter pass all reported errors
|
|
// to devtools and cache them for returning here.
|
|
messages.push(CachedConsoleMessage::PageError {
|
|
__type__: "pageError".to_owned(),
|
|
errorMessage: "page error test".to_owned(),
|
|
sourceName: String::new(),
|
|
lineText: String::new(),
|
|
lineNumber: 0,
|
|
columnNumber: 0,
|
|
category: String::new(),
|
|
timeStamp: 0,
|
|
error: false,
|
|
warning: false,
|
|
exception: false,
|
|
strict: false,
|
|
private: false,
|
|
})
|
|
}
|
|
reply.send(messages).unwrap();
|
|
}
|
|
|
|
pub fn handle_modify_attribute(page: &Rc<Page>,
|
|
pipeline: PipelineId,
|
|
node_id: String,
|
|
modifications: Vec<Modification>) {
|
|
let node = find_node_by_unique_id(&*page, pipeline, node_id);
|
|
let elem = ElementCast::to_ref(node.r()).expect("should be getting layout of element");
|
|
|
|
for modification in modifications.iter(){
|
|
match modification.newValue {
|
|
Some(ref string) => {
|
|
let _ = elem.SetAttribute(modification.attributeName.clone(), string.clone());
|
|
},
|
|
None => elem.RemoveAttribute(modification.attributeName.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn handle_wants_live_notifications(page: &Rc<Page>, pipeline_id: PipelineId, send_notifications: bool) {
|
|
let page = get_page(&*page, pipeline_id);
|
|
let window = page.window();
|
|
window.r().set_devtools_wants_updates(send_notifications);
|
|
}
|
|
|
|
pub fn handle_set_timeline_markers(page: &Rc<Page>,
|
|
script_task: &ScriptTask,
|
|
marker_types: Vec<TimelineMarkerType>,
|
|
reply: Sender<TimelineMarker>) {
|
|
for marker_type in &marker_types {
|
|
match *marker_type {
|
|
TimelineMarkerType::Reflow => {
|
|
let window = page.window();
|
|
window.r().set_devtools_timeline_marker(TimelineMarkerType::Reflow, reply.clone());
|
|
}
|
|
TimelineMarkerType::DOMEvent => {
|
|
script_task.set_devtools_timeline_marker(TimelineMarkerType::DOMEvent, reply.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn handle_drop_timeline_markers(page: &Rc<Page>,
|
|
script_task: &ScriptTask,
|
|
marker_types: Vec<TimelineMarkerType>) {
|
|
let window = page.window();
|
|
for marker_type in &marker_types {
|
|
match *marker_type {
|
|
TimelineMarkerType::Reflow => {
|
|
window.r().drop_devtools_timeline_markers();
|
|
}
|
|
TimelineMarkerType::DOMEvent => {
|
|
script_task.drop_devtools_timeline_markers();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn handle_request_animation_frame(page: &Rc<Page>, id: PipelineId, callback: Box<Fn(f64, )>) {
|
|
let page = page.find(id).expect("There is no such page");
|
|
let doc = page.document();
|
|
doc.r().request_animation_frame(callback);
|
|
}
|