]> rtime.felk.cvut.cz Git - l4.git/blobdiff - l4/pkg/lua/lib/contrib/src/lbaselib.c
update
[l4.git] / l4 / pkg / lua / lib / contrib / src / lbaselib.c
index 2a4c079d3b0457d8f62ae2311102727789615385..c359c7f6ea7fe06856322f839d8fdfc717fdd6d5 100644 (file)
@@ -18,7 +18,9 @@
 
 #include "lauxlib.h"
 #include "lualib.h"
-
+#include "llimits.h"
+#include "lobject.h"
+#include "lapi.h"
 
 
 
@@ -54,21 +56,30 @@ static int luaB_tonumber (lua_State *L) {
   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;
       }
     }
@@ -144,7 +155,7 @@ static int luaB_setfenv (lua_State *L) {
   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);
@@ -201,7 +212,7 @@ static int luaB_collectgarbage (lua_State *L) {
   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: {
@@ -209,7 +220,7 @@ static int luaB_collectgarbage (lua_State *L) {
       return 1;
     }
     default: {
-      lua_pushnumber(L, res);
+      lua_pushinteger(L, res);
       return 1;
     }
   }
@@ -631,6 +642,8 @@ static void base_open (lua_State *L) {
   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);