Browse Source

Update console global (#143)

* Added create_constructor method

* Added new console constructor to realm
pull/147/head
Dom Parfitt 5 years ago committed by Jason Williams
parent
commit
6e90b74639
  1. 7
      src/lib/js/console.rs
  2. 2
      src/lib/realm.rs

7
src/lib/js/console.rs

@ -118,15 +118,12 @@ pub fn error(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
println!("{}", args.join(" ")); println!("{}", args.join(" "));
Ok(Gc::new(ValueData::Undefined)) Ok(Gc::new(ValueData::Undefined))
} }
/// Create a new `console` object /// Create a new `console` object
pub fn _create(global: &Value) -> Value { pub fn create_constructor(global: &Value) -> Value {
let console = ValueData::new_obj(Some(global)); let console = ValueData::new_obj(Some(global));
console.set_field_slice("log", to_value(log as NativeFunctionData)); console.set_field_slice("log", to_value(log as NativeFunctionData));
console.set_field_slice("error", to_value(error as NativeFunctionData)); console.set_field_slice("error", to_value(error as NativeFunctionData));
console.set_field_slice("exception", to_value(error as NativeFunctionData)); console.set_field_slice("exception", to_value(error as NativeFunctionData));
console console
} }
/// Initialise the global object with the `console` object
pub fn init(global: &Value) {
global.set_field_slice("console", _create(global));
}

2
src/lib/realm.rs

@ -53,7 +53,6 @@ impl Realm {
let global = &self.global_obj; let global = &self.global_obj;
// Create intrinsics, add global objects here // Create intrinsics, add global objects here
object::init(global); object::init(global);
console::init(global);
math::init(global); math::init(global);
function::init(global); function::init(global);
json::init(global); json::init(global);
@ -62,6 +61,7 @@ impl Realm {
global.set_field_slice("RegExp", regexp::create_constructor(global)); global.set_field_slice("RegExp", regexp::create_constructor(global));
global.set_field_slice("Array", array::create_constructor(global)); global.set_field_slice("Array", array::create_constructor(global));
global.set_field_slice("Boolean", boolean::create_constructor(global)); global.set_field_slice("Boolean", boolean::create_constructor(global));
global.set_field_slice("console", console::create_constructor(global));
} }
} }

Loading…
Cancel
Save