use std::rc::Rc;
use rhue_vm::{NativeCtx, NativeError, NativeFn};
use crate::context::ScriptError;
use crate::convert::{Args, FromArg, IntoValues};
use crate::{Context, Value};
pub trait IntoNativeResult {
fn into_native_result(self, cx: &mut Context<'_>) -> Result<Vec<Value>, ScriptError>;
#[doc(hidden)]
fn lower_result(self, cx: &mut Context<'_>, out: &mut rhue_vm::Rets) -> Result<(), ScriptError>
where
Self: Sized,
{
for v in self.into_native_result(cx)? {
let v = cx.lower(v);
out.push(v);
}
Ok(())
}
fn result_type() -> String
where
Self: Sized;
#[doc(hidden)]
fn declare_types(_vm: &mut rhue_vm::Vm)
where
Self: Sized,
{
}
}
impl<T: IntoValues> IntoNativeResult for T {
fn into_native_result(self, cx: &mut Context<'_>) -> Result<Vec<Value>, ScriptError> {
Ok(self.into_values(cx))
}
fn lower_result(
self,
cx: &mut Context<'_>,
out: &mut rhue_vm::Rets,
) -> Result<(), ScriptError> {
self.lower_into(cx, out);
Ok(())
}
fn result_type() -> String {
T::pack_type()
}
fn declare_types(vm: &mut rhue_vm::Vm) {
T::declare_types(vm)
}
}
impl<T: IntoValues, E: Into<ScriptError>> IntoNativeResult for Result<T, E> {
fn into_native_result(self, cx: &mut Context<'_>) -> Result<Vec<Value>, ScriptError> {
self.map(|v| v.into_values(cx)).map_err(Into::into)
}
fn lower_result(
self,
cx: &mut Context<'_>,
out: &mut rhue_vm::Rets,
) -> Result<(), ScriptError> {
self.map(|v| v.lower_into(cx, out)).map_err(Into::into)
}
fn result_type() -> String {
T::pack_type()
}
fn declare_types(vm: &mut rhue_vm::Vm) {
T::declare_types(vm)
}
}
pub(crate) fn fn_signature(params: &[String], result: &str) -> String {
format!("({}) -> {result}", params.join(", "))
}
pub struct WithContext;
pub trait IntoNative<Marker> {
fn into_native(self, name: Rc<str>) -> NativeFn;
fn signature() -> String
where
Self: Sized;
#[doc(hidden)]
fn declare_types(_vm: &mut rhue_vm::Vm)
where
Self: Sized,
{
}
}
pub(crate) fn run_native(
ctx: &mut NativeCtx<'_>,
args: &[rhue_vm::Value],
first_index: usize,
body: impl FnOnce(&mut Context<'_>, &mut Args, &mut rhue_vm::Rets) -> Result<(), ScriptError>,
) -> Result<rhue_vm::Rets, NativeError> {
let mut cx = Context::new(ctx.vm());
let mut args = Args::raw(args, first_index);
let mut out = rhue_vm::Rets::new();
match body(&mut cx, &mut args, &mut out) {
Ok(()) => Ok(out),
Err(e) => Err(e.into_native(&mut cx)),
}
}
macro_rules! natives {
($($A:ident),*) => {
impl<Func, Ret, $($A,)*> IntoNative<(Ret, $($A,)*)> for Func
where
Func: Fn($($A),*) -> Ret + 'static,
Ret: IntoNativeResult,
$($A: FromArg,)*
{
#[allow(non_snake_case, unused_variables, unused_mut)]
fn into_native(self, name: Rc<str>) -> NativeFn {
Rc::new(move |ctx, args| {
run_native(ctx, args, 1, |cx, args, out| {
$(let $A = $A::from_arg(args, cx)
.map_err(|e| ScriptError::argument(&name, e))?;)*
(self)($($A),*).lower_result(cx, out)
})
})
}
fn signature() -> String {
fn_signature(&[$($A::param_type()),*], &Ret::result_type())
}
#[allow(unused_variables)]
fn declare_types(vm: &mut rhue_vm::Vm) {
$($A::declare_types(vm);)*
Ret::declare_types(vm);
}
}
impl<Func, Ret, $($A,)*> IntoNative<(WithContext, Ret, $($A,)*)> for Func
where
Func: for<'a, 'b> Fn(&'a mut Context<'b>, $($A),*) -> Ret + 'static,
Ret: IntoNativeResult,
$($A: FromArg,)*
{
#[allow(non_snake_case, unused_variables, unused_mut)]
fn into_native(self, name: Rc<str>) -> NativeFn {
Rc::new(move |ctx, args| {
run_native(ctx, args, 1, |cx, args, out| {
$(let $A = $A::from_arg(args, cx)
.map_err(|e| ScriptError::argument(&name, e))?;)*
(self)(cx, $($A),*).lower_result(cx, out)
})
})
}
fn signature() -> String {
fn_signature(&[$($A::param_type()),*], &Ret::result_type())
}
#[allow(unused_variables)]
fn declare_types(vm: &mut rhue_vm::Vm) {
$($A::declare_types(vm);)*
Ret::declare_types(vm);
}
}
};
}
natives!();
natives!(A);
natives!(A, B);
natives!(A, B, C);
natives!(A, B, C, D);
natives!(A, B, C, D, E);
natives!(A, B, C, D, E, F);
natives!(A, B, C, D, E, F, G);
natives!(A, B, C, D, E, F, G, H);
pub trait IntoAsyncNative<Marker> {
#[doc(hidden)]
fn into_async_native(
self,
name: Rc<str>,
state: Rc<std::cell::RefCell<crate::asyncs::AsyncState>>,
) -> NativeFn;
fn signature() -> String
where
Self: Sized;
#[doc(hidden)]
fn declare_types(_vm: &mut rhue_vm::Vm)
where
Self: Sized,
{
}
}
macro_rules! async_natives {
($($A:ident),*) => {
impl<Func, Fut, Ret, $($A,)*> IntoAsyncNative<(Fut, Ret, $($A,)*)> for Func
where
Func: Fn($($A),*) -> Fut + 'static,
Fut: std::future::Future<Output = Ret> + 'static,
Ret: IntoNativeResult + 'static,
$($A: FromArg,)*
{
#[allow(non_snake_case, unused_variables, unused_mut)]
fn into_async_native(
self,
name: Rc<str>,
state: Rc<std::cell::RefCell<crate::asyncs::AsyncState>>,
) -> NativeFn {
Rc::new(move |ctx, args| {
run_native(ctx, args, 1, |cx, args, _out| {
$(let $A = $A::from_arg(args, cx)
.map_err(|e| ScriptError::argument(&name, e))?;)*
let future = (self)($($A),*);
let future = Box::pin(async move {
let out = future.await;
Box::new(move |cx: &mut Context<'_>| out.into_native_result(cx))
as crate::asyncs::Finish
});
crate::asyncs::suspend(cx, &name, &state, future)
})
})
}
fn signature() -> String {
fn_signature(&[$($A::param_type()),*], &Ret::result_type())
}
#[allow(unused_variables)]
fn declare_types(vm: &mut rhue_vm::Vm) {
$($A::declare_types(vm);)*
Ret::declare_types(vm);
}
}
};
}
async_natives!();
async_natives!(A);
async_natives!(A, B);
async_natives!(A, B, C);
async_natives!(A, B, C, D);
async_natives!(A, B, C, D, E);
async_natives!(A, B, C, D, E, F);