Skip to content

Commit

Permalink
Add ConversionFailed to FfiReturn
Browse files Browse the repository at this point in the history
Signed-off-by: Shanin Roman <shanin1000@yandex.ru>
  • Loading branch information
Erigara committed Aug 9, 2022
1 parent 948eb82 commit 47bc640
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 17 deletions.
8 changes: 5 additions & 3 deletions ffi/derive/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ fn variant_discriminants(enum_: &syn::DataEnum) -> Vec<syn::Expr> {
}

fn derive_try_from_repr_c_for_opaque_item_wrapper(name: &Ident) -> TokenStream2 {
let opaque_item_slice_try_from_repr_c_derive = derive_try_from_repr_c_for_opaque_item_slice(name);
let opaque_item_slice_try_from_repr_c_derive =
derive_try_from_repr_c_for_opaque_item_slice(name);
let opaque_item_vec_try_from_repr_c_derive = derive_try_from_repr_c_for_opaque_item_vec(name);

quote! {
Expand Down Expand Up @@ -146,7 +147,8 @@ fn derive_try_from_repr_c_for_opaque_item_wrapper(name: &Ident) -> TokenStream2
}

fn derive_try_from_repr_c_for_opaque_item(name: &Ident) -> TokenStream2 {
let opaque_item_slice_try_from_repr_c_derive = derive_try_from_repr_c_for_opaque_item_slice(name);
let opaque_item_slice_try_from_repr_c_derive =
derive_try_from_repr_c_for_opaque_item_slice(name);
let opaque_item_vec_try_from_repr_c_derive = derive_try_from_repr_c_for_opaque_item_vec(name);

quote! {
Expand Down Expand Up @@ -460,7 +462,7 @@ fn derive_into_ffi_for_fieldless_enum(enum_name: &Ident, repr: &[syn::NestedMeta
quote! {
impl iroha_ffi::IntoFfi for #enum_name {
type Target = <#ffi_type as iroha_ffi::IntoFfi>::Target;

fn into_ffi(self) -> Self::Target {
(self as #ffi_type).into_ffi()
}
Expand Down
2 changes: 2 additions & 0 deletions ffi/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ macro_rules! def_ffi_fn {
handle_id: $crate::handle::Id,
left_handle_ptr: *const core::ffi::c_void,
right_handle_ptr: *const core::ffi::c_void,
// Pointer to FFI-safe representation of `bool` (u8 or u32 if `wasm` feature is active)
output_ptr: *mut <bool as $crate::IntoFfi>::Target,
) -> $crate::FfiReturn {
$crate::def_ffi_fn!(@catch_unwind {
Expand Down Expand Up @@ -152,6 +153,7 @@ macro_rules! def_ffi_fn {
handle_id: $crate::handle::Id,
left_handle_ptr: *const core::ffi::c_void,
right_handle_ptr: *const core::ffi::c_void,
// Pointer to FFI-safe representation of `Ordering` (i8 or i32 if `wasm` feature is active)
output_ptr: *mut <core::cmp::Ordering as $crate::IntoFfi>::Target,
) -> $crate::FfiReturn {
$crate::def_ffi_fn!(@catch_unwind {
Expand Down
2 changes: 2 additions & 0 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub trait Output: Sized {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i8)]
pub enum FfiReturn {
/// The input argument provided to FFI function can't be converted into inner rust representation.
ConversionFailed = -7,
/// The input argument provided to FFI function has a trap representation.
TrapRepresentation = -6,
/// FFI function execution panicked.
Expand Down
18 changes: 6 additions & 12 deletions ffi/src/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};

use crate::{
slice::{OutBoxedSlice, SliceRef},
AsReprCRef, FfiReturn, IntoFfi, Output, ReprC, TryFromReprC, Result
AsReprCRef, FfiReturn, IntoFfi, Output, ReprC, Result, TryFromReprC,
};

/// Trait that facilitates the implementation of [`IntoFfi`] for vectors of foreign types
Expand All @@ -31,9 +31,9 @@ pub trait TryFromReprCVec<'slice>: Sized {
///
/// # Errors
///
/// * [`FfiResult::ArgIsNull`] - given pointer is null
/// * [`FfiResult::UnknownHandle`] - given id doesn't identify any known handle
/// * [`FfiResult::TrapRepresentation`] - given value contains trap representation
/// * [`FfiReturn::ArgIsNull`] - given pointer is null
/// * [`FfiReturn::UnknownHandle`] - given id doesn't identify any known handle
/// * [`FfiReturn::TrapRepresentation`] - given value contains trap representation
///
/// # Safety
///
Expand Down Expand Up @@ -192,10 +192,7 @@ impl<'itm> TryFromReprC<'itm> for String {
type Source = <Vec<u8> as TryFromReprC<'itm>>::Source;
type Store = ();

unsafe fn try_from_repr_c(
source: Self::Source,
_: &mut Self::Store,
) -> Result<Self> {
unsafe fn try_from_repr_c(source: Self::Source, _: &mut Self::Store) -> Result<Self> {
String::from_utf8(source.into_rust().ok_or(FfiReturn::ArgIsNull)?.to_owned())
.map_err(|_e| FfiReturn::Utf8Error)
}
Expand All @@ -204,10 +201,7 @@ impl<'itm> TryFromReprC<'itm> for &'itm str {
type Source = <&'itm [u8] as TryFromReprC<'itm>>::Source;
type Store = ();

unsafe fn try_from_repr_c(
source: Self::Source,
_: &mut Self::Store,
) -> Result<Self> {
unsafe fn try_from_repr_c(source: Self::Source, _: &mut Self::Store) -> Result<Self> {
core::str::from_utf8(source.into_rust().ok_or(FfiReturn::ArgIsNull)?)
.map_err(|_e| FfiReturn::Utf8Error)
}
Expand Down
2 changes: 1 addition & 1 deletion ffi/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ macro_rules! primitive_impls {
type Store = ();

unsafe fn try_from_repr_c(source: Self::Source, _: &mut Self::Store) -> Result<Self> {
source.try_into().map_err(|_| FfiReturn::TrapRepresentation)
source.try_into().map_err(|_| FfiReturn::ConversionFailed)
}
}

Expand Down
20 changes: 20 additions & 0 deletions ffi/tests/ffi_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ impl FfiStruct {
}
}

#[ffi_export]
/// Return byte
pub fn simple(byte: u8) -> u8 {
byte
}

fn get_new_struct() -> FfiStruct {
let name = Name(String::from("X"));

Expand Down Expand Up @@ -296,3 +302,17 @@ fn return_result() {
assert_eq!(42, output.assume_init());
}
}

#[cfg(feature = "wasm")]
#[test]
fn conversion_failed() {
let byte: u32 = u32::MAX;
let mut output = MaybeUninit::new(0);

unsafe {
assert_eq!(
FfiReturn::ConversionFailed,
__simple(byte, output.as_mut_ptr())
)
}
}
1 change: 0 additions & 1 deletion ffi/tests/gen_shared_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ fn gen_shared_fns() {
TryFromReprC::try_from_repr_c(is_equal.assume_init(), &mut ()).unwrap();
assert!(is_equal);

// TODO: Fix
let mut ordering = MaybeUninit::new(1);
__ord(
FfiStruct1::ID,
Expand Down

0 comments on commit 47bc640

Please sign in to comment.