Skip to content

Commit

Permalink
new macro 'cvt2num' to better control whether strings are convertible
Browse files Browse the repository at this point in the history
to numbers
  • Loading branch information
roberto-ieru committed Jul 30, 2014
1 parent 34ac039 commit d861706
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lvm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.220 2014/07/21 16:02:10 roberto Exp roberto $
** $Id: lvm.c,v 2.221 2014/07/30 14:00:14 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -66,7 +66,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
*n = fltvalue(obj);
return 1;
}
else if (ttisstring(obj) && /* string convertible to number? */
else if (cvt2num(obj) && /* string convertible to number? */
luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
obj = &v;
goto again; /* convert result from 'luaO_str2num' to a float */
Expand Down Expand Up @@ -98,7 +98,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
*p = ivalue(obj);
return 1;
}
else if (ttisstring(obj) &&
else if (cvt2num(obj) &&
luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
obj = &v;
goto again; /* convert result from 'luaO_str2num' to an integer */
Expand Down
23 changes: 15 additions & 8 deletions lvm.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lvm.h,v 2.31 2014/05/26 17:10:22 roberto Exp roberto $
** $Id: lvm.h,v 2.32 2014/07/30 14:00:14 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
Expand All @@ -13,6 +13,20 @@
#include "ltm.h"


#if !defined(LUA_NOCVTN2S)
#define cvt2str(o) ttisnumber(o)
#else
#define cvt2str(o) 0 /* no convertion from numbers to strings */
#endif


#if !defined(LUA_NOCVTS2N)
#define cvt2num(o) ttisstring(o)
#else
#define cvt2num(o) 0 /* no convertion from strings to numbers */
#endif


#define tonumber(o,n) \
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))

Expand All @@ -24,13 +38,6 @@
#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)


#if !defined(LUA_NOCVTN2S)
#define cvt2str(o) ttisnumber(o)
#else
#define cvt2str(o) 0 /* no convertion from numbers to strings */
#endif


LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
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);
Expand Down

0 comments on commit d861706

Please sign in to comment.