diff --git a/src/servo/content/content_task.rs b/src/servo/content/content_task.rs index b96f6c52ad7..67019edc29b 100644 --- a/src/servo/content/content_task.rs +++ b/src/servo/content/content_task.rs @@ -51,7 +51,7 @@ use ptr::null; enum ControlMsg { ParseMsg(Url), ExecuteMsg(Url), - Timer(~dom::bindings::window::TimerData), + Timer(~dom::base::TimerData), ExitMsg } diff --git a/src/servo/dom/bindings/window.rs b/src/servo/dom/bindings/window.rs index aeec2b670a9..edb81aa418a 100644 --- a/src/servo/dom/bindings/window.rs +++ b/src/servo/dom/bindings/window.rs @@ -23,49 +23,23 @@ extern fn alert(cx: *JSContext, argc: c_uint, vp: *jsval) -> JSBool { assert (argc == 1); // Abstract this pattern and use it in debug, too? let jsstr = JS_ValueToString(cx, *ptr::offset(argv, 0)); - // Right now, just print to the console - io::println(#fmt("ALERT: %s", jsval_to_rust_str(cx, jsstr))); + + (*unwrap(JS_THIS_OBJECT(cx, vp))).payload.alert(jsval_to_rust_str(cx, jsstr)); + JS_SET_RVAL(cx, vp, JSVAL_NULL); } 1_i32 } -// Holder for the various JS values associated with setTimeout -// (ie. function value to invoke and all arguments to pass -// to the function when calling it) -struct TimerData { - funval: jsval, - args: DVec, -} - -fn TimerData(argc: c_uint, argv: *jsval) -> TimerData unsafe { - let data = TimerData { - funval : *argv, - args : DVec(), - }; - - let mut i = 2; - while i < argc as uint { - data.args.push(*ptr::offset(argv, i)); - i += 1; - }; - - data -} - - extern fn setTimeout(cx: *JSContext, argc: c_uint, vp: *jsval) -> JSBool unsafe { let argv = JS_ARGV(cx, vp); assert (argc >= 2); //TODO: don't crash when passed a non-integer value for the timeout - // Post a delayed message to the per-window timer task; it will dispatch it - // to the relevant content handler that will deal with it. - std::timer::delayed_send(std::uv_global_loop::get(), - RUST_JSVAL_TO_INT(*ptr::offset(argv, 1)) as uint, - (*unwrap(JS_THIS_OBJECT(cx, vp))).payload.timer_chan, - TimerMessage_Fire(~TimerData(argc, argv))); + (*unwrap(JS_THIS_OBJECT(cx, vp))).payload.setTimeout( + RUST_JSVAL_TO_INT(*ptr::offset(argv, 1)) as int, + argc, argv); JS_SET_RVAL(cx, vp, JSVAL_NULL); return 1; diff --git a/src/servo/dom/node.rs b/src/servo/dom/node.rs index b538b99edbe..0569213a611 100644 --- a/src/servo/dom/node.rs +++ b/src/servo/dom/node.rs @@ -23,7 +23,6 @@ enum NodeData = { kind: ~NodeKind, }; - /* The tree holding Nodes (read-only) */ enum NodeTree { NodeTree } @@ -176,4 +175,4 @@ impl NodeScope : tree::WriteMethods { fn with_tree_fields(node: Node, f: fn(tree::Tree) -> R) -> R { self.write(node, |n| f(n.tree)) } -} \ No newline at end of file +} diff --git a/src/servo/dom/window.rs b/src/servo/dom/window.rs index b98f1b5dff8..ac37628af9b 100644 --- a/src/servo/dom/window.rs +++ b/src/servo/dom/window.rs @@ -2,7 +2,7 @@ use comm::{Port, Chan}; use content::content_task::{ControlMsg, Timer}; enum TimerControlMsg { - TimerMessage_Fire(~dom::bindings::window::TimerData), + TimerMessage_Fire(~TimerData), TimerMessage_Close } @@ -14,6 +14,46 @@ struct Window { } } +// Holder for the various JS values associated with setTimeout +// (ie. function value to invoke and all arguments to pass +// to the function when calling it) +struct TimerData { + funval: jsval, + args: DVec, +} + +fn TimerData(argc: libc::c_uint, argv: *jsval) -> TimerData unsafe { + let data = TimerData { + funval : *argv, + args : DVec(), + }; + + let mut i = 2; + while i < argc as uint { + data.args.push(*ptr::offset(argv, i)); + i += 1; + }; + + data +} + +impl Window { + fn alert(s: &str) { + // Right now, just print to the console + io::println(#fmt("ALERT: %s", s)); + } + + fn setTimeout(&self, timeout: int, argc: libc::c_uint, argv: *jsval) { + let timeout = int::max(0, timeout) as uint; + + // Post a delayed message to the per-window timer task; it will dispatch it + // to the relevant content handler that will deal with it. + std::timer::delayed_send(std::uv_global_loop::get(), + timeout, self.timer_chan, + TimerMessage_Fire(~TimerData(argc, argv))); + } +} + fn Window(content_port: Port) -> Window { let content_chan = Chan(content_port);