diff --git a/soroban-env-common/env.json b/soroban-env-common/env.json index 083a2622c..f8af5d745 100644 --- a/soroban-env-common/env.json +++ b/soroban-env-common/env.json @@ -453,14 +453,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", - "type": "U256Object" + "type": "U256Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked integer addition. Computes `lhs + rhs`, returning `ScError` if overflow occurred. " }, { @@ -469,14 +469,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", - "type": "U256Object" + "type": "U256Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked integer subtraction. Computes `lhs - rhs`, returning `ScError` if overflow occurred. " }, { @@ -485,14 +485,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", - "type": "U256Object" + "type": "U256Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked integer multiplication. Computes `lhs * rhs`, returning `ScError` if overflow occurred. " }, { @@ -501,14 +501,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", - "type": "U256Object" + "type": "U256Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked integer division. Computes `lhs / rhs`, returning `ScError` if `rhs == 0` or overflow occurred. " }, { @@ -517,14 +517,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", "type": "U32Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked exponentiation. Computes `lhs.exp(rhs)`, returning `ScError` if overflow occurred. " }, { @@ -533,14 +533,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", "type": "U32Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked shift left. Computes `lhs << rhs`, returning `ScError` if `rhs` is larger than or equal to the number of bits in `lhs`." }, { @@ -549,14 +549,14 @@ "args": [ { "name": "lhs", - "type": "U256Object" + "type": "U256Val" }, { "name": "rhs", "type": "U32Val" } ], - "return": "U256Object", + "return": "U256Val", "docs": "Performs checked shift right. Computes `lhs >> rhs`, returning `ScError` if `rhs` is larger than or equal to the number of bits in `lhs`." }, { @@ -565,14 +565,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", - "type": "I256Object" + "type": "I256Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked integer addition. Computes `lhs + rhs`, returning `ScError` if overflow occurred. " }, { @@ -581,14 +581,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", - "type": "I256Object" + "type": "I256Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked integer subtraction. Computes `lhs - rhs`, returning `ScError` if overflow occurred. " }, { @@ -597,14 +597,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", - "type": "I256Object" + "type": "I256Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked integer multiplication. Computes `lhs * rhs`, returning `ScError` if overflow occurred. " }, { @@ -613,14 +613,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", - "type": "I256Object" + "type": "I256Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked integer division. Computes `lhs / rhs`, returning `ScError` if `rhs == 0` or overflow occurred. " }, { @@ -629,14 +629,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", "type": "U32Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked exponentiation. Computes `lhs.exp(rhs)`, returning `ScError` if overflow occurred. " }, { @@ -645,14 +645,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", "type": "U32Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked shift left. Computes `lhs << rhs`, returning `ScError` if `rhs` is larger than or equal to the number of bits in `lhs`." }, { @@ -661,14 +661,14 @@ "args": [ { "name": "lhs", - "type": "I256Object" + "type": "I256Val" }, { "name": "rhs", "type": "U32Val" } ], - "return": "I256Object", + "return": "I256Val", "docs": "Performs checked shift right. Computes `lhs >> rhs`, returning `ScError` if `rhs` is larger than or equal to the number of bits in `lhs`." }, { diff --git a/soroban-env-common/src/convert.rs b/soroban-env-common/src/convert.rs index e1644526e..b4bf0170e 100644 --- a/soroban-env-common/src/convert.rs +++ b/soroban-env-common/src/convert.rs @@ -1,7 +1,8 @@ use crate::{ num::{i256_from_pieces, i256_into_pieces, u256_from_pieces, u256_into_pieces}, - ConversionError, DurationSmall, DurationVal, Env, I128Small, I256Small, I64Small, - TimepointSmall, TimepointVal, U128Small, U256Small, U64Small, U64Val, Val, I256, U256, + ConversionError, DurationSmall, DurationVal, Env, I128Small, I128Val, I256Small, I256Val, + I64Small, TimepointSmall, TimepointVal, U128Small, U128Val, U256Small, U256Val, U64Small, + U64Val, Val, I256, U256, }; use core::fmt::Debug; use stellar_xdr::int128_helpers; @@ -94,12 +95,7 @@ impl TryFromVal for Val { type Error = ConversionError; fn try_from_val(env: &E, v: &u64) -> Result { - let v = *v; - if let Ok(so) = U64Small::try_from(v) { - Ok(so.into()) - } else { - Ok(env.obj_from_u64(v).map_err(|_| ConversionError)?.to_val()) - } + Ok(U64Val::try_from_val(env, v)?.to_val()) } } @@ -209,9 +205,18 @@ impl TryFromVal for i128 { } } } + impl TryFromVal for Val { type Error = ConversionError; + fn try_from_val(env: &E, v: &i128) -> Result { + Ok(I128Val::try_from_val(env, v)?.to_val()) + } +} + +impl TryFromVal for I128Val { + type Error = ConversionError; + fn try_from_val(env: &E, v: &i128) -> Result { let v = *v; if let Ok(so) = I128Small::try_from(v) { @@ -242,9 +247,18 @@ impl TryFromVal for u128 { } } } + impl TryFromVal for Val { type Error = ConversionError; + fn try_from_val(env: &E, v: &u128) -> Result { + Ok(U128Val::try_from_val(env, v)?.to_val()) + } +} + +impl TryFromVal for U128Val { + type Error = ConversionError; + fn try_from_val(env: &E, v: &u128) -> Result { let v = *v; if let Ok(so) = U128Small::try_from(v) { @@ -276,9 +290,18 @@ impl TryFromVal for I256 { } } } + impl TryFromVal for Val { type Error = ConversionError; + fn try_from_val(env: &E, v: &I256) -> Result { + Ok(I256Val::try_from_val(env, v)?.to_val()) + } +} + +impl TryFromVal for I256Val { + type Error = ConversionError; + fn try_from_val(env: &E, v: &I256) -> Result { let v = *v; if let Ok(so) = I256Small::try_from(v) { @@ -311,9 +334,18 @@ impl TryFromVal for U256 { } } } + impl TryFromVal for Val { type Error = ConversionError; + fn try_from_val(env: &E, v: &U256) -> Result { + Ok(U256Val::try_from_val(env, v)?.to_val()) + } +} + +impl TryFromVal for U256Val { + type Error = ConversionError; + fn try_from_val(env: &E, v: &U256) -> Result { let v = *v; if let Ok(so) = U256Small::try_from(v) { diff --git a/soroban-env-common/src/env.rs b/soroban-env-common/src/env.rs index 596b599de..dff4a438a 100644 --- a/soroban-env-common/src/env.rs +++ b/soroban-env-common/src/env.rs @@ -2,9 +2,9 @@ use soroban_env_macros::generate_call_macro_with_all_host_functions; use super::Symbol; use super::{ - AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I64Object, - MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, U256Object, - U32Val, U64Object, U64Val, Val, VecObject, Void, + AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I256Val, + I64Object, MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, + U256Object, U256Val, U32Val, U64Object, U64Val, Val, VecObject, Void, }; use core::any; diff --git a/soroban-env-common/src/meta.rs b/soroban-env-common/src/meta.rs index f9ed1d461..5aa5d378e 100644 --- a/soroban-env-common/src/meta.rs +++ b/soroban-env-common/src/meta.rs @@ -42,7 +42,7 @@ pub const ENV_META_V0_SECTION_NAME: &str = "contractenvmetav0"; soroban_env_macros::generate_env_meta_consts!( ledger_protocol_version: 20, - pre_release_version: 49, + pre_release_version: 50, ); pub fn get_ledger_protocol_version(interface_version: u64) -> u32 { diff --git a/soroban-env-common/src/num.rs b/soroban-env-common/src/num.rs index a09585b57..230590efb 100644 --- a/soroban-env-common/src/num.rs +++ b/soroban-env-common/src/num.rs @@ -308,6 +308,87 @@ impl TryFrom<&I256Small> for ScVal { } } +// Some explicit convenience "small" constructors that take 32-bit inputs in +// order to remain infallible and are also `const fn`. + +impl I64Small { + pub const fn from_i32(small: i32) -> Self { + let extended = small as i64; + unsafe { I64Small::from_body(extended as u64) } + } +} + +impl I64Val { + pub const fn from_i32(small: i32) -> Self { + Self::from_small(I64Small::from_i32(small)) + } +} + +impl U64Small { + pub const fn from_u32(small: u32) -> Self { + let extended = small as u64; + unsafe { U64Small::from_body(extended) } + } +} + +impl U64Val { + pub const fn from_u32(small: u32) -> Self { + Self::from_small(U64Small::from_u32(small)) + } +} + +impl I128Small { + pub const fn from_i32(small: i32) -> Self { + let extended = small as i64; + unsafe { I128Small::from_body(extended as u64) } + } +} + +impl I128Val { + pub const fn from_i32(small: i32) -> Self { + Self::from_small(I128Small::from_i32(small)) + } +} + +impl U128Small { + pub const fn from_u32(small: u32) -> Self { + let extended = small as u64; + unsafe { U128Small::from_body(extended) } + } +} + +impl U128Val { + pub const fn from_u32(small: u32) -> Self { + Self::from_small(U128Small::from_u32(small)) + } +} + +impl I256Small { + pub const fn from_i32(small: i32) -> Self { + let extended = small as i64; + unsafe { I256Small::from_body(extended as u64) } + } +} + +impl I256Val { + pub const fn from_i32(small: i32) -> Self { + Self::from_small(I256Small::from_i32(small)) + } +} + +impl U256Small { + pub const fn from_u32(small: u32) -> Self { + let extended = small as u64; + unsafe { U256Small::from_body(extended) } + } +} + +impl U256Val { + pub const fn from_u32(small: u32) -> Self { + Self::from_small(U256Small::from_u32(small)) + } +} + pub const fn is_small_u64(u: u64) -> bool { u == ((u << TAG_BITS) >> TAG_BITS) } diff --git a/soroban-env-common/src/unimplemented_env.rs b/soroban-env-common/src/unimplemented_env.rs index 1e68fb9e8..a601a73ba 100644 --- a/soroban-env-common/src/unimplemented_env.rs +++ b/soroban-env-common/src/unimplemented_env.rs @@ -1,8 +1,8 @@ use super::{call_macro_with_all_host_functions, Env, EnvBase, Symbol}; use super::{ - AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I64Object, - MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, U256Object, - U32Val, U64Object, U64Val, Val, VecObject, Void, + AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I256Val, + I64Object, MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, + U256Object, U256Val, U32Val, U64Object, U64Val, Val, VecObject, Void, }; use core::{any, convert::Infallible}; diff --git a/soroban-env-common/src/vmcaller_env.rs b/soroban-env-common/src/vmcaller_env.rs index b812ccdc9..9fa4bcafd 100644 --- a/soroban-env-common/src/vmcaller_env.rs +++ b/soroban-env-common/src/vmcaller_env.rs @@ -2,9 +2,9 @@ use stellar_xdr::{ScErrorCode, ScErrorType}; use super::{ - AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I64Object, - MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, U256Object, - U32Val, U64Object, U64Val, Val, VecObject, Void, + AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I256Val, + I64Object, MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, + U256Object, U256Val, U32Val, U64Object, U64Val, Val, VecObject, Void, }; use crate::call_macro_with_all_host_functions; use crate::{EnvBase, Symbol}; diff --git a/soroban-env-guest/src/guest.rs b/soroban-env-guest/src/guest.rs index a095b7b00..b50dbc9de 100644 --- a/soroban-env-guest/src/guest.rs +++ b/soroban-env-guest/src/guest.rs @@ -6,9 +6,9 @@ use core::convert::Infallible; use soroban_env_common::call_macro_with_all_host_functions; use super::{ - AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I64Object, - MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, U256Object, - U32Val, U64Object, U64Val, Val, VecObject, Void, + AddressObject, Bool, BytesObject, DurationObject, Error, I128Object, I256Object, I256Val, + I64Object, MapObject, StorageType, StringObject, SymbolObject, TimepointObject, U128Object, + U256Object, U256Val, U32Val, U64Object, U64Val, Val, VecObject, Void, }; use super::{Env, EnvBase, Symbol}; #[cfg(target_family = "wasm")] @@ -438,7 +438,7 @@ macro_rules! generate_extern_modules { #[allow(unused_imports)] use crate::{Val,Object,Symbol,Error,MapObject,VecObject,BytesObject}; #[allow(unused_imports)] - use crate::{I128Object, I256Object, I64Object, I64Val, U128Object, U256Object, U32Val, U64Object, U64Val, StorageType, TimepointObject, DurationObject}; + use crate::{I128Object, I256Object, I256Val, I64Object, I64Val, U128Object, U256Object, U256Val, U32Val, U64Object, U64Val, StorageType, TimepointObject, DurationObject}; #[allow(unused_imports)] use crate::{Void,AddressObject,SymbolObject,StringObject,Bool}; #[link(wasm_import_module = $mod_str)] diff --git a/soroban-env-host/src/cost_runner/cost_types/num_ops.rs b/soroban-env-host/src/cost_runner/cost_types/num_ops.rs index 4be43eeb7..18583c851 100644 --- a/soroban-env-host/src/cost_runner/cost_types/num_ops.rs +++ b/soroban-env-host/src/cost_runner/cost_types/num_ops.rs @@ -1,6 +1,6 @@ use std::hint::black_box; -use crate::{cost_runner::CostRunner, xdr::ContractCostType, Env, I256Object, U32Val}; +use crate::{cost_runner::CostRunner, xdr::ContractCostType, Env, I256Val, U32Val}; macro_rules! impl_int256_cost_runner { ($runner:ident, $method:ident, $cost:ident, $sample_type: ty) => { @@ -11,7 +11,7 @@ macro_rules! impl_int256_cost_runner { type SampleType = $sample_type; - type RecycledType = Option; + type RecycledType = Option; fn run_iter( host: &crate::Host, @@ -33,13 +33,8 @@ macro_rules! impl_int256_cost_runner { } }; } -impl_int256_cost_runner!( - Int256AddSubRun, - i256_add, - Int256AddSub, - (I256Object, I256Object) -); -impl_int256_cost_runner!(Int256MulRun, i256_mul, Int256Mul, (I256Object, I256Object)); -impl_int256_cost_runner!(Int256DivRun, i256_div, Int256Div, (I256Object, I256Object)); -impl_int256_cost_runner!(Int256PowRun, i256_pow, Int256Pow, (I256Object, U32Val)); -impl_int256_cost_runner!(Int256ShiftRun, i256_shl, Int256Shift, (I256Object, U32Val)); +impl_int256_cost_runner!(Int256AddSubRun, i256_add, Int256AddSub, (I256Val, I256Val)); +impl_int256_cost_runner!(Int256MulRun, i256_mul, Int256Mul, (I256Val, I256Val)); +impl_int256_cost_runner!(Int256DivRun, i256_div, Int256Div, (I256Val, I256Val)); +impl_int256_cost_runner!(Int256PowRun, i256_pow, Int256Pow, (I256Val, U32Val)); +impl_int256_cost_runner!(Int256ShiftRun, i256_shl, Int256Shift, (I256Val, U32Val)); diff --git a/soroban-env-host/src/host.rs b/soroban-env-host/src/host.rs index 539769a49..67ff05ef2 100644 --- a/soroban-env-host/src/host.rs +++ b/soroban-env-host/src/host.rs @@ -1362,21 +1362,21 @@ impl VmCallerEnv for Host { }) } - impl_bignum_host_fns!(u256_add, checked_add, U256, Int256AddSub); - impl_bignum_host_fns!(u256_sub, checked_sub, U256, Int256AddSub); - impl_bignum_host_fns!(u256_mul, checked_mul, U256, Int256Mul); - impl_bignum_host_fns!(u256_div, checked_div, U256, Int256Div); - impl_bignum_host_fns_rhs_u32!(u256_pow, checked_pow, U256, Int256Pow); - impl_bignum_host_fns_rhs_u32!(u256_shl, checked_shl, U256, Int256Shift); - impl_bignum_host_fns_rhs_u32!(u256_shr, checked_shr, U256, Int256Shift); - - impl_bignum_host_fns!(i256_add, checked_add, I256, Int256AddSub); - impl_bignum_host_fns!(i256_sub, checked_sub, I256, Int256AddSub); - impl_bignum_host_fns!(i256_mul, checked_mul, I256, Int256Mul); - impl_bignum_host_fns!(i256_div, checked_div, I256, Int256Div); - impl_bignum_host_fns_rhs_u32!(i256_pow, checked_pow, I256, Int256Pow); - impl_bignum_host_fns_rhs_u32!(i256_shl, checked_shl, I256, Int256Shift); - impl_bignum_host_fns_rhs_u32!(i256_shr, checked_shr, I256, Int256Shift); + impl_bignum_host_fns!(u256_add, checked_add, U256, U256Val, Int256AddSub); + impl_bignum_host_fns!(u256_sub, checked_sub, U256, U256Val, Int256AddSub); + impl_bignum_host_fns!(u256_mul, checked_mul, U256, U256Val, Int256Mul); + impl_bignum_host_fns!(u256_div, checked_div, U256, U256Val, Int256Div); + impl_bignum_host_fns_rhs_u32!(u256_pow, checked_pow, U256, U256Val, Int256Pow); + impl_bignum_host_fns_rhs_u32!(u256_shl, checked_shl, U256, U256Val, Int256Shift); + impl_bignum_host_fns_rhs_u32!(u256_shr, checked_shr, U256, U256Val, Int256Shift); + + impl_bignum_host_fns!(i256_add, checked_add, I256, I256Val, Int256AddSub); + impl_bignum_host_fns!(i256_sub, checked_sub, I256, I256Val, Int256AddSub); + impl_bignum_host_fns!(i256_mul, checked_mul, I256, I256Val, Int256Mul); + impl_bignum_host_fns!(i256_div, checked_div, I256, I256Val, Int256Div); + impl_bignum_host_fns_rhs_u32!(i256_pow, checked_pow, I256, I256Val, Int256Pow); + impl_bignum_host_fns_rhs_u32!(i256_shl, checked_shl, I256, I256Val, Int256Shift); + impl_bignum_host_fns_rhs_u32!(i256_shr, checked_shr, I256, I256Val, Int256Shift); fn map_new(&self, _vmcaller: &mut VmCaller) -> Result { self.add_host_object(HostMap::new()) diff --git a/soroban-env-host/src/host/num.rs b/soroban-env-host/src/host/num.rs index 808f7dcfb..d483805d9 100644 --- a/soroban-env-host/src/host/num.rs +++ b/soroban-env-host/src/host/num.rs @@ -26,52 +26,51 @@ macro_rules! impl_wrapping_obj_to_num { #[macro_export] macro_rules! impl_bignum_host_fns { - ($host_fn: ident, $method: ident, $num: ty, $cost: ident) => { + ($host_fn: ident, $method: ident, $num: ty, $valty: ty, $cost: ident) => { fn $host_fn( &self, vmcaller: &mut VmCaller, - lhs_obj: <$num as HostObjectType>::Wrapper, - rhs_obj: <$num as HostObjectType>::Wrapper, - ) -> Result<<$num as HostObjectType>::Wrapper, Self::Error> { + lhs_val: $valty, + rhs_val: $valty, + ) -> Result<$valty, Self::Error> { + use soroban_env_common::TryIntoVal; self.charge_budget(ContractCostType::$cost, None)?; - let res = self.visit_obj(lhs_obj, move |lhs: &$num| { - self.visit_obj(rhs_obj, move |rhs: &$num| { - lhs.$method(*rhs).ok_or_else(|| { - self.err( - ScErrorType::Object, - ScErrorCode::ArithDomain, - "overflow has occured", - &[lhs_obj.to_val(), rhs_obj.to_val()], - ) - }) - }) + let lhs: $num = lhs_val.to_val().try_into_val(self)?; + let rhs: $num = rhs_val.to_val().try_into_val(self)?; + let res: $num = lhs.$method(rhs).ok_or_else(|| { + self.err( + ScErrorType::Object, + ScErrorCode::ArithDomain, + "overflow has occured", + &[lhs_val.to_val(), rhs_val.to_val()], + ) })?; - self.add_host_object(res) + Ok(res.try_into_val(self)?) } }; } #[macro_export] macro_rules! impl_bignum_host_fns_rhs_u32 { - ($host_fn: ident, $method: ident, $num: ty, $cost: ident) => { + ($host_fn: ident, $method: ident, $num: ty, $valty: ty, $cost: ident) => { fn $host_fn( &self, vmcaller: &mut VmCaller, - lhs_obj: <$num as HostObjectType>::Wrapper, + lhs_val: $valty, rhs_val: U32Val, - ) -> Result<<$num as HostObjectType>::Wrapper, Self::Error> { + ) -> Result<$valty, Self::Error> { + use soroban_env_common::TryIntoVal; self.charge_budget(ContractCostType::$cost, None)?; - let res = self.visit_obj(lhs_obj, move |lhs: &$num| { - lhs.$method(rhs_val.into()).ok_or_else(|| { - self.err( - ScErrorType::Object, - ScErrorCode::ArithDomain, - "overflow has occured", - &[lhs_obj.to_val(), rhs_val.to_val()], - ) - }) + let lhs: $num = lhs_val.to_val().try_into_val(self)?; + let res = lhs.$method(rhs_val.into()).ok_or_else(|| { + self.err( + ScErrorType::Object, + ScErrorCode::ArithDomain, + "overflow has occured", + &[lhs_val.to_val(), rhs_val.to_val()], + ) })?; - self.add_host_object(res) + Ok(res.try_into_val(self)?) } }; } diff --git a/soroban-env-host/src/test/num.rs b/soroban-env-host/src/test/num.rs index d3cf686fd..49804c35b 100644 --- a/soroban-env-host/src/test/num.rs +++ b/soroban-env-host/src/test/num.rs @@ -1,10 +1,10 @@ use soroban_env_common::{ num::*, xdr::{ScErrorCode, ScErrorType, ScVal}, - Compare, Env, EnvBase, Object, TryFromVal, TryIntoVal, I256, + Compare, Env, EnvBase, TryFromVal, TryIntoVal, I256, }; -use crate::{budget::AsBudget, host_object::HostObjectType, Host, HostError, Val}; +use crate::{budget::AsBudget, Host, HostError, Val}; use core::fmt::Debug; use std::cmp::Ordering; @@ -200,25 +200,30 @@ fn test_num_rawval_scval_roundtrip_ordering() { check_roundtrip_compare_ok::(&host, input_vec); } -fn check_num_arith_ok(host: &Host, lhs: T, rhs: T, f: F, expected: T) -> Result<(), HostError> +fn check_num_arith_ok( + host: &Host, + lhs: T, + rhs: T, + f: F, + expected: T, +) -> Result<(), HostError> where - T: HostObjectType, - F: FnOnce( - &Host, - ::Wrapper, - ::Wrapper, - ) -> Result<::Wrapper, HostError>, + V: TryFromVal + Into, + HostError: From<>::Error>, + F: FnOnce(&Host, V, V) -> Result, { - let res: Object = host.add_host_object(expected)?.into(); - let res_back: Object = f(host, host.add_host_object(lhs)?, host.add_host_object(rhs)?)?.into(); + let res: V = V::try_from_val(host, &expected)?; + let lhs: V = V::try_from_val(host, &lhs)?; + let rhs: V = V::try_from_val(host, &rhs)?; + let res_back: V = f(host, lhs, rhs)?; assert_eq!( - host.compare(res.as_val(), res_back.as_val()).unwrap(), + host.compare(&res.into(), &res_back.into()).unwrap(), Ordering::Equal ); Ok(()) } -fn check_num_arith_rhs_u32_ok( +fn check_num_arith_rhs_u32_ok( host: &Host, lhs: T, rhs: u32, @@ -226,52 +231,47 @@ fn check_num_arith_rhs_u32_ok( expected: T, ) -> Result<(), HostError> where - T: HostObjectType, - F: FnOnce( - &Host, - ::Wrapper, - U32Val, - ) -> Result<::Wrapper, HostError>, + V: TryFromVal + Into, + HostError: From<>::Error>, + F: FnOnce(&Host, V, U32Val) -> Result, { - let res: Object = host.add_host_object(expected)?.into(); - let res_back: Object = f(host, host.add_host_object(lhs)?, U32Val::from(rhs))?.into(); + let res: V = V::try_from_val(host, &expected)?; + let lhs: V = V::try_from_val(host, &lhs)?; + let res_back: V = f(host, lhs, U32Val::from(rhs))?; assert_eq!( - host.compare(res.as_val(), res_back.as_val()).unwrap(), + host.compare(&res.into(), &res_back.into()).unwrap(), Ordering::Equal ); Ok(()) } -fn check_num_arith_expect_err(host: &Host, lhs: T, rhs: T, f: F) -> Result<(), HostError> +fn check_num_arith_expect_err(host: &Host, lhs: T, rhs: T, f: F) -> Result<(), HostError> where - T: HostObjectType, - F: FnOnce( - &Host, - ::Wrapper, - ::Wrapper, - ) -> Result<::Wrapper, HostError>, + V: TryFromVal + Into, + HostError: From<>::Error>, + F: FnOnce(&Host, V, V) -> Result, { - let res_back = f(host, host.add_host_object(lhs)?, host.add_host_object(rhs)?); + let lhs: V = V::try_from_val(host, &lhs)?; + let rhs: V = V::try_from_val(host, &rhs)?; + let res_back: Result = f(host, lhs, rhs); let code = (ScErrorType::Object, ScErrorCode::ArithDomain); assert!(HostError::result_matches_err(res_back, code)); Ok(()) } -fn check_num_arith_rhs_u32_expect_err( +fn check_num_arith_rhs_u32_expect_err( host: &Host, lhs: T, rhs: u32, f: F, ) -> Result<(), HostError> where - T: HostObjectType, - F: FnOnce( - &Host, - ::Wrapper, - U32Val, - ) -> Result<::Wrapper, HostError>, + V: TryFromVal + Into, + HostError: From<>::Error>, + F: FnOnce(&Host, V, U32Val) -> Result, { - let res_back = f(host, host.add_host_object(lhs)?, U32Val::from(rhs)); + let lhs: V = V::try_from_val(host, &lhs)?; + let res_back: Result = f(host, lhs, U32Val::from(rhs)); let code = (ScErrorType::Object, ScErrorCode::ArithDomain); assert!(HostError::result_matches_err(res_back, code)); Ok(()) diff --git a/soroban-env-host/src/vm/dispatch.rs b/soroban-env-host/src/vm/dispatch.rs index 9d5b68214..505e97d67 100644 --- a/soroban-env-host/src/vm/dispatch.rs +++ b/soroban-env-host/src/vm/dispatch.rs @@ -1,9 +1,9 @@ use super::FuelRefillable; use crate::{xdr::ContractCostType, Host, HostError, VmCaller, VmCallerEnv}; use crate::{ - AddressObject, BytesObject, DurationObject, Error, I128Object, I256Object, I64Object, + AddressObject, BytesObject, DurationObject, Error, I128Object, I256Object, I256Val, I64Object, MapObject, StorageType, StringObject, Symbol, SymbolObject, TimepointObject, U128Object, - U256Object, U32Val, U64Object, Val, VecObject, + U256Object, U256Val, U32Val, U64Object, Val, VecObject, }; use soroban_env_common::{call_macro_with_all_host_functions, WasmiMarshal}; use wasmi::{ diff --git a/soroban-test-wasms/wasm-workspace/Cargo.lock b/soroban-test-wasms/wasm-workspace/Cargo.lock index 842cbfc93..f3d2de8d6 100644 --- a/soroban-test-wasms/wasm-workspace/Cargo.lock +++ b/soroban-test-wasms/wasm-workspace/Cargo.lock @@ -887,6 +887,15 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "ryu" version = "1.0.13" @@ -907,6 +916,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "semver" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" + [[package]] name = "serde" version = "1.0.164" @@ -1036,7 +1051,7 @@ dependencies = [ "soroban-env-macros", "soroban-wasmi", "static_assertions", - "stellar-xdr 0.0.16 (git+https://github.com/stellar/rs-stellar-xdr?rev=6750a11325a7c4eed1e79372d136106e5bfecaff)", + "stellar-xdr", ] [[package]] @@ -1081,7 +1096,7 @@ dependencies = [ "quote", "serde", "serde_json", - "stellar-xdr 0.0.16 (git+https://github.com/stellar/rs-stellar-xdr?rev=6750a11325a7c4eed1e79372d136106e5bfecaff)", + "stellar-xdr", "syn 2.0.18", "thiserror", ] @@ -1127,15 +1142,17 @@ dependencies = [ name = "soroban-sdk-macros" version = "0.8.4" dependencies = [ + "crate-git-revision", "darling", "itertools", "proc-macro2", "quote", + "rustc_version", "sha2 0.9.9", "soroban-env-common", "soroban-spec", "soroban-spec-rust", - "stellar-xdr 0.0.16 (git+https://github.com/stellar/rs-stellar-xdr?rev=518171b12025283cac1233c7de8e4c20088d95b5)", + "stellar-xdr", "syn 2.0.18", ] @@ -1144,7 +1161,7 @@ name = "soroban-spec" version = "0.8.4" dependencies = [ "base64 0.13.1", - "stellar-xdr 0.0.16 (git+https://github.com/stellar/rs-stellar-xdr?rev=518171b12025283cac1233c7de8e4c20088d95b5)", + "stellar-xdr", "thiserror", "wasmparser", ] @@ -1158,7 +1175,7 @@ dependencies = [ "quote", "sha2 0.9.9", "soroban-spec", - "stellar-xdr 0.0.16 (git+https://github.com/stellar/rs-stellar-xdr?rev=518171b12025283cac1233c7de8e4c20088d95b5)", + "stellar-xdr", "syn 2.0.18", "thiserror", ] @@ -1218,17 +1235,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "stellar-xdr" -version = "0.0.16" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=518171b12025283cac1233c7de8e4c20088d95b5#518171b12025283cac1233c7de8e4c20088d95b5" -dependencies = [ - "crate-git-revision", - "hex", - "serde", - "serde_with", -] - [[package]] name = "stellar-xdr" version = "0.0.16" diff --git a/soroban-test-wasms/wasm-workspace/add_f32/src/lib.rs b/soroban-test-wasms/wasm-workspace/add_f32/src/lib.rs index b37f673d7..d9bf693d3 100644 --- a/soroban-test-wasms/wasm-workspace/add_f32/src/lib.rs +++ b/soroban-test-wasms/wasm-workspace/add_f32/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -use soroban_sdk::{contract, contractimpl, Env, Symbol}; +use soroban_sdk::{contract, contractimpl, Env, symbol_short}; #[contract] pub struct Contract; @@ -7,7 +7,7 @@ pub struct Contract; #[contractimpl] impl Contract { pub fn add(env: Env, a: i32, b: i32) -> i32 { - env.events().publish((Symbol::short("add"),), (a, b)); + env.events().publish((symbol_short!("add"),), (a, b)); let a = a as f32; let b = b as f32; (a + b) as i32 diff --git a/soroban-test-wasms/wasm-workspace/add_i32/src/lib.rs b/soroban-test-wasms/wasm-workspace/add_i32/src/lib.rs index c54255ea3..7464789a7 100644 --- a/soroban-test-wasms/wasm-workspace/add_i32/src/lib.rs +++ b/soroban-test-wasms/wasm-workspace/add_i32/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -use soroban_sdk::{contract, contractimpl, Env, Symbol}; +use soroban_sdk::{contract, contractimpl, Env, symbol_short}; #[contract] pub struct Contract; @@ -7,7 +7,7 @@ pub struct Contract; #[contractimpl] impl Contract { pub fn add(env: Env, a: i32, b: i32) -> i32 { - env.events().publish((Symbol::short("add"),), (a, b)); + env.events().publish((symbol_short!("add"),), (a, b)); a + b } } diff --git a/soroban-test-wasms/wasm-workspace/auth/src/lib.rs b/soroban-test-wasms/wasm-workspace/auth/src/lib.rs index 5a6d0185f..5cb36e764 100644 --- a/soroban-test-wasms/wasm-workspace/auth/src/lib.rs +++ b/soroban-test-wasms/wasm-workspace/auth/src/lib.rs @@ -1,7 +1,7 @@ #![no_std] use soroban_sdk::{ auth::{ContractContext, InvokerContractAuthEntry, SubContractInvocation}, - contract, contractimpl, contracttype, vec, Address, Env, IntoVal, Symbol, Vec, + contract, contractimpl, contracttype, vec, Address, Env, IntoVal, symbol_short, Vec, }; #[contract] @@ -31,7 +31,7 @@ impl AuthContract { for child in tree.children.iter() { env.invoke_contract::<()>( &child.contract.clone(), - &Symbol::short("tree_fn"), + &symbol_short!("tree_fn"), (addresses.clone(), child).into_val(&env), ); } @@ -40,7 +40,7 @@ impl AuthContract { pub fn order_fn(env: Env, addr: Address, child_id: Address) { env.invoke_contract::<()>( &child_id, - &Symbol::short("do_auth"), + &symbol_short!("do_auth"), (&addr, 10_u32).into_val(&env), ); addr.require_auth(); @@ -61,7 +61,7 @@ impl AuthContract { let curr_address = env.current_contract_address(); env.invoke_contract::<()>( &tree.contract, - &Symbol::short("tree_fn"), + &symbol_short!("tree_fn"), (vec![&env, curr_address], tree.clone()).into_val(&env), ); } @@ -77,7 +77,7 @@ fn tree_to_invoker_contract_auth(env: &Env, tree: &TreeNode) -> InvokerContractA InvokerContractAuthEntry::Contract(SubContractInvocation { context: ContractContext { contract: tree.contract.clone(), - fn_name: Symbol::short("tree_fn"), + fn_name: symbol_short!("tree_fn"), args: vec![&env], }, sub_invocations, diff --git a/soroban-test-wasms/wasm-workspace/complex/src/lib.rs b/soroban-test-wasms/wasm-workspace/complex/src/lib.rs index e34c71a7c..83111853d 100644 --- a/soroban-test-wasms/wasm-workspace/complex/src/lib.rs +++ b/soroban-test-wasms/wasm-workspace/complex/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -use soroban_sdk::{contract, contractimpl, contracttype, Bytes, BytesN, Env, Symbol, Vec}; +use soroban_sdk::{contract, contractimpl, contracttype, Bytes, BytesN, Env, symbol_short, Vec}; // This is a "complex" contract that uses a nontrivial amount of the host // interface from the guest: UDTs (thus maps), vectors, byte arrays and linear @@ -32,7 +32,7 @@ impl Contract { seq: ledger.sequence(), time: ledger.timestamp(), }; - let data = Symbol::short("data"); + let data = symbol_short!("data"); let hash = e.crypto().sha256(&my_ledger.network_id.clone().into()); let mut buf: [u8; 32] = [0; 32]; hash.copy_into_slice(&mut buf); diff --git a/soroban-test-wasms/wasm-workspace/invoke_contract/src/lib.rs b/soroban-test-wasms/wasm-workspace/invoke_contract/src/lib.rs index 93338c30b..3a8e6a5c0 100644 --- a/soroban-test-wasms/wasm-workspace/invoke_contract/src/lib.rs +++ b/soroban-test-wasms/wasm-workspace/invoke_contract/src/lib.rs @@ -1,5 +1,5 @@ #![no_std] -use soroban_sdk::{contract, contractimpl, vec, Address, Env, IntoVal, Symbol}; +use soroban_sdk::{contract, contractimpl, vec, Address, Env, IntoVal, symbol_short}; #[contract] pub struct Contract; @@ -7,16 +7,16 @@ pub struct Contract; #[contractimpl] impl Contract { pub fn add(env: Env, a: i32, b: i32) -> i32 { - env.events().publish((Symbol::short("add"),), (a, b)); + env.events().publish((symbol_short!("add"),), (a, b)); a + b } pub fn add_with(env: Env, x: i32, y: i32, contract_id: Address) -> i32 { env.events() - .publish((Symbol::short("add_with"),), (x, y, contract_id.clone())); + .publish((symbol_short!("add_with"),), (x, y, contract_id.clone())); env.invoke_contract( &contract_id, - &Symbol::short("add"), + &symbol_short!("add"), vec![&env, x.into_val(&env), y.into_val(&env)], ) } diff --git a/soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm b/soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm index 56741df37..3461a8551 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm and b/soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm b/soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm index c7ee86018..fb74a6b1b 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm b/soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm index a6e1dceed..4448cf3df 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_complex.wasm b/soroban-test-wasms/wasm-workspace/opt/example_complex.wasm index dcc0a0862..88b79a8da 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_complex.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_complex.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm b/soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm index dd7ac6c7f..7ac865f47 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_create_contract.wasm b/soroban-test-wasms/wasm-workspace/opt/example_create_contract.wasm index b0985aeec..480be65e3 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_create_contract.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_create_contract.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm b/soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm index 7c47f95c9..25403dbad 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_fib.wasm b/soroban-test-wasms/wasm-workspace/opt/example_fib.wasm index 0e8f2d5af..62e4ee34b 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_fib.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_fib.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm b/soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm index c7f080ec0..0fcc06bb0 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_invoke_contract.wasm b/soroban-test-wasms/wasm-workspace/opt/example_invoke_contract.wasm index cc53f3101..3e3d80aff 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_invoke_contract.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_invoke_contract.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm b/soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm index c35e05646..cc7a1c167 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_simple_account.wasm b/soroban-test-wasms/wasm-workspace/opt/example_simple_account.wasm index 66ef0783c..d52661a43 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_simple_account.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_simple_account.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_updateable_contract.wasm b/soroban-test-wasms/wasm-workspace/opt/example_updateable_contract.wasm index cf2fd5c28..3f6effad5 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_updateable_contract.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_updateable_contract.wasm differ diff --git a/soroban-test-wasms/wasm-workspace/opt/example_vec.wasm b/soroban-test-wasms/wasm-workspace/opt/example_vec.wasm index bda444f2a..6285cee45 100644 Binary files a/soroban-test-wasms/wasm-workspace/opt/example_vec.wasm and b/soroban-test-wasms/wasm-workspace/opt/example_vec.wasm differ