feat(quickjs): add backend console log support
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user