Skip to content

Commit cd7bd37

Browse files
committed
cleanup
1 parent ca18729 commit cd7bd37

2 files changed

Lines changed: 39 additions & 18 deletions

File tree

vm/src/stdlib/ctypes/array.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::builtins::PyBytes;
12
use crate::types::Callable;
23
use crate::{Py, PyObjectRef, PyPayload};
34
use crate::{PyResult, VirtualMachine, builtins::PyTypeRef, types::Constructor};
@@ -95,3 +96,12 @@ impl PyCArray {
9596
*self.value.write() = value;
9697
}
9798
}
99+
100+
impl PyCArray {
101+
pub fn to_arg(&self, _vm: &VirtualMachine) -> PyResult<libffi::middle::Arg> {
102+
let value = self.value.read();
103+
let py_bytes = value.payload::<PyBytes>().unwrap();
104+
let bytes = py_bytes.as_ref().to_vec();
105+
Ok(libffi::middle::Arg::new(&bytes))
106+
}
107+
}

vm/src/stdlib/ctypes/function.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,43 @@ impl Function {
3636
ret_type: &Option<PyTypeRef>,
3737
vm: &VirtualMachine,
3838
) -> PyResult<Self> {
39-
dbg!("Start load");
4039
// map each arg to a PyCSimple
4140
let args = args
4241
.iter()
4342
.map(|arg| {
4443
if let Some(arg) = arg.payload_if_subclass::<PyCSimple>(vm) {
45-
dbg!(arg);
4644
let converted = ffi_type_from_str(&arg._type_);
47-
dbg!(&converted);
48-
match converted {
45+
return match converted {
4946
Some(t) => Ok(t),
5047
None => Err(vm.new_type_error(format!(
5148
"Invalid type" // TODO: add type name
5249
))),
53-
}
50+
};
51+
}
52+
if let Some(arg) = arg.payload_if_subclass::<PyCArray>(vm) {
53+
let t = arg.typ.read();
54+
let ty_attributes = t.attributes.read();
55+
let ty_pystr = ty_attributes.get(vm.ctx.intern_str("_type_")).ok_or_else(|| {
56+
vm.new_type_error("Expected a ctypes simple type".to_string())
57+
})?;
58+
let ty_str = ty_pystr.downcast_ref::<PyStr>().ok_or_else(|| {
59+
vm.new_type_error("Expected a ctypes simple type".to_string())
60+
})?.to_string();
61+
let converted = ffi_type_from_str(&ty_str);
62+
return match converted {
63+
Some(_t) => {
64+
// TODO: Use
65+
Ok(Type::void())
66+
}
67+
None => Err(vm.new_type_error(format!(
68+
"Invalid type" // TODO: add type name
69+
))),
70+
};
5471
} else {
5572
Err(vm.new_type_error("Expected a ctypes simple type".to_string()))
5673
}
5774
})
5875
.collect::<PyResult<Vec<Type>>>()?;
59-
dbg!();
6076
let terminated = format!("{}\0", function);
6177
let pointer: Symbol<'_, FP> = unsafe {
6278
library
@@ -73,7 +89,6 @@ impl Function {
7389
None => Type::c_int(),
7490
};
7591
let cif = Cif::new(args.clone(), return_type);
76-
dbg!();
7792
Ok(Function {
7893
args,
7994
cif,
@@ -86,30 +101,26 @@ impl Function {
86101
args: Vec<PyObjectRef>,
87102
vm: &VirtualMachine,
88103
) -> PyResult<PyObjectRef> {
89-
dbg!("Start call");
90104
let args = args
91105
.into_iter()
92106
.enumerate()
93107
.map(|(count, arg)| {
94-
dbg!("Arg iter", &arg);
95-
dbg!(&arg);
108+
// none type check
109+
if vm.is_none(&arg) {
110+
return Ok(Arg::new(std::ptr::null()));
111+
}
96112
if let Some(d) = arg.payload_if_subclass::<PyCSimple>(vm) {
97-
dbg!(d);
98-
dbg!(&d._type_);
99-
unsafe {
100-
dbg!(d.value.as_ptr().as_ref().unwrap());
101-
}
102113
return Ok(d.to_arg(self.args[count].clone(), vm).unwrap());
103114
}
104115
if let Some(d) = arg.payload_if_subclass::<PyCArray>(vm) {
105-
dbg!(d);
116+
return Ok(d.to_arg(vm).unwrap());
106117
}
107118
Err(vm.new_type_error("Expected a ctypes simple type".to_string()))
108119
})
109120
.collect::<PyResult<Vec<Arg>>>()?;
110-
// TODO: FIX return type
121+
dbg!(&args);
122+
// TODO: FIX return
111123
let result: i32 = unsafe { self.cif.call(self.pointer, &args) };
112-
dbg!("Almost end call");
113124
Ok(vm.ctx.new_int(result).into())
114125
}
115126
}

0 commit comments

Comments
 (0)