Skip to content

Commit fbed171

Browse files
committed
Revert "Support 52-bit integers for Luau"
This reverts commit ef8b8e1.
1 parent 298d48f commit fbed171

File tree

7 files changed

+44
-75
lines changed

7 files changed

+44
-75
lines changed

mlua-sys/src/luau/compat.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ pub unsafe fn lua_isinteger(L: *mut lua_State, idx: c_int) -> c_int {
127127
0
128128
}
129129

130-
#[inline(always)]
131-
pub unsafe fn lua_pushinteger(L: *mut lua_State, i: lua_Integer) {
132-
lua_pushnumber(L, i as lua_Number);
133-
}
134-
135130
#[inline(always)]
136131
pub unsafe fn lua_tointeger(L: *mut lua_State, i: c_int) -> lua_Integer {
137132
lua_tointegerx(L, i, ptr::null_mut())
@@ -183,7 +178,6 @@ pub unsafe fn lua_geti(L: *mut lua_State, mut idx: c_int, n: lua_Integer) -> c_i
183178

184179
#[inline(always)]
185180
pub unsafe fn lua_rawgeti(L: *mut lua_State, idx: c_int, n: lua_Integer) -> c_int {
186-
let n = n.try_into().expect("cannot convert index from lua_Integer");
187181
lua_rawgeti_(L, idx, n)
188182
}
189183

@@ -219,7 +213,6 @@ pub unsafe fn lua_seti(L: *mut lua_State, mut idx: c_int, n: lua_Integer) {
219213

220214
#[inline(always)]
221215
pub unsafe fn lua_rawseti(L: *mut lua_State, idx: c_int, n: lua_Integer) {
222-
let n = n.try_into().expect("cannot convert index from lua_Integer");
223216
lua_rawseti_(L, idx, n)
224217
}
225218

@@ -322,24 +315,6 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char)
322315
}
323316
}
324317

325-
#[inline(always)]
326-
pub unsafe fn luaL_checkinteger(L: *mut lua_State, narg: c_int) -> lua_Integer {
327-
let mut isnum = 0;
328-
let int = lua_tointegerx(L, narg, &mut isnum);
329-
if isnum == 0 {
330-
luaL_typeerror(L, narg, lua_typename(L, LUA_TNUMBER));
331-
}
332-
int
333-
}
334-
335-
pub unsafe fn luaL_optinteger(L: *mut lua_State, narg: c_int, def: lua_Integer) -> lua_Integer {
336-
if lua_isnoneornil(L, narg) != 0 {
337-
def
338-
} else {
339-
luaL_checkinteger(L, narg)
340-
}
341-
}
342-
343318
#[inline(always)]
344319
pub unsafe fn luaL_getmetafield(L: *mut lua_State, obj: c_int, e: *const c_char) -> c_int {
345320
if luaL_getmetafield_(L, obj, e) != 0 {

mlua-sys/src/luau/lauxlib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::os::raw::{c_char, c_float, c_int, c_void};
44
use std::ptr;
55

6-
use super::lua::{self, lua_CFunction, lua_Number, lua_State, lua_Unsigned, LUA_REGISTRYINDEX};
6+
use super::lua::{self, lua_CFunction, lua_Integer, lua_Number, lua_State, lua_Unsigned, LUA_REGISTRYINDEX};
77

88
#[repr(C)]
99
pub struct luaL_Reg {
@@ -33,10 +33,8 @@ extern "C-unwind" {
3333
pub fn luaL_checkboolean(L: *mut lua_State, narg: c_int) -> c_int;
3434
pub fn luaL_optboolean(L: *mut lua_State, narg: c_int, def: c_int) -> c_int;
3535

36-
#[link_name = "luaL_checkinteger"]
37-
pub fn luaL_checkinteger_(L: *mut lua_State, narg: c_int) -> c_int;
38-
#[link_name = "luaL_optinteger"]
39-
pub fn luaL_optinteger_(L: *mut lua_State, narg: c_int, def: c_int) -> c_int;
36+
pub fn luaL_checkinteger(L: *mut lua_State, narg: c_int) -> lua_Integer;
37+
pub fn luaL_optinteger(L: *mut lua_State, narg: c_int, def: lua_Integer) -> lua_Integer;
4038
pub fn luaL_checkunsigned(L: *mut lua_State, narg: c_int) -> lua_Unsigned;
4139
pub fn luaL_optunsigned(L: *mut lua_State, narg: c_int, def: lua_Unsigned) -> lua_Unsigned;
4240

mlua-sys/src/luau/lua.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ pub const LUA_MINSTACK: c_int = 20;
7070
/// A Lua number, usually equivalent to `f64`.
7171
pub type lua_Number = c_double;
7272

73-
/// A Lua integer, usually equivalent to `i64`
74-
#[cfg(target_pointer_width = "32")]
75-
pub type lua_Integer = i32;
76-
#[cfg(target_pointer_width = "64")]
77-
pub type lua_Integer = i64;
73+
/// A Lua integer, equivalent to `i32`.
74+
pub type lua_Integer = c_int;
7875

7976
/// A Lua unsigned integer, equivalent to `u32`.
8077
pub type lua_Unsigned = c_uint;
@@ -139,7 +136,7 @@ extern "C-unwind" {
139136

140137
pub fn lua_tonumberx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Number;
141138
#[link_name = "lua_tointegerx"]
142-
pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> c_int;
139+
pub fn lua_tointegerx_(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Integer;
143140
pub fn lua_tounsignedx(L: *mut lua_State, idx: c_int, isnum: *mut c_int) -> lua_Unsigned;
144141
pub fn lua_tovector(L: *mut lua_State, idx: c_int) -> *const c_float;
145142
pub fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int;
@@ -163,8 +160,7 @@ extern "C-unwind" {
163160
//
164161
pub fn lua_pushnil(L: *mut lua_State);
165162
pub fn lua_pushnumber(L: *mut lua_State, n: lua_Number);
166-
#[link_name = "lua_pushinteger"]
167-
pub fn lua_pushinteger_(L: *mut lua_State, n: c_int);
163+
pub fn lua_pushinteger(L: *mut lua_State, n: lua_Integer);
168164
pub fn lua_pushunsigned(L: *mut lua_State, n: lua_Unsigned);
169165
#[cfg(not(feature = "luau-vector4"))]
170166
pub fn lua_pushvector(L: *mut lua_State, x: c_float, y: c_float, z: c_float);
@@ -314,13 +310,13 @@ extern "C-unwind" {
314310
//
315311

316312
#[inline(always)]
317-
pub unsafe fn lua_tonumber(L: *mut lua_State, idx: c_int) -> lua_Number {
318-
lua_tonumberx(L, idx, ptr::null_mut())
313+
pub unsafe fn lua_tonumber(L: *mut lua_State, i: c_int) -> lua_Number {
314+
lua_tonumberx(L, i, ptr::null_mut())
319315
}
320316

321317
#[inline(always)]
322-
pub unsafe fn lua_tointeger_(L: *mut lua_State, idx: c_int) -> c_int {
323-
lua_tointegerx_(L, idx, ptr::null_mut())
318+
pub unsafe fn lua_tointeger_(L: *mut lua_State, i: c_int) -> lua_Integer {
319+
lua_tointegerx_(L, i, ptr::null_mut())
324320
}
325321

326322
#[inline(always)]

src/conversion.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,9 +808,15 @@ macro_rules! lua_convert_int {
808808
impl IntoLua for $x {
809809
#[inline]
810810
fn into_lua(self, _: &Lua) -> Result<Value> {
811-
Ok(cast(self)
811+
cast(self)
812812
.map(Value::Integer)
813-
.unwrap_or_else(|| Value::Number(self as ffi::lua_Number)))
813+
.or_else(|| cast(self).map(Value::Number))
814+
// This is impossible error because conversion to Number never fails
815+
.ok_or_else(|| Error::ToLuaConversionError {
816+
from: stringify!($x).to_string(),
817+
to: "number",
818+
message: Some("out of range".to_owned()),
819+
})
814820
}
815821

816822
#[inline]
@@ -893,7 +899,13 @@ macro_rules! lua_convert_float {
893899
impl IntoLua for $x {
894900
#[inline]
895901
fn into_lua(self, _: &Lua) -> Result<Value> {
896-
Ok(Value::Number(self as _))
902+
cast(self)
903+
.ok_or_else(|| Error::ToLuaConversionError {
904+
from: stringify!($x).to_string(),
905+
to: "number",
906+
message: Some("out of range".to_string()),
907+
})
908+
.map(Value::Number)
897909
}
898910
}
899911

@@ -902,19 +914,33 @@ macro_rules! lua_convert_float {
902914
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
903915
let ty = value.type_name();
904916
lua.coerce_number(value)?
905-
.map(|n| n as $x)
906917
.ok_or_else(|| Error::FromLuaConversionError {
907918
from: ty,
908919
to: stringify!($x).to_string(),
909920
message: Some("expected number or string coercible to number".to_string()),
910921
})
922+
.and_then(|n| {
923+
cast(n).ok_or_else(|| Error::FromLuaConversionError {
924+
from: ty,
925+
to: stringify!($x).to_string(),
926+
message: Some("number out of range".to_string()),
927+
})
928+
})
911929
}
912930

913931
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
914932
let state = lua.state();
915933
let type_id = ffi::lua_type(state, idx);
916934
if type_id == ffi::LUA_TNUMBER {
917-
return Ok(ffi::lua_tonumber(state, idx) as _);
935+
let mut ok = 0;
936+
let i = ffi::lua_tonumberx(state, idx, &mut ok);
937+
if ok != 0 {
938+
return cast(i).ok_or_else(|| Error::FromLuaConversionError {
939+
from: "number",
940+
to: stringify!($x).to_string(),
941+
message: Some("out of range".to_owned()),
942+
});
943+
}
918944
}
919945
// Fallback to default
920946
Self::from_lua(lua.stack_value(idx, Some(type_id)), lua.lua())

src/luau/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ unsafe extern "C-unwind" fn lua_collectgarbage(state: *mut ffi::lua_State) -> c_
5151
1
5252
}
5353
Ok("step") => {
54-
let res = ffi::lua_gc(state, ffi::LUA_GCSTEP, arg as _);
54+
let res = ffi::lua_gc(state, ffi::LUA_GCSTEP, arg);
5555
ffi::lua_pushboolean(state, res);
5656
1
5757
}

src/value.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,7 @@ impl Value {
272272
/// If the value is a Lua [`Integer`], try to convert it to `i64` or return `None` otherwise.
273273
#[inline]
274274
pub fn as_i64(&self) -> Option<i64> {
275-
#[cfg(target_pointer_width = "64")]
276-
return self.as_integer();
277-
#[cfg(not(target_pointer_width = "64"))]
278-
return self.as_integer().map(i64::from);
275+
self.as_integer().map(i64::from)
279276
}
280277

281278
/// Cast the value to `u64`.

tests/tests.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -501,29 +501,6 @@ fn test_panic() -> Result<()> {
501501
Ok(())
502502
}
503503

504-
#[cfg(target_pointer_width = "64")]
505-
#[test]
506-
fn test_safe_integers() -> Result<()> {
507-
const MAX_SAFE_INTEGER: i64 = 2i64.pow(53) - 1;
508-
const MIN_SAFE_INTEGER: i64 = -2i64.pow(53) + 1;
509-
510-
let lua = Lua::new();
511-
let f = lua.load("return ...").into_function()?;
512-
513-
assert_eq!(f.call::<i64>(MAX_SAFE_INTEGER)?, MAX_SAFE_INTEGER);
514-
assert_eq!(f.call::<i64>(MIN_SAFE_INTEGER)?, MIN_SAFE_INTEGER);
515-
516-
// For Lua versions that does not support 64-bit integers, the values will be converted to f64
517-
#[cfg(any(feature = "luau", feature = "lua51", feature = "luajit"))]
518-
{
519-
assert_ne!(f.call::<i64>(MAX_SAFE_INTEGER + 2)?, MAX_SAFE_INTEGER + 2);
520-
assert_ne!(f.call::<i64>(MIN_SAFE_INTEGER - 2)?, MIN_SAFE_INTEGER - 2);
521-
assert_eq!(f.call::<f64>(i64::MAX)?, i64::MAX as f64);
522-
}
523-
524-
Ok(())
525-
}
526-
527504
#[test]
528505
fn test_num_conversion() -> Result<()> {
529506
let lua = Lua::new();

0 commit comments

Comments
 (0)