Skip to content

Commit

Permalink
Additional type system support (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 6, 2021
1 parent 5ca7704 commit 4a29151
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
54 changes: 52 additions & 2 deletions crates/gen/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,78 @@ use squote::{quote, TokenStream};
pub struct Function {
pub name: TypeName,
pub method: winmd::MethodDef,
pub params: Vec<(&'static str, Type)>,
pub return_type: Option<Type>,
}

impl Function {
pub fn new(name: TypeName, method: &winmd::MethodDef) -> Self {
let mut blob = method.sig();

if blob.read_unsigned() & 0x10 != 0 {
blob.read_unsigned();
}

let param_count = blob.read_unsigned();
blob.read_modifiers();
blob.read_expected(0x10);

let mut params = Vec::with_capacity(param_count as usize);

let return_type = if blob.read_expected(0x01) {
None
} else {
Some(Type::from_blob(&mut blob, &[], name.namespace))
};

for param in method.params() {
if return_type.is_none() || param.sequence() != 0 {
blob.read_modifiers(); // const
blob.read_expected(0x10); // ref
params.push((
param.name(),
Type::from_blob(&mut blob, &[], name.namespace),
));
}
}

Self {
name,
method: *method,
params,
return_type,
}
}

pub fn gen(&self) -> TokenStream {
let name = format_ident(self.method.name());

let return_type = if let Some(t) = &self.return_type {
let tokens = t.gen_field();
quote! { -> #tokens }
} else {
TokenStream::new()
};

let params = self.params.iter().map(|(name, t)| {
let name = format_ident(name);
let tokens = t.gen_field();
quote! { #name: #tokens }
});

quote! {
#[link(name = "onecoreuap")]
extern "system" {
pub fn #name();
pub fn #name(#(#params),*) #return_type;
}
}
}

pub fn dependencies(&self) -> Vec<winmd::TypeDef> {
Vec::new()
self.return_type
.iter()
.chain(self.params.iter().map(|(_, t)| t))
.flat_map(|t| t.kind.dependencies())
.collect()
}
}
3 changes: 2 additions & 1 deletion crates/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ fn main() {
windows::win32::{
ACCESS_MODE, CHOOSECOLORW, DXGI_ADAPTER_FLAG, DXGI_FORMAT, DXGI_MODE_DESC, DXGI_MODE_SCALING,
DXGI_MODE_SCANLINE_ORDER, DXGI_RATIONAL, RECT, WM_KEYUP, ALLJOYN_BIG_ENDIAN, ALLJOYN_CRED_CERT_CHAIN,
D3D12_DEFAULT_BLEND_FACTOR_ALPHA, UIA_ScrollPatternNoScroll, D3DCOMPILER_DLL
D3D12_DEFAULT_BLEND_FACTOR_ALPHA, UIA_ScrollPatternNoScroll, D3DCOMPILER_DLL,
CreateEventW, SetEvent, WaitForSingleObject, CloseHandle
}
);
}
25 changes: 21 additions & 4 deletions crates/tests/tests/win32.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use tests::windows::win32::{
UIA_ScrollPatternNoScroll, ACCESS_MODE, ALLJOYN_BIG_ENDIAN, ALLJOYN_CRED_CERT_CHAIN,
CHOOSECOLORW, D3D12_DEFAULT_BLEND_FACTOR_ALPHA, D3DCOMPILER_DLL, DXGI_ADAPTER_FLAG,
DXGI_FORMAT, DXGI_MODE_DESC, DXGI_MODE_SCALING, DXGI_MODE_SCANLINE_ORDER, DXGI_RATIONAL, RECT,
WM_KEYUP,
CloseHandle, CreateEventW, SetEvent, UIA_ScrollPatternNoScroll, WaitForSingleObject,
ACCESS_MODE, ALLJOYN_BIG_ENDIAN, ALLJOYN_CRED_CERT_CHAIN, CHOOSECOLORW,
D3D12_DEFAULT_BLEND_FACTOR_ALPHA, D3DCOMPILER_DLL, DXGI_ADAPTER_FLAG, DXGI_FORMAT,
DXGI_MODE_DESC, DXGI_MODE_SCALING, DXGI_MODE_SCANLINE_ORDER, DXGI_RATIONAL, RECT, WM_KEYUP,
};
use winrt::Abi;

Expand Down Expand Up @@ -96,3 +96,20 @@ fn constant() {
assert!(UIA_ScrollPatternNoScroll == -1f64);
assert!(D3DCOMPILER_DLL == "d3dcompiler_47.dll");
}

#[test]
fn function() {
unsafe {
let event = CreateEventW(std::ptr::null_mut(), 1, 0, std::ptr::null_mut());
assert!(event != 0);

let result = SetEvent(event);
assert!(result != 0);

let result = WaitForSingleObject(event, 0);
assert!(result == 0); // https://github.com/microsoft/win32metadata/issues/77

let result = CloseHandle(event);
assert!(result != 0);
}
}
2 changes: 1 addition & 1 deletion crates/winmd/src/parsed/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Param {
self.reader.u32(self.row, 1)
}

pub fn name(&self) -> &str {
pub fn name(&self) -> &'static str {
self.reader.str(self.row, 2)
}
}
Expand Down

0 comments on commit 4a29151

Please sign in to comment.