functions lua_tonumber/lua_tointeger replaced by lua_tonumberx/lua_tointegerx

that have an extra out parameter with conversion status
This commit is contained in:
Roberto Ierusalimschy
2010-07-02 14:35:06 -03:00
parent 7192afafee
commit a9dc7c8828
5 changed files with 37 additions and 24 deletions

19
lapi.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.130 2010/05/31 16:08:55 roberto Exp roberto $ ** $Id: lapi.c,v 2.131 2010/06/04 13:05:29 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -316,27 +316,34 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
} }
LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
TValue n; TValue n;
const TValue *o = index2addr(L, idx); const TValue *o = index2addr(L, idx);
if (tonumber(o, &n)) if (tonumber(o, &n)) {
if (isnum) *isnum = 1;
return nvalue(o); return nvalue(o);
else }
else {
if (isnum) *isnum = 0;
return 0; return 0;
}
} }
LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
TValue n; TValue n;
const TValue *o = index2addr(L, idx); const TValue *o = index2addr(L, idx);
if (tonumber(o, &n)) { if (tonumber(o, &n)) {
lua_Integer res; lua_Integer res;
lua_Number num = nvalue(o); lua_Number num = nvalue(o);
lua_number2integer(res, num); lua_number2integer(res, num);
if (isnum) *isnum = 1;
return res; return res;
} }
else else {
if (isnum) *isnum = 0;
return 0; return 0;
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.216 2010/06/30 17:40:27 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.217 2010/07/02 11:38:13 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -308,8 +308,9 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
lua_Number d = lua_tonumber(L, narg); int isnum;
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ lua_Number d = lua_tonumberx(L, narg, &isnum);
if (!isnum)
tag_error(L, narg, LUA_TNUMBER); tag_error(L, narg, LUA_TNUMBER);
return d; return d;
} }
@@ -321,8 +322,9 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
lua_Integer d = lua_tointeger(L, narg); int isnum;
if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ lua_Integer d = lua_tointegerx(L, narg, &isnum);
if (!isnum)
tag_error(L, narg, LUA_TNUMBER); tag_error(L, narg, LUA_TNUMBER);
return d; return d;
} }
@@ -624,9 +626,10 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
LUALIB_API int luaL_len (lua_State *L, int idx) { LUALIB_API int luaL_len (lua_State *L, int idx) {
int l; int l;
int isnum;
lua_len(L, idx); lua_len(L, idx);
l = lua_tointeger(L, -1); l = lua_tointegerx(L, -1, &isnum);
if (l == 0 && !lua_isnumber(L, -1)) if (!isnum)
luaL_error(L, "object length is not a number"); luaL_error(L, "object length is not a number");
lua_pop(L, 1); /* remove object */ lua_pop(L, 1); /* remove object */
return l; return l;

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lbitlib.c,v 1.4 2010/02/11 15:55:29 roberto Exp roberto $ ** $Id: lbitlib.c,v 1.5 2010/07/02 11:38:13 roberto Exp roberto $
** Standard library for bitwise operations ** Standard library for bitwise operations
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -23,8 +23,9 @@ typedef unsigned LUA_INT32 b_uint;
static b_uint getuintarg (lua_State *L, int arg) { static b_uint getuintarg (lua_State *L, int arg) {
b_uint r; b_uint r;
lua_Number x = lua_tonumber(L, arg); int isnum;
if (x == 0) luaL_checktype(L, arg, LUA_TNUMBER); lua_Number x = lua_tonumberx(L, arg, &isnum);
if (!isnum) luaL_typeerror(L, arg, "number");
lua_number2uint(r, x); lua_number2uint(r, x);
return r; return r;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: loslib.c,v 1.29 2009/12/17 13:08:51 roberto Exp roberto $ ** $Id: loslib.c,v 1.30 2010/07/02 11:38:13 roberto Exp roberto $
** Standard Operating System library ** Standard Operating System library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -146,11 +146,10 @@ static int getboolfield (lua_State *L, const char *key) {
static int getfield (lua_State *L, const char *key, int d) { static int getfield (lua_State *L, const char *key, int d) {
int res; int res, isnum;
lua_getfield(L, -1, key); lua_getfield(L, -1, key);
if (lua_isnumber(L, -1)) res = (int)lua_tointegerx(L, -1, &isnum);
res = (int)lua_tointeger(L, -1); if (!isnum) {
else {
if (d < 0) if (d < 0)
return luaL_error(L, "field " LUA_QS " missing in date table", key); return luaL_error(L, "field " LUA_QS " missing in date table", key);
res = d; res = d;

9
lua.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.269 2010/05/10 13:50:20 roberto Exp roberto $ ** $Id: lua.h,v 1.270 2010/05/12 14:09:20 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@@ -153,8 +153,8 @@ LUA_API int (lua_isuserdata) (lua_State *L, int idx);
LUA_API int (lua_type) (lua_State *L, int idx); LUA_API int (lua_type) (lua_State *L, int idx);
LUA_API const char *(lua_typename) (lua_State *L, int tp); LUA_API const char *(lua_typename) (lua_State *L, int tp);
LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
LUA_API int (lua_toboolean) (lua_State *L, int idx); LUA_API int (lua_toboolean) (lua_State *L, int idx);
LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
LUA_API size_t (lua_rawlen) (lua_State *L, int idx); LUA_API size_t (lua_rawlen) (lua_State *L, int idx);
@@ -296,6 +296,9 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
** =============================================================== ** ===============================================================
*/ */
#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL)
#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)
#define lua_pop(L,n) lua_settop(L, -(n)-1) #define lua_pop(L,n) lua_settop(L, -(n)-1)
#define lua_newtable(L) lua_createtable(L, 0, 0) #define lua_newtable(L) lua_createtable(L, 0, 0)