use anyhow::Result;
use llrt_modules::module_builder::ModuleBuilder;
use rquickjs::{AsyncContext, AsyncRuntime, async_with, embed, loader::Bundle};
#[rust_analyzer::skip]
static BUNDLE: Bundle = embed! {
"bundle": "js/build/bundle.js",
};
pub async fn test_js() -> Result<()> {
let runtime = AsyncRuntime::new().unwrap();
let context = AsyncContext::full(&runtime).await.unwrap();
let module_builder = ModuleBuilder::default();
let (module_resolver, module_loader, global_attachment) = module_builder.build();
runtime
.set_loader((BUNDLE, module_resolver), (BUNDLE, module_loader))
.await;
async_with!(context => |ctx| {
global_attachment.attach(&ctx)?;
ctx.eval::<(),_>("delete crypto['randomBytes']").map_err(|err| {
dbg!(ctx.catch());
err })?;
ctx.eval_promise("const {get_cpp_sources_from_graph} = await import('bundle')")?.into_future::<()>().await?;
match ctx.eval_promise("const res = await get_cpp_sources_from_graph()")?.into_future::<()>().await {
Ok(_res) => {
},
Err(_) => {
dbg!(ctx.catch());
}
};
let res: i32 = ctx.eval("res")?;
println!("Through running the graph : {}", res);
anyhow::Ok(())
})
.await?;
runtime.idle().await;
Ok(())
}