feat(quickjs): add backend console log support

This commit is contained in:
2020-07-12 13:46:36 +08:00
parent bc73717adb
commit 1a2df8c4e7

View File

@@ -1,13 +1,34 @@
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::new().unwrap();
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#" var x = myCallback(10, 20); x; "#;
let script = r#" console.debug('this is log'); var x = myCallback(10, 20); x; "#;
let value = context.eval(script).unwrap();
println!("js: callback = {:?}", value);