aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.in4
-rw-r--r--src/components/macros/macros.rs68
-rw-r--r--src/components/main/css/matching.rs11
-rw-r--r--src/components/main/layout/box_.rs5
-rw-r--r--src/components/main/layout/context.rs6
-rw-r--r--src/components/main/layout/layout_task.rs8
-rw-r--r--src/components/main/layout/parallel.rs1
-rw-r--r--src/components/style/properties.rs.mako12
-rw-r--r--src/components/style/style.rs12
9 files changed, 87 insertions, 40 deletions
diff --git a/Makefile.in b/Makefile.in
index 4f000c683f4..b51ddf50642 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -261,7 +261,7 @@ DONE_script = $(B)src/components/script/libscript.dummy
DEPS_script = $(CRATE_script) $(SRC_script) $(DONE_SUBMODULES) $(DONE_util) $(DONE_style) $(DONE_net) $(DONE_msg) $(DONE_macros)
-RFLAGS_style = $(strip $(CFG_RUSTC_FLAGS)) $(addprefix -L $(B)src/,$(DEPS_SUBMODULES)) -L $(B)src/components/util
+RFLAGS_style = $(strip $(CFG_RUSTC_FLAGS)) $(addprefix -L $(B)src/,$(DEPS_SUBMODULES)) -L $(B)src/components/util -L$(B)src/components/macros
MAKO_ZIP = $(S)src/components/style/Mako-0.9.1.zip
MAKO_style = $(S)src/components/style/properties.rs
MAKO_SRC_style = $(MAKO_style).mako
@@ -269,7 +269,7 @@ SRC_style = $(call rwildcard,$(S)src/components/style/,*.rs) $(MAKO_style)
CRATE_style = $(S)src/components/style/style.rs
DONE_style = $(B)src/components/style/libstyle.dummy
-DEPS_style = $(CRATE_style) $(SRC_style) $(DONE_SUBMODULES) $(DONE_util)
+DEPS_style = $(CRATE_style) $(SRC_style) $(DONE_SUBMODULES) $(DONE_util) $(DONE_macros)
RFLAGS_servo = $(strip $(CFG_RUSTC_FLAGS)) $(addprefix -L $(B)src/,$(DEPS_SUBMODULES)) -L $(B)src/components/gfx -L $(B)src/components/util -L $(B)src/components/net -L $(B)src/components/script -L $(B)src/components/style -L $(B)src/components/msg -L$(B)src/components/macros
diff --git a/src/components/macros/macros.rs b/src/components/macros/macros.rs
index 9203567075b..76ceb8d1515 100644
--- a/src/components/macros/macros.rs
+++ b/src/components/macros/macros.rs
@@ -9,6 +9,11 @@
#![feature(macro_rules)]
+
+#[cfg(test)]
+extern crate sync;
+
+
#[macro_export]
macro_rules! bitfield(
($bitfieldname:ident, $getter:ident, $setter:ident, $value:expr) => (
@@ -28,3 +33,66 @@ macro_rules! bitfield(
)
)
+
+#[macro_export]
+macro_rules! lazy_init(
+ ($(static ref $N:ident : $T:ty = $e:expr;)*) => (
+ $(
+ #[allow(non_camel_case_types)]
+ struct $N {__unit__: ()}
+ static $N: $N = $N {__unit__: ()};
+ impl Deref<$T> for $N {
+ fn deref<'a>(&'a self) -> &'a $T {
+ unsafe {
+ static mut s: *$T = 0 as *$T;
+ static mut ONCE: ::sync::one::Once = ::sync::one::ONCE_INIT;
+ ONCE.doit(|| {
+ s = ::std::cast::transmute::<~$T, *$T>(~($e));
+ });
+ &*s
+ }
+ }
+ }
+
+ )*
+ )
+)
+
+
+#[cfg(test)]
+mod tests {
+ extern crate collections;
+
+ lazy_init! {
+ static ref NUMBER: uint = times_two(3);
+ static ref VEC: [~uint, ..3] = [~1, ~2, ~3];
+ static ref OWNED_STRING: ~str = ~"hello";
+ static ref HASHMAP: collections::HashMap<uint, &'static str> = {
+ let mut m = collections::HashMap::new();
+ m.insert(0u, "abc");
+ m.insert(1, "def");
+ m.insert(2, "ghi");
+ m
+ };
+ }
+
+ fn times_two(n: uint) -> uint {
+ n * 2
+ }
+
+ #[test]
+ fn test_basic() {
+ assert_eq!(*OWNED_STRING, ~"hello");
+ assert_eq!(*NUMBER, 6);
+ assert!(HASHMAP.find(&1).is_some());
+ assert!(HASHMAP.find(&3).is_none());
+ assert_eq!(VEC.as_slice(), &[~1, ~2, ~3]);
+ }
+
+ #[test]
+ fn test_repeat() {
+ assert_eq!(*NUMBER, 6);
+ assert_eq!(*NUMBER, 6);
+ assert_eq!(*NUMBER, 6);
+ }
+}
diff --git a/src/components/main/css/matching.rs b/src/components/main/css/matching.rs
index 45dd79b9fbc..205fc5c6c0a 100644
--- a/src/components/main/css/matching.rs
+++ b/src/components/main/css/matching.rs
@@ -309,7 +309,6 @@ pub trait MatchMethods {
unsafe fn cascade_node(&self,
parent: Option<LayoutNode>,
- initial_values: &ComputedValues,
applicable_declarations: &ApplicableDeclarations,
applicable_declarations_cache: &mut ApplicableDeclarationsCache);
}
@@ -319,7 +318,6 @@ trait PrivateMatchMethods {
parent_style: Option<&Arc<ComputedValues>>,
applicable_declarations: &[MatchedProperty],
style: &mut Option<Arc<ComputedValues>>,
- initial_values: &ComputedValues,
applicable_declarations_cache: &mut
ApplicableDeclarationsCache,
shareable: bool);
@@ -335,7 +333,6 @@ impl<'ln> PrivateMatchMethods for LayoutNode<'ln> {
parent_style: Option<&Arc<ComputedValues>>,
applicable_declarations: &[MatchedProperty],
style: &mut Option<Arc<ComputedValues>>,
- initial_values: &ComputedValues,
applicable_declarations_cache: &mut
ApplicableDeclarationsCache,
shareable: bool) {
@@ -352,7 +349,6 @@ impl<'ln> PrivateMatchMethods for LayoutNode<'ln> {
let (the_style, is_cacheable) = cascade(applicable_declarations,
shareable,
Some(&***parent_style),
- initial_values,
cached_computed_values);
cacheable = is_cacheable;
this_style = Arc::new(the_style);
@@ -361,7 +357,6 @@ impl<'ln> PrivateMatchMethods for LayoutNode<'ln> {
let (the_style, is_cacheable) = cascade(applicable_declarations,
shareable,
None,
- initial_values,
None);
cacheable = is_cacheable;
this_style = Arc::new(the_style);
@@ -492,9 +487,7 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
}
unsafe {
- let initial_values = &*layout_context.initial_css_values;
self.cascade_node(parent,
- initial_values,
applicable_declarations,
applicable_declarations_cache)
}
@@ -528,7 +521,6 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
unsafe fn cascade_node(&self,
parent: Option<LayoutNode>,
- initial_values: &ComputedValues,
applicable_declarations: &ApplicableDeclarations,
applicable_declarations_cache: &mut ApplicableDeclarationsCache) {
// Get our parent's style. This must be unsafe so that we don't touch the parent's
@@ -559,14 +551,12 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
self.cascade_node_pseudo_element(parent_style,
applicable_declarations.normal.as_slice(),
&mut layout_data.shared_data.style,
- initial_values,
applicable_declarations_cache,
applicable_declarations.normal_shareable);
if applicable_declarations.before.len() > 0 {
self.cascade_node_pseudo_element(parent_style,
applicable_declarations.before.as_slice(),
&mut layout_data.data.before_style,
- initial_values,
applicable_declarations_cache,
false);
}
@@ -574,7 +564,6 @@ impl<'ln> MatchMethods for LayoutNode<'ln> {
self.cascade_node_pseudo_element(parent_style,
applicable_declarations.after.as_slice(),
&mut layout_data.data.after_style,
- initial_values,
applicable_declarations_cache,
false);
}
diff --git a/src/components/main/layout/box_.rs b/src/components/main/layout/box_.rs
index 5b867e06bfc..22d1e2fe0e8 100644
--- a/src/components/main/layout/box_.rs
+++ b/src/components/main/layout/box_.rs
@@ -44,7 +44,7 @@ use std::from_str::FromStr;
use std::iter::AdditiveIterator;
use std::mem;
use std::num::Zero;
-use style::{ComputedValues, TElement, TNode, cascade, initial_values};
+use style::{ComputedValues, TElement, TNode, cascade};
use style::computed_values::{LengthOrPercentageOrAuto, overflow, LPA_Auto, background_attachment};
use style::computed_values::{background_repeat, border_style, clear, position, text_align};
use style::computed_values::{text_decoration, vertical_align, visibility, white_space};
@@ -343,8 +343,7 @@ impl Box {
//
// Anonymous table boxes, TableRowBox and TableCellBox, are generated around `Foo`, but it shouldn't inherit the border.
- let (node_style, _) = cascade(&[], false, Some(&**node.style()),
- &initial_values(), None);
+ let (node_style, _) = cascade(&[], false, Some(&**node.style()), None);
Box {
node: OpaqueNodeMethods::from_thread_safe_layout_node(node),
style: Arc::new(node_style),
diff --git a/src/components/main/layout/context.rs b/src/components/main/layout/context.rs
index 3910de1a3ac..a0f92035bc2 100644
--- a/src/components/main/layout/context.rs
+++ b/src/components/main/layout/context.rs
@@ -24,8 +24,7 @@ use std::ptr;
use std::rt::local::Local;
#[cfg(not(target_os="android"))]
use std::rt::task::Task;
-use style::{ComputedValues, Stylist};
-use sync::Arc;
+use style::Stylist;
use url::Url;
#[cfg(target_os="android")]
@@ -77,9 +76,6 @@ pub struct LayoutContext {
/// FIXME(pcwalton): Make this no longer an unsafe pointer once we have fast `RWArc`s.
pub stylist: *Stylist,
- /// The initial set of CSS properties.
- pub initial_css_values: Arc<ComputedValues>,
-
/// The root node at which we're starting the layout.
pub reflow_root: OpaqueNode,
diff --git a/src/components/main/layout/layout_task.rs b/src/components/main/layout/layout_task.rs
index e21cd7a52c8..91fab27f1c5 100644
--- a/src/components/main/layout/layout_task.rs
+++ b/src/components/main/layout/layout_task.rs
@@ -59,8 +59,7 @@ use std::comm::{channel, Sender, Receiver};
use std::mem;
use std::ptr;
use std::task;
-use style::{AuthorOrigin, ComputedValues, Stylesheet, Stylist};
-use style;
+use style::{AuthorOrigin, Stylesheet, Stylist};
use sync::{Arc, Mutex};
use url::Url;
@@ -98,9 +97,6 @@ pub struct LayoutTask {
pub stylist: ~Stylist,
- /// The initial set of CSS values.
- pub initial_css_values: Arc<ComputedValues>,
-
/// The workers that we use for parallel operation.
pub parallel_traversal: Option<WorkQueue<*mut LayoutContext,PaddedUnsafeFlow>>,
@@ -344,7 +340,6 @@ impl LayoutTask {
display_list: None,
stylist: ~new_stylist(),
- initial_css_values: Arc::new(style::initial_values()),
parallel_traversal: parallel_traversal,
profiler_chan: profiler_chan,
opts: opts.clone(),
@@ -374,7 +369,6 @@ impl LayoutTask {
layout_chan: self.chan.clone(),
font_context_info: font_context_info,
stylist: &*self.stylist,
- initial_css_values: self.initial_css_values.clone(),
url: (*url).clone(),
reflow_root: OpaqueNodeMethods::from_layout_node(reflow_root),
opts: self.opts.clone(),
diff --git a/src/components/main/layout/parallel.rs b/src/components/main/layout/parallel.rs
index 57d6a32deca..0f35c50c119 100644
--- a/src/components/main/layout/parallel.rs
+++ b/src/components/main/layout/parallel.rs
@@ -274,7 +274,6 @@ fn recalc_style_for_node(unsafe_layout_node: UnsafeLayoutNode,
// Perform the CSS cascade.
node.cascade_node(parent_opt,
- &*layout_context.initial_css_values,
&applicable_declarations,
layout_context.applicable_declarations_cache());
diff --git a/src/components/style/properties.rs.mako b/src/components/style/properties.rs.mako
index 62cce5f1ef4..682a8d025be 100644
--- a/src/components/style/properties.rs.mako
+++ b/src/components/style/properties.rs.mako
@@ -1593,9 +1593,9 @@ impl ComputedValues {
}
}
-/// Returns the initial values for all style structs as defined by the specification.
-pub fn initial_values() -> ComputedValues {
- ComputedValues {
+/// The initial values for all style structs as defined by the specification.
+lazy_init! {
+ static ref INITIAL_VALUES: ComputedValues = ComputedValues {
% for style_struct in STYLE_STRUCTS:
${style_struct.name}: CowArc::new(style_structs::${style_struct.name} {
% for longhand in style_struct.longhands:
@@ -1604,7 +1604,7 @@ pub fn initial_values() -> ComputedValues {
}),
% endfor
shareable: true,
- }
+ };
}
/// Fast path for the function below. Only computes new inherited styles.
@@ -1702,8 +1702,6 @@ fn cascade_with_cached_declarations(applicable_declarations: &[MatchedProperty],
///
/// * `parent_style`: The parent style, if applicable; if `None`, this is the root node.
///
-/// * `initial_values`: The initial set of CSS values as defined by the specification.
-///
/// * `cached_style`: If present, cascading is short-circuited for everything but inherited
/// values and these values are used instead. Obviously, you must be careful when supplying
/// this that it is safe to only provide inherited declarations. If `parent_style` is `None`,
@@ -1713,9 +1711,9 @@ fn cascade_with_cached_declarations(applicable_declarations: &[MatchedProperty],
pub fn cascade(applicable_declarations: &[MatchedProperty],
shareable: bool,
parent_style: Option< &ComputedValues >,
- initial_values: &ComputedValues,
cached_style: Option< &ComputedValues >)
-> (ComputedValues, bool) {
+ let initial_values = &*INITIAL_VALUES;
let (is_root_element, inherited_style) = match parent_style {
Some(parent_style) => (false, parent_style),
None => (true, initial_values),
diff --git a/src/components/style/style.rs b/src/components/style/style.rs
index 1bc572e2891..47d34dae930 100644
--- a/src/components/style/style.rs
+++ b/src/components/style/style.rs
@@ -15,15 +15,19 @@
#![feature(phase)]
#[phase(syntax, link)] extern crate log;
-extern crate cssparser;
extern crate collections;
-extern crate encoding;
extern crate num;
extern crate serialize;
-extern crate servo_util = "util";
extern crate sync;
extern crate url;
+extern crate cssparser;
+extern crate encoding;
+
+#[phase(syntax)]
+extern crate servo_macros = "macros";
+extern crate servo_util = "util";
+
// Public API
pub use stylesheets::{Stylesheet, CSSRule, StyleRule};
@@ -31,7 +35,7 @@ pub use selector_matching::{Stylist, StylesheetOrigin, UserAgentOrigin, AuthorOr
pub use selector_matching::{MatchedProperty};
pub use properties::{cascade, PropertyDeclaration, ComputedValues, computed_values, style_structs};
pub use properties::{PropertyDeclarationBlock, parse_style_attribute}; // Style attributes
-pub use properties::{initial_values, CSSFloat, DeclaredValue, PropertyDeclarationParseResult};
+pub use properties::{CSSFloat, DeclaredValue, PropertyDeclarationParseResult};
pub use properties::longhands;
pub use errors::with_errors_silenced;
pub use node::{TElement, TNode};