#include "lauxlib.h"
#include "lualib.h"
-
+#include "llimits.h"
+#include "lobject.h"
+#include "lapi.h"
int base = luaL_optint(L, 2, 10);
if (base == 10) { /* standard conversion */
luaL_checkany(L, 1);
- if (lua_isnumber(L, 1)) {
+ if (lua_isnumber(L, 1)) { /* numeric string, or a number */
+#ifdef LUA_TINT
+ lua_pushvalue_as_number(L,1); /* API extension (not to lose accuracy here) */
+#else
lua_pushnumber(L, lua_tonumber(L, 1));
+#endif
return 1;
- }
+ }
}
else {
const char *s1 = luaL_checkstring(L, 1);
char *s2;
- unsigned long n;
+ unsigned LUA_INTEGER n;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
- n = strtoul(s1, &s2, base);
+ n = lua_str2ul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
if (*s2 == '\0') { /* no invalid trailing characters? */
- lua_pushnumber(L, (lua_Number)n);
+
+ /* Push as number, there needs to be separate 'luaB_tointeger' for
+ * when the caller wants to preserve the bits (matters if unsigned
+ * values are used).
+ */
+ lua_pushnumber(L, cast_num(n));
return 1;
}
}
luaL_checktype(L, 2, LUA_TTABLE);
getfunc(L, 0);
lua_pushvalue(L, 2);
- if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
+ if (lua_isnumber(L, 1) && lua_tointeger(L, 1) == 0) {
/* change environment of current thread */
lua_pushthread(L);
lua_insert(L, -2);
switch (optsnum[o]) {
case LUA_GCCOUNT: {
int b = lua_gc(L, LUA_GCCOUNTB, 0);
- lua_pushnumber(L, res + ((lua_Number)b/1024));
+ lua_pushnumber(L, res + (cast_num(b)/1024));
return 1;
}
case LUA_GCSTEP: {
return 1;
}
default: {
- lua_pushnumber(L, res);
+ lua_pushinteger(L, res);
return 1;
}
}
luaL_register(L, "_G", base_funcs);
lua_pushliteral(L, LUA_VERSION);
lua_setglobal(L, "_VERSION"); /* set global _VERSION */
+ lua_pushliteral(L, LUA_LNUM);
+ lua_setglobal(L, "_LNUM"); /* "[complex] double|float|ldouble [int16|int32|int64]" */
/* `ipairs' and `pairs' need auxliliary functions as upvalues */
auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
auxopen(L, "pairs", luaB_pairs, luaB_next);