Make LOCAL_CONTEXT_KEY safe and non-leaky.

`LOCAL_CONTEXT_KEY` is currently a `Cell<*mut LocalLayoutContext>`. The use
of the raw pointer means that the `LocalLayoutContext` is not dropped when
the thread dies; this leaks FreeType instances and probably other
things. There are also some unsafe getter functions in `LayoutContext`
(`font_context`, `applicable_declarations_cache` and
`style_sharing_candidate_cache`) that @eddyb says involve undefined
behaviour.

This changeset changes `LOCAL_CONTEXT_KEY` to
`RefCell<Option<Rc<LocalLayoutContext>>>`. This fixes the leak and also
results in safe getters.

(Fixes #6282.)
This commit is contained in:
Nicholas Nethercote 2015-06-04 20:08:19 -07:00
parent 2ca606aaba
commit 9b4d39d6d1
7 changed files with 38 additions and 48 deletions

View file

@ -856,7 +856,7 @@ impl Fragment {
self.border_box.size,
SpecificFragmentInfo::UnscannedText(UnscannedTextFragmentInfo::from_text(
"".to_owned()))));
let ellipsis_fragments = TextRunScanner::new().scan_for_runs(layout_context.font_context(),
let ellipsis_fragments = TextRunScanner::new().scan_for_runs(&mut layout_context.font_context(),
unscanned_ellipsis_fragments);
debug_assert!(ellipsis_fragments.len() == 1);
ellipsis_fragments.fragments.into_iter().next().unwrap()
@ -1005,7 +1005,7 @@ impl Fragment {
pub fn calculate_line_height(&self, layout_context: &LayoutContext) -> Au {
let font_style = self.style.get_font_arc();
let font_metrics = text::font_metrics_for_style(layout_context.font_context(), font_style);
let font_metrics = text::font_metrics_for_style(&mut layout_context.font_context(), font_style);
text::line_height_from_style(&*self.style, &font_metrics)
}
@ -1819,7 +1819,7 @@ impl Fragment {
// See CSS 2.1 § 10.8.1.
let block_flow = info.flow_ref.as_immutable_block();
let font_style = self.style.get_font_arc();
let font_metrics = text::font_metrics_for_style(layout_context.font_context(),
let font_metrics = text::font_metrics_for_style(&mut layout_context.font_context(),
font_style);
InlineMetrics::from_block_height(&font_metrics,
block_flow.base.position.size.block,