Files
simple-rust-tests/__lang/quickjs/src/main.rs
2020-10-17 11:57:26 +08:00

36 lines
1.1 KiB
Rust

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);
}