soroban_env_host/host/
num.rs

1#[macro_export]
2macro_rules! impl_wrapping_obj_from_num {
3    ($host_fn: ident, $hot: ty, $obj: ty, $num: ty) => {
4        fn $host_fn(&self, _vmcaller: &mut VmCaller<Host>, u: $num) -> Result<$obj, HostError> {
5            self.add_host_object(<$hot>::from(u))
6        }
7    };
8}
9
10#[macro_export]
11macro_rules! impl_wrapping_obj_to_num {
12    ($host_fn: ident, $data: ty, $obj: ty, $num: ty) => {
13        fn $host_fn(&self, _vmcaller: &mut VmCaller<Host>, obj: $obj) -> Result<$num, HostError> {
14            self.visit_obj(obj, |t: &$data| Ok(t.metered_clone(self)?.into()))
15        }
16    };
17}
18
19#[macro_export]
20macro_rules! impl_bignum_host_fns {
21    ($host_fn: ident, $method: ident, $num: ty, $valty: ty, $cost: ident) => {
22        fn $host_fn(
23            &self,
24            _vmcaller: &mut VmCaller<Self::VmUserState>,
25            lhs_val: $valty,
26            rhs_val: $valty,
27        ) -> Result<$valty, Self::Error> {
28            use soroban_env_common::TryIntoVal;
29            self.charge_budget(ContractCostType::$cost, None)?;
30            let lhs: $num = lhs_val.to_val().try_into_val(self)?;
31            let rhs: $num = rhs_val.to_val().try_into_val(self)?;
32            let res: $num = lhs.$method(rhs).ok_or_else(|| {
33                self.err(
34                    ScErrorType::Object,
35                    ScErrorCode::ArithDomain,
36                    "overflow has occured",
37                    &[lhs_val.to_val(), rhs_val.to_val()],
38                )
39            })?;
40            Ok(res.try_into_val(self)?)
41        }
42    };
43}
44
45#[macro_export]
46macro_rules! impl_bignum_host_fns_rhs_u32 {
47    ($host_fn: ident, $method: ident, $num: ty, $valty: ty, $cost: ident) => {
48        fn $host_fn(
49            &self,
50            _vmcaller: &mut VmCaller<Self::VmUserState>,
51            lhs_val: $valty,
52            rhs_val: U32Val,
53        ) -> Result<$valty, Self::Error> {
54            use soroban_env_common::TryIntoVal;
55            self.charge_budget(ContractCostType::$cost, None)?;
56            let lhs: $num = lhs_val.to_val().try_into_val(self)?;
57            let res = lhs.$method(rhs_val.into()).ok_or_else(|| {
58                self.err(
59                    ScErrorType::Object,
60                    ScErrorCode::ArithDomain,
61                    "overflow has occured",
62                    &[lhs_val.to_val(), rhs_val.to_val()],
63                )
64            })?;
65            Ok(res.try_into_val(self)?)
66        }
67    };
68}
69
70#[macro_export]
71macro_rules! impl_bls12_381_fr_arith_host_fns {
72    ($host_fn: ident, $method: ident) => {
73        fn $host_fn(
74            &self,
75            _vmcaller: &mut VmCaller<Self::VmUserState>,
76            lhs: U256Val,
77            rhs: U256Val,
78        ) -> Result<U256Val, Self::Error> {
79            let mut lhs = self.fr_from_u256val(lhs)?;
80            let rhs = self.fr_from_u256val(rhs)?;
81            self.$method(&mut lhs, &rhs)?;
82            self.fr_to_u256val(lhs)
83        }
84    };
85}