Skip to content

Commit

Permalink
fix: Fix conversion from Janet to 32bit integers
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Apr 24, 2024
1 parent 050cddb commit faa4305
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,37 @@ impl TryFrom<Janet> for i32 {
#[inline]
fn try_from(value: Janet) -> Result<Self, Self::Error> {
match value.kind() {
JanetType::Number => Ok(unsafe { evil_janet::janet_unwrap_integer(value.inner) }),
JanetType::Number => {
let val = unsafe { evil_janet::janet_unwrap_number(value.inner) };
if val >= i32::MIN as f64 && val <= i32::MAX as f64 {
Ok(val.trunc() as i32)
} else {
Err(JanetConversionError::InvalidInt32(val))
}
},
got => Err(JanetConversionError::wrong_kind(JanetType::Number, got)),
}
}
}

impl TryFrom<Janet> for u32 {
type Error = JanetConversionError;

#[inline]
fn try_from(value: Janet) -> Result<Self, Self::Error> {
match value.kind() {
JanetType::Number => {
let val = unsafe { evil_janet::janet_unwrap_number(value.inner) };
if val >= 0.0 && val <= i32::MAX as f64 {
Ok(val.trunc() as u32)
} else {
Err(JanetConversionError::InvalidUInt32(val))
}
},
got => Err(JanetConversionError::wrong_kind(JanetType::Number, got)),
}
}
}
got => Err(JanetConversionError::wrong_kind(JanetType::Number, got)),
}
}
Expand Down

0 comments on commit faa4305

Please sign in to comment.