Skip to main content

calimero_sys/
lib.rs

1#![allow(dead_code, reason = "Will be used in the future")]
2
3mod types;
4
5pub use types::*;
6
7wasm_imports! {
8    "env" => {
9        fn panic(loc: Ref<Location<'_>>) -> !;
10        fn panic_utf8(msg: Ref<Buffer<'_>>, loc: Ref<Location<'_>>) -> !;
11        // --
12        fn register_len(register_id: RegisterId) -> PtrSizedInt;
13        fn read_register(register_id: RegisterId, buf: Ref<BufferMut<'_>>) -> Bool;
14        // --
15        fn context_id(register_id: RegisterId);
16        fn executor_id(register_id: RegisterId);
17        // --
18        fn input(register_id: RegisterId);
19        fn value_return(value: Ref<ValueReturn<'_>>);
20        fn log_utf8(msg: Ref<Buffer<'_>>);
21        fn emit(event: Ref<Event<'_>>);
22        fn emit_with_handler(event: Ref<Event<'_>>, handler: Ref<Buffer<'_>>);
23        // --
24        fn xcall(call: Ref<XCall<'_>>);
25        // --
26        fn commit(root: Ref<Buffer<'_>>, artifact: Ref<Buffer<'_>>);
27        // --
28        fn storage_read(key: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
29        fn storage_remove(key: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
30        fn storage_write(key: Ref<Buffer<'_>>, value: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
31        // --
32        // Private storage functions (node-local, NOT synchronized)
33        fn private_storage_read(key: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
34        fn private_storage_remove(key: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
35        fn private_storage_write(key: Ref<Buffer<'_>>, value: Ref<Buffer<'_>>) -> Bool;
36        // --
37        fn fetch(
38            url: Ref<Buffer<'_>>,
39            method: Ref<Buffer<'_>>,
40            headers: Ref<Buffer<'_>>,
41            body: Ref<Buffer<'_>>,
42            register_id: RegisterId
43        ) -> Bool;
44        // --
45        fn random_bytes(buf: Ref<BufferMut<'_>>);
46        fn time_now(buf: Ref<BufferMut<'_>>);
47        // --
48        // Crypto functions
49        fn ed25519_verify(signature_ptr: Ref<Buffer<'_>>, public_key_ptr: Ref<Buffer<'_>>, message_ptr: Ref<Buffer<'_>>) -> Bool;
50        // --
51        fn send_proposal(value: Ref<Buffer<'_>>, buf: Ref<BufferMut<'_>>);
52        fn approve_proposal(value: Ref<Buffer<'_>>);
53        // --
54        // Context Management functions
55        fn context_create(
56            protocol_ptr: Ref<Buffer<'_>>,
57            app_id_ptr: Ref<Buffer<'_>>,
58            args_ptr: Ref<Buffer<'_>>,
59            alias: Ref<Buffer<'_>>
60        );
61        fn context_delete(context_id_ptr: Ref<Buffer<'_>>);
62        fn context_add_member(public_key_ptr: Ref<Buffer<'_>>);
63        fn context_remove_member(public_key_ptr: Ref<Buffer<'_>>);
64        fn context_is_member(public_key_ptr: Ref<Buffer<'_>>) -> Bool;
65        fn context_members(register_id: RegisterId);
66        // Check if alias resolves to a context ID
67        fn context_resolve_alias(alias: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
68        // --
69        // Streaming blob functions
70        fn blob_create() -> PtrSizedInt;
71        fn blob_open(blob_id: Ref<Buffer<'_>>) -> PtrSizedInt;
72        fn blob_read(fd: PtrSizedInt, buf: Ref<BufferMut<'_>>) -> PtrSizedInt;
73        fn blob_write(fd: PtrSizedInt, data: Ref<Buffer<'_>>) -> PtrSizedInt;
74        fn blob_close(fd: PtrSizedInt, blob_id_buf: Ref<BufferMut<'_>>) -> Bool;
75        // --
76        // Network blob functions
77        fn blob_announce_to_context(blob_id: Ref<Buffer<'_>>, context_id: Ref<Buffer<'_>>) -> Bool;
78        // --
79    }
80}
81
82macro_rules! wasm_imports {
83    ($module:literal => { $(fn $func_name:ident($($arg:ident: $arg_ty:ty),*) $(-> $returns:ty)?;)* }) => {
84        cfg_if::cfg_if! {
85            if #[cfg(target_arch = "wasm32")] {
86                #[link(wasm_import_module = $module)]
87                extern "C" {
88                    $(
89                        pub fn $func_name($($arg: $arg_ty),*) $(-> $returns)?;
90                    )*
91                }
92            } else {
93                $(
94                    #[allow(unused_variables, reason = "Needed due to macro expansion")]
95                    pub unsafe fn $func_name($($arg: $arg_ty),*) $(-> $returns)? {
96                        panic!("host function `{}` is only available when compiled for wasm32", stringify!($func_name));
97                    }
98                )*
99            }
100        }
101    };
102}
103
104use wasm_imports;