mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 00:58:32 +02:00
A bit more playing with JS runtime
This commit is contained in:
@@ -1 +1 @@
|
|||||||
console.log('Hello world, from plugin!');
|
sayHello('Plugin');
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
use boa_engine::{js_string, property::Attribute, Context, Source};
|
use boa_engine::{
|
||||||
|
js_string, property::Attribute, Context, JsArgs, NativeFunction, Source,
|
||||||
|
};
|
||||||
use boa_runtime::Console;
|
use boa_runtime::Console;
|
||||||
use tauri::AppHandle;
|
use tauri::AppHandle;
|
||||||
|
|
||||||
@@ -13,24 +15,28 @@ pub fn test_plugins(app_handle: &AppHandle) {
|
|||||||
let mut context = Context::default();
|
let mut context = Context::default();
|
||||||
add_runtime(&mut context);
|
add_runtime(&mut context);
|
||||||
|
|
||||||
// Parse the source code
|
// Add globals
|
||||||
match context.eval(src) {
|
context
|
||||||
Ok(res) => {
|
.register_global_builtin_callable(
|
||||||
println!(
|
"sayHello",
|
||||||
"RESULT: {}",
|
1,
|
||||||
res.to_string(&mut context).unwrap().to_std_string_escaped()
|
NativeFunction::from_fn_ptr(|_, args, context| {
|
||||||
);
|
let value: String = args
|
||||||
}
|
.get_or_undefined(0)
|
||||||
Err(e) => {
|
.try_js_into(context)
|
||||||
// Pretty print the error
|
.expect("failed to convert arg");
|
||||||
eprintln!("Uncaught {e}");
|
|
||||||
}
|
println!("Hello {}!", value);
|
||||||
};
|
|
||||||
|
Ok(value.into())
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.expect("failed to register global");
|
||||||
|
|
||||||
|
context.eval(src).expect("failed to execute script");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds the custom runtime to the context.
|
|
||||||
fn add_runtime(context: &mut 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);
|
let console = Console::init(context);
|
||||||
context
|
context
|
||||||
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
|
.register_global_property(js_string!(Console::NAME), console, Attribute::all())
|
||||||
|
|||||||
Reference in New Issue
Block a user