Skip to content

Commit

Permalink
add BigNum::clamped_sub() and some iOS fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
v-almonacid committed Feb 19, 2021
1 parent 290c29e commit bb52f57
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public final void bigNumCheckedSub(String bigNumPtr, String otherPtr, Promise pr
.pour(promise);
}

@ReactMethod
public final void bigNumClampedSub(String bigNumPtr, String otherPtr, Promise promise) {
Native.I
.bigNumClampedSub(new RPtr(bigNumPtr), new RPtr(otherPtr))
.map(RPtr::toJs)
.pour(promise);
}

@ReactMethod
public final void bigNumCompare(String bigNumPtr, String rhsPtr, Promise promise) {
Native.I
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private Native() { }
public final native Result<String> bigNumToStr(RPtr bigNum);
public final native Result<RPtr> bigNumCheckedAdd(RPtr bigNum, RPtr other);
public final native Result<RPtr> bigNumCheckedSub(RPtr bigNum, RPtr other);
public final native Result<RPtr> bigNumClampedSub(RPtr bigNum, RPtr other);
public final native Result<Integer> bigNumCompare(RPtr bigNum, RPtr rhs);

// Value
Expand Down
4 changes: 4 additions & 0 deletions example/runtimeTests/bigNum.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const bigNum: () => void = async () => {
(await (await bigNumPtr.checked_sub(bigNum2)).to_str()) === '999500',
'BigNum.checked_sub()',
)
assert(
(await (await bigNum2.clamped_sub(bigNumPtr)).to_str()) === '0',
'BigNum.clamped_sub()',
)
assert((await bigNumPtr.compare(bigNum2)) === 1, 'BigNum.compare()')
}

Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export class BigNum extends Ptr {
*/
checked_sub(other): Promise<BigNum>;

/**
* @param {BigNum} other
* @returns {Promise<BigNum>}
*/
clamped_sub(other: BigNum): Promise<BigNum>;

/**
* @param {BigNum} rhs
* @returns {Promise<number>}
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export class BigNum extends Ptr {
return Ptr._wrap(ret, BigNum);
}

/**
* @param {BigNum} other
* @returns {Promise<BigNum>}
*/
async clamped_sub(other) {
const otherPtr = Ptr._assertClass(other, BigNum);
const ret = await HaskellShelley.bigNumClampedSub(this.ptr, otherPtr);
return Ptr._wrap(ret, BigNum);
}

/**
* @param {BigNum} rhs
* @returns {Promise<number | undefined>}
Expand Down
12 changes: 12 additions & 0 deletions ios/HaskellShelley.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ @implementation HaskellShelley
}] exec:@[ptr1, ptr2] andResolve:resolve orReject:reject];
}

RCT_EXPORT_METHOD(bigNumClampedSub:(nonnull NSString *)ptr1 other:(nonnull NSString *)ptr2 withResolve:(RCTPromiseResolveBlock)resolve andReject:(RCTPromiseRejectBlock)reject)
{
[[CSafeOperation new:^NSString*(NSArray<NSString*>* ptrs, CharPtr* error) {
RPtr result;
return big_num_clamped_sub([[ptrs objectAtIndex:0] rPtr],
[[ptrs objectAtIndex:1] rPtr],
&result, error)
? [NSString stringFromPtr:result]
: nil;
}] exec:@[ptr1, ptr2] andResolve:resolve orReject:reject];
}

RCT_EXPORT_METHOD(bigNumCompare:(nonnull NSString *)bigNumPtr other:(nonnull NSString *)rhsPtr withResolve:(RCTPromiseResolveBlock)resolve andReject:(RCTPromiseRejectBlock)reject)
{
[[CSafeOperation new:^NSNumber*(NSArray<NSString*>* ptrs, CharPtr* error) {
Expand Down
20 changes: 20 additions & 0 deletions rust/src/android/big_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_bigNumCheckedSub
.jresult(&env)
}

#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_bigNumClampedSub(
env: JNIEnv, _: JObject, ptr: JObject, other: JObject
) -> jobject {
handle_exception_result(|| {
let rptr = ptr.rptr(&env)?;
let rptr_other = other.rptr(&env)?;
rptr
.typed_ref::<BigNum>()
.zip(rptr_other.typed_ref::<BigNum>())
.map(|(val, other)| val.clamped_sub(other))
.and_then(|res| {
res.rptr().jptr(&env)
})
})
.jresult(&env)
}


#[allow(non_snake_case)]
#[no_mangle]
pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_bigNumCompare(
Expand Down
15 changes: 14 additions & 1 deletion rust/src/ios/big_num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,27 @@ pub unsafe extern "C" fn big_num_checked_sub(
.response(result, error)
}

#[no_mangle]
pub unsafe extern "C" fn big_num_clamped_sub(
big_num: RPtr, other: RPtr, result: &mut RPtr, error: &mut CharPtr
) -> bool {
handle_exception_result(|| {
let val = big_num.typed_ref::<BigNum>()?;
other.typed_ref::<BigNum>()
.map(|other| val.clamped_sub(other))
.map(|val| val.rptr())
})
.response(result, error)
}

#[no_mangle]
pub unsafe extern "C" fn big_num_compare(
big_num: RPtr, rhs: RPtr, result: &mut i8, error: &mut CharPtr
) -> bool {
handle_exception_result(|| {
let big_num = big_num.typed_ref::<BigNum>()?;
rhs.typed_ref::<BigNum>()
.map(|rhs| value.compare(rhs))
.map(|rhs| big_num.compare(rhs))
})
.response(result, error)
}
11 changes: 9 additions & 2 deletions rust/src/ios/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ use crate::utils::ToFromBytes;
use crate::panic::*;
use crate::ptr::{RPtr, RPtrRepresentable};

use cardano_serialization_lib::utils::{hash_transaction, make_vkey_witness, make_icarus_bootstrap_witness, min_ada_required, Value, BigNum};
use cardano_serialization_lib::utils::{
hash_transaction,
make_vkey_witness,
make_icarus_bootstrap_witness,
min_ada_required,
Value,
BigNum
};
use cardano_serialization_lib::{TransactionBody};
use cardano_serialization_lib::crypto::{Bip32PrivateKey, PrivateKey, TransactionHash};
use cardano_serialization_lib::address::ByronAddress;
Expand Down Expand Up @@ -85,7 +92,7 @@ pub unsafe extern "C" fn utils_min_ada_required(
assets.typed_ref::<Value>()
.zip(min_utxo_val.typed_ref::<BigNum>())
.map(|(assets, min_utxo_val)| {
make_vkey_witness(assets, min_utxo_val)
min_ada_required(assets, min_utxo_val)
})
})
.map(|min_ada| min_ada.rptr())
Expand Down
6 changes: 0 additions & 6 deletions rust/src/ios/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ pub unsafe extern "C" fn value_clamped_sub(
pub unsafe extern "C" fn value_compare(
value: RPtr, rhs: RPtr, result: &mut i8, error: &mut CharPtr
) -> bool {
// handle_exception_result(|| {
// let value = value.typed_ref::<Value>()?;
// rhs.typed_ref::<Value>()
// .map(|rhs| value.compare(rhs).unwrap())
// })
// .response(result, error)
let res = handle_exception_result(|| {
let value = value.typed_ref::<Value>()?;
rhs.typed_ref::<Value>()
Expand Down

0 comments on commit bb52f57

Please sign in to comment.