Skip to content

Commit

Permalink
Move timer firing implemention into Window #1992
Browse files Browse the repository at this point in the history
  • Loading branch information
ebalint committed Jul 16, 2014
1 parent e8996d5 commit 0b5a1b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
27 changes: 27 additions & 0 deletions src/components/script/dom/window.rs
Expand Up @@ -26,9 +26,12 @@ use servo_util::str::DOMString;
use servo_util::task::{spawn_named};
use servo_util::url::parse_url;

use js::jsapi::JS_CallFunctionValue;
use js::jsapi::JSContext;
use js::jsapi::{JS_GC, JS_GetRuntime};
use js::jsval::JSVal;
use js::jsval::NullValue;
use js::rust::with_compartment;

use std::collections::hashmap::HashMap;
use std::cell::{Cell, RefCell};
Expand All @@ -37,6 +40,7 @@ use std::comm::{channel, Sender};
use std::comm::Select;
use std::hash::{Hash, sip};
use std::io::timer::Timer;
use std::ptr;
use std::rc::Rc;

use time;
Expand Down Expand Up @@ -284,6 +288,7 @@ pub trait WindowHelpers {
fn wait_until_safe_to_modify_dom(&self);
fn init_browser_context(&self, doc: &JSRef<Document>);
fn load_url(&self, href: DOMString);
fn handle_fire_timer(&self, timer_id: TimerId, cx: *mut JSContext);
}

trait PrivateWindowHelpers {
Expand Down Expand Up @@ -321,6 +326,28 @@ impl<'a> WindowHelpers for JSRef<'a, Window> {
script_chan.send(TriggerLoadMsg(self.page.id, url));
}
}

fn handle_fire_timer(&self, timer_id: TimerId, cx: *mut JSContext) {
let this_value = self.reflector().get_jsobject();

let data = match self.active_timers.deref().borrow().find(&timer_id) {
None => return,
Some(timer_handle) => timer_handle.data,
};

// TODO: Support extra arguments. This requires passing a `*JSVal` array as `argv`.
with_compartment(cx, this_value, || {
let mut rval = NullValue();
unsafe {
JS_CallFunctionValue(cx, this_value, *data.funval,
0, ptr::mut_null(), &mut rval);
}
});

if !data.is_interval {
self.active_timers.deref().borrow_mut().remove(&timer_id);
}
}
}

impl<'a> PrivateWindowHelpers for JSRef<'a, Window> {
Expand Down
25 changes: 1 addition & 24 deletions src/components/script/script_task.rs
Expand Up @@ -32,10 +32,8 @@ use layout_interface;
use page::{Page, IterablePage, Frame};

use geom::point::Point2D;
use js::jsapi::JS_CallFunctionValue;
use js::jsapi::{JS_SetWrapObjectCallbacks, JS_SetGCZeal, JS_DEFAULT_ZEAL_FREQ, JS_GC};
use js::jsapi::{JSContext, JSRuntime};
use js::jsval::NullValue;
use js::rust::{Cx, RtUtils};
use js::rust::with_compartment;
use js;
Expand All @@ -51,7 +49,6 @@ use servo_util::task::send_on_failure;
use std::cell::RefCell;
use std::comm::{channel, Sender, Receiver};
use std::mem::replace;
use std::ptr;
use std::rc::Rc;
use std::task::TaskBuilder;
use url::Url;
Expand Down Expand Up @@ -416,27 +413,7 @@ impl ScriptTask {
pipeline ID not associated with this script task. This is a bug.");
let frame = page.frame();
let window = frame.get_ref().window.root();

let this_value = window.deref().reflector().get_jsobject();

let data = match window.deref().active_timers.deref().borrow().find(&timer_id) {
None => return,
Some(timer_handle) => timer_handle.data,
};

// TODO: Support extra arguments. This requires passing a `*JSVal` array as `argv`.
let cx = self.get_cx();
with_compartment(cx, this_value, || {
let mut rval = NullValue();
unsafe {
JS_CallFunctionValue(cx, this_value, *data.funval,
0, ptr::mut_null(), &mut rval);
}
});

if !data.is_interval {
window.deref().active_timers.deref().borrow_mut().remove(&timer_id);
}
window.handle_fire_timer(timer_id, self.get_cx());
}

/// Handles a notification that reflow completed.
Expand Down

0 comments on commit 0b5a1b2

Please sign in to comment.