aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 14:35:06 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-07-02 14:35:06 -0300
commita9dc7c88283a8046ad40c592f9e626d93e8e14a1 (patch)
tree693fa40d1ca9beceb5af5d4d4325401abe3f96a2
parent7192afafeeb1a96b3de60af90a72cd8762b09d94 (diff)
downloadlua-a9dc7c88283a8046ad40c592f9e626d93e8e14a1.tar.gz
lua-a9dc7c88283a8046ad40c592f9e626d93e8e14a1.tar.bz2
lua-a9dc7c88283a8046ad40c592f9e626d93e8e14a1.zip
functions lua_tonumber/lua_tointeger replaced by lua_tonumberx/lua_tointegerx
that have an extra out parameter with conversion status
-rw-r--r--lapi.c19
-rw-r--r--lauxlib.c17
-rw-r--r--lbitlib.c7
-rw-r--r--loslib.c9
-rw-r--r--lua.h9
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 @@
1/* 1/*
2** $Id: lapi.c,v 2.130 2010/05/31 16:08:55 roberto Exp roberto $ 2** $Id: lapi.c,v 2.131 2010/06/04 13:05:29 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -316,27 +316,34 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
316} 316}
317 317
318 318
319LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { 319LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *isnum) {
320 TValue n; 320 TValue n;
321 const TValue *o = index2addr(L, idx); 321 const TValue *o = index2addr(L, idx);
322 if (tonumber(o, &n)) 322 if (tonumber(o, &n)) {
323 if (isnum) *isnum = 1;
323 return nvalue(o); 324 return nvalue(o);
324 else 325 }
326 else {
327 if (isnum) *isnum = 0;
325 return 0; 328 return 0;
329 }
326} 330}
327 331
328 332
329LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { 333LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
330 TValue n; 334 TValue n;
331 const TValue *o = index2addr(L, idx); 335 const TValue *o = index2addr(L, idx);
332 if (tonumber(o, &n)) { 336 if (tonumber(o, &n)) {
333 lua_Integer res; 337 lua_Integer res;
334 lua_Number num = nvalue(o); 338 lua_Number num = nvalue(o);
335 lua_number2integer(res, num); 339 lua_number2integer(res, num);
340 if (isnum) *isnum = 1;
336 return res; 341 return res;
337 } 342 }
338 else 343 else {
344 if (isnum) *isnum = 0;
339 return 0; 345 return 0;
346 }
340} 347}
341 348
342 349
diff --git a/lauxlib.c b/lauxlib.c
index 2f0168f1..38bc27d4 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.216 2010/06/30 17:40:27 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.217 2010/07/02 11:38:13 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -308,8 +308,9 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,
308 308
309 309
310LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { 310LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {
311 lua_Number d = lua_tonumber(L, narg); 311 int isnum;
312 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 312 lua_Number d = lua_tonumberx(L, narg, &isnum);
313 if (!isnum)
313 tag_error(L, narg, LUA_TNUMBER); 314 tag_error(L, narg, LUA_TNUMBER);
314 return d; 315 return d;
315} 316}
@@ -321,8 +322,9 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {
321 322
322 323
323LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { 324LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
324 lua_Integer d = lua_tointeger(L, narg); 325 int isnum;
325 if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ 326 lua_Integer d = lua_tointegerx(L, narg, &isnum);
327 if (!isnum)
326 tag_error(L, narg, LUA_TNUMBER); 328 tag_error(L, narg, LUA_TNUMBER);
327 return d; 329 return d;
328} 330}
@@ -624,9 +626,10 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
624 626
625LUALIB_API int luaL_len (lua_State *L, int idx) { 627LUALIB_API int luaL_len (lua_State *L, int idx) {
626 int l; 628 int l;
629 int isnum;
627 lua_len(L, idx); 630 lua_len(L, idx);
628 l = lua_tointeger(L, -1); 631 l = lua_tointegerx(L, -1, &isnum);
629 if (l == 0 && !lua_isnumber(L, -1)) 632 if (!isnum)
630 luaL_error(L, "object length is not a number"); 633 luaL_error(L, "object length is not a number");
631 lua_pop(L, 1); /* remove object */ 634 lua_pop(L, 1); /* remove object */
632 return l; 635 return l;
diff --git a/lbitlib.c b/lbitlib.c
index cae1b1f5..21cf95b6 100644
--- a/lbitlib.c
+++ b/lbitlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbitlib.c,v 1.4 2010/02/11 15:55:29 roberto Exp roberto $ 2** $Id: lbitlib.c,v 1.5 2010/07/02 11:38:13 roberto Exp roberto $
3** Standard library for bitwise operations 3** Standard library for bitwise operations
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -23,8 +23,9 @@ typedef unsigned LUA_INT32 b_uint;
23 23
24static b_uint getuintarg (lua_State *L, int arg) { 24static b_uint getuintarg (lua_State *L, int arg) {
25 b_uint r; 25 b_uint r;
26 lua_Number x = lua_tonumber(L, arg); 26 int isnum;
27 if (x == 0) luaL_checktype(L, arg, LUA_TNUMBER); 27 lua_Number x = lua_tonumberx(L, arg, &isnum);
28 if (!isnum) luaL_typeerror(L, arg, "number");
28 lua_number2uint(r, x); 29 lua_number2uint(r, x);
29 return r; 30 return r;
30} 31}
diff --git a/loslib.c b/loslib.c
index 2c378e9b..6baebd91 100644
--- a/loslib.c
+++ b/loslib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loslib.c,v 1.29 2009/12/17 13:08:51 roberto Exp roberto $ 2** $Id: loslib.c,v 1.30 2010/07/02 11:38:13 roberto Exp roberto $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -146,11 +146,10 @@ static int getboolfield (lua_State *L, const char *key) {
146 146
147 147
148static int getfield (lua_State *L, const char *key, int d) { 148static int getfield (lua_State *L, const char *key, int d) {
149 int res; 149 int res, isnum;
150 lua_getfield(L, -1, key); 150 lua_getfield(L, -1, key);
151 if (lua_isnumber(L, -1)) 151 res = (int)lua_tointegerx(L, -1, &isnum);
152 res = (int)lua_tointeger(L, -1); 152 if (!isnum) {
153 else {
154 if (d < 0) 153 if (d < 0)
155 return luaL_error(L, "field " LUA_QS " missing in date table", key); 154 return luaL_error(L, "field " LUA_QS " missing in date table", key);
156 res = d; 155 res = d;
diff --git a/lua.h b/lua.h
index 586c9388..87c81e75 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.269 2010/05/10 13:50:20 roberto Exp roberto $ 2** $Id: lua.h,v 1.270 2010/05/12 14:09:20 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -153,8 +153,8 @@ LUA_API int (lua_isuserdata) (lua_State *L, int idx);
153LUA_API int (lua_type) (lua_State *L, int idx); 153LUA_API int (lua_type) (lua_State *L, int idx);
154LUA_API const char *(lua_typename) (lua_State *L, int tp); 154LUA_API const char *(lua_typename) (lua_State *L, int tp);
155 155
156LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); 156LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
157LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); 157LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
158LUA_API int (lua_toboolean) (lua_State *L, int idx); 158LUA_API int (lua_toboolean) (lua_State *L, int idx);
159LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); 159LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
160LUA_API size_t (lua_rawlen) (lua_State *L, int idx); 160LUA_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);
296** =============================================================== 296** ===============================================================
297*/ 297*/
298 298
299#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL)
300#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)
301
299#define lua_pop(L,n) lua_settop(L, -(n)-1) 302#define lua_pop(L,n) lua_settop(L, -(n)-1)
300 303
301#define lua_newtable(L) lua_createtable(L, 0, 0) 304#define lua_newtable(L) lua_createtable(L, 0, 0)