rune/shared/
caller.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use core::marker::PhantomData;

use rust_alloc::sync::Arc;

use crate::runtime::{FixedArgs, FunctionHandler, InstAddress, Output, VmResult};
use crate::FromValue;

/// Helper struct to conveniently call native functions.
///
/// Note: This can only be used with functions that take at least one argument.
/// Otherwise it will panic.
#[derive(Clone)]
pub(crate) struct Caller<A, const N: usize, T> {
    handler: Arc<FunctionHandler>,
    _marker: PhantomData<(A, T)>,
}

impl<A, const N: usize, T> Caller<A, N, T>
where
    A: FixedArgs<N>,
    T: FromValue,
{
    /// Construct a new caller helper
    pub(crate) fn new(handler: Arc<FunctionHandler>) -> Self {
        Self {
            handler,
            _marker: PhantomData,
        }
    }

    /// Modify the return value of the caller.
    pub(crate) fn with_return<U>(&self) -> Caller<A, N, U>
    where
        U: FromValue,
    {
        Caller {
            handler: self.handler.clone(),
            _marker: PhantomData,
        }
    }

    /// Perform a call.
    pub(crate) fn call(&self, args: A) -> VmResult<T> {
        const {
            assert!(N > 0, "Must be used with non-zero arguments");
        }

        let mut args = vm_try!(args.into_array());

        vm_try!((self.handler)(
            &mut args,
            InstAddress::ZERO,
            N,
            Output::keep(0)
        ));

        let Some(value) = args.into_iter().next() else {
            unreachable!();
        };

        VmResult::Ok(vm_try!(T::from_value(value)))
    }
}

// SAFETY: The marker doesn't matter.
unsafe impl<A, const N: usize, T> Send for Caller<A, N, T> {}
unsafe impl<A, const N: usize, T> Sync for Caller<A, N, T> {}