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 fn register_len(register_id: RegisterId) -> PtrSizedInt;
13 fn read_register(register_id: RegisterId, buf: Ref<BufferMut<'_>>) -> Bool;
14 fn context_id(register_id: RegisterId);
16 fn executor_id(register_id: RegisterId);
17 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 commit(root: Ref<Buffer<'_>>, artifact: Ref<Buffer<'_>>);
24 fn storage_read(key: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
26 fn storage_remove(key: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
27 fn storage_write(key: Ref<Buffer<'_>>, value: Ref<Buffer<'_>>, register_id: RegisterId) -> Bool;
28 fn fetch(
30 url: Ref<Buffer<'_>>,
31 method: Ref<Buffer<'_>>,
32 headers: Ref<Buffer<'_>>,
33 body: Ref<Buffer<'_>>,
34 register_id: RegisterId
35 ) -> Bool;
36 fn random_bytes(buf: Ref<BufferMut<'_>>);
38 fn time_now(buf: Ref<BufferMut<'_>>);
39 fn send_proposal(value: Ref<Buffer<'_>>, buf: Ref<BufferMut<'_>>);
41 fn approve_proposal(value: Ref<Buffer<'_>>);
42 fn blob_create() -> PtrSizedInt;
45 fn blob_open(blob_id: Ref<Buffer<'_>>) -> PtrSizedInt;
46 fn blob_read(fd: PtrSizedInt, buf: Ref<BufferMut<'_>>) -> PtrSizedInt;
47 fn blob_write(fd: PtrSizedInt, data: Ref<Buffer<'_>>) -> PtrSizedInt;
48 fn blob_close(fd: PtrSizedInt, blob_id_buf: Ref<BufferMut<'_>>) -> Bool;
49 fn blob_announce_to_context(blob_id: Ref<Buffer<'_>>, context_id: Ref<Buffer<'_>>) -> Bool;
51 }
52}
53
54macro_rules! wasm_imports {
55 ($module:literal => { $(fn $func_name:ident($($arg:ident: $arg_ty:ty),*) $(-> $returns:ty)?;)* }) => {
56 cfg_if::cfg_if! {
57 if #[cfg(target_arch = "wasm32")] {
58 #[link(wasm_import_module = $module)]
59 extern "C" {
60 $(
61 pub fn $func_name($($arg: $arg_ty),*) $(-> $returns)?;
62 )*
63 }
64 } else {
65 $(
66 #[allow(unused_variables, reason = "Needed due to macro expansion")]
67 pub unsafe fn $func_name($($arg: $arg_ty),*) $(-> $returns)? {
68 panic!("host function `{}` is only available when compiled for wasm32", stringify!($func_name));
69 }
70 )*
71 }
72 }
73 };
74}
75
76use wasm_imports;