1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use gc::Gc;
use js::function::NativeFunctionData;
use js::value::{from_value, to_value, ResultValue, Value, ValueData};
use std::iter::FromIterator;
use time::{now, strftime};
pub fn log(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
let args: Vec<String> = FromIterator::from_iter(
args.iter()
.map(|x| from_value::<String>(x.clone()).unwrap()),
);
println!("{}: {}", strftime("%X", &now()).unwrap(), args.join(" "));
Ok(Gc::new(ValueData::Undefined))
}
pub fn error(_: Value, _: Value, args: Vec<Value>) -> ResultValue {
let args: Vec<String> = FromIterator::from_iter(
args.iter()
.map(|x| from_value::<String>(x.clone()).unwrap()),
);
eprintln!("{}: {}", strftime("%X", &now()).unwrap(), args.join(" "));
Ok(Gc::new(ValueData::Undefined))
}
pub fn _create(global: Value) -> Value {
let console = ValueData::new_obj(Some(global));
console.set_field_slice("log", to_value(log as NativeFunctionData));
console.set_field_slice("error", to_value(error as NativeFunctionData));
console.set_field_slice("exception", to_value(error as NativeFunctionData));
console
}
pub fn init(global: Value) {
global.set_field_slice("console", _create(global.clone()));
}