_ctypes pt. 3 by arihant2math · Pull Request #5530 · RustPython/RustPython
Conversation
Here comes the meat of the code and the hardest part implementation-wise.
The main goal is to implement (atleast partially) CFuncPtr. By partially, I mean that not supporting arguments to functions is probably fine. At any rate the cpython implementation can be used as a reference since they use libffi. The main challenge is determining the return type of the loaded function from the cvoid it'll return (unless we don't have to do that for some reason).
- Use libffi to manage functions (Can't pass in arbitrary arguments to functions through libloading)
- Constructor for
CFuncPtr - Give inner function class the ability to load functions
- Give it the ability to call them and get the return
- Cast the return properly
- Callable for
CFuncPtrthat encapsulated the entire thing
The next pr can probably just do some cleanup on this and get the basics for struct and union return types working.
Known issues
- Arguments cause errors to be thrown
- Return type of functions is
c_intand cannot be changed - Error handling isn't great.
import ctypesfails- creating string buffers is broken
Some updates:
I've gotten arbitrary function argument passing to work on a test, I still am figuring out return type inference however.
Here is some test code for windows that works:
use std::ffi::c_void; use std::path::Path; use libffi::middle as libffi; use libloading::Symbol; type FP = unsafe extern "C" fn (); fn main() { let path = "C:\\Windows\\System32\\kernel32.dll"; let path = Path::new(path); let lib = unsafe { libloading::Library::new(path) }.unwrap(); let function_bytes = b"Sleep\0"; let function: Symbol<FP> = unsafe { lib.get(function_bytes) }.unwrap(); let function = *function; let args = vec![libffi::Type::u32()]; let cif = libffi::Cif::new(args.into_iter(), libffi::Type::void()); let ms_wait: u32 = 5000; println!("Calling function"); let now = std::time::Instant::now(); let _res: *const c_void = unsafe { cif.call(libffi::CodePtr(function as *mut _), &[libffi::arg(&ms_wait)]) }; println!("Function returned in {}ms", now.elapsed().as_millis()); println!("Function called"); }
Calling function
Function returned in 5001ms
Function called
Ready for review.
The current working scope is no-argument functions that return integers.
(this is from the extra test)
libc = cdll.msvcrt print("rand", libc.rand())
/cc @youknowone
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you so much!
Please check if the extra indent is intended or not.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍