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
Fixing some ref problems in functions.rs
  • Loading branch information
darleybarreto authored and youknowone committed Aug 7, 2021
commit 8adf4dd008faa18ab42b23e253d6c0ec74106243
51 changes: 23 additions & 28 deletions vm/src/stdlib/ctypes/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@ use libloading::Library;
use crate::builtins::PyTypeRef;
use crate::common::lock::PyRwLock;
use crate::common::rc::PyRc;
use crate::pyobject::{PyObjectRef, PyValue, StaticType};
use crate::pyobject::{PyObjectRc, PyValue, StaticType};
use crate::VirtualMachine;

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

pub fn convert_type(ty: &str) -> middle::Type {
match ty {
"?" => middle::Type::c_uchar(),
"c" => middle::Type::c_schar(),
"u" => middle::Type::c_int(),
"b" => middle::Type::i8(),
"B" => middle::Type::c_uchar(),
"h" => middle::Type::c_ushort(),
"H" => middle::Type::u16(),
"i" => middle::Type::c_int(),
Expand All @@ -33,8 +31,8 @@ pub fn convert_type(ty: &str) -> middle::Type {
"f" => middle::Type::f32(),
"d" => middle::Type::f64(),
"g" => middle::Type::longdouble(),
"z" => middle::Type::pointer(),
"Z" => middle::Type::pointer(),
"?" | "B" => middle::Type::c_uchar(),
"z" | "Z" => middle::Type::pointer(),
"P" | _ => middle::Type::void(),
}
}
Expand All @@ -58,13 +56,20 @@ pub fn lib_call(
}
}
}

#[pyclass(module = false, name = "SharedLibrary")]
#[derive(Debug)]
pub struct SharedLibrary {
path_name: String,
lib: Library,
}

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

#[pyimpl(flags(BASETYPE))]
impl SharedLibrary {
pub fn new(name: &str) -> Result<SharedLibrary, libloading::Error> {
Ok(SharedLibrary {
Expand All @@ -73,18 +78,8 @@ impl SharedLibrary {
})
}

pub fn get_name(&self) -> &String {
&self.path_name
}

pub fn get_lib(&self) -> &Library {
&self.lib
}
}

impl PyValue for SharedLibrary {
fn class(vm: &VirtualMachine) -> &PyTypeRef {
&vm.ctx.types.object_type
pub fn get_sym(&self, name: &str) -> Result<*const i32, libloading::Error> {
unsafe { self.lib.get(name.as_bytes()).map(|f| *f) }
}
}

Expand All @@ -99,11 +94,11 @@ impl ExternalFunctions {
}
}

pub unsafe fn get_or_insert_lib(
&mut self,
library_path: &str,
vm: &VirtualMachine,
) -> Result<PyObjectRef, libloading::Error> {
pub unsafe fn get_or_insert_lib<'a, 'b>(
&'b mut self,
library_path: &'a str,
vm: &'a VirtualMachine,
) -> Result<PyObjectRc, libloading::Error> {
let library = self
.libraries
.entry(library_path.to_string())
Expand All @@ -113,17 +108,13 @@ impl ExternalFunctions {
}
}

lazy_static::lazy_static! {
pub static ref CDATACACHE: PyRwLock<ExternalFunctions> = PyRwLock::new(ExternalFunctions::new());
}

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

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

Expand All @@ -133,3 +124,7 @@ impl CDataObject {
// There's also other traits that should have different implementations for some functions
// present here
}

lazy_static::lazy_static! {
pub static ref CDATACACHE: PyRwLock<ExternalFunctions> = PyRwLock::new(ExternalFunctions::new());
}
38 changes: 25 additions & 13 deletions vm/src/stdlib/ctypes/dll.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
extern crate libloading;

use crate::builtins::pystr::PyStrRef;
use crate::pyobject::{PyObjectRef, PyResult};
use crate::pyobject::{PyObjectRc, PyObjectRef, PyResult, StaticType};
use crate::VirtualMachine;

use crate::stdlib::ctypes::common::{SharedLibrary, CDATACACHE};

pub fn dlopen(lib_path: PyStrRef, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
let library = unsafe {
pub fn dlopen(lib_path: PyObjectRc, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
// Match this error first
let lib_str_path = match vm.isinstance(&lib_path, &vm.ctx.types.str_type) {
Ok(_) => Ok(lib_path.to_string()),
Err(e) => Err(e),
}?;

let result = unsafe {
CDATACACHE
.write()
.get_or_insert_lib(lib_path.as_ref(), vm)
.expect("Failed to load library")
.get_or_insert_lib(lib_str_path.as_ref(), vm)
};
Ok(library)

match result {
Ok(lib) => Ok(lib),
Err(_) => Err(vm.new_os_error(format!(
"{} : cannot open shared object file: No such file or directory",
lib_path.to_string()
))),
}
}

pub fn dlsym(slib: PyObjectRef, func_name: PyStrRef, vm: &VirtualMachine) -> PyResult<*const i32> {
// cast to PyRef<SharedLibrary>
match vm.cast(slib, SharedLibrary) {
pub fn dlsym(slib: PyObjectRc, func_name: PyStrRef, vm: &VirtualMachine) -> PyResult<*const i32> {
// match vm.isinstance(&slib, &SharedLibrary::static_type()) {
match slib.downcast::<SharedLibrary>() {
Ok(lib) => {
let ptr_res = unsafe { lib.get_lib().get(func_name.as_ref().as_bytes()).map(|f| *f) };
if ptr_res.is_err() {
if let Ok(ptr) = lib.get_sym(func_name.as_ref()) {
Ok(ptr)
} else {
// @TODO: Change this error message
Err(vm.new_runtime_error(format!(
"Error while opening symbol {}",
func_name.as_ref()
)))
} else {
Ok(ptr_res.unwrap())
}
}
Err(_) => Err(vm.new_value_error("argument slib is not a valid SharedLibrary".to_string())),
Expand Down
67 changes: 37 additions & 30 deletions vm/src/stdlib/ctypes/function.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
extern crate libffi;

use ::std::mem;

use crate::builtins::pystr::{PyStr, PyStrRef};
use crate::builtins::PyTypeRef;
use crate::common::rc::PyRc;
use crate::common::lock::PyRwLock;

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

use crate::stdlib::ctypes::common::{
Expand All @@ -20,9 +19,9 @@ use crate::stdlib::ctypes::dll::dlsym;
#[derive(Debug)]
pub struct PyCFuncPtr {
pub _name_: String,
pub _argtypes_: Vec<PyObjectRef>,
pub _restype_: Box<String>,
_handle: PyRef<SharedLibrary>,
pub _argtypes_: PyRwLock<Vec<PyObjectRef>>,
pub _restype_: PyRwLock<String>,
_handle: PyObjectRc,
}

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

#[pyproperty(name = "_restype_")]
fn restype(&self, vm: &VirtualMachine) -> PyObjectRef {
PyStr::from(self._restype_.as_str()).into_object(vm)
PyStr::from(self._restype_.read().as_str()).into_object(vm)
}

#[pyproperty(name = "_argtypes_", setter)]
Expand Down Expand Up @@ -68,23 +67,23 @@ impl PyCFuncPtr {
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!(
"object at {} is not an instance of _CDataObject, type {} found",
idx,
inner_obj.class()
inner_obj.to_string()
))),
}
})
.collect();

self._argtypes_.clear();
self._argtypes_.extend(c_args?.into_iter());

self._argtypes_.write().clear();
self._argtypes_.write().extend(c_args?.into_iter());
Ok(())
} else {
Err(vm.new_type_error(format!(
"argtypes must be Tuple or List, {} found.",
argtypes.class()
argtypes.to_string()
)))
}
}
Expand All @@ -98,17 +97,25 @@ impl PyCFuncPtr {
&& _type_.to_string().len() == 1
&& SIMPLE_TYPE_CHARS.contains(_type_.to_string().as_str()) =>
{
let old = self._restype_.as_mut();
let new = _type_.to_string();
mem::replace(old, new);
// SAFETY: Values in _type_ are valid utf-8
unsafe {
self._restype_.write().as_mut_vec().clear();

self._restype_
.write()
.as_mut_vec()
.extend(_type_.to_string().as_mut_vec().iter())
};

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.class()
restype.to_string()
))),
}
}
Expand Down Expand Up @@ -142,18 +149,19 @@ impl PyCFuncPtr {
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
if let Ok(h) = vm.get_attribute(arg.clone(), "_handle") {
if let Some(handle) = h.payload::<SharedLibrary>() {
if let Ok(handle) = h.downcast::<SharedLibrary>() {
PyCFuncPtr {
_name_: func_name.to_string(),
_argtypes_: Vec::new(),
_restype_: Box::new("".to_string()),
_handle: handle.into_ref(vm),
_argtypes_: PyRwLock::new(Vec::new()),
_restype_: PyRwLock::new("".to_string()),
_handle: handle.into_object().clone(),
}
.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.class().name
arg.to_string()
)))
}
} else {
Expand All @@ -165,33 +173,32 @@ impl PyCFuncPtr {
}

impl Callable for PyCFuncPtr {
// @TODO: Build args e result before calling.
fn call(zelf: &PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
if args.args.len() != zelf._argtypes_.len() {
if args.args.len() != zelf._argtypes_.read().len() {
return Err(vm.new_runtime_error(format!(
"invalid number of arguments, required {}, but {} found",
zelf._argtypes_.len(),
zelf._argtypes_.read().len(),
args.args.len()
)));
}

// Needs to check their types and convert to middle::Arg based on zelf._argtypes_
// Something similar to the set of _argtypes_
// arg_vec = ...
let arg_vec = Vec::new();

// This is not optimal, but I can't simply store a vector of middle::Type inside PyCFuncPtr
let c_args = zelf
._argtypes_
.read()
.iter()
.map(|str_type| convert_type(str_type.to_string().as_str()))
.collect();

let arg_vec = Vec::new();

let ret_type = convert_type(zelf._restype_.to_string().as_ref());
let ret_type = convert_type(zelf._restype_.read().as_ref());

let name_py_ref = PyStr::from(&zelf._name_).into_ref(vm);
let ptr_fn = dlsym(zelf._handle.into_ref(vm), name_py_ref, vm).ok();
let ptr_fn = dlsym(zelf._handle.clone(), name_py_ref, vm).ok();
let ret = lib_call(c_args, ret_type, arg_vec, ptr_fn, vm);

Ok(vm.new_pyobj(ret))
Expand Down