Skip to content
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
8 changes: 4 additions & 4 deletions fixtures/arithmetic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub fn multiply(left: u32, right: u32) -> u32 {
left * right
}

#[uniffi::export]
pub fn devide(left: u32, right: u32) -> Option<u32> {
left.checked_div(right)
}
// #[uniffi::export]
// pub fn devide(left: u32, right: u32) -> Option<u32> {
// left.checked_div(right)
// }

#[uniffi::export]
pub fn add_u8(left: u8, right: u8) -> u8 {
Expand Down
14 changes: 7 additions & 7 deletions fixtures/arithmetic/test/simple_arithmetic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ void main() {
expect(api.multiply(2, 8), 16);
});
test('u8', () {
expect(api.add_u8(2, 2), 4);
expect(api.addU8(2, 2), 4);
});
test('u16', () {
expect(api.add_u16(2, 2), 4);
expect(api.addU16(2, 2), 4);
});
test('u64', () {
expect(api.add_u64(2, 2), 4);
expect(api.addU64(2, 2), 4);
});

test('i8', () {
expect(api.add_i8(2, 2), 4);
expect(api.addI8(2, 2), 4);
});
test('i16', () {
expect(api.add_i16(2, 2), 4);
expect(api.addI16(2, 2), 4);
});
test('i32', () {
expect(api.add_i32(2, 2), 4);
expect(api.addI32(2, 2), 4);
});
test('i64', () {
expect(api.add_i64(2, 2), 4);
expect(api.addI64(2, 2), 4);
});
}
1 change: 1 addition & 0 deletions src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use uniffi_bindgen::{BindingGenerator, BindingGeneratorConfig, ComponentInterfac
mod enums;
mod functions;
mod objects;
mod primitives;
mod records;
mod types;
mod utils;
Expand Down
77 changes: 77 additions & 0 deletions src/gen/primitives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use paste::paste;
use uniffi_bindgen::backend::{CodeOracle, CodeType, Literal};
use uniffi_bindgen::interface::{types::Type, Radix};

fn render_literal(_oracle: &dyn CodeOracle, literal: &Literal) -> String {
fn typed_number(type_: &Type, num_str: String) -> String {
match type_ {
Type::Int8
| Type::UInt8
| Type::Int16
| Type::UInt16
| Type::Int32
| Type::UInt32
| Type::UInt64
| Type::Float32
| Type::Float64 => num_str,
_ => panic!("Unexpected literal: {} is not a number", num_str),
}
}

match literal {
Literal::Boolean(v) => format!("{}", v),
Literal::String(s) => format!("\"{}\"", s),
Literal::Int(i, radix, type_) => typed_number(
type_,
match radix {
Radix::Octal => format!("{:#x}", i),
Radix::Decimal => format!("{}", i),
Radix::Hexadecimal => format!("{:#x}", i),
},
),
Literal::UInt(i, radix, type_) => typed_number(
type_,
match radix {
Radix::Octal => format!("{:#x}", i),
Radix::Decimal => format!("{}", i),
Radix::Hexadecimal => format!("{:#x}", i),
},
),
Literal::Float(string, type_) => typed_number(type_, string.clone()),

_ => unreachable!("Literal"),
}
}

macro_rules! impl_code_type_for_primitive {
($T:ty, $class_name:literal) => {
paste! {
#[derive(Debug)]
pub struct $T;

impl CodeType for $T {
fn type_label(&self, _oracle: &dyn CodeOracle) -> String {
$class_name.into()
}

fn literal(&self, oracle: &dyn CodeOracle, literal: &Literal) -> String {
render_literal(oracle, &literal)
}
}
}
};
}

impl_code_type_for_primitive!(BooleanCodeType, "bool");
impl_code_type_for_primitive!(StringCodeType, "String");
impl_code_type_for_primitive!(BytesCodeType, "Uint8List");
impl_code_type_for_primitive!(Int8CodeType, "int");
impl_code_type_for_primitive!(Int16CodeType, "int");
impl_code_type_for_primitive!(Int32CodeType, "int");
impl_code_type_for_primitive!(Int64CodeType, "int");
impl_code_type_for_primitive!(UInt8CodeType, "int");
impl_code_type_for_primitive!(UInt16CodeType, "int");
impl_code_type_for_primitive!(UInt32CodeType, "int");
impl_code_type_for_primitive!(UInt64CodeType, "int");
impl_code_type_for_primitive!(Float32CodeType, "double");
impl_code_type_for_primitive!(Float64CodeType, "double");
48 changes: 44 additions & 4 deletions src/gen/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ pub fn generate_ffi_type(ret: Option<&FfiType>) -> dart::Tokens {
return quote!(Void)
};
match *ret_type {
FfiType::UInt8 => quote!(Uint8),
FfiType::UInt16 => quote!(Uint16),
FfiType::UInt32 => quote!(Uint32),
FfiType::Int8 => quote!(Uint8),
FfiType::UInt64 => quote!(Uint64),
FfiType::Int8 => quote!(Int8),
FfiType::Int16 => quote!(Int16),
FfiType::Int32 => quote!(Int32),
FfiType::Int64 => quote!(Int64),
FfiType::RustBuffer(ref inner) => match inner {
Some(i) => quote!($i),
_ => quote!(RustBuffer),
Expand All @@ -22,8 +28,14 @@ pub fn generate_ffi_dart_type(ret: Option<&FfiType>) -> dart::Tokens {
return quote!(void)
};
match *ret_type {
FfiType::UInt8 => quote!(int),
FfiType::UInt16 => quote!(int),
FfiType::UInt32 => quote!(int),
FfiType::UInt64 => quote!(int),
FfiType::Int8 => quote!(int),
FfiType::Int16 => quote!(int),
FfiType::Int32 => quote!(int),
FfiType::Int64 => quote!(int),
FfiType::RustBuffer(ref inner) => match inner {
Some(i) => quote!($i),
_ => quote!(RustBuffer),
Expand All @@ -35,7 +47,16 @@ pub fn generate_ffi_dart_type(ret: Option<&FfiType>) -> dart::Tokens {

pub fn generate_type(ty: &Type) -> dart::Tokens {
match ty {
Type::UInt8 | Type::UInt32 => quote!(int),
Type::UInt8
| Type::UInt32
| Type::Int8
| Type::Int16
| Type::Int64
| Type::UInt16
| Type::Int32
| Type::UInt64
| Type::Float32
| Type::Float64 => quote!(int),
Type::String => quote!(String),
Type::Object(name) => quote!($name),
Type::Boolean => quote!(bool),
Expand Down Expand Up @@ -87,7 +108,16 @@ pub fn convert_to_rust_buffer(ty: &Type, inner: dart::Tokens) -> dart::Tokens {

pub fn type_lift_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
match ty {
Type::UInt32 => inner,
Type::Int8
| Type::UInt8
| Type::Int16
| Type::UInt16
| Type::Int32
| Type::Int64
| Type::UInt32
| Type::UInt64
| Type::Float32
| Type::Float64 => inner,
Type::Boolean => quote!(($inner) > 0),
Type::String => quote!(liftString(api, $inner)),
Type::Object(name) => quote!($name.lift(api, $inner)),
Expand All @@ -100,7 +130,17 @@ pub fn type_lift_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {

pub fn type_lower_fn(ty: &Type, inner: dart::Tokens) -> dart::Tokens {
match ty {
Type::UInt32 | Type::Boolean => inner,
Type::UInt32
| Type::Int8
| Type::UInt8
| Type::Int16
| Type::UInt16
| Type::Int32
| Type::Int64
| Type::UInt64
| Type::Float32
| Type::Float64
| Type::Boolean => inner,
Type::String => quote!(lowerString(api, $inner)),
Type::Object(name) => quote!($name.lower(api, $inner)),
Type::Optional(o) => {
Expand Down