Skip to content

Instantly share code, notes, and snippets.

@hishamhm
Created March 24, 2014 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hishamhm/9750528 to your computer and use it in GitHub Desktop.
Save hishamhm/9750528 to your computer and use it in GitHub Desktop.
diff -ur lua-5.2.3/src/lapi.c lua-5.2.3-new/src/lapi.c
--- lua-5.2.3/src/lapi.c 2013-04-12 15:48:47.000000000 -0300
+++ lua-5.2.3-new/src/lapi.c 2014-03-24 19:00:42.000000000 -0300
@@ -345,6 +345,19 @@
}
}
+LUA_API lua_Number lua_tonumberx2 (lua_State *L, int idx, int *isnum) {
+ TValue n;
+ const TValue *o = index2addr(L, idx);
+ if (tonumber2(o, &n)) {
+ if (isnum) *isnum = 1;
+ return nvalue(o);
+ }
+ else {
+ if (isnum) *isnum = 0;
+ return 0;
+ }
+}
+
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
TValue n;
diff -ur lua-5.2.3/src/lbaselib.c lua-5.2.3-new/src/lbaselib.c
--- lua-5.2.3/src/lbaselib.c 2013-04-12 15:48:47.000000000 -0300
+++ lua-5.2.3-new/src/lbaselib.c 2014-03-24 19:00:49.000000000 -0300
@@ -48,7 +48,7 @@
static int luaB_tonumber (lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion */
int isnum;
- lua_Number n = lua_tonumberx(L, 1, &isnum);
+ lua_Number n = lua_tonumberx2(L, 1, &isnum);
if (isnum) {
lua_pushnumber(L, n);
return 1;
diff -ur lua-5.2.3/src/lua.h lua-5.2.3-new/src/lua.h
--- lua-5.2.3/src/lua.h 2013-11-11 10:09:16.000000000 -0200
+++ lua-5.2.3-new/src/lua.h 2014-03-24 19:02:09.000000000 -0300
@@ -166,6 +166,7 @@
LUA_API const char *(lua_typename) (lua_State *L, int tp);
LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
+LUA_API lua_Number (lua_tonumberx2) (lua_State *L, int idx, int *isnum);
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum);
LUA_API int (lua_toboolean) (lua_State *L, int idx);
diff -ur lua-5.2.3/src/lvm.c lua-5.2.3-new/src/lvm.c
--- lua-5.2.3/src/lvm.c 2013-04-12 15:48:47.000000000 -0300
+++ lua-5.2.3-new/src/lvm.c 2014-03-24 19:02:32.000000000 -0300
@@ -33,16 +33,30 @@
const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
+ /*
lua_Number num;
+ */
if (ttisnumber(obj)) return obj;
+ /*
if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
setnvalue(n, num);
return n;
}
+ */
else
return NULL;
}
+const TValue *luaV_tonumber2 (const TValue *obj, TValue *n) {
+ lua_Number num;
+ if (ttisnumber(obj)) return obj;
+ if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
+ setnvalue(n, num);
+ return n;
+ }
+ else
+ return NULL;
+}
int luaV_tostring (lua_State *L, StkId obj) {
if (!ttisnumber(obj))
diff -ur lua-5.2.3/src/lvm.h lua-5.2.3-new/src/lvm.h
--- lua-5.2.3/src/lvm.h 2013-04-12 15:48:47.000000000 -0300
+++ lua-5.2.3-new/src/lvm.h 2014-03-24 19:01:54.000000000 -0300
@@ -17,6 +17,8 @@
#define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
+#define tonumber2(o,n) (ttisnumber(o) || (((o) = luaV_tonumber2(o,n)) != NULL))
+
#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2))
#define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2)
@@ -29,6 +31,7 @@
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
+LUAI_FUNC const TValue *luaV_tonumber2 (const TValue *obj, TValue *n);
LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
StkId val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment