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
Starting primitive types impl
  • Loading branch information
darleybarreto authored and youknowone committed Aug 7, 2021
commit b09ebe09cfca5523db8fd2577520fcafb37d39d9
29 changes: 16 additions & 13 deletions vm/src/stdlib/ctypes/basics.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use crate::builtins::PyTypeRef;
use crate::pyobject::{PyResult, PyValue, StaticType};
use crate::pyobject::{PyRef, PyResult, PyValue, StaticType};
use crate::VirtualMachine;

use crate::stdlib::ctypes::common::CDataObject;

#[pyclass(module = "_ctypes", name = "_CData")]
// This class is the equivalent of PyCData_Type on tp_base for
// PyCStructType_Type, UnionType_Type, PyCPointerType_Type
// PyCArrayType_Type, PyCSimpleType_Type, PyCFuncPtrType_Type
#[pyclass(module = false, name = "_CData")]
#[derive(Debug)]
pub struct PyCData {
_type_: String,
}
pub struct PyCData {}

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

#[pyimpl]
#[pyimpl(flags(BASETYPE))]
impl PyCData {
#[pymethod(name = "__init__")]
fn init(&self, _vm: &VirtualMachine) -> PyResult<()> {
Ok(())
}
// 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)
// }
}
38 changes: 6 additions & 32 deletions vm/src/stdlib/ctypes/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate lazy_static;
extern crate libffi;
extern crate libloading;

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

use libffi::low::{
call as ffi_call, ffi_abi_FFI_DEFAULT_ABI as ABI, ffi_cif, ffi_type, prep_cif, CodePtr,
Expand All @@ -14,13 +14,9 @@ use num_bigint::BigInt;

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

pub const SIMPLE_TYPE_CHARS: &str = "cbBhHiIlLdfuzZqQP?g";

macro_rules! ffi_type {
($name: ident) => {
middle::Type::$name().as_raw_ptr()
Expand Down Expand Up @@ -57,12 +53,12 @@ macro_rules! match_ffi_type {
t if t == $type => { ffi_type!($body) }
)+
)+
_ => ffi_type!(void)
_ => unreachable!()
}
}
}

fn str_to_type(ty: &str) -> *mut ffi_type {
pub fn str_to_type(ty: &str) -> *mut ffi_type {
match_ffi_type!(
ty,
"c" => c_schar
Expand Down Expand Up @@ -91,7 +87,6 @@ fn py_to_ffi(ty: *mut *mut ffi_type, obj: PyObjectRef, vm: &VirtualMachine) -> *
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();
Expand Down Expand Up @@ -145,7 +140,6 @@ pub struct Function {
cif: ffi_cif,
arguments: Vec<*mut ffi_type>,
return_type: Box<*mut ffi_type>,
// @TODO: Do we need to free the memory of these ffi_type?
}

impl Function {
Expand All @@ -165,7 +159,8 @@ impl Function {
}

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

pub fn call(
Expand Down Expand Up @@ -356,27 +351,6 @@ impl ExternalLibs {
}
}

#[pyclass(module = false, name = "_CDataObject")]
#[derive(Debug)]
pub struct CDataObject {}

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

#[pyimpl(flags(BASETYPE))]
impl CDataObject {
#[pyslot]
fn tp_new(cls: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
CDataObject {}.into_ref_with_type(vm, cls)
}
// A lot of the logic goes in this trait
// There's also other traits that should have different implementations for some functions
// present here
}

lazy_static::lazy_static! {
pub static ref CDATACACHE: PyRwLock<ExternalLibs> = PyRwLock::new(ExternalLibs::new());
}
104 changes: 49 additions & 55 deletions vm/src/stdlib/ctypes/function.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
extern crate libffi;

use std::{mem, os::raw::c_void};
use std::{fmt, os::raw::c_void};

use crate::builtins::pystr::{PyStr, PyStrRef};
use crossbeam_utils::atomic::AtomicCell;

use crate::builtins::pystr::PyStrRef;
use crate::builtins::PyTypeRef;
use crate::common::lock::PyRwLock;

use crate::function::FuncArgs;
use crate::pyobject::{
PyObjectRc, PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject,
PyObjectRc, PyObjectRef, PyRef, PyResult, PyValue, StaticType, TryFromObject, TypeProtocol,
};
use crate::VirtualMachine;

use crate::stdlib::ctypes::common::{CDataObject, Function, SharedLibrary, SIMPLE_TYPE_CHARS};
use crate::stdlib::ctypes::basics::PyCData;
use crate::stdlib::ctypes::common::{Function, SharedLibrary};

use crate::slots::Callable;
use crate::stdlib::ctypes::dll::dlsym;
Expand All @@ -21,33 +24,32 @@ fn map_types_to_res(args: &[PyObjectRc], vm: &VirtualMachine) -> PyResult<Vec<Py
args.iter()
.enumerate()
.map(|(idx, inner_obj)| {
match vm.isinstance(inner_obj, CDataObject::static_type()) {
Ok(_) => match vm.get_attribute(inner_obj.clone(), "_type_") {
Ok(_type_) if SIMPLE_TYPE_CHARS.contains(_type_.to_string().as_str()) => {
Ok(_type_)
}
Ok(_type_) => Err(vm.new_attribute_error("invalid _type_ value".to_string())),
Err(_) => Err(vm.new_attribute_error("atribute _type_ not found".to_string())),
},
// @TODO: Needs to return the name of the type, not String(inner_obj)
match vm.isinstance(inner_obj, PyCData::static_type()) {
// @TODO: checks related to _type_ are temporary
Ok(_) => Ok(vm.get_attribute(inner_obj.clone(), "_type_").unwrap()),
Err(_) => Err(vm.new_type_error(format!(
"object at {} is not an instance of _CDataObject, type {} found",
"object at {} is not an instance of _CData, type {} found",
idx,
inner_obj.to_string()
inner_obj.class().name
))),
}
})
.collect()
}

#[pyclass(module = "_ctypes", name = "CFuncPtr", base = "CDataObject")]
#[derive(Debug)]
#[pyclass(module = "_ctypes", name = "CFuncPtr", base = "PyCData")]
pub struct PyCFuncPtr {
pub _name_: String,
pub _argtypes_: PyRwLock<Vec<PyObjectRef>>,
pub _restype_: PyRwLock<Box<PyObjectRef>>,
pub _argtypes_: AtomicCell<Vec<PyObjectRef>>,
pub _restype_: AtomicCell<PyObjectRef>,
_handle: PyObjectRc,
_f: PyRwLock<Box<Function>>,
_f: PyRwLock<Function>,
}

impl fmt::Debug for PyCFuncPtr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PyCFuncPtr {{ _name_, _argtypes_, _restype_}}")
}
}

impl PyValue for PyCFuncPtr {
Expand All @@ -60,12 +62,13 @@ impl PyValue for PyCFuncPtr {
impl PyCFuncPtr {
#[pyproperty(name = "_argtypes_")]
fn argtypes(&self, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.new_list(self._argtypes_.read().clone())
vm.ctx
.new_list(unsafe { &*self._argtypes_.as_ptr() }.clone())
}

#[pyproperty(name = "_restype_")]
fn restype(&self, vm: &VirtualMachine) -> PyObjectRef {
self._restype_.read().as_ref().clone()
fn restype(&self, _vm: &VirtualMachine) -> PyObjectRef {
unsafe { &*self._restype_.as_ptr() }.clone()
}

#[pyproperty(name = "_argtypes_", setter)]
Expand All @@ -77,11 +80,7 @@ impl PyCFuncPtr {

let c_args = map_types_to_res(&args, vm)?;

self._argtypes_.write().clear();
self._argtypes_.write().extend(c_args.clone().into_iter());

let mut f_guard = self._f.write();
let fn_ptr = f_guard.as_mut();
self._argtypes_.store(c_args.clone());

let str_types: Result<Vec<String>, _> = c_args
.iter()
Expand All @@ -94,6 +93,7 @@ impl PyCFuncPtr {
})
.collect();

let mut fn_ptr = self._f.write();
fn_ptr.set_args(str_types.unwrap());

Ok(())
Expand All @@ -107,29 +107,23 @@ impl PyCFuncPtr {

#[pyproperty(name = "_restype_", setter)]
fn set_restype(&self, restype: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
match vm.isinstance(&restype, CDataObject::static_type()) {
match vm.isinstance(&restype, PyCData::static_type()) {
// @TODO: checks related to _type_ are temporary
Ok(_) => match vm.get_attribute(restype.clone(), "_type_") {
Ok(_type_)
if vm.isinstance(&_type_, &vm.ctx.types.str_type)?
&& _type_.to_string().len() == 1
&& SIMPLE_TYPE_CHARS.contains(_type_.to_string().as_str()) =>
{
let mut r_guard = self._restype_.write();
mem::replace(r_guard.as_mut(), restype.clone());

let mut a_guard = self._f.write();
let fn_ptr = a_guard.as_mut();
Ok(_type_) => {
self._restype_.store(restype.clone());

let mut fn_ptr = self._f.write();
fn_ptr.set_ret(_type_.to_string().as_str());

Ok(())
}
Ok(_type_) => Err(vm.new_attribute_error("invalid _type_ value".to_string())),
Err(_) => Err(vm.new_attribute_error("atribute _type_ not found".to_string())),
},
// @TODO: Needs to return the name of the type, not String(inner_obj)

Err(_) => Err(vm.new_type_error(format!(
"value is not an instance of _CDataObject, type {} found",
restype
"value is not an instance of _CData, type {} found",
restype.class().name
))),
}
}
Expand All @@ -145,7 +139,7 @@ impl PyCFuncPtr {
match vm.get_attribute(cls.as_object().to_owned(), "_argtypes_") {
Ok(_) => Self::from_dll(cls, func_name, arg, vm),
Err(_) => Err(vm.new_type_error(
"cannot construct instance of this class: no argtypes".to_string(),
"cannot construct instance of this class: no argtypes slot".to_string(),
)),
}
}
Expand All @@ -154,7 +148,7 @@ impl PyCFuncPtr {
/// # Arguments
///
/// * `func_name` - A string that names the function symbol
/// * `dll` - A Python object with _handle attribute of type SharedLibrary
/// * `arg` - A Python object with _handle attribute of type SharedLibrary
///
fn from_dll(
cls: PyTypeRef,
Expand All @@ -170,21 +164,20 @@ impl PyCFuncPtr {

PyCFuncPtr {
_name_: func_name.to_string(),
_argtypes_: PyRwLock::new(Vec::new()),
_restype_: PyRwLock::new(Box::new(vm.ctx.none())),
_argtypes_: AtomicCell::default(),
_restype_: AtomicCell::new(vm.ctx.none()),
_handle: handle_obj.clone(),
_f: PyRwLock::new(Box::new(Function::new(
_f: PyRwLock::new(Function::new(
fn_ptr,
Vec::new(),
"P", // put a default here
))),
)),
}
.into_ref_with_type(vm, cls)
} else {
// @TODO: Needs to return the name of the type, not String(inner_obj)
Err(vm.new_type_error(format!(
"_handle must be SharedLibrary not {}",
arg.to_string()
arg.class().name
)))
}
} else {
Expand All @@ -197,17 +190,18 @@ impl PyCFuncPtr {

impl Callable for PyCFuncPtr {
fn call(zelf: &PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
if args.args.len() != zelf._argtypes_.read().len() {
let inner_args = unsafe { &*zelf._argtypes_.as_ptr() };

if args.args.len() != inner_args.len() {
return Err(vm.new_runtime_error(format!(
"invalid number of arguments, required {}, but {} found",
zelf._argtypes_.read().len(),
inner_args.len(),
args.args.len()
)));
}

// Needs to check their types and convert to middle::Arg based on zelf._argtypes_
let arg_vec = map_types_to_res(&args.args, vm)?;

zelf._f.write().as_mut().call(arg_vec, vm)
(*zelf._f.write()).call(arg_vec, vm)
}
}
2 changes: 0 additions & 2 deletions vm/src/stdlib/ctypes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ mod dll;
mod function;
mod primitive;

use crate::stdlib::ctypes::basics::*;
use crate::stdlib::ctypes::dll::*;
use crate::stdlib::ctypes::function::*;
use crate::stdlib::ctypes::primitive::*;
Expand All @@ -22,7 +21,6 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"dlclose" => ctx.new_function(dlclose),

"CFuncPtr" => PyCFuncPtr::make_class(ctx),
"_CData" => PyCData::make_class(ctx),
"_SimpleCData" => PySimpleType::make_class(ctx)
})
}
Loading