spacevm_sys/
lib.rs

1//! SpaceVM system interface
2
3use crate::abi::Buffer;
4use anyhow::Result;
5use spacejam_service::{
6    api::{AccumulateArgs, Accumulated, AuthorizeArgs, RefineArgs},
7    service::result::{Executed, Refined},
8};
9
10/// Run the accumulate invocation
11pub fn authorize(args: AuthorizeArgs) -> Result<Executed> {
12    let encoded = serde_jam::encode(&args)?;
13    let input = Buffer {
14        ptr: encoded.as_ptr(),
15        len: encoded.len(),
16    };
17
18    let output = unsafe { abi::authorize(input) };
19    serde_jam::decode(output.as_slice()).map_err(Into::into)
20}
21
22/// Run the refine invocation
23pub fn refine(args: RefineArgs) -> Result<Refined> {
24    let encoded = serde_jam::encode(&args)?;
25    let input = Buffer {
26        ptr: encoded.as_ptr(),
27        len: encoded.len(),
28    };
29
30    let output = unsafe { abi::refine(input) };
31    serde_jam::decode(output.as_slice()).map_err(Into::into)
32}
33
34/// Run the accumulate invocation
35pub fn accumulate(args: AccumulateArgs) -> Result<Accumulated> {
36    let encoded = serde_jam::encode(&args)?;
37    let input = Buffer {
38        ptr: encoded.as_ptr(),
39        len: encoded.len(),
40    };
41
42    let output = unsafe { abi::accumulate(input) };
43    serde_jam::decode(output.as_slice()).map_err(Into::into)
44}
45
46mod abi {
47    #[repr(C)]
48    #[derive(Copy, Clone)]
49    pub struct Buffer {
50        pub ptr: *const u8,
51        pub len: usize,
52    }
53
54    impl Buffer {
55        /// Get the buffer as a byte slice
56        pub fn as_slice(&self) -> &[u8] {
57            unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
58        }
59    }
60
61    unsafe extern "C" {
62        /// Run accumulate invocation
63        pub fn accumulate(args: Buffer) -> Buffer;
64
65        /// Run the refine invocation
66        pub fn refine(args: Buffer) -> Buffer;
67
68        /// Run the is_authorized invocation
69        pub fn authorize(args: Buffer) -> Buffer;
70    }
71}