Skip to content
Open
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
implement more of the concrete object layer
  • Loading branch information
arihant2math committed Apr 17, 2025
commit d520d5f4a4ebf0dfec2cd494d68015b344bdfdd2
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ crate-type = ["cdylib"]

[dependencies]
malachite-bigint = { workspace = true }
num-complex = { workspace = true }

rustpython-common = { workspace = true }
rustpython-vm = { workspace = true }

[lints]
workspace = true
[lints.rust]
unsafe_code = "allow"
unsafe_op_in_unsafe_fn = "deny"
elided_lifetimes_in_paths = "warn"

[lints.clippy]
perf = "warn"
style = "warn"
complexity = "warn"
suspicious = "warn"
correctness = "warn"
missing_safety_doc = "allow"
15 changes: 15 additions & 0 deletions capi/src/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// https://docs.python.org/3/c-api/bool.html

use std::ffi;

use rustpython_vm::{PyObject, PyObjectRef};

// TODO: Everything else

#[unsafe(export_name = "PyBool_FromLong")]
pub unsafe extern "C" fn bool_from_long(value: ffi::c_long) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_bool(value != 0))
.into_raw()
.as_ptr()
}
130 changes: 130 additions & 0 deletions capi/src/complex.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// https://docs.python.org/3/c-api/complex.html

use std::ffi;

use rustpython_vm::{PyObject, PyObjectRef, builtins::PyComplex};

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct CPyComplex {
pub real: ffi::c_double,
pub imag: ffi::c_double,
}

impl From<CPyComplex> for PyComplex {
fn from(value: CPyComplex) -> Self {
PyComplex::new(num_complex::Complex64::new(value.real, value.imag))
}
}

impl From<CPyComplex> for num_complex::Complex64 {
fn from(value: CPyComplex) -> Self {
num_complex::Complex64::new(value.real, value.imag)
}
}

impl From<PyComplex> for CPyComplex {
fn from(value: PyComplex) -> Self {
let complex = value.to_complex();
CPyComplex {
real: complex.re,
imag: complex.im,
}
}
}

impl From<num_complex::Complex64> for CPyComplex {
fn from(value: num_complex::Complex64) -> Self {
CPyComplex {
real: value.re,
imag: value.im,
}
}
}

// Associated functions for CPyComplex
// Always convert to PyComplex to do operations

#[unsafe(export_name = "_Py_c_sum")]
pub unsafe extern "C" fn c_sum(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex {
let a: PyComplex = unsafe { *a }.into();
let b: PyComplex = unsafe { *b }.into();
(a.to_complex() + b.to_complex()).into()
}

#[unsafe(export_name = "_Py_c_diff")]
pub unsafe extern "C" fn c_diff(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex {
let a: PyComplex = unsafe { *a }.into();
let b: PyComplex = unsafe { *b }.into();
(a.to_complex() - b.to_complex()).into()
}

#[unsafe(export_name = "_Py_c_neg")]
pub unsafe extern "C" fn c_neg(a: *const CPyComplex) -> CPyComplex {
let a: PyComplex = unsafe { *a }.into();
(-a.to_complex()).into()
}

#[unsafe(export_name = "_Py_c_prod")]
pub unsafe extern "C" fn c_prod(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex {
let a: PyComplex = unsafe { *a }.into();
let b: PyComplex = unsafe { *b }.into();
(a.to_complex() * b.to_complex()).into()
}

#[unsafe(export_name = "_Py_c_quot")]
pub unsafe extern "C" fn c_quot(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex {
let a: PyComplex = unsafe { *a }.into();
let b: PyComplex = unsafe { *b }.into();
(a.to_complex() / b.to_complex()).into()
}

#[unsafe(export_name = "_Py_c_pow")]
pub unsafe extern "C" fn c_pow(a: *const CPyComplex, b: *const CPyComplex) -> CPyComplex {
let a: PyComplex = unsafe { *a }.into();
let b: PyComplex = unsafe { *b }.into();
(a.to_complex() * b.to_complex()).into()
}

#[unsafe(export_name = "PyComplex_FromCComplex")]
pub unsafe extern "C" fn complex_from_ccomplex(value: CPyComplex) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_complex(value.into()))
.into_raw()
.as_ptr()
}

#[unsafe(export_name = "PyComplex_FromDoubles")]
pub unsafe extern "C" fn complex_from_doubles(
real: ffi::c_double,
imag: ffi::c_double,
) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_complex(num_complex::Complex64::new(real, imag)))
.into_raw()
.as_ptr()
}

#[unsafe(export_name = "PyComplex_RealAsDouble")]
pub unsafe extern "C" fn complex_real_as_double(value: *mut PyObject) -> ffi::c_double {
let vm = crate::get_vm();
let value = crate::cast_obj_ptr(value).unwrap();
let (complex, _) = value.try_complex(&vm).unwrap().unwrap();
complex.re
}

#[unsafe(export_name = "PyComplex_ImagAsDouble")]
pub unsafe extern "C" fn complex_imag_as_double(value: *mut PyObject) -> ffi::c_double {
let vm = crate::get_vm();
let value = crate::cast_obj_ptr(value).unwrap();
let (complex, _) = value.try_complex(&vm).unwrap().unwrap();
complex.im
}

#[unsafe(export_name = "PyComplex_AsCComplex")]
pub unsafe extern "C" fn complex_as_ccomplex(value: *mut PyObject) -> CPyComplex {
let vm = crate::get_vm();
let value = crate::cast_obj_ptr(value).unwrap();
let (complex, _) = value.try_complex(&vm).unwrap().unwrap();
complex.into()
}
26 changes: 26 additions & 0 deletions capi/src/float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// https://docs.python.org/3/c-api/float.html

use std::ffi;

use rustpython_vm::{PyObject, PyObjectRef};

/// Returns null if the string is not a valid float.
#[unsafe(export_name = "PyFloat_FromString")]
pub unsafe extern "C" fn float_from_string(value: *const ffi::c_char) -> *mut PyObject {
let vm = crate::get_vm();
let value_str = unsafe { std::ffi::CStr::from_ptr(value).to_str().unwrap() };
match value_str.parse::<f64>() {
Ok(value) => Into::<PyObjectRef>::into(vm.ctx.new_float(value))
.into_raw()
.as_ptr(),
Err(_) => std::ptr::null_mut(),
}
}

#[unsafe(export_name = "PyFloat_FromDouble")]
pub unsafe extern "C" fn float_from_double(value: ffi::c_double) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_float(value))
.into_raw()
.as_ptr()
}
35 changes: 33 additions & 2 deletions capi/src/int.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
// https://docs.python.org/3/c-api/long.html

use std::ffi;

use rustpython_vm::{PyObject, PyObjectRef};

#[unsafe(export_name = "PyLong_FromLong")]
pub unsafe extern "C" fn long_from_long(value: i64) -> *mut PyObject {
pub unsafe extern "C" fn long_from_long(value: ffi::c_long) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_int(value))
.into_raw()
.as_ptr()
}

#[unsafe(export_name = "PyLong_FromUnsignedLong")]
pub unsafe extern "C" fn long_from_unsigned_long(value: u64) -> *mut PyObject {
pub unsafe extern "C" fn long_from_unsigned_long(value: ffi::c_ulong) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_int(value))
.into_raw()
.as_ptr()
}

// TODO: PyLong_FromSsize_t
// TODO: PyLong_FromSize_t
#[unsafe(export_name = "PyLong_FromLongLong")]
pub unsafe extern "C" fn long_from_long_long(value: ffi::c_longlong) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_int(value))
.into_raw()
.as_ptr()
}

#[unsafe(export_name = "PyLong_FromUnsignedLongLong")]
pub unsafe extern "C" fn long_from_unsigned_long_long(value: ffi::c_ulonglong) -> *mut PyObject {
let vm = crate::get_vm();
Into::<PyObjectRef>::into(vm.ctx.new_int(value))
.into_raw()
.as_ptr()
}

#[unsafe(export_name = "PyLong_FromDouble")]
pub unsafe extern "C" fn long_from_double(value: ffi::c_double) -> *mut PyObject {
let vm = crate::get_vm();
let value = value as i64;
Into::<PyObjectRef>::into(vm.ctx.new_int(value))
.into_raw()
.as_ptr()
Expand Down
15 changes: 11 additions & 4 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::{cell::RefCell, ffi, sync::Arc};

use rustpython_vm as vm;
use rustpython_vm::{self as vm, PyObject, PyObjectRef};

mod error;
mod int;
mod tuple;
pub mod bool;
pub mod complex;
pub mod error;
pub mod float;
pub mod int;
pub mod tuple;

thread_local! {
pub static VM: RefCell<Option<Arc<vm::VirtualMachine>>> = const { RefCell::new(None) };
Expand All @@ -14,6 +17,10 @@ fn get_vm() -> Arc<vm::VirtualMachine> {
VM.with(|vm| vm.borrow().as_ref().unwrap().clone())
}

fn cast_obj_ptr(obj: *mut PyObject) -> Option<PyObjectRef> {
Some(unsafe { PyObjectRef::from_raw(std::ptr::NonNull::new(obj)?) })
}

#[repr(C)]
pub enum PyStatusType {
PyStatusTypeOk = 0,
Expand Down
4 changes: 4 additions & 0 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub struct PyComplex {
}

impl PyComplex {
pub fn new(value: Complex64) -> Self {
PyComplex { value }
}

pub fn to_complex64(self) -> Complex64 {
self.value
}
Expand Down