backport math.type from 5.3

This commit is contained in:
Francois Perrad 2017-03-23 15:26:46 +01:00
parent 78f5f1cef1
commit 636aaed7cc
3 changed files with 23 additions and 0 deletions

View File

@ -94,6 +94,21 @@ LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN)
} }
LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX) LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX)
LJLIB_CF(math_type)
{
if (lua_type(L, 1) == LUA_TNUMBER) {
if (lua_isinteger(L, 1))
lua_pushliteral(L, "integer");
else
lua_pushliteral(L, "float");
}
else {
luaL_checkany(L, 1);
lua_pushnil(L);
}
return 1;
}
LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi) LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi)
LJLIB_PUSH(1e310) LJLIB_SET(huge) LJLIB_PUSH(1e310) LJLIB_SET(huge)

View File

@ -231,6 +231,12 @@ LUA_API int lua_iscfunction(lua_State *L, int idx)
return tvisfunc(o) && !isluafunc(funcV(o)); return tvisfunc(o) && !isluafunc(funcV(o));
} }
LUA_API int lua_isinteger (lua_State *L, int idx) {
cTValue *o = index2adr(L, idx);
return tvisint(o) ||
(tvisnumber(o) && ((lua_Number)(lua_Integer)numV(o) == numV(o)));
}
LUA_API int lua_isnumber(lua_State *L, int idx) LUA_API int lua_isnumber(lua_State *L, int idx)
{ {
cTValue *o = index2adr(L, idx); cTValue *o = index2adr(L, idx);

View File

@ -347,6 +347,8 @@ LUA_API void *lua_upvalueid (lua_State *L, int idx, int n);
LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2);
LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt,
const char *chunkname, const char *mode); const char *chunkname, const char *mode);
/* From Lua 5.3. */
LUA_API int lua_isinteger (lua_State *L, int idx);
struct lua_Debug { struct lua_Debug {