Make methods storing layout-originating nodes unsafe.

This commit is contained in:
Josh Matthews 2017-05-10 18:20:31 -04:00
parent 2ca80a800f
commit b0bf2b4bad
4 changed files with 39 additions and 22 deletions

View file

@ -826,6 +826,7 @@ impl Document {
} }
} }
#[allow(unsafe_code)]
pub fn handle_mouse_event(&self, pub fn handle_mouse_event(&self,
js_runtime: *mut JSRuntime, js_runtime: *mut JSRuntime,
button: MouseButton, button: MouseButton,
@ -841,7 +842,9 @@ impl Document {
let node = match self.window.hit_test_query(client_point, false) { let node = match self.window.hit_test_query(client_point, false) {
Some(node_address) => { Some(node_address) => {
debug!("node address is {:?}", node_address); debug!("node address is {:?}", node_address);
node::from_untrusted_node_address(js_runtime, node_address) unsafe {
node::from_untrusted_node_address(js_runtime, node_address)
}
}, },
None => return, None => return,
}; };
@ -988,13 +991,16 @@ impl Document {
*self.last_click_info.borrow_mut() = Some((now, click_pos)); *self.last_click_info.borrow_mut() = Some((now, click_pos));
} }
#[allow(unsafe_code)]
pub fn handle_touchpad_pressure_event(&self, pub fn handle_touchpad_pressure_event(&self,
js_runtime: *mut JSRuntime, js_runtime: *mut JSRuntime,
client_point: Point2D<f32>, client_point: Point2D<f32>,
pressure: f32, pressure: f32,
phase_now: TouchpadPressurePhase) { phase_now: TouchpadPressurePhase) {
let node = match self.window.hit_test_query(client_point, false) { let node = match self.window.hit_test_query(client_point, false) {
Some(node_address) => node::from_untrusted_node_address(js_runtime, node_address), Some(node_address) => unsafe {
node::from_untrusted_node_address(js_runtime, node_address)
},
None => return None => return
}; };
@ -1089,6 +1095,7 @@ impl Document {
event.fire(target); event.fire(target);
} }
#[allow(unsafe_code)]
pub fn handle_mouse_move_event(&self, pub fn handle_mouse_move_event(&self,
js_runtime: *mut JSRuntime, js_runtime: *mut JSRuntime,
client_point: Option<Point2D<f32>>, client_point: Option<Point2D<f32>>,
@ -1104,7 +1111,7 @@ impl Document {
}; };
let maybe_new_target = self.window.hit_test_query(client_point, true).and_then(|address| { let maybe_new_target = self.window.hit_test_query(client_point, true).and_then(|address| {
let node = node::from_untrusted_node_address(js_runtime, address); let node = unsafe { node::from_untrusted_node_address(js_runtime, address) };
node.inclusive_ancestors() node.inclusive_ancestors()
.filter_map(Root::downcast::<Element>) .filter_map(Root::downcast::<Element>)
.next() .next()
@ -1186,6 +1193,7 @@ impl Document {
ReflowReason::MouseEvent); ReflowReason::MouseEvent);
} }
#[allow(unsafe_code)]
pub fn handle_touch_event(&self, pub fn handle_touch_event(&self,
js_runtime: *mut JSRuntime, js_runtime: *mut JSRuntime,
event_type: TouchEventType, event_type: TouchEventType,
@ -1202,7 +1210,9 @@ impl Document {
}; };
let node = match self.window.hit_test_query(point, false) { let node = match self.window.hit_test_query(point, false) {
Some(node_address) => node::from_untrusted_node_address(js_runtime, node_address), Some(node_address) => unsafe {
node::from_untrusted_node_address(js_runtime, node_address)
},
None => return TouchEventResult::Processed(false), None => return TouchEventResult::Processed(false),
}; };
let el = match node.downcast::<Element>() { let el = match node.downcast::<Element>() {
@ -3480,7 +3490,9 @@ impl DocumentMethods for Document {
Some(untrusted_node_address) => { Some(untrusted_node_address) => {
let js_runtime = unsafe { JS_GetRuntime(window.get_cx()) }; let js_runtime = unsafe { JS_GetRuntime(window.get_cx()) };
let node = node::from_untrusted_node_address(js_runtime, untrusted_node_address); let node = unsafe {
node::from_untrusted_node_address(js_runtime, untrusted_node_address)
};
let parent_node = node.GetParentNode().unwrap(); let parent_node = node.GetParentNode().unwrap();
let element_ref = node.downcast::<Element>().unwrap_or_else(|| { let element_ref = node.downcast::<Element>().unwrap_or_else(|| {
parent_node.downcast::<Element>().unwrap() parent_node.downcast::<Element>().unwrap()
@ -3515,7 +3527,9 @@ impl DocumentMethods for Document {
// Step 1 and Step 3 // Step 1 and Step 3
let mut elements: Vec<Root<Element>> = self.nodes_from_point(point).iter() let mut elements: Vec<Root<Element>> = self.nodes_from_point(point).iter()
.flat_map(|&untrusted_node_address| { .flat_map(|&untrusted_node_address| {
let node = node::from_untrusted_node_address(js_runtime, untrusted_node_address); let node = unsafe {
node::from_untrusted_node_address(js_runtime, untrusted_node_address)
};
Root::downcast::<Element>(node) Root::downcast::<Element>(node)
}).collect(); }).collect();

View file

@ -927,20 +927,18 @@ fn first_node_not_in<I>(mut nodes: I, not_in: &[NodeOrString]) -> Option<Root<No
/// If the given untrusted node address represents a valid DOM node in the given runtime, /// If the given untrusted node address represents a valid DOM node in the given runtime,
/// returns it. /// returns it.
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub fn from_untrusted_node_address(_runtime: *mut JSRuntime, candidate: UntrustedNodeAddress) pub unsafe fn from_untrusted_node_address(_runtime: *mut JSRuntime, candidate: UntrustedNodeAddress)
-> Root<Node> { -> Root<Node> {
unsafe { // https://github.com/servo/servo/issues/6383
// https://github.com/servo/servo/issues/6383 let candidate: uintptr_t = mem::transmute(candidate.0);
let candidate: uintptr_t = mem::transmute(candidate.0);
// let object: *mut JSObject = jsfriendapi::bindgen::JS_GetAddressableObject(runtime, // let object: *mut JSObject = jsfriendapi::bindgen::JS_GetAddressableObject(runtime,
// candidate); // candidate);
let object: *mut JSObject = mem::transmute(candidate); let object: *mut JSObject = mem::transmute(candidate);
if object.is_null() { if object.is_null() {
panic!("Attempted to create a `JS<Node>` from an invalid pointer!") panic!("Attempted to create a `JS<Node>` from an invalid pointer!")
}
let boxed_node = conversions::private_from_object(object) as *const Node;
Root::from_ref(&*boxed_node)
} }
let boxed_node = conversions::private_from_object(object) as *const Node;
Root::from_ref(&*boxed_node)
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]

View file

@ -1241,7 +1241,7 @@ impl Window {
let id = image.id; let id = image.id;
let js_runtime = self.js_runtime.borrow(); let js_runtime = self.js_runtime.borrow();
let js_runtime = js_runtime.as_ref().unwrap(); let js_runtime = js_runtime.as_ref().unwrap();
let node = from_untrusted_node_address(js_runtime.rt(), image.node); let node = unsafe { from_untrusted_node_address(js_runtime.rt(), image.node) };
if let PendingImageState::Unrequested(ref url) = image.state { if let PendingImageState::Unrequested(ref url) = image.state {
fetch_image_for_layout(url.clone(), &*node, id, self.image_cache.clone()); fetch_image_for_layout(url.clone(), &*node, id, self.image_cache.clone());
@ -1261,7 +1261,9 @@ impl Window {
} }
} }
ScriptThread::note_newly_transitioning_nodes(complete.newly_transitioning_nodes); unsafe {
ScriptThread::note_newly_transitioning_nodes(complete.newly_transitioning_nodes);
}
true true
} }
@ -1457,6 +1459,7 @@ impl Window {
DOMString::from(resolved) DOMString::from(resolved)
} }
#[allow(unsafe_code)]
pub fn offset_parent_query(&self, node: TrustedNodeAddress) -> (Option<Root<Element>>, Rect<Au>) { pub fn offset_parent_query(&self, node: TrustedNodeAddress) -> (Option<Root<Element>>, Rect<Au>) {
if !self.reflow(ReflowGoal::ForScriptQuery, if !self.reflow(ReflowGoal::ForScriptQuery,
ReflowQueryType::OffsetParentQuery(node), ReflowQueryType::OffsetParentQuery(node),
@ -1468,7 +1471,7 @@ impl Window {
let js_runtime = self.js_runtime.borrow(); let js_runtime = self.js_runtime.borrow();
let js_runtime = js_runtime.as_ref().unwrap(); let js_runtime = js_runtime.as_ref().unwrap();
let element = response.node_address.and_then(|parent_node_address| { let element = response.node_address.and_then(|parent_node_address| {
let node = from_untrusted_node_address(js_runtime.rt(), parent_node_address); let node = unsafe { from_untrusted_node_address(js_runtime.rt(), parent_node_address) };
Root::downcast(node) Root::downcast(node)
}); });
(element, response.rect) (element, response.rect)

View file

@ -576,9 +576,9 @@ impl ScriptThreadFactory for ScriptThread {
} }
impl ScriptThread { impl ScriptThread {
pub fn note_newly_transitioning_nodes(nodes: Vec<UntrustedNodeAddress>) { pub unsafe fn note_newly_transitioning_nodes(nodes: Vec<UntrustedNodeAddress>) {
SCRIPT_THREAD_ROOT.with(|root| { SCRIPT_THREAD_ROOT.with(|root| {
let script_thread = unsafe { &*root.get().unwrap() }; let script_thread = &*root.get().unwrap();
let js_runtime = script_thread.js_runtime.rt(); let js_runtime = script_thread.js_runtime.rt();
let new_nodes = nodes let new_nodes = nodes
.into_iter() .into_iter()
@ -1619,7 +1619,9 @@ impl ScriptThread {
/// Handles firing of transition events. /// Handles firing of transition events.
fn handle_transition_event(&self, unsafe_node: UntrustedNodeAddress, name: String, duration: f64) { fn handle_transition_event(&self, unsafe_node: UntrustedNodeAddress, name: String, duration: f64) {
let js_runtime = self.js_runtime.rt(); let js_runtime = self.js_runtime.rt();
let node = from_untrusted_node_address(js_runtime, unsafe_node); let node = unsafe {
from_untrusted_node_address(js_runtime, unsafe_node)
};
let idx = self.transitioning_nodes let idx = self.transitioning_nodes
.borrow() .borrow()