#[macro_use]
extern crate lazy_static;
extern crate core;
pub mod builder;
pub mod facades;
#[cfg(any(
feature = "settimeout",
feature = "setinterval",
feature = "console",
feature = "setimmediate"
))]
pub mod features;
pub mod jsutils;
pub mod quickjs_utils;
pub mod quickjsrealmadapter;
pub mod quickjsruntimeadapter;
pub mod quickjsvalueadapter;
pub mod reflection;
#[cfg(feature = "typescript")]
pub mod typescript;
pub mod values;
pub use libquickjs_sys;
#[cfg(test)]
pub mod tests {
use crate::builder::QuickJsRuntimeBuilder;
use crate::facades::tests::init_test_rt;
use crate::facades::QuickJsRuntimeFacade;
use crate::jsutils::jsproxies::JsProxy;
use crate::jsutils::{JsError, Script};
use crate::quickjsrealmadapter::QuickJsRealmAdapter;
use crate::values::{JsValueConvertable, JsValueFacade};
use futures::executor::block_on;
use std::thread;
use std::time::Duration;
#[test]
fn test_examples() {
let rt = QuickJsRuntimeBuilder::new().build();
let outcome = block_on(run_examples(&rt));
if outcome.is_err() {
log::error!("an error occured: {}", outcome.err().unwrap());
}
log::info!("done");
}
#[test]
fn test_st() {
let rt = init_test_rt();
let _res = rt
.eval_sync(
None,
Script::new(
"t.js",
r#"
async function a(){
await b();
}
async function b(){
throw Error("poof");
}
a().then(() => {
console.log("a done");
}).catch(() => {
console.log("a error");
});
1
"#,
),
)
.expect("script failed");
thread::sleep(Duration::from_secs(1));
}
async fn take_long() -> i32 {
std::thread::sleep(Duration::from_millis(500));
537
}
async fn run_examples(rt: &QuickJsRuntimeFacade) -> Result<(), JsError> {
let eval_res = rt.eval(None, Script::new("simple_eval.js", "2*7;")).await?;
log::info!("simple eval:{}", eval_res.get_i32());
let meth_res = rt
.invoke_function(None, &["Math"], "round", vec![12.321.to_js_value_facade()])
.await?;
log::info!("Math.round(12.321) = {}", meth_res.get_i32());
let cb = JsValueFacade::new_callback(|args| {
let a = args[0].get_i32();
let b = args[1].get_i32();
log::info!("rust cb was called with a:{} and b:{}", a, b);
Ok(JsValueFacade::Null)
});
rt.invoke_function(
None,
&[],
"setTimeout",
vec![
cb,
10.to_js_value_facade(),
12.to_js_value_facade(),
13.to_js_value_facade(),
],
)
.await?;
std::thread::sleep(Duration::from_millis(20));
log::info!("rust cb should have been called by now");
rt.loop_realm_sync(None, |_rt_adapter, realm_adapter| {
let proxy = JsProxy::new()
.namespace(&["com", "mystuff"])
.name("MyProxy")
.static_method(
"doSomething",
|_rt_adapter, realm_adapter: &QuickJsRealmAdapter, _args| {
realm_adapter.create_resolving_promise_async(
async { Ok(take_long().await) },
|realm_adapter, producer_result| {
realm_adapter.create_i32(producer_result)
},
)
},
);
realm_adapter
.install_proxy(proxy, true)
.expect("could not install proxy");
});
rt.eval(
None,
Script::new(
"testMyProxy.js",
"async function a() {\
console.log('a called at %s ms', new Date().getTime());\
let res = await com.mystuff.MyProxy.doSomething();\
console.log('a got result %s at %s ms', res, new Date().getTime());\
}; a();",
),
)
.await?;
std::thread::sleep(Duration::from_millis(600));
log::info!("a should have been called by now");
Ok(())
}
}