Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for integers in automatic FFI generation #4251

Merged
merged 7 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ include/TrustWalletCore/TWEthereumChainID.h

# Generated
include/TrustWalletCore/TWTONAddressConverter.h
include/TrustWalletCore/TWFFITest.h

# Wasm
emsdk/
Expand Down
14 changes: 12 additions & 2 deletions codegen-v2/src/codegen/cpp/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ fn convert_rust_type_to_cpp(ty: &str) -> String {
{
format!("{} *_Nullable", &captures[1])
} else {
ty.to_string()
match ty {
"u8" => "uint8_t".to_string(),
"u16" => "uint16_t".to_string(),
"u32" => "uint32_t".to_string(),
"u64" => "uint64_t".to_string(),
"i8" => "int8_t".to_string(),
"i16" => "int16_t".to_string(),
"i32" => "int32_t".to_string(),
"i64" => "int64_t".to_string(),
_ => ty.to_string(),
}
}
}

Expand Down Expand Up @@ -199,7 +209,7 @@ fn generate_return_type(func: &TWStaticFunction, converted_args: &Vec<String>) -
.map_err(|e| BadFormat(e.to_string()))?;
}
_ => {
writeln!(&mut return_string, " return Rust::{}", func.rust_name)
write!(&mut return_string, " return Rust::{}", func.rust_name)
.map_err(|e| BadFormat(e.to_string()))?;
return_string += generate_function_call(&converted_args)?.as_str();
}
Expand Down
1 change: 1 addition & 0 deletions rust/chains/tw_sui/src/transaction/raw_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum InputObjectArg {
#[serde(rename_all = "camelCase")]
Receiving {
digest: String,
#[serde(with = "as_string")]
version: u64,
object_id: String,
},
Expand Down
66 changes: 66 additions & 0 deletions rust/chains/tw_sui/tests/aftermath_json_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,69 @@ fn test_raw_json_with_all_transactions() {
let result = TransactionBuilder::raw_json(raw_json, 0, 0);
assert!(result.is_ok());
}

#[test]
fn test_raw_json_with_rall_inputs() {
let raw_json = r#"
{
"version": 1,
"sender": "0x1",
"expiration": null,
"gasConfig": {
"budget": "30216120",
"price": "750",
"payment": []
},
"inputs": [
{
"kind": "Input",
"index": 1,
"value": {
"Pure": [
89,
93,
60,
0,
0,
0,
0,
0
]
},
"type": "pure"
},
{
"kind": "Input",
"index": 2,
"value": {
"Object": {
"Shared": {
"mutable": false,
"initialSharedVersion": "228660837",
"objectId": "0x72fbc93a45192357c87557fe73ea62fe5968efb5482834e9243f850377251534"
}
}
},
"type": "object"
},
{
"kind": "Input",
"index": 3,
"value": {
"Object": {
"Receiving": {
"digest": "3ruaZRSLjur2FPsUmcueZF91umNEckEitmxTNrgHrJFc",
"version": "488916618",
"objectId": "0x72fbc93a45192357c87557fe73ea62fe5968efb5482834e9243f850377251534"
}
}
},
"type": "object"
}
],
"transactions": []
}
"#;
let result = TransactionBuilder::raw_json(raw_json, 0, 0);
assert!(result.is_ok());
}
80 changes: 80 additions & 0 deletions rust/wallet_core_rs/src/ffi/ffi_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#![allow(clippy::missing_safety_doc)]

use tw_macros::tw_ffi;
use tw_memory::ffi::{tw_string::TWString, Nonnull, NullableMut, RawPtrTrait};
use tw_misc::try_or_else;

/// Sum two unsigned integers of 8 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU8)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u8(a: u8, b: u8) -> u8 {
a.checked_add(b).unwrap_or(u8::MAX)
}

/// Sum two unsigned integers of 16 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU16)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u16(a: u16, b: u16) -> u16 {
a.checked_add(b).unwrap_or(u16::MAX)
}

/// Sum two unsigned integers of 32 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU32)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u32(a: u32, b: u32) -> u32 {
a.checked_add(b).unwrap_or(u32::MAX)
}

/// Sum two unsigned integers of 64 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = UnsignedSumU64)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_unsigned_sum_u64(a: u64, b: u64) -> u64 {
a.checked_add(b).unwrap_or(u64::MAX)
}

/// Sum two signed integers of 8 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI8)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_signed_sum_i8(a: i8, b: i8) -> i8 {
a.checked_add(b).unwrap_or(i8::MAX)
}

/// Sum two signed integers of 16 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI16)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_signed_sum_i16(a: i16, b: i16) -> i16 {
a.checked_add(b).unwrap_or(i16::MAX)
}

/// Sum two signed integers of 32 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI32)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_signed_sum_i32(a: i32, b: i32) -> i32 {
a.checked_add(b).unwrap_or(i32::MAX)
}

/// Sum two signed integers of 64 bits
#[tw_ffi(ty = static_function, class = TWFFITest, name = SignedSumI64)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_signed_sum_i64(a: i64, b: i64) -> i64 {
a.checked_add(b).unwrap_or(i64::MAX)
}

/// Concatenate a string with a character
#[tw_ffi(ty = static_function, class = TWFFITest, name = StringWithU8)]
#[no_mangle]
pub unsafe extern "C" fn tw_ffi_test_string_with_u8(
a: Nonnull<TWString>,
b: u8,
) -> NullableMut<TWString> {
let a = try_or_else!(TWString::from_ptr_as_ref(a), std::ptr::null_mut);
let a_str = try_or_else!(a.as_str(), std::ptr::null_mut);
let mut result = String::new();
result.push_str(a_str);
result.push(b as char);
TWString::from(result).into_ptr()
}
2 changes: 2 additions & 0 deletions rust/wallet_core_rs/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
pub mod bitcoin;
#[cfg(feature = "ethereum")]
pub mod ethereum;
// Test file for FFI
// pub mod ffi_test;
#[cfg(feature = "solana")]
pub mod solana;
#[cfg(feature = "ton")]
Expand Down
Loading