From a9dc7c88283a8046ad40c592f9e626d93e8e14a1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 2 Jul 2010 14:35:06 -0300 Subject: [PATCH] functions lua_tonumber/lua_tointeger replaced by lua_tonumberx/lua_tointegerx that have an extra out parameter with conversion status --- lapi.c | 19 +++++++++++++------ lauxlib.c | 17 ++++++++++------- lbitlib.c | 7 ++++--- loslib.c | 9 ++++----- lua.h | 9 ++++++--- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lapi.c b/lapi.c index c811be99..3b0d13a9 100644 --- a/lapi.c +++ b/lapi.c @@ -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 ** 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; const TValue *o = index2addr(L, idx); - if (tonumber(o, &n)) + if (tonumber(o, &n)) { + if (isnum) *isnum = 1; return nvalue(o); - else + } + else { + if (isnum) *isnum = 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; const TValue *o = index2addr(L, idx); if (tonumber(o, &n)) { lua_Integer res; lua_Number num = nvalue(o); lua_number2integer(res, num); + if (isnum) *isnum = 1; return res; } - else + else { + if (isnum) *isnum = 0; return 0; + } } diff --git a/lauxlib.c b/lauxlib.c index 2f0168f1..38bc27d4 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -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 ** 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) { - lua_Number d = lua_tonumber(L, narg); - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ + int isnum; + lua_Number d = lua_tonumberx(L, narg, &isnum); + if (!isnum) tag_error(L, narg, LUA_TNUMBER); 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) { - lua_Integer d = lua_tointeger(L, narg); - if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ + int isnum; + lua_Integer d = lua_tointegerx(L, narg, &isnum); + if (!isnum) tag_error(L, narg, LUA_TNUMBER); 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) { int l; + int isnum; lua_len(L, idx); - l = lua_tointeger(L, -1); - if (l == 0 && !lua_isnumber(L, -1)) + l = lua_tointegerx(L, -1, &isnum); + if (!isnum) luaL_error(L, "object length is not a number"); lua_pop(L, 1); /* remove object */ return l; diff --git a/lbitlib.c b/lbitlib.c index cae1b1f5..21cf95b6 100644 --- a/lbitlib.c +++ b/lbitlib.c @@ -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 ** 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) { b_uint r; - lua_Number x = lua_tonumber(L, arg); - if (x == 0) luaL_checktype(L, arg, LUA_TNUMBER); + int isnum; + lua_Number x = lua_tonumberx(L, arg, &isnum); + if (!isnum) luaL_typeerror(L, arg, "number"); lua_number2uint(r, x); return r; } diff --git a/loslib.c b/loslib.c index 2c378e9b..6baebd91 100644 --- a/loslib.c +++ b/loslib.c @@ -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 ** 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) { - int res; + int res, isnum; lua_getfield(L, -1, key); - if (lua_isnumber(L, -1)) - res = (int)lua_tointeger(L, -1); - else { + res = (int)lua_tointegerx(L, -1, &isnum); + if (!isnum) { if (d < 0) return luaL_error(L, "field " LUA_QS " missing in date table", key); res = d; diff --git a/lua.h b/lua.h index 586c9388..87c81e75 100644 --- a/lua.h +++ b/lua.h @@ -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.org, PUC-Rio, Brazil (http://www.lua.org) ** 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 const char *(lua_typename) (lua_State *L, int tp); -LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); -LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); +LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); +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 const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 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_newtable(L) lua_createtable(L, 0, 0)