Skip to content

Commit

Permalink
Migrated -Z trace-layout to serde_json
Browse files Browse the repository at this point in the history
  • Loading branch information
shinglyu committed Nov 7, 2016
1 parent f48b3fe commit 8bea421
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 114 deletions.
3 changes: 2 additions & 1 deletion components/layout/Cargo.toml
Expand Up @@ -31,11 +31,12 @@ parking_lot = {version = "0.3.3", features = ["nightly"]}
plugins = {path = "../plugins"}
profile_traits = {path = "../profile_traits"}
range = {path = "../range"}
rustc-serialize = "0.3"
script_layout_interface = {path = "../script_layout_interface"}
script_traits = {path = "../script_traits"}
selectors = "0.14"
serde = "0.8"
serde_derive = "0.8"
serde_json = "0.8"
servo_atoms = {path = "../atoms"}
smallvec = "0.1"
style = {path = "../style"}
Expand Down
12 changes: 6 additions & 6 deletions components/layout/block.rs
Expand Up @@ -48,10 +48,10 @@ use gfx_traits::print_tree::PrintTree;
use layout_debug;
use model::{CollapsibleMargins, IntrinsicISizes, MarginCollapseInfo, MaybeAuto};
use model::{specified, specified_or_none};
use rustc_serialize::{Encodable, Encoder};
use script_layout_interface::restyle_damage::{BUBBLE_ISIZES, REFLOW, REFLOW_OUT_OF_FLOW};
use script_layout_interface::restyle_damage::REPOSITION;
use sequential;
use serde::{Serialize, Serializer};
use std::cmp::{max, min};
use std::fmt;
use std::sync::Arc;
Expand All @@ -65,7 +65,7 @@ use style::values::computed::LengthOrPercentageOrAuto;
use util::clamp;

/// Information specific to floated blocks.
#[derive(Clone, RustcEncodable)]
#[derive(Clone, Serialize)]
pub struct FloatedBlockInfo {
/// The amount of inline size that is available for the float.
pub containing_inline_size: Au,
Expand Down Expand Up @@ -502,7 +502,7 @@ pub enum FormattingContextType {
}

// A block formatting context.
#[derive(RustcEncodable)]
#[derive(Serialize)]
pub struct BlockFlow {
/// Data common to all flows.
pub base: BaseFlow,
Expand All @@ -526,9 +526,9 @@ bitflags! {
}
}

impl Encodable for BlockFlowFlags {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
self.bits().encode(e)
impl Serialize for BlockFlowFlags {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
self.bits().serialize(serializer)
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/layout/floats.rs
Expand Up @@ -14,7 +14,7 @@ use style::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
use style::values::computed::LengthOrPercentageOrAuto;

/// The kind of float: left or right.
#[derive(Clone, RustcEncodable, Debug, Copy)]
#[derive(Clone, Serialize, Debug, Copy)]
pub enum FloatKind {
Left,
Right
Expand Down
55 changes: 14 additions & 41 deletions components/layout/flow.rs
Expand Up @@ -41,9 +41,9 @@ use inline::InlineFlow;
use model::{CollapsibleMargins, IntrinsicISizes, MarginCollapseInfo};
use multicol::MulticolFlow;
use parallel::FlowParallelInfo;
use rustc_serialize::{Encodable, Encoder};
use script_layout_interface::restyle_damage::{RECONSTRUCT_FLOW, REFLOW, REFLOW_OUT_OF_FLOW};
use script_layout_interface::restyle_damage::{REPAINT, REPOSITION, RestyleDamage};
use serde::{Serialize, Serializer};
use std::{fmt, mem, raw};
use std::iter::Zip;
use std::slice::IterMut;
Expand Down Expand Up @@ -551,7 +551,7 @@ pub trait MutableOwnedFlowUtils {
absolute_descendants: &mut AbsoluteDescendants);
}

#[derive(Copy, Clone, RustcEncodable, PartialEq, Debug)]
#[derive(Copy, Clone, Serialize, PartialEq, Debug)]
pub enum FlowClass {
Block,
Inline,
Expand Down Expand Up @@ -821,7 +821,7 @@ impl EarlyAbsolutePositionInfo {

/// Information needed to compute absolute (i.e. viewport-relative) flow positions (not to be
/// confused with absolutely-positioned flows) that is computed during final position assignment.
#[derive(RustcEncodable, Copy, Clone)]
#[derive(Serialize, Copy, Clone)]
pub struct LateAbsolutePositionInfo {
/// The position of the absolute containing block relative to the nearest ancestor stacking
/// context. If the absolute containing block establishes the stacking context for this flow,
Expand Down Expand Up @@ -978,44 +978,17 @@ impl fmt::Debug for BaseFlow {
}
}

impl Encodable for BaseFlow {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_struct("base", 5, |e| {
try!(e.emit_struct_field("id", 0, |e| self.debug_id().encode(e)));
try!(e.emit_struct_field("stacking_relative_position",
1,
|e| self.stacking_relative_position.encode(e)));
try!(e.emit_struct_field("intrinsic_inline_sizes",
2,
|e| self.intrinsic_inline_sizes.encode(e)));
try!(e.emit_struct_field("position", 3, |e| self.position.encode(e)));
e.emit_struct_field("children", 4, |e| {
e.emit_seq(self.children.len(), |e| {
for (i, c) in self.children.iter().enumerate() {
try!(e.emit_seq_elt(i, |e| {
try!(e.emit_struct("flow", 2, |e| {
try!(e.emit_struct_field("class", 0, |e| c.class().encode(e)));
e.emit_struct_field("data", 1, |e| {
match c.class() {
FlowClass::Block => c.as_block().encode(e),
FlowClass::Inline => c.as_inline().encode(e),
FlowClass::Table => c.as_table().encode(e),
FlowClass::TableWrapper => c.as_table_wrapper().encode(e),
FlowClass::TableRowGroup => c.as_table_rowgroup().encode(e),
FlowClass::TableRow => c.as_table_row().encode(e),
FlowClass::TableCell => c.as_table_cell().encode(e),
_ => { Ok(()) } // TODO: Support captions
}
})
}));
Ok(())
}));
}
Ok(())
})

})
})
impl Serialize for BaseFlow {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
let mut state = try!(serializer.serialize_struct("base", 5));
try!(serializer.serialize_struct_elt(&mut state, "id", self.debug_id()));
try!(serializer.serialize_struct_elt(&mut state, "stacking_relative_position",
&self.stacking_relative_position));
try!(serializer.serialize_struct_elt(&mut state, "intrinsic_inline_sizes",
&self.intrinsic_inline_sizes));
try!(serializer.serialize_struct_elt(&mut state, "position", &self.position));
try!(serializer.serialize_struct_elt(&mut state, "children", &self.children));
serializer.serialize_struct_end(state)
}
}

Expand Down
32 changes: 31 additions & 1 deletion components/layout/flow_list.rs
Expand Up @@ -2,8 +2,11 @@
* 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 flow::Flow;
use flow::{Flow, FlowClass};
use flow_ref::FlowRef;
use serde::{Serialize, Serializer};
use serde_json::{to_value, Value};
use serde_json::builder::ObjectBuilder;
use std::collections::{LinkedList, linked_list};
use std::sync::Arc;

Expand All @@ -20,6 +23,33 @@ pub struct FlowList {
flows: LinkedList<FlowRef>,
}

impl Serialize for FlowList {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
let mut state = try!(serializer.serialize_seq(Some(self.len())));
for f in self.iter() {
let flow_val = ObjectBuilder::new()
.insert("class", f.class())
.insert("data", match f.class() {
FlowClass::Block => to_value(f.as_block()),
FlowClass::Inline => to_value(f.as_inline()),
FlowClass::Table => to_value(f.as_table()),
FlowClass::TableWrapper => to_value(f.as_table_wrapper()),
FlowClass::TableRowGroup => to_value(f.as_table_rowgroup()),
FlowClass::TableRow => to_value(f.as_table_row()),
FlowClass::TableCell => to_value(f.as_table_cell()),
FlowClass::ListItem | FlowClass::TableColGroup | FlowClass::TableCaption |
FlowClass::Multicol | FlowClass::MulticolColumn | FlowClass::Flex => {
Value::Null // Not implemented yet
}
})
.build();

try!(serializer.serialize_seq_elt(&mut state, flow_val));
}
serializer.serialize_seq_end(state)
}
}

pub struct MutFlowListIterator<'a> {
it: linked_list::IterMut<'a, FlowRef>,
}
Expand Down
1 change: 1 addition & 0 deletions components/layout/flow_ref.rs
Expand Up @@ -63,3 +63,4 @@ impl WeakFlowRef {
self.0.upgrade().map(FlowRef)
}
}

31 changes: 15 additions & 16 deletions components/layout/fragment.rs
Expand Up @@ -28,11 +28,11 @@ use msg::constellation_msg::PipelineId;
use net_traits::image::base::{Image, ImageMetadata};
use net_traits::image_cache_thread::{ImageOrMetadataAvailable, UsePlaceholder};
use range::*;
use rustc_serialize::{Encodable, Encoder};
use script_layout_interface::HTMLCanvasData;
use script_layout_interface::SVGSVGData;
use script_layout_interface::restyle_damage::{RECONSTRUCT_FLOW, RestyleDamage};
use script_layout_interface::wrapper_traits::{PseudoElementType, ThreadSafeLayoutElement, ThreadSafeLayoutNode};
use serde::{Serialize, Serializer};
use std::borrow::ToOwned;
use std::cmp::{max, min};
use std::collections::LinkedList;
Expand Down Expand Up @@ -133,13 +133,13 @@ pub struct Fragment {
pub stacking_context_id: StackingContextId,
}

impl Encodable for Fragment {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_struct("fragment", 3, |e| {
try!(e.emit_struct_field("id", 0, |e| self.debug_id.encode(e)));
try!(e.emit_struct_field("border_box", 1, |e| self.border_box.encode(e)));
e.emit_struct_field("margin", 2, |e| self.margin.encode(e))
})
impl Serialize for Fragment {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
let mut state = try!(serializer.serialize_struct("fragment", 3));
try!(serializer.serialize_struct_elt(&mut state, "id", &self.debug_id));
try!(serializer.serialize_struct_elt(&mut state, "border_box", &self.border_box));
try!(serializer.serialize_struct_elt(&mut state, "margin", &self.margin));
serializer.serialize_struct_end(state)
}
}

Expand Down Expand Up @@ -503,7 +503,7 @@ impl ImageFragmentInfo {
absolute_anchor_origin,
image_size);
*tile_spacing = Au(0);
*size = image_size;;
*size = image_size;
return;
}

Expand Down Expand Up @@ -3162,16 +3162,15 @@ impl fmt::Display for DebugId {
}

#[cfg(not(debug_assertions))]
impl Encodable for DebugId {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_str(&format!("{:p}", &self))
impl Serialize for DebugId {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_str(&format!("{:p}", &self))
}
}

#[cfg(debug_assertions)]
impl Encodable for DebugId {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_u16(self.0)
impl Serialize for DebugId {
fn serialize<S: Serializer>(&self, serializer: &mut S) -> Result<(), S::Error> {
serializer.serialize_u16(self.0)
}
}

12 changes: 6 additions & 6 deletions components/layout/inline.rs
Expand Up @@ -65,7 +65,7 @@ use unicode_bidi;
/// with a float or a horizontal wall of the containing block. The block-start
/// inline-start corner of the green zone is the same as that of the line, but
/// the green zone can be taller and wider than the line itself.
#[derive(RustcEncodable, Debug, Clone)]
#[derive(Serialize, Debug, Clone)]
pub struct Line {
/// A range of line indices that describe line breaks.
///
Expand Down Expand Up @@ -207,7 +207,7 @@ impl Line {
}

int_range_index! {
#[derive(RustcEncodable)]
#[derive(Serialize)]
#[doc = "The index of a fragment in a flattened vector of DOM elements."]
struct FragmentIndex(isize)
}
Expand Down Expand Up @@ -791,7 +791,7 @@ impl LineBreaker {
}

/// Represents a list of inline fragments, including element ranges.
#[derive(RustcEncodable, Clone)]
#[derive(Serialize, Clone)]
pub struct InlineFragments {
/// The fragments themselves.
pub fragments: Vec<Fragment>,
Expand Down Expand Up @@ -828,7 +828,7 @@ impl InlineFragments {
}

/// Flows for inline layout.
#[derive(RustcEncodable)]
#[derive(Serialize)]
pub struct InlineFlow {
/// Data common to all flows.
pub base: BaseFlow,
Expand Down Expand Up @@ -1780,7 +1780,7 @@ fn inline_contexts_are_equal(inline_context_a: &Option<InlineFragmentContext>,
///
/// Descent is not included in this structure because it can be computed from the fragment's
/// border/content box and the ascent.
#[derive(Clone, Copy, Debug, RustcEncodable)]
#[derive(Clone, Copy, Debug, Serialize)]
pub struct InlineMetrics {
/// The amount of space above the baseline needed for this fragment.
pub space_above_baseline: Au,
Expand Down Expand Up @@ -1831,7 +1831,7 @@ enum LineFlushMode {
Flush,
}

#[derive(Copy, Clone, Debug, RustcEncodable)]
#[derive(Copy, Clone, Debug, Serialize)]
pub struct LineMetrics {
pub space_above_baseline: Au,
pub space_below_baseline: Au,
Expand Down
22 changes: 11 additions & 11 deletions components/layout/layout_debug.rs
Expand Up @@ -10,7 +10,7 @@

use flow;
use flow_ref::FlowRef;
use rustc_serialize::json;
use serde_json::{to_string, to_value, Value};
use std::borrow::ToOwned;
use std::cell::RefCell;
use std::fs::File;
Expand All @@ -36,20 +36,20 @@ macro_rules! layout_debug_scope(
)
);

#[derive(RustcEncodable)]
#[derive(Serialize)]
struct ScopeData {
name: String,
pre: String,
post: String,
pre: Value,
post: Value,
children: Vec<Box<ScopeData>>,
}

impl ScopeData {
fn new(name: String, pre: String) -> ScopeData {
fn new(name: String, pre: Value) -> ScopeData {
ScopeData {
name: name,
pre: pre,
post: String::new(),
post: Value::Null,
children: vec!(),
}
}
Expand All @@ -67,7 +67,7 @@ impl Scope {
STATE_KEY.with(|ref r| {
match *r.borrow_mut() {
Some(ref mut state) => {
let flow_trace = json::encode(&flow::base(&*state.flow_root)).unwrap();
let flow_trace = to_value(&flow::base(&*state.flow_root));
let data = box ScopeData::new(name.clone(), flow_trace);
state.scope_stack.push(data);
}
Expand All @@ -85,7 +85,7 @@ impl Drop for Scope {
match *r.borrow_mut() {
Some(ref mut state) => {
let mut current_scope = state.scope_stack.pop().unwrap();
current_scope.post = json::encode(&flow::base(&*state.flow_root)).unwrap();
current_scope.post = to_value(&flow::base(&*state.flow_root));
let previous_scope = state.scope_stack.last_mut().unwrap();
previous_scope.children.push(current_scope);
}
Expand All @@ -109,7 +109,7 @@ pub fn begin_trace(flow_root: FlowRef) {
assert!(STATE_KEY.with(|ref r| r.borrow().is_none()));

STATE_KEY.with(|ref r| {
let flow_trace = json::encode(&flow::base(&*flow_root)).unwrap();
let flow_trace = to_value(&flow::base(&*flow_root));
let state = State {
scope_stack: vec![box ScopeData::new("root".to_owned(), flow_trace)],
flow_root: flow_root.clone(),
Expand All @@ -125,9 +125,9 @@ pub fn end_trace(generation: u32) {
let mut thread_state = STATE_KEY.with(|ref r| r.borrow_mut().take().unwrap());
assert!(thread_state.scope_stack.len() == 1);
let mut root_scope = thread_state.scope_stack.pop().unwrap();
root_scope.post = json::encode(&flow::base(&*thread_state.flow_root)).unwrap();
root_scope.post = to_value(&flow::base(&*thread_state.flow_root));

let result = json::encode(&root_scope).unwrap();
let result = to_string(&root_scope).unwrap();
let mut file = File::create(format!("layout_trace-{}.json", generation)).unwrap();
file.write_all(result.as_bytes()).unwrap();
}

0 comments on commit 8bea421

Please sign in to comment.