Skip to content

Commit

Permalink
Change [IU]256 arithmetic host functions to take *Val not *Object. (#889
Browse files Browse the repository at this point in the history
)

* Change [IU]256 arithmetic host functions to take *Val not *Object.

* Add a bunch of convenience (const) constructors for small number variants.

* regenerate wasms
  • Loading branch information
graydon authored Jun 24, 2023
1 parent a9dbd06 commit aff56a8
Show file tree
Hide file tree
Showing 33 changed files with 298 additions and 185 deletions.
72 changes: 36 additions & 36 deletions soroban-env-common/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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`."
},
{
Expand All @@ -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`."
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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. "
},
{
Expand All @@ -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`."
},
{
Expand All @@ -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`."
},
{
Expand Down
48 changes: 40 additions & 8 deletions soroban-env-common/src/convert.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -94,12 +95,7 @@ impl<E: Env> TryFromVal<E, u64> for Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &u64) -> Result<Self, Self::Error> {
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())
}
}

Expand Down Expand Up @@ -209,9 +205,18 @@ impl<E: Env> TryFromVal<E, Val> for i128 {
}
}
}

impl<E: Env> TryFromVal<E, i128> for Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &i128) -> Result<Self, Self::Error> {
Ok(I128Val::try_from_val(env, v)?.to_val())
}
}

impl<E: Env> TryFromVal<E, i128> for I128Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &i128) -> Result<Self, Self::Error> {
let v = *v;
if let Ok(so) = I128Small::try_from(v) {
Expand Down Expand Up @@ -242,9 +247,18 @@ impl<E: Env> TryFromVal<E, Val> for u128 {
}
}
}

impl<E: Env> TryFromVal<E, u128> for Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &u128) -> Result<Self, Self::Error> {
Ok(U128Val::try_from_val(env, v)?.to_val())
}
}

impl<E: Env> TryFromVal<E, u128> for U128Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &u128) -> Result<Self, Self::Error> {
let v = *v;
if let Ok(so) = U128Small::try_from(v) {
Expand Down Expand Up @@ -276,9 +290,18 @@ impl<E: Env> TryFromVal<E, Val> for I256 {
}
}
}

impl<E: Env> TryFromVal<E, I256> for Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &I256) -> Result<Self, Self::Error> {
Ok(I256Val::try_from_val(env, v)?.to_val())
}
}

impl<E: Env> TryFromVal<E, I256> for I256Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &I256) -> Result<Self, Self::Error> {
let v = *v;
if let Ok(so) = I256Small::try_from(v) {
Expand Down Expand Up @@ -311,9 +334,18 @@ impl<E: Env> TryFromVal<E, Val> for U256 {
}
}
}

impl<E: Env> TryFromVal<E, U256> for Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &U256) -> Result<Self, Self::Error> {
Ok(U256Val::try_from_val(env, v)?.to_val())
}
}

impl<E: Env> TryFromVal<E, U256> for U256Val {
type Error = ConversionError;

fn try_from_val(env: &E, v: &U256) -> Result<Self, Self::Error> {
let v = *v;
if let Ok(so) = U256Small::try_from(v) {
Expand Down
6 changes: 3 additions & 3 deletions soroban-env-common/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit aff56a8

Please sign in to comment.