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

Raw type system support for strings #85

Merged
merged 4 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Implement CString/CStr support for Raw type system
  • Loading branch information
Rantanen committed Oct 13, 2018
commit e0a7450d288c5ed202c7d3833cead4ddf2e0fa36
1 change: 1 addition & 0 deletions intercom-attributes/tests/data/com_impl.source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Foo
fn tuple_result_method(&self) -> Result<(u8, u16, u32), i32> { Ok(0) }

fn string_method(&self, input : String) -> String { input }
fn string_result_method(&self, input : String) -> ComResult<String> { Ok(input) }

fn complete_method(&mut self, a: u16, b: i16) -> ComResult<bool>
{
Expand Down
118 changes: 110 additions & 8 deletions intercom-attributes/tests/data/com_impl.target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ impl Foo {
fn tuple_result_method(&self) -> Result<(u8, u16, u32), i32> { Ok(0) }

fn string_method(&self, input: String) -> String { input }
fn string_result_method(&self, input: String) -> ComResult<String> {
Ok(input)
}

fn complete_method(&mut self, a: u16, b: i16) -> ComResult<bool> {
Ok(true)
Expand Down Expand Up @@ -370,7 +373,9 @@ unsafe extern "C" fn __Foo_Foo_Automation_string_method_Automation(self_vtable:
let __result =
self_struct.string_method(<String as
::intercom::FromWithTemporary<&::intercom::BStr>>::from_temporary(&mut __input_temporary)?);
Ok({ ::intercom::BString::from(__result).into_ptr() })
Ok({
::intercom::ComInto::<::intercom::BString>::com_into(__result)?.into_ptr()
})
})();
use ::intercom::ErrorValue;
match result {
Expand All @@ -383,6 +388,52 @@ unsafe extern "C" fn __Foo_Foo_Automation_string_method_Automation(self_vtable:
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
unsafe extern "C" fn __Foo_Foo_Automation_string_result_method_Automation(self_vtable:
::intercom::RawComPtr,
input:
::intercom::raw::InBSTR,
__out:
*mut ::intercom::raw::OutBSTR)
-> ::intercom::HRESULT {
let result: Result<::intercom::HRESULT, ::intercom::ComError> =
(||
{
let self_combox =
(self_vtable as usize -
__Foo_Foo_AutomationVtbl_offset()) as
*mut ::intercom::ComBox<Foo>;
let mut __input_temporary =
<String as
::intercom::FromWithTemporary<&::intercom::BStr>>::to_temporary(::intercom::BStr::from_ptr(input))?;
let self_struct: &Foo = &**self_combox;
let __result =
self_struct.string_result_method(<String as
::intercom::FromWithTemporary<&::intercom::BStr>>::from_temporary(&mut __input_temporary)?);
Ok({
match __result {
Ok(v1) => {
*__out =
::intercom::ComInto::<::intercom::BString>::com_into(v1)?.into_ptr();
::intercom::S_OK
}
Err(e) => {
*__out = ::std::ptr::null_mut();
::intercom::return_hresult(e)
}
}
})
})();
use ::intercom::ErrorValue;
match result {
Ok(v) => v,
Err(err) =>
<::intercom::HRESULT as
ErrorValue>::from_error(::intercom::return_hresult(err)),
}
}
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
unsafe extern "C" fn __Foo_Foo_Automation_complete_method_Automation(self_vtable:
::intercom::RawComPtr,
a:
Expand Down Expand Up @@ -443,6 +494,8 @@ const __Foo_Foo_AutomationVtbl_INSTANCE: __Foo_AutomationVtbl =
__Foo_Foo_Automation_tuple_result_method_Automation,
string_method_Automation:
__Foo_Foo_Automation_string_method_Automation,
string_result_method_Automation:
__Foo_Foo_Automation_string_result_method_Automation,
complete_method_Automation:
__Foo_Foo_Automation_complete_method_Automation,};
#[allow(non_snake_case)]
Expand Down Expand Up @@ -667,28 +720,75 @@ unsafe extern "C" fn __Foo_Foo_Raw_tuple_result_method_Raw(self_vtable:
unsafe extern "C" fn __Foo_Foo_Raw_string_method_Raw(self_vtable:
::intercom::RawComPtr,
input:
::intercom::raw::InBSTR)
-> ::intercom::raw::OutBSTR {
let result: Result<::intercom::raw::OutBSTR, ::intercom::ComError> =
::intercom::raw::InCStr)
-> ::intercom::raw::OutCStr {
let result: Result<::intercom::raw::OutCStr, ::intercom::ComError> =
(||
{
let self_combox =
(self_vtable as usize - __Foo_Foo_RawVtbl_offset()) as
*mut ::intercom::ComBox<Foo>;
let mut __input_temporary =
<String as
::intercom::FromWithTemporary<&::intercom::BStr>>::to_temporary(::intercom::BStr::from_ptr(input))?;
::intercom::FromWithTemporary<&::intercom::CStr>>::to_temporary(::intercom::CStr::from_ptr(input))?;
let self_struct: &Foo = &**self_combox;
let __result =
self_struct.string_method(<String as
::intercom::FromWithTemporary<&::intercom::BStr>>::from_temporary(&mut __input_temporary)?);
Ok({ ::intercom::BString::from(__result).into_ptr() })
::intercom::FromWithTemporary<&::intercom::CStr>>::from_temporary(&mut __input_temporary)?);
Ok({
::intercom::ComInto::<::intercom::CString>::com_into(__result)?.into_raw()
})
})();
use ::intercom::ErrorValue;
match result {
Ok(v) => v,
Err(err) =>
<::intercom::raw::OutBSTR as
<::intercom::raw::OutCStr as
ErrorValue>::from_error(::intercom::return_hresult(err)),
}
}
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
unsafe extern "C" fn __Foo_Foo_Raw_string_result_method_Raw(self_vtable:
::intercom::RawComPtr,
input:
::intercom::raw::InCStr,
__out:
*mut ::intercom::raw::OutCStr)
-> ::intercom::HRESULT {
let result: Result<::intercom::HRESULT, ::intercom::ComError> =
(||
{
let self_combox =
(self_vtable as usize - __Foo_Foo_RawVtbl_offset()) as
*mut ::intercom::ComBox<Foo>;
let mut __input_temporary =
<String as
::intercom::FromWithTemporary<&::intercom::CStr>>::to_temporary(::intercom::CStr::from_ptr(input))?;
let self_struct: &Foo = &**self_combox;
let __result =
self_struct.string_result_method(<String as
::intercom::FromWithTemporary<&::intercom::CStr>>::from_temporary(&mut __input_temporary)?);
Ok({
match __result {
Ok(v1) => {
*__out =
::intercom::ComInto::<::intercom::CString>::com_into(v1)?.into_raw();
::intercom::S_OK
}
Err(e) => {
*__out = ::std::ptr::null_mut();
::intercom::return_hresult(e)
}
}
})
})();
use ::intercom::ErrorValue;
match result {
Ok(v) => v,
Err(err) =>
<::intercom::HRESULT as
ErrorValue>::from_error(::intercom::return_hresult(err)),
}
}
Expand Down Expand Up @@ -746,5 +846,7 @@ const __Foo_Foo_RawVtbl_INSTANCE: __Foo_RawVtbl =
tuple_result_method_Raw:
__Foo_Foo_Raw_tuple_result_method_Raw,
string_method_Raw: __Foo_Foo_Raw_string_method_Raw,
string_result_method_Raw:
__Foo_Foo_Raw_string_result_method_Raw,
complete_method_Raw: __Foo_Foo_Raw_complete_method_Raw,};
impl ::intercom::HasInterface<Foo> for Foo { }
4 changes: 2 additions & 2 deletions intercom-attributes/tests/data/com_interface.target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ pub struct __Foo_RawVtbl {
pub string_method_Raw: unsafe extern "C" fn(self_vtable:
::intercom::RawComPtr,
msg:
::intercom::raw::InBSTR)
-> ::intercom::raw::OutBSTR,
::intercom::raw::InCStr)
-> ::intercom::raw::OutCStr,
pub comitf_method_Raw: unsafe extern "C" fn(self_vtable:
::intercom::RawComPtr,
itf:
Expand Down
1 change: 1 addition & 0 deletions intercom-common/src/generators/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl<'s> CppTypeInfo<'s> for TypeInfo<'s> {
match type_name.as_str() {
"RawComPtr" => "*void".to_owned(),
"InBSTR" | "OutBSTR" => "intercom::BSTR".to_owned(),
"InCStr" | "OutCStr" => "char*".to_owned(),
"usize" => "size_t".to_owned(),
"i8" => "int8_t".to_owned(),
"u8" => "uint8_t".to_owned(),
Expand Down
1 change: 1 addition & 0 deletions intercom-common/src/generators/idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ impl<'s> IdlTypeInfo<'s> for TypeInfo<'s> {
match type_name.as_str() {
"RawComPtr" => "void*".to_owned(),
"InBSTR" | "OutBSTR" => "BSTR".to_owned(),
"InCStr" | "OutCStr" => "char*".to_owned(),
"usize" => "size_t".to_owned(),
"u64" => "uint64".to_owned(),
"i64" => "int64".to_owned(),
Expand Down
11 changes: 10 additions & 1 deletion intercom-common/src/returnhandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use utils;
/// Defines return handler for handling various different return type schemes.
pub trait ReturnHandler : ::std::fmt::Debug {

/// Returns the current type system. Used internally by the trait.
fn type_system( &self ) -> ModelTypeSystem;

/// The return type of the original Rust method.
fn rust_ty( &self ) -> Type;

Expand All @@ -18,7 +21,7 @@ pub trait ReturnHandler : ::std::fmt::Debug {
&self.rust_ty(),
TypeContext::new(
Direction::Retval,
ModelTypeSystem::Automation ),
self.type_system() ),
).com_ty()
}

Expand All @@ -39,13 +42,18 @@ pub trait ReturnHandler : ::std::fmt::Debug {
struct VoidHandler;
impl ReturnHandler for VoidHandler {
fn rust_ty( &self ) -> Type { utils::unit_ty() }

// Void types do not depend on the type system.
fn type_system( &self ) -> ModelTypeSystem { ModelTypeSystem::Automation }
}

/// Simple return type with the return value as the immediate value.
#[derive(Debug)]
struct ReturnOnlyHandler( Type, ModelTypeSystem );
impl ReturnHandler for ReturnOnlyHandler {

fn type_system( &self ) -> ModelTypeSystem { self.1 }

fn rust_ty( &self ) -> Type { self.0.clone() }

fn com_to_rust_return( &self, result : &Ident ) -> TokenStream {
Expand Down Expand Up @@ -86,6 +94,7 @@ struct ErrorResultHandler {

impl ReturnHandler for ErrorResultHandler {

fn type_system( &self ) -> ModelTypeSystem { self.type_system }
fn rust_ty( &self ) -> Type { self.return_ty.clone() }
fn com_ty( &self ) -> Type { parse_quote!( ::intercom::HRESULT ) }

Expand Down
63 changes: 54 additions & 9 deletions intercom-common/src/tyhandlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,15 @@ impl TypeHandler for StringParam

fn com_ty( &self ) -> Type
{
match self.context.dir {
Direction::In => parse_quote!( ::intercom::raw::InBSTR ),
Direction::Out | Direction::Retval => parse_quote!( ::intercom::raw::OutBSTR ),
match self.context.type_system {
ModelTypeSystem::Automation => match self.context.dir {
Direction::In => parse_quote!( ::intercom::raw::InBSTR ),
Direction::Out | Direction::Retval => parse_quote!( ::intercom::raw::OutBSTR ),
},
ModelTypeSystem::Raw => match self.context.dir {
Direction::In => parse_quote!( ::intercom::raw::InCStr ),
Direction::Out | Direction::Retval => parse_quote!( ::intercom::raw::OutCStr ),
},
}
}

Expand All @@ -218,9 +224,14 @@ impl TypeHandler for StringParam

Direction::In => {

let str_type = match self.context.type_system {
ModelTypeSystem::Automation => quote!( BStr ),
ModelTypeSystem::Raw => quote!( CStr ),
};

let target_ty = self.rust_ty();
let intermediate_ty = quote!( &::intercom::BStr );
let to_intermediate = quote!( ::intercom::BStr::from_ptr( #ident ) );
let intermediate_ty = quote!( &::intercom::#str_type );
let to_intermediate = quote!( ::intercom::#str_type::from_ptr( #ident ) );
let as_trait = quote!( < #target_ty as ::intercom::FromWithTemporary< #intermediate_ty > > );

let temp_ident = Ident::new( &format!( "__{}_temporary", ident.to_string() ), Span::call_site() );
Expand All @@ -230,9 +241,21 @@ impl TypeHandler for StringParam
}
},
Direction::Out | Direction::Retval => {

// The type system input 'ident' should represent either a BString or CString
// depending on the type system.
let str_type = match self.context.type_system {
ModelTypeSystem::Automation => quote!( BString ),
ModelTypeSystem::Raw => quote!( CString ),
};

// Get the type system string as Rust string.
let ts_string = quote!( ::intercom::#str_type::from_ptr( #ident ) );

// Convert the TS string into whatever string type the method requires.
TypeConversion {
temporary: None,
value: quote!( ::intercom::BString::from_ptr( #ident ).com_into()? ),
value: quote!( #ts_string.com_into()? ),
}
},
}
Expand All @@ -244,8 +267,13 @@ impl TypeHandler for StringParam

Direction::In => {

let str_type = match self.context.type_system {
ModelTypeSystem::Automation => quote!( BStr ),
ModelTypeSystem::Raw => quote!( CStr ),
};

let target_ty = self.rust_ty();
let intermediate_ty = quote!( &::intercom::BStr );
let intermediate_ty = quote!( &::intercom::#str_type );
let as_trait = quote!( < #intermediate_ty as ::intercom::FromWithTemporary< #target_ty > > );

let temp_ident = Ident::new( &format!( "__{}_temporary", ident.to_string() ), Span::call_site() );
Expand All @@ -255,9 +283,26 @@ impl TypeHandler for StringParam
}
},
Direction::Out | Direction::Retval => {

// The Rust string value `ident` must be first converted into a type system
// compatible Rust string, either BString or CSTring.
// depending on the type system.
let str_type = match self.context.type_system {
ModelTypeSystem::Automation => quote!( BString ),
ModelTypeSystem::Raw => quote!( CString ),
};

// Convert the `ident` into the required string type.
let ts_string = quote!( ::intercom::ComInto::< ::intercom::#str_type >::com_into( #ident )? );

TypeConversion {
temporary: None,
value: quote!( ::intercom::BString::from( #ident ).into_ptr() ),
value: match self.context.type_system {
ModelTypeSystem::Automation =>
quote!( #ts_string.into_ptr() ),
ModelTypeSystem::Raw =>
quote!( #ts_string.into_raw() ),
}
}
},
}
Expand Down Expand Up @@ -298,7 +343,7 @@ fn map_by_name(
match name {

"ComItf" => Rc::new( ComItfParam { ty: original_type, context } ),
"BString" | "BStr" | "String" | "str" =>
"CString" | "CStr" | "BString" | "BStr" | "String" | "str" =>
Rc::new( StringParam { ty: original_type, context } ),
// "str" => Rc::new( StringRefParam( original_type ) ),

Expand Down
3 changes: 3 additions & 0 deletions intercom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ pub type REFCLSID = *const IID;
pub mod raw {
pub type InBSTR = *const u16;
pub type OutBSTR = *mut u16;

pub type InCStr = *const ::std::os::raw::c_char;
pub type OutCStr = *mut ::std::os::raw::c_char;

#[repr(C)]
pub struct InterfacePtr<I: ?Sized> {
Expand Down
Loading