1#![cfg_attr(not(feature = "std"), no_std)]
2
3#[cfg(feature = "alloc")]
4extern crate alloc;
5
6#[cfg(feature = "alloc")]
7use alloc::vec::Vec;
8
9pub use bobcat_maths::U;
10
11pub type Address = [u8; 20];
12
13pub use bobcat_cd::read_word_slices;
14
15#[cfg(target_arch = "wasm32")]
16mod impls {
17 #[link(wasm_import_module = "vm_hooks")]
18 unsafe extern "C" {
19 #[allow(unused)]
20 pub(crate) fn pay_for_memory_grow(pages: u16);
21 pub(crate) fn write_result(d: *const u8, l: usize);
22 pub(crate) fn read_args(out: *mut u8);
23 pub(crate) fn msg_sender(addr: *mut u8);
24 pub(crate) fn contract_address(addr: *mut u8);
25 pub(crate) fn msg_value(value: *mut u8);
26 pub(crate) fn msg_reentrant() -> bool;
27 pub fn chain_id() -> u64;
28 pub(crate) fn account_code_size(address: *const u8) -> usize;
29 }
30}
31
32#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
33mod impls {
34 use core::{ptr::copy_nonoverlapping, slice::from_raw_parts};
35
36 #[allow(unused)]
37 pub(crate) unsafe fn pay_for_memory_grow(_: u16) {}
38
39 pub(crate) unsafe fn write_result(d: *const u8, l: usize) {
40 println!("{}", const_hex::encode(unsafe { from_raw_parts(d, l) }));
41 }
42
43 pub(crate) unsafe fn read_args(_out: *mut u8) {
44 unimplemented!("read from stdin separately. todo");
45 }
46
47 pub(crate) unsafe fn msg_sender(out: *mut u8) {
48 unsafe { copy_nonoverlapping([0u8; 32].as_ptr(), out, 32) }
49 }
50
51 pub(crate) unsafe fn contract_address(out: *mut u8) {
52 unsafe { copy_nonoverlapping([0u8; 32].as_ptr(), out, 32) }
53 }
54
55 pub(crate) unsafe fn msg_value(out: *mut u8) {
56 unsafe { copy_nonoverlapping([0u8; 32].as_ptr(), out, 32) }
57 }
58
59 pub(crate) unsafe fn msg_reentrant() -> bool {
60 false
61 }
62
63 pub(crate) unsafe fn chain_id() -> u64 {
64 0
65 }
66
67 pub(crate) unsafe fn account_code_size(_: *const u8) -> usize {
68 0
69 }
70}
71
72#[cfg(all(not(target_arch = "wasm32"), not(feature = "std")))]
73mod impls {
74 pub(crate) unsafe fn pay_for_memory_grow(_: u16) {}
75
76 pub(crate) unsafe fn write_result(_: *const u8, _: usize) {}
77
78 pub(crate) unsafe fn read_args(_out: *mut u8) {}
79
80 pub(crate) unsafe fn msg_sender(_: *mut u8) {}
81
82 pub(crate) unsafe fn contract_address(_: *mut u8) {}
83
84 pub(crate) unsafe fn msg_value(_: *mut u8) {}
85
86 pub(crate) unsafe fn msg_reentrant() -> bool {
87 false
88 }
89
90 pub(crate) unsafe fn chain_id() -> u64 {
91 0
92 }
93
94 pub(crate) unsafe fn account_code_size(_: *const u8) -> usize {
95 0
96 }
97}
98
99#[unsafe(no_mangle)]
100#[cfg(not(feature = "dont-define-symbols"))]
101pub unsafe fn mark_used() {
102 unsafe { impls::pay_for_memory_grow(0) }
103 panic!();
104}
105
106pub fn write_result_slice(s: &[u8]) {
107 unsafe { impls::write_result(s.as_ptr(), s.len()) }
108}
109
110pub use bobcat_cd::leftpad_addr;
111
112#[macro_export]
113macro_rules! write_result_exit_res {
114 ($ident:expr) => {{
115 match $ident {
116 Ok(v) => {
117 $crate::write_result_slice(&v);
118 0
119 }
120 Err(v) => {
121 $crate::write_result_slice(&v);
122 1
123 }
124 }
125 }};
126}
127
128#[macro_export]
129macro_rules! write_result_exit_create {
130 ($ident:expr) => {{
131 let (addr, b, i) = $ident;
132 if addr != [0u8; 20] {
133 $crate::write_result_slice(&leftpad_addr(addr));
134 0
135 } else {
136 $crate::write_result_slice(&b[..i]);
137 1
138 }
139 }};
140}
141
142#[macro_export]
143macro_rules! write_result_exit_call {
144 ($ident:expr) => {{
145 let (rc, l, v) = $ident;
146 $crate::write_result_slice(&v[..l]);
147 if rc {
148 0
149 } else {
150 1
151 }
152 }};
153}
154
155pub fn read_args<const CAP: usize>(len: usize) -> ([u8; CAP], usize) {
156 assert!(CAP >= len, "cap not enough");
157 let mut b = [0u8; CAP];
158 unsafe { impls::read_args(b.as_mut_ptr()) };
159 (b, len)
160}
161
162#[macro_export]
163macro_rules! read_args_safe {
164 ($len:expr, $max_len:expr) => {{
165 assert!($max_len >= $len);
166 $crate::read_args::<$max_len>($len).0
167 }};
168}
169
170#[cfg(feature = "alloc")]
171pub fn read_args_vec(len: usize) -> Vec<u8> {
172 let mut b = Vec::with_capacity(len);
173 unsafe {
174 impls::read_args(b.as_mut_ptr());
175 b.set_len(len);
176 };
177 b
178}
179
180pub fn msg_sender() -> Address {
181 let mut b = [0u8; 20];
182 unsafe { impls::msg_sender(b.as_mut_ptr()) }
183 b
184}
185
186pub fn contract_address() -> Address {
187 let mut b = [0u8; 20];
188 unsafe { impls::contract_address(b.as_mut_ptr()) }
189 b
190}
191
192pub fn msg_value() -> U {
193 let mut b = [0u8; 32];
194 unsafe { impls::msg_value(b.as_mut_ptr()) }
195 U(b)
196}
197
198pub fn msg_reentrant() -> bool {
199 unsafe { impls::msg_reentrant() }
200}
201
202pub fn code_size(addr: Address) -> usize {
203 unsafe { impls::account_code_size(addr.as_ptr()) }
204}
205
206pub fn chain_id() -> u64 {
207 unsafe { impls::chain_id() }
208}