Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
41b30de
metaclass support for #[pyclass] macro
youknowone Aug 7, 2021
b4a1b80
Initial ctypes module structure with dlopen and dlsym
rodrigocam Oct 29, 2020
b9ada0a
Refactoring ctypes module to a PyPy-like file tree
darleybarreto Oct 31, 2020
1fa7e80
Add __getattr__ to deal with attributes at runtime
rodrigocam Oct 31, 2020
b30dbae
Adding a very basic and initial implementation of _SimpleCData and Fu…
darleybarreto Nov 2, 2020
89587f0
Add PyFuncPtr tp_new from DLL and basic tp_call
rodrigocam Nov 3, 2020
b4ddf4d
Adding basic functions to call libffi-rs
darleybarreto Nov 4, 2020
9177473
Fix compile errors
rodrigocam Nov 5, 2020
14b08b9
Changing some pieces of SharedLibrary
darleybarreto Nov 7, 2020
8adf4dd
Fixing some ref problems in functions.rs
darleybarreto Nov 8, 2020
4341c97
Cahnge PyRc to PyRef in data cache
rodrigocam Nov 10, 2020
eb8541c
Fixing arg type casting
darleybarreto Nov 10, 2020
e38b0a8
Refactoring PyCFuncPtr
darleybarreto Nov 12, 2020
c2df5de
Moving dlsym to from_dll
darleybarreto Nov 12, 2020
fa6b19b
Adding proper *mut c_void casting
darleybarreto Nov 13, 2020
7d220c3
Adding 'reopen' lib
darleybarreto Nov 13, 2020
0a92095
Adding function call
darleybarreto Nov 13, 2020
8ceffd9
Fixing clippy warnings
darleybarreto Nov 14, 2020
4c585ab
Fixing dangling ref
darleybarreto Nov 15, 2020
b09ebe0
Starting primitive types impl
darleybarreto Nov 19, 2020
d7888f9
Adding metaclass
darleybarreto Nov 19, 2020
145afc7
Adding PyCDataMethods trait
darleybarreto Nov 21, 2020
aff291f
Adding default value for PySimpleType
darleybarreto Nov 22, 2020
d29c3bf
Adding some comments
darleybarreto Nov 23, 2020
6fdffab
Implement PySimpleType __init__
darleybarreto Nov 26, 2020
568c838
Modifying Buffer for PyCData
darleybarreto Nov 29, 2020
fb07e53
Adding methods for PyCDataMethods
darleybarreto Nov 30, 2020
6122e40
Adding PyCData_NewGetBuffer related code
darleybarreto Dec 4, 2020
6a4f881
Fixing small fixes
darleybarreto Dec 4, 2020
e2e0ac8
Testing PySimpleType basic functionalities
darleybarreto Dec 5, 2020
c1324ce
Refactoring SharedLibrary
darleybarreto Dec 5, 2020
3f40466
Fixing several bugs
darleybarreto Dec 5, 2020
3e52625
Fixing function call
darleybarreto Dec 6, 2020
3454751
Fixing more of function calls
darleybarreto Dec 6, 2020
0f25f4d
PySimpleType from_param initial commit
darleybarreto Dec 7, 2020
c4f27fb
Adding more methods to PySimpleType
darleybarreto Dec 7, 2020
b88e544
Minor fixes to get compiling on master
coolreader18 Dec 9, 2020
4ce1123
Use static_cell for libcache
coolreader18 Dec 13, 2020
e528fe7
Adding RawBuffer & reworking low level function call
darleybarreto Dec 19, 2020
420a67a
Small fixes
darleybarreto Dec 21, 2020
d656650
Initial commit for PyCArray
darleybarreto Dec 21, 2020
2a1f6d7
Reworking CData buffers
darleybarreto Dec 30, 2020
ed44269
Adding PyCArray setitem
darleybarreto Dec 31, 2020
4d2c678
Adding some helper functions and initial tests
darleybarreto Dec 31, 2020
2c440b3
Adding PyCArray's from_param
darleybarreto Jan 1, 2021
0b65e0d
Adding several changes to make some tests pass
darleybarreto Jan 2, 2021
7c529af
Fix build on master
youknowone Aug 6, 2021
fe01b1a
hide ctypes test
youknowone Aug 6, 2021
25e0403
clean up ctypes::array
youknowone Aug 7, 2021
efabe9b
skeleton PyCSimpleType
youknowone Aug 7, 2021
b79477a
submodule extension for ctypes::dll
youknowone Aug 7, 2021
2904015
Merge pull request #4 from youknowone/dll-submodule
darleybarreto Aug 7, 2021
66a91cb
Add all tests from CPython and PyPy
darleybarreto Aug 8, 2021
577c5ae
Add suggestions and bump dependencies
darleybarreto Aug 8, 2021
6eb0794
Starting to add Metas to primitive and array ctypes
darleybarreto Aug 8, 2021
5916218
Fixing some terribly wrong impls and bugs.
darleybarreto Aug 15, 2021
c2ba116
Fix compilation
darleybarreto Aug 21, 2021
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
Prev Previous commit
Next Next commit
Adding metaclass
  • Loading branch information
darleybarreto authored and youknowone committed Aug 7, 2021
commit d7888f9202cf1cdb0177d12bc555a694ea9998c8
75 changes: 65 additions & 10 deletions vm/src/stdlib/ctypes/basics.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,83 @@
use crate::builtins::PyTypeRef;
use crate::pyobject::{PyRef, PyResult, PyValue, StaticType};
use crate::pyobject::{PyObjectRc, PyRef, PyResult, PyValue, StaticType};
use crate::VirtualMachine;

use crate::stdlib::ctypes::common::PyCData_as_buffer;

// This class is the equivalent of PyCData_Type on tp_base for
// PyCStructType_Type, UnionType_Type, PyCPointerType_Type
// PyCArrayType_Type, PyCSimpleType_Type, PyCFuncPtrType_Type
// Struct_Type, Union_Type, PyCPointer_Type
// PyCArray_Type, PyCSimple_Type, PyCFuncPtr_Type

#[pyclass(module = false, name = "_CData")]
#[derive(Debug)]
pub struct PyCData {}
pub struct PyCData {
_objects: Vec<PyObjectRc>,
}

impl PyValue for PyCData {
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
Self::static_baseclass()
Self::init_bare_type()
}
}

#[pyimpl(flags(BASETYPE))]
#[pyimpl]
impl PyCData {
// Methods here represent PyCData_methods

#[pymethod]
pub fn __ctypes_from_outparam__() {}

#[pymethod]
pub fn __reduce__() {}

#[pymethod]
pub fn __setstate__() {}
}

// #[pyimpl]
// impl PyCData_as_buffer for PyCData {

// }

// This class has no attributes and we care about it's methods
#[pyclass(module = false, name = "_CDataMeta")]
#[derive(Debug)]
pub struct PyCDataMeta;

impl PyValue for PyCDataMeta {
fn class(_vm: &VirtualMachine) -> &PyTypeRef {
Self::static_baseclass()
}
}

#[pyimpl]
impl PyCDataMeta {
// A lot of the logic goes in this trait
// There's also other traits that should have different implementations for some functions
// present here

// #[pyslot]
// fn tp_new(cls: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
// PyCData {}.into_ref_with_type(vm, cls)
// }
// The default methods (representing CDataType_methods) here are for:
// StructType_Type
// UnionType_Type
// PyCArrayType_Type
// PyCFuncPtrType_Type

#[pymethod]
pub fn from_param() {}

#[pymethod]
pub fn from_address() {}

#[pymethod]
pub fn from_buffer() {}

#[pymethod]
pub fn from_buffer_copy() {}

#[pymethod]
pub fn in_dll() {}
}

// CDataType_as_sequence methods are default for all types inherinting from PyCDataMeta
// Basically the sq_repeat slot is CDataType_repeat
// which transforms into a Array
265 changes: 7 additions & 258 deletions vm/src/stdlib/ctypes/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,273 +2,22 @@ extern crate lazy_static;
extern crate libffi;
extern crate libloading;

use ::std::{collections::HashMap, os::raw::*, ptr};
use ::std::{collections::HashMap, os::raw::c_void};

use libffi::low::{
call as ffi_call, ffi_abi_FFI_DEFAULT_ABI as ABI, ffi_cif, ffi_type, prep_cif, CodePtr,
Error as FFIError,
};
use libffi::middle;
use libloading::Library;
use num_bigint::BigInt;

use crate::builtins::PyTypeRef;
use crate::common::lock::PyRwLock;
use crate::pyobject::{PyObjectRc, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject};
use crate::pyobject::{PyRef, PyValue};
use crate::slots::BufferProtocol;
use crate::VirtualMachine;

macro_rules! ffi_type {
($name: ident) => {
middle::Type::$name().as_raw_ptr()
};
// This trait will be used by all types
pub trait PyCData_as_buffer: BufferProtocol {
// @TODO: Translate PyCData_NewGetBuffer
// fn get_buffer(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<Box<dyn Buffer>>;
}

macro_rules! match_ffi_type {
(
$pointer: expr,

$(
$($type: ident)|+ => $body: expr
)+
) => {
match $pointer {
$(
$(
t if t == ffi_type!($type) => { $body }
)+
)+
_ => unreachable!()
}
};
(
$kind: expr,

$(
$($type: tt)|+ => $body: ident
)+
) => {
match $kind {
$(
$(
t if t == $type => { ffi_type!($body) }
)+
)+
_ => unreachable!()
}
}
}

pub fn str_to_type(ty: &str) -> *mut ffi_type {
match_ffi_type!(
ty,
"c" => c_schar
"u" => c_int
"b" => i8
"h" => c_short
"H" => c_ushort
"i" => c_int
"I" => c_uint
"l" => c_long
"q" => c_longlong
"L" => c_ulong
"Q" => c_ulonglong
"f" => f32
"d" => f64
"g" => longdouble
"?" | "B" => c_uchar
"z" | "Z" => pointer
"P" => void
)
}

fn py_to_ffi(ty: *mut *mut ffi_type, obj: PyObjectRef, vm: &VirtualMachine) -> *mut c_void {
match_ffi_type!(
unsafe { *ty },
c_schar => {
let mut r = i8::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
c_int => {
let mut r = i32::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
c_short => {
let mut r = i16::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
c_ushort => {
let mut r = u16::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
c_uint => {
let mut r = u32::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
//@ TODO: Convert c*longlong from BigInt?
c_long | c_longlong => {
let mut r = i64::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
c_ulong | c_ulonglong => {
let mut r = u64::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
f32 => {
let mut r = f32::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
f64 | longdouble=> {
let mut r = f64::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
c_uchar => {
let mut r = u8::try_from_object(vm, obj).unwrap();
&mut r as *mut _ as *mut c_void
}
pointer => {
usize::try_from_object(vm, obj).unwrap() as *mut c_void
}
void => {
ptr::null_mut()
}
)
}

#[derive(Debug)]
pub struct Function {
pointer: *const c_void,
cif: ffi_cif,
arguments: Vec<*mut ffi_type>,
return_type: Box<*mut ffi_type>,
}

impl Function {
pub fn new(fn_ptr: *const c_void, arguments: Vec<String>, return_type: &str) -> Function {
Function {
pointer: fn_ptr,
cif: Default::default(),
arguments: arguments.iter().map(|s| str_to_type(s.as_str())).collect(),

return_type: Box::new(str_to_type(return_type)),
}
}
pub fn set_args(&mut self, args: Vec<String>) {
self.arguments.clear();
self.arguments
.extend(args.iter().map(|s| str_to_type(s.as_str())));
}

pub fn set_ret(&mut self, ret: &str) {
(*self.return_type.as_mut()) = str_to_type(ret);
// mem::replace(self.return_type.as_mut(), str_to_type(ret));
}

pub fn call(
&mut self,
arg_ptrs: Vec<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<PyObjectRc> {
let return_type: *mut ffi_type = &mut unsafe { self.return_type.read() };

let result = unsafe {
prep_cif(
&mut self.cif,
ABI,
self.arguments.len(),
return_type,
self.arguments.as_mut_ptr(),
)
};

if let Err(FFIError::Typedef) = result {
return Err(vm.new_runtime_error(
"The type representation is invalid or unsupported".to_string(),
));
} else if let Err(FFIError::Abi) = result {
return Err(vm.new_runtime_error("The ABI is invalid or unsupported".to_string()));
}

let mut argument_pointers: Vec<*mut c_void> = arg_ptrs
.iter()
.zip(self.arguments.iter_mut())
.map(|(o, t)| {
let tt: *mut *mut ffi_type = t;
py_to_ffi(tt, o.clone(), vm)
})
.collect();

let cif_ptr = &self.cif as *const _ as *mut _;
let fun_ptr = CodePtr::from_ptr(self.pointer);
let args_ptr = argument_pointers.as_mut_ptr();

let ret_ptr = unsafe {
match_ffi_type!(
return_type,
c_schar => {
let r: c_schar = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as i8)
}
c_int => {
let r: c_int = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as i32)
}
c_short => {
let r: c_short = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as i16)
}
c_ushort => {
let r: c_ushort = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as u16)
}
c_uint => {
let r: c_uint = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as u32)
}
c_long => {
let r: c_long = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as i64)
}
c_longlong => {
let r: c_longlong = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(BigInt::from(r as i128))
}
c_ulong => {
let r: c_ulong = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as u64)
}
c_ulonglong => {
let r: c_ulonglong = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(BigInt::from(r as u128))
}
f32 => {
let r: c_float = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as f32)
}
f64 | longdouble => {
let r: c_double = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as f64)
}
c_uchar => {
let r: c_uchar = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as u8)
}
pointer => {
let r: *mut c_void = ffi_call(cif_ptr, fun_ptr, args_ptr);
vm.new_pyobj(r as *const _ as usize)
}
void => {
vm.ctx.none()
}
)
};

Ok(ret_ptr)
}
}

unsafe impl Send for Function {}
unsafe impl Sync for Function {}

#[derive(Debug)]
pub struct SharedLibrary {
path_name: String,
Expand Down
Loading