Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
cleanup
  • Loading branch information
arihant2math committed Mar 24, 2025
commit 79282e58db1264a57e20d886e73701ee86a789a9
10 changes: 10 additions & 0 deletions vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::builtins::PyBytes;
use crate::types::Callable;
use crate::{Py, PyObjectRef, PyPayload};
use crate::{PyResult, VirtualMachine, builtins::PyTypeRef, types::Constructor};
Expand Down Expand Up @@ -95,3 +96,12 @@ impl PyCArray {
*self.value.write() = value;
}
}

impl PyCArray {
pub fn to_arg(&self, _vm: &VirtualMachine) -> PyResult<libffi::middle::Arg> {
let value = self.value.read();
let py_bytes = value.payload::<PyBytes>().unwrap();
let bytes = py_bytes.as_ref().to_vec();
Ok(libffi::middle::Arg::new(&bytes))
}
}
47 changes: 29 additions & 18 deletions vm/src/stdlib/ctypes/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,43 @@ impl Function {
ret_type: &Option<PyTypeRef>,
vm: &VirtualMachine,
) -> PyResult<Self> {
dbg!("Start load");
// map each arg to a PyCSimple
let args = args
.iter()
.map(|arg| {
if let Some(arg) = arg.payload_if_subclass::<PyCSimple>(vm) {
dbg!(arg);
let converted = ffi_type_from_str(&arg._type_);
dbg!(&converted);
match converted {
return match converted {
Some(t) => Ok(t),
None => Err(vm.new_type_error(format!(
"Invalid type" // TODO: add type name
))),
}
};
}
if let Some(arg) = arg.payload_if_subclass::<PyCArray>(vm) {
let t = arg.typ.read();
let ty_attributes = t.attributes.read();
let ty_pystr = ty_attributes.get(vm.ctx.intern_str("_type_")).ok_or_else(|| {
vm.new_type_error("Expected a ctypes simple type".to_string())
})?;
let ty_str = ty_pystr.downcast_ref::<PyStr>().ok_or_else(|| {
vm.new_type_error("Expected a ctypes simple type".to_string())
})?.to_string();
let converted = ffi_type_from_str(&ty_str);
return match converted {
Some(_t) => {
// TODO: Use
Ok(Type::void())
}
None => Err(vm.new_type_error(format!(
"Invalid type" // TODO: add type name
))),
};
} else {
Err(vm.new_type_error("Expected a ctypes simple type".to_string()))
}
})
.collect::<PyResult<Vec<Type>>>()?;
dbg!();
let terminated = format!("{}\0", function);
let pointer: Symbol<'_, FP> = unsafe {
library
Expand All @@ -73,7 +89,6 @@ impl Function {
None => Type::c_int(),
};
let cif = Cif::new(args.clone(), return_type);
dbg!();
Ok(Function {
args,
cif,
Expand All @@ -86,30 +101,26 @@ impl Function {
args: Vec<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
dbg!("Start call");
let args = args
.into_iter()
.enumerate()
.map(|(count, arg)| {
dbg!("Arg iter", &arg);
dbg!(&arg);
// none type check
if vm.is_none(&arg) {
return Ok(Arg::new(std::ptr::null()));
}
if let Some(d) = arg.payload_if_subclass::<PyCSimple>(vm) {
dbg!(d);
dbg!(&d._type_);
unsafe {
dbg!(d.value.as_ptr().as_ref().unwrap());
}
return Ok(d.to_arg(self.args[count].clone(), vm).unwrap());
}
if let Some(d) = arg.payload_if_subclass::<PyCArray>(vm) {
dbg!(d);
return Ok(d.to_arg(vm).unwrap());
}
Err(vm.new_type_error("Expected a ctypes simple type".to_string()))
})
.collect::<PyResult<Vec<Arg>>>()?;
// TODO: FIX return type
dbg!(&args);
// TODO: FIX return
let result: i32 = unsafe { self.cif.call(self.pointer, &args) };
dbg!("Almost end call");
Ok(vm.ctx.new_int(result).into())
}
}
Expand Down