mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-23 18:01:08 +01:00
39 lines
1.2 KiB
Rust
39 lines
1.2 KiB
Rust
use boa_engine::{js_string, property::Attribute, Context, Source};
|
|
use boa_runtime::Console;
|
|
use tauri::AppHandle;
|
|
|
|
pub fn test_plugins(app_handle: &AppHandle) {
|
|
let file = app_handle
|
|
.path_resolver()
|
|
.resolve_resource("plugins/hello-world.js")
|
|
.expect("failed to resolve resource");
|
|
let src = Source::from_filepath(&file).expect("Error opening file");
|
|
|
|
// Instantiate the execution context
|
|
let mut context = Context::default();
|
|
add_runtime(&mut context);
|
|
|
|
// Parse the source code
|
|
match context.eval(src) {
|
|
Ok(res) => {
|
|
println!(
|
|
"RESULT: {}",
|
|
res.to_string(&mut context).unwrap().to_std_string_escaped()
|
|
);
|
|
}
|
|
Err(e) => {
|
|
// Pretty print the error
|
|
eprintln!("Uncaught {e}");
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Adds the custom runtime to the context.
|
|
fn add_runtime(context: &mut Context<'_>) {
|
|
// We first add the `console` object, to be able to call `console.log()`.
|
|
let console = Console::init(context);
|
|
context
|
|
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
|
|
.expect("the console builtin shouldn't exist");
|
|
}
|