Initial "Hello World" for plugins

This commit is contained in:
Gregory Schier
2023-10-29 16:43:28 -07:00
parent 46470160a9
commit 9a56b2f331
6 changed files with 649 additions and 3 deletions

38
src-tauri/src/plugins.rs Normal file
View File

@@ -0,0 +1,38 @@
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");
}