Skip to content

Commit

Permalink
moving to oncecell
Browse files Browse the repository at this point in the history
adding oncecell for JS references

removing option<JS<T>> to <JS<T>>

changing return types

removing get method and refactoring the function

changing getElements method

lint fixes

moving to default

simplifying return

ordering

Removing elements

linting
  • Loading branch information
sendilkumarn committed Sep 9, 2017
1 parent 2719e65 commit 8b33e0f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions components/script/Cargo.toml
Expand Up @@ -56,6 +56,7 @@ lazy_static = "0.2"
libc = "0.2"
log = "0.3.5"
metrics = {path = "../metrics"}
mitochondria = "1.1.2"
mime = "0.2.1"
mime_guess = "1.8.0"
msg = {path = "../msg"}
Expand Down
50 changes: 50 additions & 0 deletions components/script/dom/bindings/js.rs
Expand Up @@ -32,6 +32,7 @@ use dom::bindings::trace::trace_reflector;
use dom::node::Node;
use heapsize::HeapSizeOf;
use js::jsapi::{JSObject, JSTracer};
use mitochondria::OnceCell;
use script_layout_interface::TrustedNodeAddress;
use script_thread::STACK_ROOTS;
use std::cell::UnsafeCell;
Expand Down Expand Up @@ -391,6 +392,55 @@ impl<T: DomObject> HeapSizeOf for MutNullableJS<T> {
}
}

/// A holder that allows to lazily initialize the value only once
/// `JS<T>`, using OnceCell
/// Essentially a `OnceCell<JS<T>>`.
///
/// This should only be used as a field in other DOM objects; see warning
/// on `JS<T>`.
#[must_root]
pub struct OnceCellJS<T: DomObject> {
ptr: OnceCell<JS<T>>,
}

impl<T: DomObject> OnceCellJS<T> {
/// Retrieve a copy of the current inner value. If it is `None`, it is
/// initialized with the result of `cb` first.
#[allow(unrooted_must_root)]
pub fn init_once<F>(&self, cb: F) -> &T
where F: FnOnce() -> Root<T>
{
debug_assert!(thread_state::get().is_script());
&self.ptr.init_once(|| JS::from_ref(&cb()))
}
}

impl<T: DomObject> Default for OnceCellJS<T> {
#[allow(unrooted_must_root)]
fn default() -> OnceCellJS<T> {
debug_assert!(thread_state::get().is_script());
OnceCellJS {
ptr: OnceCell::new(),
}
}
}

impl<T: DomObject> HeapSizeOf for OnceCellJS<T> {
fn heap_size_of_children(&self) -> usize {
// See comment on HeapSizeOf for JS<T>.
0
}
}

#[allow(unrooted_must_root)]
unsafe impl<T: DomObject> JSTraceable for OnceCellJS<T> {
unsafe fn trace(&self, trc: *mut JSTracer) {
if let Some(ptr) = self.ptr.as_ref() {
ptr.trace(trc);
}
}
}

impl<T: DomObject> LayoutJS<T> {
/// Returns an unsafe pointer to the interior of this JS object. This is
/// the only method that be safely accessed from layout. (The fact that
Expand Down
18 changes: 7 additions & 11 deletions components/script/dom/htmlformelement.rs
Expand Up @@ -13,7 +13,7 @@ use dom::bindings::codegen::Bindings::HTMLFormElementBinding::HTMLFormElementMet
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use dom::bindings::inheritance::{Castable, ElementTypeId, HTMLElementTypeId, NodeTypeId};
use dom::bindings::js::{JS, MutNullableJS, Root, RootedReference};
use dom::bindings::js::{JS, OnceCellJS, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::bindings::reflector::DomObject;
use dom::bindings::str::DOMString;
Expand Down Expand Up @@ -66,7 +66,7 @@ pub struct GenerationId(u32);
pub struct HTMLFormElement {
htmlelement: HTMLElement,
marked_for_reset: Cell<bool>,
elements: MutNullableJS<HTMLFormControlsCollection>,
elements: OnceCellJS<HTMLFormControlsCollection>,
generation_id: Cell<GenerationId>,
controls: DOMRefCell<Vec<JS<Element>>>,
}
Expand Down Expand Up @@ -168,10 +168,6 @@ impl HTMLFormElementMethods for HTMLFormElement {

// https://html.spec.whatwg.org/multipage/#dom-form-elements
fn Elements(&self) -> Root<HTMLFormControlsCollection> {
if let Some(elements) = self.elements.get() {
return elements;
}

#[derive(HeapSizeOf, JSTraceable)]
struct ElementsFilter {
form: Root<HTMLFormElement>
Expand Down Expand Up @@ -222,11 +218,11 @@ impl HTMLFormElementMethods for HTMLFormElement {
}
}
}
let filter = box ElementsFilter { form: Root::from_ref(self) };
let window = window_from_node(self);
let elements = HTMLFormControlsCollection::new(&window, self.upcast(), filter);
self.elements.set(Some(&elements));
elements
Root::from_ref(self.elements.init_once(|| {
let filter = box ElementsFilter { form: Root::from_ref(self) };
let window = window_from_node(self);
HTMLFormControlsCollection::new(&window, self.upcast(), filter)
}))
}

// https://html.spec.whatwg.org/multipage/#dom-form-length
Expand Down
1 change: 1 addition & 0 deletions components/script/lib.rs
Expand Up @@ -68,6 +68,7 @@ extern crate metrics;
#[macro_use]
extern crate mime;
extern crate mime_guess;
extern crate mitochondria;
extern crate msg;
extern crate net_traits;
extern crate num_traits;
Expand Down

0 comments on commit 8b33e0f

Please sign in to comment.