diff --git a/quickjs/src/main.rs b/quickjs/src/main.rs index 2ef4694..36e4537 100644 --- a/quickjs/src/main.rs +++ b/quickjs/src/main.rs @@ -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) { + // 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);