Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass around event types as Atoms instead of Strings #8930

Merged
merged 1 commit into from Dec 11, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Pass around event types as Atoms instead of Strings

`Event` internally stores the `type` as an `Atom`, and we're `String`s
everywhere, which can cause unnecessary allocations to occur since
they'll end up as `Atom`s anyways.
  • Loading branch information
frewsxcv committed Dec 11, 2015
commit 4accaf50b21a6344e9ee7518f7ab07c1dde7c36c
@@ -12,6 +12,7 @@ use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::event::{Event, EventBubbles, EventCancelable};
use script_task::ScriptChan;
use string_cache::Atom;
use util::str::DOMString;

#[dom_struct]
@@ -33,7 +34,7 @@ impl CloseEvent {
}

pub fn new(global: GlobalRef,
type_: DOMString,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
wasClean: bool,
@@ -44,9 +45,9 @@ impl CloseEvent {
let ev = reflect_dom_object(event, global, CloseEventBinding::Wrap);
{
let event = ev.upcast::<Event>();
event.InitEvent(type_,
bubbles == EventBubbles::Bubbles,
cancelable == EventCancelable::Cancelable);
event.init_event(type_,
bubbles == EventBubbles::Bubbles,
cancelable == EventCancelable::Cancelable);
}
ev
}
@@ -66,7 +67,7 @@ impl CloseEvent {
EventCancelable::NotCancelable
};
Ok(CloseEvent::new(global,
type_,
Atom::from(&*type_),
bubbles,
cancelable,
init.wasClean,
@@ -13,6 +13,7 @@ use dom::bindings::reflector::reflect_dom_object;
use dom::event::Event;
use js::jsapi::{HandleValue, JSContext};
use js::jsval::JSVal;
use string_cache::Atom;
use util::str::DOMString;

// https://dom.spec.whatwg.org/#interface-customevent
@@ -37,13 +38,13 @@ impl CustomEvent {
CustomEventBinding::Wrap)
}
pub fn new(global: GlobalRef,
type_: DOMString,
type_: Atom,
bubbles: bool,
cancelable: bool,
detail: HandleValue)
-> Root<CustomEvent> {
let ev = CustomEvent::new_uninitialized(global);
ev.InitCustomEvent(global.get_cx(), type_, bubbles, cancelable, detail);
ev.init_custom_event(global.get_cx(), type_, bubbles, cancelable, detail);
ev
}
#[allow(unsafe_code)]
@@ -52,11 +53,26 @@ impl CustomEvent {
init: &CustomEventBinding::CustomEventInit)
-> Fallible<Root<CustomEvent>> {
Ok(CustomEvent::new(global,
type_,
Atom::from(&*type_),
init.parent.bubbles,
init.parent.cancelable,
unsafe { HandleValue::from_marked_location(&init.detail) }))
}

fn init_custom_event(&self,
_cx: *mut JSContext,
type_: Atom,
can_bubble: bool,
cancelable: bool,
detail: HandleValue) {
let event = self.upcast::<Event>();
if event.dispatching() {
return;
}

self.detail.set(detail.get());
event.init_event(type_, can_bubble, cancelable);
}
}

impl CustomEventMethods for CustomEvent {
@@ -72,12 +88,6 @@ impl CustomEventMethods for CustomEvent {
can_bubble: bool,
cancelable: bool,
detail: HandleValue) {
let event = self.upcast::<Event>();
if event.dispatching() {
return;
}

self.detail.set(detail.get());
event.InitEvent(type_, can_bubble, cancelable);
self.init_custom_event(_cx, Atom::from(&*type_), can_bubble, cancelable, detail)
}
}
@@ -524,7 +524,7 @@ impl Document {
self.ready_state.set(state);

let event = Event::new(GlobalRef::Window(&self.window),
DOMString::from("readystatechange"),
atom!("readystatechange"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
let target = self.upcast::<EventTarget>();
@@ -1326,7 +1326,7 @@ impl Document {
update_with_current_time(&self.dom_content_loaded_event_start);

let event = Event::new(GlobalRef::Window(self.window()),
DOMString::from("DOMContentLoaded"),
atom!("DOMContentLoaded"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
let doctarget = self.upcast::<EventTarget>();
@@ -2464,7 +2464,7 @@ impl DocumentProgressHandler {
let document = self.addr.root();
let window = document.window();
let event = Event::new(GlobalRef::Window(window),
DOMString::from("load"),
atom!("load"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
let wintarget = window.upcast::<EventTarget>();
@@ -16,6 +16,7 @@ use dom::event::{Event, EventBubbles, EventCancelable};
use js::jsapi::{RootedValue, HandleValue, JSContext};
use js::jsval::JSVal;
use std::cell::Cell;
use string_cache::Atom;
use util::str::DOMString;

#[dom_struct]
@@ -48,7 +49,7 @@ impl ErrorEvent {
}

pub fn new(global: GlobalRef,
type_: DOMString,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable,
message: DOMString,
@@ -59,8 +60,8 @@ impl ErrorEvent {
let ev = ErrorEvent::new_uninitialized(global);
{
let event = ev.upcast::<Event>();
event.InitEvent(type_, bubbles == EventBubbles::Bubbles,
cancelable == EventCancelable::Cancelable);
event.init_event(type_, bubbles == EventBubbles::Bubbles,
cancelable == EventCancelable::Cancelable);
*ev.message.borrow_mut() = message;
*ev.filename.borrow_mut() = filename;
ev.lineno.set(lineno);
@@ -98,7 +99,7 @@ impl ErrorEvent {
// Dictionaries need to be rooted
// https://github.com/servo/servo/issues/6381
let error = RootedValue::new(global.get_cx(), init.error);
let event = ErrorEvent::new(global, type_,
let event = ErrorEvent::new(global, Atom::from(&*type_),
bubbles, cancelable,
msg, file_name,
line_num, col_num,
@@ -83,11 +83,11 @@ impl Event {
}

pub fn new(global: GlobalRef,
type_: DOMString,
type_: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable) -> Root<Event> {
let event = Event::new_uninitialized(global);
event.InitEvent(type_, bubbles == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable);
event.init_event(type_, bubbles == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable);
event
}

@@ -96,7 +96,23 @@ impl Event {
init: &EventBinding::EventInit) -> Fallible<Root<Event>> {
let bubbles = if init.bubbles { EventBubbles::Bubbles } else { EventBubbles::DoesNotBubble };
let cancelable = if init.cancelable { EventCancelable::Cancelable } else { EventCancelable::NotCancelable };
Ok(Event::new(global, type_, bubbles, cancelable))
Ok(Event::new(global, Atom::from(&*type_), bubbles, cancelable))
}

pub fn init_event(&self, type_: Atom, bubbles: bool, cancelable: bool) {
if self.dispatching.get() {
return;
}

self.initialized.set(true);
self.stop_propagation.set(false);
self.stop_immediate.set(false);
self.canceled.set(false);
self.trusted.set(false);
self.target.set(None);
*self.type_.borrow_mut() = type_;
self.bubbles.set(bubbles);
self.cancelable.set(cancelable);
}

#[inline]
@@ -224,19 +240,7 @@ impl EventMethods for Event {
type_: DOMString,
bubbles: bool,
cancelable: bool) {
if self.dispatching.get() {
return;
}

self.initialized.set(true);
self.stop_propagation.set(false);
self.stop_immediate.set(false);
self.canceled.set(false);
self.trusted.set(false);
self.target.set(None);
*self.type_.borrow_mut() = Atom::from(&*type_);
self.bubbles.set(bubbles);
self.cancelable.set(cancelable);
self.init_event(Atom::from(&*type_), bubbles, cancelable)
}

// https://dom.spec.whatwg.org/#dom-event-istrusted
@@ -27,6 +27,7 @@ use script_task::{CommonScriptMsg, Runnable, ScriptChan, ScriptPort};
use std::cell::Cell;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use string_cache::Atom;
use util::str::DOMString;
use util::task::spawn_named;

@@ -119,10 +120,10 @@ impl FileReader {
let exception = DOMException::new(global.r(), error);
fr.error.set(Some(&exception));

fr.dispatch_progress_event("error".to_owned(), 0, None);
fr.dispatch_progress_event(atom!("error"), 0, None);
return_on_abort!();
// Step 3
fr.dispatch_progress_event("loadend".to_owned(), 0, None);
fr.dispatch_progress_event(atom!("loadend"), 0, None);
return_on_abort!();
// Step 4
fr.terminate_ongoing_reading();
@@ -141,7 +142,7 @@ impl FileReader {
);
return_on_abort!();
//FIXME Step 7 send current progress
fr.dispatch_progress_event("progress".to_owned(), 0, None);
fr.dispatch_progress_event(atom!("progress"), 0, None);
}

// https://w3c.github.io/FileAPI/#dfn-readAsText
@@ -157,7 +158,7 @@ impl FileReader {
);
return_on_abort!();
// Step 6
fr.dispatch_progress_event("loadstart".to_owned(), 0, None);
fr.dispatch_progress_event(atom!("loadstart"), 0, None);
}

// https://w3c.github.io/FileAPI/#dfn-readAsText
@@ -187,11 +188,11 @@ impl FileReader {
*fr.result.borrow_mut() = Some(output);

// Step 8.3
fr.dispatch_progress_event("load".to_owned(), 0, None);
fr.dispatch_progress_event(atom!("load"), 0, None);
return_on_abort!();
// Step 8.4
if fr.ready_state.get() != FileReaderReadyState::Loading {
fr.dispatch_progress_event("loadend".to_owned(), 0, None);
fr.dispatch_progress_event(atom!("loadend"), 0, None);
}
return_on_abort!();
// Step 9
@@ -297,8 +298,8 @@ impl FileReaderMethods for FileReader {

self.terminate_ongoing_reading();
// Steps 5 & 6
self.dispatch_progress_event("abort".to_owned(), 0, None);
self.dispatch_progress_event("loadend".to_owned(), 0, None);
self.dispatch_progress_event(atom!("abort"), 0, None);
self.dispatch_progress_event(atom!("loadend"), 0, None);
}

// https://w3c.github.io/FileAPI/#dfn-error
@@ -319,11 +320,11 @@ impl FileReaderMethods for FileReader {


impl FileReader {
fn dispatch_progress_event(&self, type_: String, loaded: u64, total: Option<u64>) {
fn dispatch_progress_event(&self, type_: Atom, loaded: u64, total: Option<u64>) {

let global = self.global.root();
let progressevent = ProgressEvent::new(global.r(),
DOMString::from(type_), EventBubbles::DoesNotBubble, EventCancelable::NotCancelable,
type_, EventBubbles::DoesNotBubble, EventCancelable::NotCancelable,
total.is_some(), loaded, total.unwrap_or(0));
progressevent.upcast::<Event>().fire(self.upcast());
}
@@ -346,7 +347,7 @@ impl FileReader {
let exception = DOMException::new(global.r(), DOMErrorName::InvalidStateError);
self.error.set(Some(&exception));

self.dispatch_progress_event("error".to_owned(), 0, None);
self.dispatch_progress_event(atom!("error"), 0, None);
return Ok(());
}

@@ -162,7 +162,7 @@ impl HTMLFormElement {
// TODO: Handle browsing contexts
// TODO: Handle validation
let event = Event::new(GlobalRef::Window(win.r()),
DOMString::from("submit"),
atom!("submit"),
EventBubbles::Bubbles,
EventCancelable::Cancelable);
event.fire(self.upcast());
@@ -315,7 +315,7 @@ impl HTMLFormElement {

let win = window_from_node(self);
let event = Event::new(GlobalRef::Window(win.r()),
DOMString::from("reset"),
atom!("reset"),
EventBubbles::Bubbles,
EventCancelable::Cancelable);
event.fire(self.upcast());
@@ -144,7 +144,7 @@ impl HTMLIFrameElement {
let _ar = JSAutoRequest::new(cx);
let _ac = JSAutoCompartment::new(cx, window.reflector().get_jsobject().get());
let mut detail = RootedValue::new(cx, UndefinedValue());
let event_name = DOMString::from(event.name().to_owned());
let event_name = Atom::from(event.name());
self.build_mozbrowser_event_detail(event, cx, detail.handle_mut());
CustomEvent::new(GlobalRef::Window(window.r()),
event_name,
@@ -210,7 +210,7 @@ impl HTMLIFrameElement {
// Step 4
let window = window_from_node(self);
let event = Event::new(GlobalRef::Window(window.r()),
DOMString::from("load".to_owned()),
atom!("load"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(self.upcast());
@@ -78,7 +78,7 @@ impl Runnable for ImageResponseHandlerRunnable {
// Fire image.onload
let window = window_from_node(document.r());
let event = Event::new(GlobalRef::Window(window.r()),
DOMString::from("load"),
atom!("load"),
EventBubbles::DoesNotBubble,
EventCancelable::NotCancelable);
event.fire(element.upcast());
@@ -850,13 +850,13 @@ impl Activatable for HTMLInputElement {
let target = self.upcast();

let event = Event::new(GlobalRef::Window(win.r()),
DOMString::from("input"),
atom!("input"),
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
event.fire(target);

let event = Event::new(GlobalRef::Window(win.r()),
DOMString::from("change"),
atom!("change"),
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
event.fire(target);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.