Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable wasm32 compilation by wrapping libc #1362

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ pub(crate) fn impl_wrap_setter(
#[allow(unused_mut)]
unsafe extern "C" fn __wrap(
_slf: *mut pyo3::ffi::PyObject,
_value: *mut pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> pyo3::libc::c_int
_value: *mut pyo3::ffi::PyObject, _: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, #1367 may conflict with this line (I'm sorry!) and you will need to merge that manually.

{
const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#python_name),"()");
pyo3::callback_body_without_convert!(_py, {
Expand Down
14 changes: 7 additions & 7 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ fn native_element_type_from_type_char(type_char: u8) -> ElementType {
bytes: mem::size_of::<raw::c_ulonglong>(),
},
b'n' => SignedInteger {
bytes: mem::size_of::<libc::ssize_t>(),
bytes: mem::size_of::<crate::libc::ssize_t>(),
},
b'N' => UnsignedInteger {
bytes: mem::size_of::<libc::size_t>(),
bytes: mem::size_of::<crate::libc::size_t>(),
},
b'e' => Float { bytes: 2 },
b'f' => Float { bytes: 4 },
Expand Down Expand Up @@ -300,15 +300,15 @@ impl<T: Element> PyBuffer<T> {
#[inline]
pub fn is_c_contiguous(&self) -> bool {
unsafe {
ffi::PyBuffer_IsContiguous(&*self.0 as *const ffi::Py_buffer, b'C' as libc::c_char) != 0
ffi::PyBuffer_IsContiguous(&*self.0 as *const ffi::Py_buffer, b'C' as raw::c_char) != 0
}
}

/// Gets whether the buffer is contiguous in Fortran-style order (first index varies fastest when visiting items in order of memory address).
#[inline]
pub fn is_fortran_contiguous(&self) -> bool {
unsafe {
ffi::PyBuffer_IsContiguous(&*self.0 as *const ffi::Py_buffer, b'F' as libc::c_char) != 0
ffi::PyBuffer_IsContiguous(&*self.0 as *const ffi::Py_buffer, b'F' as raw::c_char) != 0
}
}

Expand Down Expand Up @@ -441,7 +441,7 @@ impl<T: Element> PyBuffer<T> {
target.as_ptr() as *mut raw::c_void,
&*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer,
self.0.len,
fort as libc::c_char,
fort as raw::c_char,
),
)
}
Expand Down Expand Up @@ -475,7 +475,7 @@ impl<T: Element> PyBuffer<T> {
vec.as_mut_ptr() as *mut raw::c_void,
&*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer,
self.0.len,
fort as libc::c_char,
fort as raw::c_char,
),
)?;
// set vector length to mark the now-initialized space as usable
Expand Down Expand Up @@ -528,7 +528,7 @@ impl<T: Element> PyBuffer<T> {
&*self.0 as *const ffi::Py_buffer as *mut ffi::Py_buffer,
source.as_ptr() as *mut raw::c_void,
self.0.len,
fort as libc::c_char,
fort as raw::c_char,
),
)
}
Expand Down
10 changes: 5 additions & 5 deletions src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl PyCallbackOutput for *mut ffi::PyObject {
const ERR_VALUE: Self = std::ptr::null_mut();
}

impl PyCallbackOutput for libc::c_int {
impl PyCallbackOutput for c_int {
const ERR_VALUE: Self = -1;
}

Expand Down Expand Up @@ -62,14 +62,14 @@ impl IntoPyCallbackOutput<Self> for *mut ffi::PyObject {
}
}

impl IntoPyCallbackOutput<libc::c_int> for () {
fn convert(self, _: Python) -> PyResult<libc::c_int> {
impl IntoPyCallbackOutput<c_int> for () {
fn convert(self, _: Python) -> PyResult<c_int> {
Ok(0)
}
}

impl IntoPyCallbackOutput<libc::c_int> for bool {
fn convert(self, _: Python) -> PyResult<libc::c_int> {
impl IntoPyCallbackOutput<c_int> for bool {
fn convert(self, _: Python) -> PyResult<c_int> {
Ok(self as c_int)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/class/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ macro_rules! py_func_set {
slf: *mut $crate::ffi::PyObject,
name: *mut $crate::ffi::PyObject,
value: *mut $crate::ffi::PyObject,
) -> libc::c_int
) -> std::os::raw::c_int
where
T: for<'p> $trait_name<'p>,
{
Expand Down Expand Up @@ -260,7 +260,7 @@ macro_rules! py_func_del {
slf: *mut $crate::ffi::PyObject,
name: *mut $crate::ffi::PyObject,
value: *mut $crate::ffi::PyObject,
) -> libc::c_int
) -> std::os::raw::c_int
where
T: for<'p> $trait_name<'p>,
{
Expand Down Expand Up @@ -288,7 +288,7 @@ macro_rules! py_func_set_del {
slf: *mut $crate::ffi::PyObject,
name: *mut $crate::ffi::PyObject,
value: *mut $crate::ffi::PyObject,
) -> libc::c_int
) -> std::os::raw::c_int
where
T: for<'p> $trait1<'p> + for<'p> $trait2<'p>,
{
Expand Down
2 changes: 1 addition & 1 deletion src/class/methods.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) 2017-present PyO3 Project and Contributors

use crate::{ffi, PyObject, Python};
use libc::c_int;
use std::ffi::CStr;
use std::fmt;
use std::os::raw::c_int;

/// `PyMethodDefType` represents different types of Python callable objects.
/// It is used by the `#[pymethods]` and `#[pyproto]` annotations.
Expand Down
2 changes: 1 addition & 1 deletion src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::{
AsPyPointer, FromPyPointer, IntoPy, Py, PyAny, PyNativeType, PyObject, Python,
ToBorrowedObject, ToPyObject,
};
use libc::c_int;
use std::borrow::Cow;
use std::cell::UnsafeCell;
use std::ffi::CString;
use std::os::raw::c_char;
use std::os::raw::c_int;
use std::ptr::NonNull;

mod err_state;
Expand Down
5 changes: 3 additions & 2 deletions src/ffi/cpython/abstract_.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::ffi::{PyObject, Py_buffer, Py_ssize_t};
use libc::{c_char, c_int, c_void};
use std::ffi::c_void;
use std::os::raw::{c_char, c_int};

#[cfg(all(Py_3_8, not(PyPy)))]
use crate::ffi::{
vectorcallfunc, PyCallable_Check, PyThreadState, PyThreadState_GET, PyTuple_Check,
PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL,
};
#[cfg(all(Py_3_8, not(PyPy)))]
use libc::size_t;
use crate::libc::size_t;

extern "C" {
#[cfg(all(Py_3_8, not(PyPy)))]
Expand Down
3 changes: 2 additions & 1 deletion src/ffi/initconfig.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* --- PyStatus ----------------------------------------------- */

use crate::ffi::Py_ssize_t;
use libc::{c_char, c_int, c_ulong, wchar_t};
use crate::libc::wchar_t;
use std::os::raw::{c_char, c_int, c_ulong};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum _PyStatus_TYPE {
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/longobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use libc::size_t;
use crate::libc::size_t;
use std::os::raw::{
c_char, c_double, c_int, c_long, c_longlong, c_uchar, c_ulong, c_ulonglong, c_void,
};
Expand Down
6 changes: 3 additions & 3 deletions src/ffi/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ pub type freefunc = unsafe extern "C" fn(arg1: *mut c_void);
pub type destructor = unsafe extern "C" fn(arg1: *mut PyObject);
#[cfg(not(Py_LIMITED_API))]
pub type printfunc =
unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut ::libc::FILE, arg3: c_int) -> c_int;
unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut crate::libc::FILE, arg3: c_int) -> c_int;
pub type getattrfunc =
unsafe extern "C" fn(arg1: *mut PyObject, arg2: *mut c_char) -> *mut PyObject;
pub type getattrofunc =
Expand Down Expand Up @@ -245,7 +245,7 @@ pub type allocfunc =
pub type vectorcallfunc = unsafe extern "C" fn(
callable: *mut PyObject,
args: *const *mut PyObject,
nargsf: libc::size_t,
nargsf: crate::libc::size_t,
kwnames: *mut PyObject,
) -> *mut PyObject;

Expand Down Expand Up @@ -657,7 +657,7 @@ extern "C" {

#[cfg(not(Py_LIMITED_API))]
#[cfg_attr(PyPy, link_name = "PyPyObject_Print")]
pub fn PyObject_Print(o: *mut PyObject, fp: *mut ::libc::FILE, flags: c_int) -> c_int;
pub fn PyObject_Print(o: *mut PyObject, fp: *mut crate::libc::FILE, flags: c_int) -> c_int;
#[cfg_attr(PyPy, link_name = "PyPyObject_Repr")]
pub fn PyObject_Repr(o: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyObject_Str")]
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/objimpl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use libc::size_t;
use crate::libc::size_t;
use std::os::raw::{c_int, c_void};

extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/pylifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ffi::pystate::PyThreadState;
#[cfg(all(Py_3_8, not(any(PY_LIMITED_API, PyPy))))]
use crate::ffi::{PyConfig, PyPreConfig, PyStatus, Py_ssize_t};

use libc::wchar_t;
use crate::libc::wchar_t;
use std::os::raw::{c_char, c_int};

extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/pymem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libc::size_t;
use crate::libc::size_t;
use std::os::raw::c_void;

#[cfg(not(Py_LIMITED_API))]
Expand Down
8 changes: 4 additions & 4 deletions src/ffi/pyport.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pub type Py_uintptr_t = ::libc::uintptr_t;
pub type Py_intptr_t = ::libc::intptr_t;
pub type Py_ssize_t = ::libc::ssize_t;
pub type Py_uintptr_t = crate::libc::uintptr_t;
pub type Py_intptr_t = crate::libc::intptr_t;
pub type Py_ssize_t = crate::libc::ssize_t;

pub type Py_hash_t = Py_ssize_t;
pub type Py_uhash_t = ::libc::size_t;
pub type Py_uhash_t = crate::libc::size_t;

pub const PY_SSIZE_T_MIN: Py_ssize_t = ::std::isize::MIN as Py_ssize_t;
pub const PY_SSIZE_T_MAX: Py_ssize_t = ::std::isize::MAX as Py_ssize_t;
2 changes: 1 addition & 1 deletion src/ffi/pythonrun.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ffi::object::*;
#[cfg(not(Py_LIMITED_API))]
use crate::ffi::pyarena::PyArena;
use libc::FILE;
use crate::libc::FILE;
use std::os::raw::{c_char, c_int};
use std::ptr;

Expand Down
2 changes: 1 addition & 1 deletion src/ffi/sysmodule.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::object::PyObject;
use crate::ffi::pyport::Py_ssize_t;
use libc::wchar_t;
use crate::libc::wchar_t;
use std::os::raw::{c_char, c_int};

extern "C" {
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/unicodeobject.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use libc::wchar_t;
use crate::libc::wchar_t;
use std::os::raw::{c_char, c_int, c_void};

#[cfg(not(Py_LIMITED_API))]
Expand Down
2 changes: 1 addition & 1 deletion src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn prepare_freethreaded_python() {
}
}
}
libc::atexit(finalize);
crate::libc::atexit(finalize);

// > Changed in version 3.7: This function is now called by Py_Initialize(), so you don’t have
// > to call it yourself anymore.
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ pub use {
unindent, // Re-exported for py_run
};

// Re-exported for the `__wrap` functions
#[doc(hidden)]
pub use libc;
use crate::libc_wrapper::libc;

// The CPython stable ABI does not include PyBuffer.
#[cfg(not(Py_LIMITED_API))]
Expand All @@ -193,6 +191,7 @@ mod gil;
mod instance;
#[macro_use]
mod internal_tricks;
mod libc_wrapper;
#[cfg(not(Py_LIMITED_API))]
pub mod marshal;
pub mod once_cell;
Expand Down
30 changes: 30 additions & 0 deletions src/libc_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! This file re-exports various libc functions and types, and adds a custom libc implementation
//! for wasm32-unknown-unknown, since libc does not support wasm32-unknown-unknown.
//!
//! When compiled for wasm32-unknown-unknown, this is expected to be used in an
//! emscripten environment, and the definitions are chosen accordingly.

#![allow(non_camel_case_types)]
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub mod libc {
extern "C" {
pub fn atexit(cb: extern "C" fn()) -> std::os::raw::c_int;
}
pub type intptr_t = isize;
pub type size_t = usize;
pub type ssize_t = isize;
pub type uintptr_t = usize;
pub type wchar_t = u32;
pub enum FILE {}
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub mod libc {
pub use libc::atexit;
pub use libc::intptr_t;
pub use libc::size_t;
pub use libc::ssize_t;
pub use libc::uintptr_t;
pub use libc::wchar_t;
pub use libc::FILE;
}
2 changes: 1 addition & 1 deletion src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::exceptions::PyTypeError;
use crate::type_object::PyTypeObject;
use crate::types::{PyDict, PyIterator, PyList, PyString, PyTuple, PyType};
use crate::{err, ffi, Py, PyNativeType, PyObject};
use libc::c_int;
use std::cell::UnsafeCell;
use std::cmp::Ordering;
use std::os::raw::c_int;

/// A Python object with GIL lifetime
///
Expand Down