Skip to content

Commit efc5a4b

Browse files
committed
initial _ctypes implementation with _CData, get_errno, and set_errno
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 707876d commit efc5a4b

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ uname = "0.1.1"
9999
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
100100
rustyline = { workspace = true }
101101
which = "6"
102+
errno = "0.3"
102103

103104
[target.'cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))'.dependencies]
104105
num_cpus = "1.13.1"

vm/src/stdlib/ctypes.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub(crate) use _ctypes::make_module;
2+
3+
#[pymodule]
4+
mod _ctypes {
5+
use crate::{
6+
common::lock::PyRwLock,
7+
PyObjectRef
8+
};
9+
use crossbeam_utils::atomic::AtomicCell;
10+
11+
pub struct RawBuffer {
12+
pub inner: Box<[u8]>,
13+
pub size: usize,
14+
}
15+
16+
#[pyattr]
17+
#[pyclass(name = "_CData")]
18+
pub struct PyCData {
19+
_objects: AtomicCell<Vec<PyObjectRef>>,
20+
_buffer: PyRwLock<RawBuffer>,
21+
}
22+
23+
#[pyclass]
24+
impl PyCData {}
25+
26+
#[pyfunction]
27+
fn get_errno() -> i32 {
28+
errno::errno().0
29+
}
30+
31+
#[pyfunction]
32+
fn set_errno(value: i32) {
33+
errno::set_errno(errno::Errno(value));
34+
}
35+
}

vm/src/stdlib/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub mod sys;
4747
mod winapi;
4848
#[cfg(windows)]
4949
mod winreg;
50+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
51+
mod ctypes;
5052

5153
use crate::{builtins::PyModule, PyRef, VirtualMachine};
5254
use std::{borrow::Cow, collections::HashMap};
@@ -124,5 +126,9 @@ pub fn get_module_inits() -> StdlibMap {
124126
"_winapi" => winapi::make_module,
125127
"winreg" => winreg::make_module,
126128
}
129+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
130+
{
131+
"_ctypes" => ctypes::make_module,
132+
}
127133
}
128134
}

0 commit comments

Comments
 (0)