use quick_js::Context;
use quick_js::JsValue;
use quick_js::console::Level;
use quick_js::console::ConsoleBackend;

struct ConsoleBackendImpl;
impl ConsoleBackend for ConsoleBackendImpl {
    fn log(&self, level: Level, values: Vec<JsValue>) {
        // match level {
        //     Level::Trace => {},
        //     Level::Debug => {},
        //     Level::Log   => {},
        //     Level::Info  => {},
        //     Level::Warn  => {},
        //     Level::Error => {},
        // }
        println!("[{:>5}] - {:?}", format!("{:?}", level).to_uppercase(), values);
    }
}

fn main() {
    let context = Context::builder().memory_limit(16 * 1024 * 1024)
                                    .console(ConsoleBackendImpl{})
                                    .build()
                                    .unwrap();

    let value = context.eval("1 + 2").unwrap();
    println!("js: 1 + 2 = {:?}", value);

    context.add_callback("myCallback", |a: i32, b: i32| a + b * b).unwrap();
    let script = r#" console.debug('this is log');  var x = myCallback(10, 20);  x;    "#;

    let value = context.eval(script).unwrap();
    println!("js: callback = {:?}", value);
}
