spacevm_sys/
lib.rs

1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3
4use crate::abi::Buffer;
5pub use abi::init_logger;
6use anyhow::Result;
7use service::{
8    api::{AccumulateArgs, Accumulated, AuthorizeArgs, RefineArgs},
9    service::result::{Executed, Refined},
10};
11
12/// Run the accumulate invocation
13pub fn authorize(args: AuthorizeArgs) -> Result<Executed> {
14    let encoded = codec::encode(&args)?;
15    let input = Buffer {
16        ptr: encoded.as_ptr(),
17        len: encoded.len(),
18    };
19
20    let output: Buffer = unsafe { abi::authorize(input) };
21    codec::decode(&output.to_vec()).map_err(Into::into)
22}
23
24/// Run the refine invocation
25pub fn refine(args: RefineArgs) -> Result<Refined> {
26    let encoded = codec::encode(&args)?;
27    let input = Buffer {
28        ptr: encoded.as_ptr(),
29        len: encoded.len(),
30    };
31
32    let output = unsafe { abi::refine(input) };
33    codec::decode(&output.to_vec()).map_err(Into::into)
34}
35
36/// Run the accumulate invocation
37pub fn accumulate(args: AccumulateArgs) -> Result<Accumulated> {
38    let encoded = codec::encode(&args)?;
39    let input = Buffer {
40        ptr: encoded.as_ptr(),
41        len: encoded.len(),
42    };
43
44    let output = unsafe { abi::accumulate(input) };
45    codec::decode(&output.to_vec()).map_err(Into::into)
46}
47
48mod abi {
49    #[cfg(feature = "interp")]
50    pub use {
51        interp_accumulate as accumulate, interp_authorize as authorize, interp_refine as refine,
52    };
53
54    #[cfg(not(feature = "interp"))]
55    pub use {comp_accumulate as accumulate, comp_authorize as authorize, comp_refine as refine};
56
57    #[repr(C)]
58    #[derive(Copy, Clone, Debug)]
59    pub struct Buffer {
60        pub ptr: *const u8,
61        pub len: usize,
62    }
63
64    impl Buffer {
65        /// Get the buffer as a byte slice
66        pub fn to_vec(self) -> Vec<u8> {
67            let result = unsafe { core::slice::from_raw_parts(self.ptr, self.len).to_vec() };
68            unsafe {
69                let layout = std::alloc::Layout::from_size_align(self.len, 1).unwrap();
70                std::alloc::dealloc(self.ptr as *mut _, layout);
71            }
72            result
73        }
74    }
75
76    unsafe extern "C" {
77        /// Initialize the logger
78        pub fn init_logger(ansi: bool, timer: bool);
79
80        /// Run the authorize invocation
81        #[cfg(not(feature = "interp"))]
82        pub fn comp_authorize(args: Buffer) -> Buffer;
83
84        /// Run the refine invocation
85        #[cfg(not(feature = "interp"))]
86        pub fn comp_refine(args: Buffer) -> Buffer;
87
88        /// Run the accumulate invocation
89        #[cfg(not(feature = "interp"))]
90        pub fn comp_accumulate(args: Buffer) -> Buffer;
91
92        /// Run the is_authorized invocation
93        #[cfg(feature = "interp")]
94        pub fn interp_authorize(args: Buffer) -> Buffer;
95
96        /// Run the refine invocation
97        #[cfg(feature = "interp")]
98        pub fn interp_refine(args: Buffer) -> Buffer;
99
100        /// Run accumulate invocation
101        #[cfg(feature = "interp")]
102        pub fn interp_accumulate(args: Buffer) -> Buffer;
103    }
104}