Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e26aec2

Browse filesBrowse files
committed
arguments for ctypes function
1 parent fa80c52 commit e26aec2
Copy full SHA for e26aec2

File tree

2 files changed

+93
-19
lines changed
Filter options

2 files changed

+93
-19
lines changed

‎vm/src/stdlib/ctypes.rs

Copy file name to clipboardExpand all lines: vm/src/stdlib/ctypes.rs
+64-3Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) mod _ctypes {
3838
use super::base::PyCSimple;
3939
use crate::builtins::PyTypeRef;
4040
use crate::class::StaticType;
41-
use crate::function::{Either, OptionalArg};
41+
use crate::function::{Either, FuncArgs, OptionalArg};
4242
use crate::stdlib::ctypes::library;
4343
use crate::{AsObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine};
4444
use crossbeam_utils::atomic::AtomicCell;
@@ -125,11 +125,12 @@ pub(crate) mod _ctypes {
125125
"d" | "g" => mem::size_of::<c_double>(),
126126
"?" | "B" => mem::size_of::<c_uchar>(),
127127
"P" | "z" | "Z" => mem::size_of::<usize>(),
128+
"O" => mem::size_of::<PyObjectRef>(),
128129
_ => unreachable!(),
129130
}
130131
}
131132

132-
const SIMPLE_TYPE_CHARS: &str = "cbBhHiIlLdfguzZPqQ?";
133+
const SIMPLE_TYPE_CHARS: &str = "cbBhHiIlLdfguzZPqQ?O";
133134

134135
pub fn new_simple_type(
135136
cls: Either<&PyObjectRef, &PyTypeRef>,
@@ -219,9 +220,14 @@ pub(crate) mod _ctypes {
219220
#[pyfunction(name = "POINTER")]
220221
pub fn pointer(_cls: PyTypeRef) {}
221222

222-
#[pyfunction]
223+
#[pyfunction(name = "pointer")]
223224
pub fn pointer_fn(_inst: PyObjectRef) {}
224225

226+
#[pyfunction]
227+
fn _pointer_type_cache() -> PyObjectRef {
228+
todo!()
229+
}
230+
225231
#[cfg(target_os = "windows")]
226232
#[pyfunction(name = "_check_HRESULT")]
227233
pub fn check_hresult(_self: PyObjectRef, hr: i32, _vm: &VirtualMachine) -> PyResult<i32> {
@@ -244,6 +250,24 @@ pub(crate) mod _ctypes {
244250
}
245251
}
246252

253+
#[pyfunction]
254+
fn byref(_args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
255+
// TODO: RUSTPYTHON
256+
return Err(vm.new_value_error("not implemented".to_string()));
257+
}
258+
259+
#[pyfunction]
260+
fn alignment(_args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
261+
// TODO: RUSTPYTHON
262+
return Err(vm.new_value_error("not implemented".to_string()));
263+
}
264+
265+
#[pyfunction]
266+
fn resize(_args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
267+
// TODO: RUSTPYTHON
268+
return Err(vm.new_value_error("not implemented".to_string()));
269+
}
270+
247271
#[pyfunction]
248272
fn get_errno() -> i32 {
249273
errno::errno().0
@@ -253,4 +277,41 @@ pub(crate) mod _ctypes {
253277
fn set_errno(value: i32) {
254278
errno::set_errno(errno::Errno(value));
255279
}
280+
281+
#[cfg(windows)]
282+
#[pyfunction]
283+
fn get_last_error() -> PyResult<u32> {
284+
Ok(unsafe { windows_sys::Win32::Foundation::GetLastError() })
285+
}
286+
287+
#[cfg(windows)]
288+
#[pyfunction]
289+
fn set_last_error(value: u32) -> PyResult<()> {
290+
unsafe { windows_sys::Win32::Foundation::SetLastError(value) };
291+
Ok(())
292+
}
293+
294+
#[pyattr]
295+
fn _memmove_addr(_vm: &VirtualMachine) -> usize {
296+
let f = libc::memmove;
297+
f as usize
298+
}
299+
300+
#[pyattr]
301+
fn _memset_addr(_vm: &VirtualMachine) -> usize {
302+
let f = libc::memset;
303+
f as usize
304+
}
305+
306+
#[pyattr]
307+
fn _string_at_addr(_vm: &VirtualMachine) -> usize {
308+
let f = libc::strnlen;
309+
f as usize
310+
}
311+
312+
#[pyattr]
313+
fn _cast_addr(_vm: &VirtualMachine) -> usize {
314+
// TODO: RUSTPYTHON
315+
return 0;
316+
}
256317
}

‎vm/src/stdlib/ctypes/function.rs

Copy file name to clipboardExpand all lines: vm/src/stdlib/ctypes/function.rs
+29-16Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::builtins::{PyStr, PyTupleRef, PyTypeRef};
2-
use crate::class::StaticType;
32
use crate::convert::ToPyObject;
43
use crate::function::FuncArgs;
4+
use crate::stdlib::ctypes::array::PyCArray;
55
use crate::stdlib::ctypes::PyCData;
66
use crate::stdlib::ctypes::base::{PyCSimple, ffi_type_from_str};
77
use crate::types::{Callable, Constructor};
@@ -17,6 +17,7 @@ use std::fmt::Debug;
1717

1818
#[derive(Debug)]
1919
pub struct Function {
20+
args: Vec<Type>,
2021
// TODO: no protection from use-after-free
2122
pointer: CodePtr,
2223
cif: Cif,
@@ -35,21 +36,20 @@ impl Function {
3536
ret_type: &Option<PyTypeRef>,
3637
vm: &VirtualMachine,
3738
) -> PyResult<Self> {
39+
dbg!("Start load");
3840
// map each arg to a PyCSimple
3941
let args = args
4042
.iter()
4143
.map(|arg| {
42-
dbg!();
43-
if arg.obj_type().is_subclass(PyCSimple::static_type().into(), vm)? {
44-
dbg!();
45-
let typ = arg.get_attr("_type_", vm)?;
46-
let typ = typ.downcast_ref::<PyStr>().unwrap();
47-
let converted = ffi_type_from_str(typ.as_str());
44+
if let Some(arg) = arg.payload_if_subclass::<PyCSimple>(vm) {
45+
dbg!(arg);
46+
let converted = ffi_type_from_str(&arg._type_);
47+
dbg!(&converted);
4848
match converted {
4949
Some(t) => Ok(t),
5050
None => Err(vm.new_type_error(format!(
51-
"Invalid type: {}",
52-
typ.as_str()
51+
"Invalid type"
52+
// TODO: add type name
5353
))),
5454
}
5555
} else {
@@ -73,8 +73,10 @@ impl Function {
7373
}
7474
None => Type::c_int(),
7575
};
76-
let cif = Cif::new(args, return_type);
76+
let cif = Cif::new(args.clone(), return_type);
77+
dbg!();
7778
Ok(Function {
79+
args,
7880
cif,
7981
pointer: code_ptr,
8082
})
@@ -85,19 +87,30 @@ impl Function {
8587
args: Vec<PyObjectRef>,
8688
vm: &VirtualMachine,
8789
) -> PyResult<PyObjectRef> {
90+
dbg!("Start call");
8891
let args = args
8992
.into_iter()
90-
.map(|arg| {
91-
if let Some(data) = arg.downcast_ref::<PyCSimple>() {
92-
dbg!(&data);
93-
todo!("HANDLE ARGUMENTS")
94-
} else {
95-
Err(vm.new_type_error("Expected a ctypes simple type".to_string()))
93+
.enumerate()
94+
.map(|(count, arg)| {
95+
dbg!("Arg iter", &arg);
96+
dbg!(&arg);
97+
if let Some(d) = arg.payload_if_subclass::<PyCSimple>(vm) {
98+
dbg!(d);
99+
dbg!(&d._type_);
100+
unsafe {
101+
dbg!(d.value.as_ptr().as_ref().unwrap());
102+
}
103+
return Ok(d.to_arg(self.args[count].clone(), vm).unwrap());
104+
}
105+
if let Some(d) = arg.payload_if_subclass::<PyCArray>(vm) {
106+
dbg!(d);
96107
}
108+
Err(vm.new_type_error("Expected a ctypes simple type".to_string()))
97109
})
98110
.collect::<PyResult<Vec<Arg>>>()?;
99111
// TODO: FIX return type
100112
let result: i32 = unsafe { self.cif.call(self.pointer, &args) };
113+
dbg!("Almost end call");
101114
Ok(vm.ctx.new_int(result).into())
102115
}
103116
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.