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
Use std::os::raw::c_* instead of libc::c_* for wasm compat
  • Loading branch information
coolreader18 committed Oct 26, 2020
commit 026f396e983691bb2e1d0fcdaa9664d93729e876
18 changes: 9 additions & 9 deletions vm/src/stdlib/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::VirtualMachine;
use crossbeam_utils::atomic::AtomicCell;
use itertools::Itertools;
use std::cmp::Ordering;
use std::fmt;
use std::{fmt, os::raw};

struct ArrayTypeSpecifierError {
_priv: (),
Expand Down Expand Up @@ -397,14 +397,14 @@ def_array_enum!(
(SignedByte, i8, 'b'),
(UnsignedByte, u8, 'B'),
// TODO: support unicode char
(SignedShort, libc::c_short, 'h'),
(UnsignedShort, libc::c_ushort, 'H'),
(SignedInt, libc::c_int, 'i'),
(UnsignedInt, libc::c_uint, 'I'),
(SignedLong, libc::c_long, 'l'),
(UnsignedLong, libc::c_ulong, 'L'),
(SignedLongLong, libc::c_longlong, 'q'),
(UnsignedLongLong, libc::c_ulonglong, 'Q'),
(SignedShort, raw::c_short, 'h'),
(UnsignedShort, raw::c_ushort, 'H'),
(SignedInt, raw::c_int, 'i'),
(UnsignedInt, raw::c_uint, 'I'),
(SignedLong, raw::c_long, 'l'),
(UnsignedLong, raw::c_ulong, 'L'),
(SignedLongLong, raw::c_longlong, 'q'),
(UnsignedLongLong, raw::c_ulonglong, 'Q'),
(Float, f32, 'f'),
(Double, f64, 'd'),
);
Expand Down
45 changes: 24 additions & 21 deletions vm/src/stdlib/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) mod _struct {
use std::convert::TryFrom;
use std::io::{Cursor, Read, Write};
use std::iter::Peekable;
use std::{fmt, mem};
use std::{fmt, mem, os::raw};

use crate::builtins::{
bytes::PyBytesRef, float::IntoPyFloat, int::try_to_primitive, pybool::IntoPyBool,
Expand Down Expand Up @@ -83,11 +83,14 @@ pub(crate) mod _struct {
}
}

type PackFunc = fn(&VirtualMachine, &PyObjectRef, &mut dyn Write) -> PyResult<()>;
type UnpackFunc = fn(&VirtualMachine, &mut dyn Read) -> PyResult;

struct FormatInfo {
size: usize,
align: usize,
pack: Option<fn(&VirtualMachine, &PyObjectRef, &mut dyn Write) -> PyResult<()>>,
unpack: Option<fn(&VirtualMachine, &mut dyn Read) -> PyResult>,
pack: Option<PackFunc>,
unpack: Option<UnpackFunc>,
}
impl fmt::Debug for FormatInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -155,33 +158,33 @@ pub(crate) mod _struct {
match e {
Endianness::Native => match self {
Pad | Str | Pascal => &FormatInfo {
size: size_of::<libc::c_char>(),
size: size_of::<raw::c_char>(),
align: 0,
pack: None,
unpack: None,
},
SByte => native_info!(libc::c_schar),
UByte => native_info!(libc::c_uchar),
SByte => native_info!(raw::c_schar),
UByte => native_info!(raw::c_uchar),
Char => &FormatInfo {
size: size_of::<libc::c_char>(),
size: size_of::<raw::c_char>(),
align: 0,
pack: Some(pack_char),
unpack: Some(unpack_char),
},
Short => native_info!(libc::c_short),
UShort => native_info!(libc::c_ushort),
Int => native_info!(libc::c_int),
UInt => native_info!(libc::c_uint),
Long => native_info!(libc::c_long),
ULong => native_info!(libc::c_ulong),
SSizeT => native_info!(libc::ssize_t),
SizeT => native_info!(libc::size_t),
LongLong => native_info!(libc::c_longlong),
ULongLong => native_info!(libc::c_ulonglong),
Short => native_info!(raw::c_short),
UShort => native_info!(raw::c_ushort),
Int => native_info!(raw::c_int),
UInt => native_info!(raw::c_uint),
Long => native_info!(raw::c_long),
ULong => native_info!(raw::c_ulong),
SSizeT => native_info!(isize), // ssize_t == isize
SizeT => native_info!(usize), // size_t == usize
LongLong => native_info!(raw::c_longlong),
ULongLong => native_info!(raw::c_ulonglong),
Bool => native_info!(bool),
Float => native_info!(libc::c_float),
Double => native_info!(libc::c_double),
VoidP => native_info!(*mut libc::c_void),
Float => native_info!(raw::c_float),
Double => native_info!(raw::c_double),
VoidP => native_info!(*mut raw::c_void),
},
Endianness::Big => match_nonnative!(self, byteorder::BigEndian),
Endianness::Little => match_nonnative!(self, byteorder::LittleEndian),
Expand Down Expand Up @@ -564,7 +567,7 @@ pub(crate) mod _struct {
make_pack_with_endianess!(f32, get_float);
make_pack_with_endianess!(f64, get_float);

Comment on lines +557 to +569
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

CPython allows pack float format with int, float or class with float() but not a float like string. Non of our function doing exactly the job. We may need one it also can be use by array.array

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think IntoPyFloat::try_from_object is correct; it accepts float, int, or anything with a __float__ method

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I modified it in #2309 to not accept a float string, since that's what PyFloat_AsDouble does

impl Packable for *mut libc::c_void {
impl Packable for *mut raw::c_void {
fn pack<Endianness: ByteOrder>(
vm: &VirtualMachine,
arg: &PyObjectRef,
Expand Down