use std::{ffi::CString, ptr};
use super::module::Module;
use crate::{check_pending_exception, check_status, Env, Result};
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EventLoopMode {
NonBlocking,
Blocking,
}
impl From<EventLoopMode> for napi_sys_ohos::napi_event_mode {
fn from(value: EventLoopMode) -> Self {
match value {
EventLoopMode::Blocking => napi_sys_ohos::napi_event_mode::napi_event_mode_default,
EventLoopMode::NonBlocking => napi_sys_ohos::napi_event_mode::napi_event_mode_nowait,
}
}
}
pub struct ArkRuntime {
pub env: Env,
}
impl ArkRuntime {
pub fn new() -> Result<Self> {
let mut env = std::ptr::null_mut();
check_status!(
unsafe { napi_sys_ohos::napi_create_ark_runtime(&mut env) },
"Create arkts runtime failed"
)?;
Ok(Self {
env: Env::from_raw(env),
})
}
pub fn run_loop(&self, mode: EventLoopMode) -> Result<()> {
check_status!(
unsafe { napi_sys_ohos::napi_run_event_loop(self.env.0, mode.into()) },
"Start event loop failed."
)?;
Ok(())
}
pub fn stop_loop(&self) -> Result<()> {
check_status!(
unsafe { napi_sys_ohos::napi_stop_event_loop(self.env.0) },
"Stop event loop failed."
)?;
Ok(())
}
pub fn load_without_info<T: AsRef<str>>(&self, path: T) -> Result<Module> {
let c_path = CString::new(path.as_ref())?;
let mut module = ptr::null_mut();
check_pending_exception!(self.env.0, unsafe {
napi_sys_ohos::napi_load_module_with_info(
self.env.0,
c_path.as_ptr().cast(),
ptr::null(),
&mut module,
)
})?;
Ok(Module::new(self.env.0, module))
}
pub fn load_with_info<T: AsRef<str>>(&self, path: T, module_info: T) -> Result<Module> {
let c_path = CString::new(path.as_ref())?;
let c_info = CString::new(module_info.as_ref())?;
let mut module = ptr::null_mut();
check_pending_exception!(self.env.0, unsafe {
napi_sys_ohos::napi_load_module_with_info(
self.env.0,
c_path.as_ptr().cast(),
c_info.as_ptr().cast(),
&mut module,
)
})?;
Ok(Module::new(self.env.0, module))
}
}
impl Drop for ArkRuntime {
fn drop(&mut self) {
unsafe {
napi_sys_ohos::napi_destroy_ark_runtime(&mut self.env.0);
}
}
}