use crate::js_utils::adapters::JsRuntimeAdapter;
use crate::js_utils::facades::values::JsValueFacade;
use crate::js_utils::modules::{CompiledModuleLoader, NativeModuleLoader, ScriptModuleLoader};
use crate::js_utils::{JsError, Script, ScriptPreProcessor};
use std::fmt::{Display, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::sync::Weak;
pub mod async_utils;
pub mod values;
pub struct JsProxy {}
pub trait JsRuntimeFacadeInner {
type JsRuntimeFacadeType: JsRuntimeFacade;
fn js_exe_rt_task_in_event_loop<
R: Send + 'static,
J: FnOnce(&<<Self as JsRuntimeFacadeInner>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType) -> R + Send + 'static,
>(
&self,
task: J,
) -> R;
fn js_add_rt_task_to_event_loop<
R: Send + 'static,
J: FnOnce(&<<Self as JsRuntimeFacadeInner>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType) -> R + Send + 'static,
>(
&self,
task: J,
) -> Pin<Box<dyn Future<Output = R> + Send>>;
fn js_add_rt_task_to_event_loop_void<J: FnOnce(&<<Self as JsRuntimeFacadeInner>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType) + Send + 'static>(
&self,
task: J,
);
}
pub trait JsRuntimeBuilder {
type JsRuntimeFacadeType: JsRuntimeFacade;
fn js_build(self) -> Self::JsRuntimeFacadeType;
fn js_runtime_init_hook<
H: FnOnce(&Self::JsRuntimeFacadeType) -> Result<(), JsError> + Send + 'static,
>(
self,
hook: H,
) -> Self;
fn js_realm_adapter_init_hook<
H: Fn(&<<Self as JsRuntimeBuilder>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType, &<<<Self as JsRuntimeBuilder>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType) -> Result<(), JsError> + Send + 'static,
>(
self,
hook: H,
) -> Self;
fn js_runtime_adapter_init_hook<
H: FnOnce(&<<Self as JsRuntimeBuilder>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType) -> Result<(), JsError> + Send + 'static,
>(
self,
hook: H,
) -> Self;
fn js_script_pre_processor<S: ScriptPreProcessor + Send + 'static>(
self,
preprocessor: S,
) -> Self;
fn js_script_module_loader<S: ScriptModuleLoader<<<<Self as JsRuntimeBuilder>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType> + Send + 'static>(
self,
module_loader: S,
) -> Self;
fn js_compiled_module_loader<
S: CompiledModuleLoader<<<<Self as JsRuntimeBuilder>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType>
+ Send
+ 'static
>(
self,
module_loader: S,
) -> Self;
fn js_native_module_loader<
S: NativeModuleLoader<<<<Self as JsRuntimeBuilder>::JsRuntimeFacadeType as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType>
+ Send
+ 'static,
>(
self,
module_loader: S,
) -> Self
where
Self: Sized;
}
pub trait JsRuntimeFacade {
type JsRuntimeAdapterType: JsRuntimeAdapter;
type JsRuntimeFacadeInnerType: JsRuntimeFacadeInner + Send + Sync;
fn js_get_runtime_facade_inner(&self) -> Weak<Self::JsRuntimeFacadeInnerType>;
fn js_realm_create(&self, name: &str) -> Result<(), JsError>;
fn js_realm_destroy(&self, name: &str) -> Result<(), JsError>;
fn js_realm_has(&self, name: &str) -> Result<bool, JsError>;
fn js_loop_sync<
R: Send + 'static,
C: FnOnce(&Self::JsRuntimeAdapterType) -> R + Send + 'static,
>(
&self,
consumer: C,
) -> R;
fn js_loop_sync_mut<
R: Send + 'static,
C: FnOnce(&mut Self::JsRuntimeAdapterType) -> R + Send + 'static,
>(
&self,
consumer: C,
) -> R;
fn js_loop<R: Send + 'static, C: FnOnce(&Self::JsRuntimeAdapterType) -> R + Send + 'static>(
&self,
consumer: C,
) -> Pin<Box<dyn Future<Output = R> + Send>>;
fn js_loop_void<C: FnOnce(&Self::JsRuntimeAdapterType) + Send + 'static>(&self, consumer: C);
fn js_loop_realm_sync<
R: Send + 'static,
C: FnOnce(
&Self::JsRuntimeAdapterType,
&<<Self as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType,
) -> R + Send
+ 'static ,
>(
&self,
realm_name: Option<&str>,
consumer: C,
) -> R;
fn js_loop_realm<
R: Send + 'static,
C: FnOnce(
&Self::JsRuntimeAdapterType,
&<<Self as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType,
) -> R + Send + 'static,
>(
&self,
realm_name: Option<&str>,
consumer: C,
) -> Pin<Box<dyn Future<Output = R>>>;
fn js_loop_realm_void<
C: FnOnce(
&Self::JsRuntimeAdapterType,
&<<Self as JsRuntimeFacade>::JsRuntimeAdapterType as JsRuntimeAdapter>::JsRealmAdapterType,
) + Send
+ 'static,
>(
&self,
realm_name: Option<&str>,
consumer: C,
);
#[allow(clippy::type_complexity)]
fn js_eval(
&self,
realm_name: Option<&str>,
script: Script,
) -> Pin<Box<dyn Future<Output = Result<JsValueFacade, JsError>>>>;
#[allow(clippy::type_complexity)]
fn js_eval_module(
&self,
realm_name: Option<&str>,
script: Script,
) -> Pin<Box<dyn Future<Output = Result<(), JsError>>>>;
fn js_function_invoke_sync(
&self,
realm_name: Option<&str>,
namespace: &[&str],
method_name: &str,
args: Vec<JsValueFacade>,
) -> Result<JsValueFacade, JsError>;
#[allow(clippy::type_complexity)]
fn js_function_invoke(
&self,
realm_name: Option<&str>,
namespace: &[&str],
method_name: &str,
args: Vec<JsValueFacade>,
) -> Pin<Box<dyn Future<Output = Result<JsValueFacade, JsError>>>>;
fn js_function_invoke_void(
&self,
realm_name: Option<&str>,
namespace: &[&str],
method_name: &str,
args: Vec<JsValueFacade>,
);
}
#[derive(PartialEq, Copy, Clone, Eq)]
pub enum JsValueType {
I32,
F64,
String,
Boolean,
Object,
Function,
BigInt,
Promise,
Date,
Null,
Undefined,
Array,
Error,
}
impl Display for JsValueType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
JsValueType::I32 => f.write_str("I32"),
JsValueType::F64 => f.write_str("F64"),
JsValueType::String => f.write_str("String"),
JsValueType::Boolean => f.write_str("Boolean"),
JsValueType::Object => f.write_str("Object"),
JsValueType::Function => f.write_str("Function"),
JsValueType::BigInt => f.write_str("BigInt"),
JsValueType::Promise => f.write_str("Promise"),
JsValueType::Date => f.write_str("Date"),
JsValueType::Null => f.write_str("Null"),
JsValueType::Undefined => f.write_str("Undefined"),
JsValueType::Array => f.write_str("Array"),
JsValueType::Error => f.write_str("Error"),
}
}
}
#[cfg(test)]
pub mod tests {
use crate::js_utils::facades::JsValueType;
#[test]
fn test_fmt() {
println!("t:{}", JsValueType::String);
}
}