Skip to content

Commit 742adb6

Browse files
committed
Refactor lmathlib
1 parent 7045c1d commit 742adb6

12 files changed

+438
-593
lines changed

src/lapi.rs

+17
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,23 @@ pub unsafe extern "C" fn lua_tointegerx(
541541
return res;
542542
}
543543

544+
/*
545+
@@ lua_numbertointeger converts a float number to an integer, or
546+
** returns 0 if float is not within the range of a lua_Integer.
547+
** (The range comparisons are tricky because of rounding. The tests
548+
** here assume a two-complement representation, where MININTEGER always
549+
** has an exact representation as a float; MAXINTEGER may not have one,
550+
** and therefore its conversion to float may have an ill-defined value.)
551+
*/
552+
#[inline]
553+
pub fn lua_numbertointeger(n: lua_Number) -> Option<lua_Integer> {
554+
if n >= lua_Integer::MIN as lua_Number && n < -(lua_Integer::MIN as lua_Number) {
555+
Some(n as lua_Integer)
556+
} else {
557+
None
558+
}
559+
}
560+
544561
#[no_mangle]
545562
pub unsafe extern "C" fn lua_toboolean(L: *mut lua_State, idx: c_int) -> c_int {
546563
let o: *const TValue = index2addr(L, idx);

src/lauxlib.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ pub unsafe fn luaL_newlib(L: *mut lua_State, l: *const luaL_Reg) {
8888
luaL_setfuncs(L, l, 0);
8989
}
9090

91+
#[inline(always)]
92+
pub unsafe fn luaL_argcheck(
93+
L: *mut lua_State,
94+
cond: bool,
95+
arg: c_int,
96+
extramsg: *const c_char,
97+
) -> bool {
98+
cond || luaL_argerror(L, arg, extramsg) != 0
99+
}
100+
91101
pub unsafe fn luaL_loadfile(L: *mut lua_State, filename: *const c_char) -> c_int {
92102
luaL_loadfilex(L, filename, ptr::null())
93103
}
@@ -369,7 +379,7 @@ pub unsafe extern "C" fn luaL_argerror(
369379
i_ci: 0 as *mut CallInfo,
370380
};
371381
if lua_getstack(L, 0 as libc::c_int, &mut ar) == 0 {
372-
return luaL_error(
382+
luaL_error(
373383
L,
374384
b"bad argument #%d (%s)\0" as *const u8 as *const libc::c_char,
375385
arg,
@@ -380,7 +390,7 @@ pub unsafe extern "C" fn luaL_argerror(
380390
if strcmp(ar.namewhat, b"method\0" as *const u8 as *const libc::c_char) == 0 as libc::c_int {
381391
arg -= 1;
382392
if arg == 0 as libc::c_int {
383-
return luaL_error(
393+
luaL_error(
384394
L,
385395
b"calling '%s' on bad self (%s)\0" as *const u8 as *const libc::c_char,
386396
ar.name,
@@ -395,7 +405,7 @@ pub unsafe extern "C" fn luaL_argerror(
395405
b"?\0" as *const u8 as *const libc::c_char
396406
};
397407
}
398-
return luaL_error(
408+
luaL_error(
399409
L,
400410
b"bad argument #%d to '%s' (%s)\0" as *const u8 as *const libc::c_char,
401411
arg,
@@ -478,11 +488,7 @@ pub unsafe extern "C" fn luaL_where(L: *mut lua_State, level: libc::c_int) {
478488
** an error with "stack overflow" instead of the given message.)
479489
*/
480490
#[no_mangle]
481-
pub unsafe extern "C" fn luaL_error(
482-
L: *mut lua_State,
483-
fmt: *const libc::c_char,
484-
args: ...
485-
) -> c_int {
491+
pub unsafe extern "C" fn luaL_error(L: *mut lua_State, fmt: *const libc::c_char, args: ...) -> ! {
486492
let mut argp: ::core::ffi::VaListImpl;
487493
argp = args.clone();
488494
luaL_where(L, 1 as libc::c_int);

src/lbaselib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ unsafe extern "C" fn luaB_print(L: *mut lua_State) -> c_int {
5050
lua_call(L, 1, 1);
5151
let s: *const c_char = lua_tolstring(L, -1, &mut l); /* get result */
5252
if s.is_null() {
53-
return luaL_error(L, cstr!("'tostring' must return a string to 'print'"));
53+
luaL_error(L, cstr!("'tostring' must return a string to 'print'"));
5454
}
5555
if i > 1 as c_int {
5656
lua_writestring(cstr!("\t"), 1);
@@ -170,7 +170,7 @@ unsafe extern "C" fn luaB_setmetatable(L: *mut lua_State) -> c_int {
170170
// TODO: FIX
171171
luaL_checktype(L, 1, LUA_TTABLE); // FIXME? t is unused?
172172
if luaL_getmetafield(L, 1, cstr!("__metatable")) != LUA_TNIL {
173-
return luaL_error(L, cstr!("cannot change a protected metatable"));
173+
luaL_error(L, cstr!("cannot change a protected metatable"));
174174
}
175175
lua_settop(L, 2);
176176
lua_setmetatable(L, 1);

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub(crate) mod libs;
2525
pub(crate) mod linit;
2626
pub(crate) mod llex;
2727
pub(crate) mod llimits;
28-
pub(crate) mod lmathlib;
2928
pub(crate) mod lmem;
3029
pub(crate) mod loadlib;
3130
pub(crate) mod lopcodes;

0 commit comments

Comments
 (0)