Skip to content

Commit

Permalink
Fix accidental sign-extension of integer values to ts-bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Nov 17, 2024
1 parent 137c213 commit 67c58b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/models/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ macro_rules! bitflags2 {
$(
members.push((
stringify!($Flag).into(),
Some(Discriminator::BinaryHex(Self::$Flag.bits() as _)),
Some(Discriminator::BinaryHex(Self::$Flag.bits().into())),
concat!($(bitflags2!(@DOC #[$inner $($args)*])),*).trim().into(),
));
)*

members.push((
"ALL".into(),
Some(Discriminator::BinaryHex(Self::all().bits() as _)),
Some(Discriminator::BinaryHex(Self::all().bits().into())),
concat!("All bitflags of ", stringify!($BitFlags)).into(),
));

Expand Down
32 changes: 30 additions & 2 deletions ts-bindgen/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@ use core::fmt;
type Comment = Cow<'static, str>;
type Name = Cow<'static, str>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct BinaryInteger(pub u128);

macro_rules! impl_binary_integer {
($($ty:ty as $uty:ty),*) => {
$(
impl From<$ty> for BinaryInteger {
#[inline(always)]
fn from(value: $ty) -> Self {
BinaryInteger(value as $uty as u128)
}
}

impl From<$uty> for BinaryInteger {
#[inline(always)]
fn from(value: $uty) -> Self {
BinaryInteger(value as u128)
}
}
)*
};
}

impl_binary_integer!(i8 as u8, i16 as u16, i32 as u32, i64 as u64, i128 as u128);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Discriminator {
Simple(i64),
BinaryHex(u64),
BinaryHex(BinaryInteger),
String(&'static str),
}

Expand All @@ -27,7 +53,9 @@ impl fmt::Display for Discriminator {
}
}
Discriminator::BinaryHex(i) => {
if i > MAX_SAFE_NUMBER {
let i = i.0;

if i > (MAX_SAFE_NUMBER as u128) {
write!(f, "\"0x{:x}\"", i)
} else {
write!(f, "0x{:x}", i)
Expand Down

0 comments on commit 67c58b0

Please sign in to comment.