Support 52-bit integers for Luau

Simply to float conversion (it actually never fails or goes out of range)
This commit is contained in:
Alex Orlenko
2025-03-13 16:18:44 +00:00
parent 2543414726
commit ef8b8e11ec
7 changed files with 77 additions and 44 deletions
+5 -31
View File
@@ -808,15 +808,9 @@ macro_rules! lua_convert_int {
impl IntoLua for $x {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
cast(self)
Ok(cast(self)
.map(Value::Integer)
.or_else(|| cast(self).map(Value::Number))
// This is impossible error because conversion to Number never fails
.ok_or_else(|| Error::ToLuaConversionError {
from: stringify!($x).to_string(),
to: "number",
message: Some("out of range".to_owned()),
})
.unwrap_or_else(|| Value::Number(self as ffi::lua_Number)))
}
#[inline]
@@ -899,13 +893,7 @@ macro_rules! lua_convert_float {
impl IntoLua for $x {
#[inline]
fn into_lua(self, _: &Lua) -> Result<Value> {
cast(self)
.ok_or_else(|| Error::ToLuaConversionError {
from: stringify!($x).to_string(),
to: "number",
message: Some("out of range".to_string()),
})
.map(Value::Number)
Ok(Value::Number(self as _))
}
}
@@ -914,33 +902,19 @@ macro_rules! lua_convert_float {
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
let ty = value.type_name();
lua.coerce_number(value)?
.map(|n| n as $x)
.ok_or_else(|| Error::FromLuaConversionError {
from: ty,
to: stringify!($x).to_string(),
message: Some("expected number or string coercible to number".to_string()),
})
.and_then(|n| {
cast(n).ok_or_else(|| Error::FromLuaConversionError {
from: ty,
to: stringify!($x).to_string(),
message: Some("number out of range".to_string()),
})
})
}
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
let state = lua.state();
let type_id = ffi::lua_type(state, idx);
if type_id == ffi::LUA_TNUMBER {
let mut ok = 0;
let i = ffi::lua_tonumberx(state, idx, &mut ok);
if ok != 0 {
return cast(i).ok_or_else(|| Error::FromLuaConversionError {
from: "number",
to: stringify!($x).to_string(),
message: Some("out of range".to_owned()),
});
}
return Ok(ffi::lua_tonumber(state, idx) as _);
}
// Fallback to default
Self::from_lua(lua.stack_value(idx, Some(type_id)), lua.lua())
+1 -1
View File
@@ -51,7 +51,7 @@ unsafe extern "C-unwind" fn lua_collectgarbage(state: *mut ffi::lua_State) -> c_
1
}
Ok("step") => {
let res = ffi::lua_gc(state, ffi::LUA_GCSTEP, arg);
let res = ffi::lua_gc(state, ffi::LUA_GCSTEP, arg as _);
ffi::lua_pushboolean(state, res);
1
}
+4 -1
View File
@@ -272,7 +272,10 @@ impl Value {
/// If the value is a Lua [`Integer`], try to convert it to `i64` or return `None` otherwise.
#[inline]
pub fn as_i64(&self) -> Option<i64> {
self.as_integer().map(i64::from)
#[cfg(target_pointer_width = "64")]
return self.as_integer();
#[cfg(not(target_pointer_width = "64"))]
return self.as_integer().map(i64::from);
}
/// Cast the value to `u64`.