use std::path::Path;
use handle::IsActive;
use private::RuntimePriv;
#[cfg(feature = "async-rt")]
use self::handle::async_handle::{AsyncHandle, dispatch::Dispatch, message::Message};
use crate::{
call::Call,
data::managed::module::JlrsCore,
error::{IOError, JlrsResult},
prelude::{JuliaString, LocalScope, Managed, Module, Target, Value},
weak_handle_unchecked,
};
#[cfg(any(feature = "local-rt", feature = "async-rt", feature = "multi-rt"))]
pub mod builder;
#[cfg(feature = "async")]
pub mod executor;
pub mod handle;
pub mod state;
pub struct RuntimeSettings<T>(T);
impl<'target, 'borrow, T> RuntimeSettings<&'borrow T>
where
T: Target<'target>,
{
pub(crate) fn new(target: &'borrow T) -> Self {
RuntimeSettings(target)
}
}
pub unsafe trait Runtime: RuntimePriv {
type Out<'a, T>
where
T: 'static,
Self: 'a;
type FallibleOut<'a, T>
where
T: 'static,
Self: 'a;
fn error_color<'a>(&'a self, enable: bool) -> Self::Out<'a, ()>;
unsafe fn include<'a, P: AsRef<Path>>(
&'a self,
path: P,
) -> JlrsResult<Self::FallibleOut<'a, ()>>;
unsafe fn using<'a, S: AsRef<str>>(&'a self, module_name: S) -> Self::Out<'a, JlrsResult<()>>;
}
unsafe impl<T: RuntimePriv + IsActive> Runtime for T {
type Out<'a, O>
= O
where
O: 'static,
Self: 'a;
type FallibleOut<'a, O>
= O
where
O: 'static,
Self: 'a;
fn error_color(&self, enable: bool) {
unsafe { set_error_color(enable) }
}
unsafe fn include<'a, P: AsRef<Path>>(&'a self, path: P) -> JlrsResult<()> {
unsafe { include(path) }
}
unsafe fn using<'a, S: AsRef<str>>(&'a self, module_name: S) -> JlrsResult<()> {
unsafe { using(module_name) }
}
}
unsafe impl<'tgt, T: Target<'tgt>> Runtime for RuntimeSettings<T> {
type Out<'a, O>
= O
where
O: 'static,
Self: 'a;
type FallibleOut<'a, O>
= O
where
O: 'static,
Self: 'a;
fn error_color(&self, enable: bool) {
unsafe { set_error_color(enable) }
}
unsafe fn include<'a, P: AsRef<Path>>(&'a self, path: P) -> JlrsResult<()> {
unsafe { include(path) }
}
unsafe fn using<'a, S: AsRef<str>>(&'a self, module_name: S) -> JlrsResult<()> {
unsafe { using(module_name) }
}
}
#[cfg(feature = "async-rt")]
unsafe impl Runtime for AsyncHandle {
type Out<'a, T>
= Dispatch<'a, Message, T>
where
T: 'static,
Self: 'a;
type FallibleOut<'a, T>
= Dispatch<'a, Message, JlrsResult<T>>
where
T: 'static,
Self: 'a;
fn error_color<'a>(&'a self, enable: bool) -> Dispatch<'a, Message, ()> {
self.error_color(enable)
}
unsafe fn include<'a, P: AsRef<Path>>(
&'a self,
path: P,
) -> JlrsResult<Dispatch<'a, Message, JlrsResult<()>>> {
unsafe { self.include(path) }
}
unsafe fn using<'a, S: AsRef<str>>(
&'a self,
module_name: S,
) -> Dispatch<'a, Message, JlrsResult<()>> {
unsafe { self.using(module_name.as_ref().into()) }
}
}
unsafe fn set_error_color(enable: bool) {
unsafe {
let handle = weak_handle_unchecked!();
let enable = if enable {
Value::true_v(&handle)
} else {
Value::false_v(&handle)
};
JlrsCore::set_error_color(&handle)
.call(&handle, [enable])
.ok();
};
}
unsafe fn include<P: AsRef<Path>>(path: P) -> JlrsResult<()> {
if path.as_ref().exists() {
unsafe {
let handle = weak_handle_unchecked!();
return handle.local_scope::<_, 2>(|mut frame| {
let path_jl_str = JuliaString::new(&mut frame, path.as_ref().to_string_lossy());
Module::main(&frame)
.global(&frame, "include")?
.as_managed()
.call(&mut frame, [path_jl_str.as_value()])?;
Ok(())
});
}
}
Err(IOError::NotFound {
path: path.as_ref().to_string_lossy().into(),
})?
}
unsafe fn using<S: AsRef<str>>(module_name: S) -> JlrsResult<()> {
unsafe {
weak_handle_unchecked!().local_scope::<_, 1>(|mut frame| {
let cmd = format!("using {}", module_name.as_ref());
Value::eval_string(&mut frame, cmd)?;
Ok(())
})
}
}
mod private {
#[cfg(feature = "async-rt")]
use super::handle::async_handle::AsyncHandle;
use super::{RuntimeSettings, handle::IsActive};
use crate::prelude::Target;
pub trait RuntimePriv {}
impl<T: IsActive> RuntimePriv for T {}
#[cfg(feature = "async-rt")]
impl RuntimePriv for AsyncHandle {}
impl<'tgt, T: Target<'tgt>> RuntimePriv for RuntimeSettings<T> {}
}