Skip to content

Commit b9a4135

Browse files
arihant2mathGovindarajan
authored and
Govindarajan
committed
ctypes overhall
1 parent 0035ab4 commit b9a4135

File tree

6 files changed

+317
-178
lines changed

6 files changed

+317
-178
lines changed

vm/src/stdlib/ctypes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub(crate) mod function;
77
pub(crate) mod library;
88
pub(crate) mod pointer;
99
pub(crate) mod structure;
10+
pub(crate) mod thunk;
1011
pub(crate) mod union;
1112
pub(crate) mod util;
1213

@@ -23,11 +24,13 @@ pub fn extend_module_nodes(vm: &VirtualMachine, module: &Py<PyModule>) {
2324
"_CData" => PyCData::make_class(ctx),
2425
"_SimpleCData" => PyCSimple::make_class(ctx),
2526
"Array" => array::PyCArray::make_class(ctx),
27+
"CField" => field::PyCField::make_class(ctx),
2628
"CFuncPtr" => function::PyCFuncPtr::make_class(ctx),
2729
"PyCPointerType" => pointer::PyCPointerType::make_class(ctx),
2830
"_Pointer" => pointer::PyCPointer::make_class(ctx),
2931
"_pointer_type_cache" => ctx.new_dict(),
3032
"Structure" => structure::PyCStructure::make_class(ctx),
33+
"CThunkObject" => thunk::PyCThunk::make_class(ctx),
3134
"Union" => union::PyCUnion::make_class(ctx),
3235
})
3336
}

vm/src/stdlib/ctypes/field.rs

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use crate::builtins::PyType;
2+
use crate::builtins::PyTypeRef;
3+
use crate::stdlib::ctypes::PyCData;
4+
use crate::types::Constructor;
5+
use crate::types::Representable;
6+
use crate::{Py, PyResult, VirtualMachine};
27

38
#[pyclass(name = "PyCFieldType", base = "PyType", module = "_ctypes")]
4-
#[derive(PyPayload)]
9+
#[derive(PyPayload, Debug)]
510
pub struct PyCFieldType {
611
pub(super) inner: PyCField,
712
}
@@ -15,6 +20,7 @@ impl PyCFieldType {}
1520
metaclass = "PyCFieldType",
1621
module = "_ctypes"
1722
)]
23+
#[derive(Debug, PyPayload)]
1824
pub struct PyCField {
1925
byte_offset: usize,
2026
byte_size: usize,
@@ -26,33 +32,53 @@ pub struct PyCField {
2632
name: String,
2733
}
2834

29-
impl Representable for PyCFuncPtr {
35+
impl Representable for PyCField {
3036
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
31-
let field = zelf.inner.read();
32-
let tp_name = field.proto.name().to_string();
33-
if field.bitfield_size != 0 {
37+
let tp_name = zelf.proto.name().to_string();
38+
if zelf.bitfield_size != false {
3439
Ok(format!(
35-
"<{} {} type={}, ofs={}, bit_size={}, bit_offset={}",
36-
field.name, tp_name, field.byte_offset, field.bitfield_size, field.bit_offset
40+
"<{} type={}, ofs={byte_offset}, bit_size={bitfield_size}, bit_offset={bit_offset}",
41+
zelf.name,
42+
tp_name,
43+
byte_offset = zelf.byte_offset,
44+
bitfield_size = zelf.bitfield_size,
45+
bit_offset = zelf.bit_offset
3746
))
3847
} else {
3948
Ok(format!(
40-
"<{} {} type={}, ofs={}, size={}",
41-
field.name, tp_name, field.byte_offset, field.byte_size
49+
"<{} type={tp_name}, ofs={}, size={}",
50+
zelf.name, zelf.byte_offset, zelf.byte_size
4251
))
4352
}
4453
}
4554
}
4655

47-
#[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Representable))]
56+
#[derive(Debug, FromArgs)]
57+
struct PyCFieldConstructorArgs {
58+
// PyObject *name, PyObject *proto,
59+
// Py_ssize_t byte_size, Py_ssize_t byte_offset,
60+
// Py_ssize_t index, int _internal_use,
61+
// PyObject *bit_size_obj, PyObject *bit_offset_obj
62+
63+
}
64+
65+
impl Constructor for PyCField {
66+
type Args = PyCFieldConstructorArgs;
67+
68+
fn py_new(_cls: PyTypeRef, _args: Self::Args, vm: &VirtualMachine) -> PyResult {
69+
Err(vm.new_type_error("Cannot instantiate a PyCField".to_string()))
70+
}
71+
}
72+
73+
#[pyclass(flags(BASETYPE, IMMUTABLETYPE), with(Constructor, Representable))]
4874
impl PyCField {
4975
#[pygetset]
5076
fn size(&self) -> usize {
5177
self.byte_size
5278
}
5379

5480
#[pygetset]
55-
fn bit_size(&self) -> u8 {
81+
fn bit_size(&self) -> bool {
5682
self.bitfield_size
5783
}
5884

@@ -65,4 +91,42 @@ impl PyCField {
6591
fn is_anonymous(&self) -> bool {
6692
self.anonymous
6793
}
94+
95+
#[pygetset]
96+
fn name(&self) -> String {
97+
self.name.clone()
98+
}
99+
100+
#[pygetset(name = "type")]
101+
fn type_(&self) -> PyTypeRef {
102+
self.proto.clone()
103+
}
104+
105+
#[pygetset]
106+
fn offset(&self) -> usize {
107+
self.byte_offset
108+
}
109+
110+
#[pygetset]
111+
fn byte_offset(&self) -> usize {
112+
self.byte_offset
113+
}
114+
115+
#[pygetset]
116+
fn byte_size(&self) -> usize {
117+
self.byte_size
118+
}
119+
120+
#[pygetset]
121+
fn bit_offset(&self) -> u8 {
122+
self.bit_offset
123+
}
124+
}
125+
126+
pub(crate) fn low_bit(offset: usize) -> usize {
127+
offset & 0xFFFF
128+
}
129+
130+
pub(crate) fn high_bit(offset: usize) -> usize {
131+
offset >> 16
68132
}

0 commit comments

Comments
 (0)