aboutsummaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-30 11:00:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-07-30 11:00:14 -0300
commit34ac039fb84e3c12fb8c96c9c99c34224c09872b (patch)
treea859f6338c6c851088a67b129765de662ed6f223 /lvm.c
parent1aa526263405fb4906eafab3011b13de4e5daf73 (diff)
downloadlua-34ac039fb84e3c12fb8c96c9c99c34224c09872b.tar.gz
lua-34ac039fb84e3c12fb8c96c9c99c34224c09872b.tar.bz2
lua-34ac039fb84e3c12fb8c96c9c99c34224c09872b.zip
new macro 'cvt2str' to better control whether numbers are convertible
to strings
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c37
1 files changed, 4 insertions, 33 deletions
diff --git a/lvm.c b/lvm.c
index ec5bf62b..89ef8997 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.219 2014/07/18 13:36:14 roberto Exp roberto $ 2** $Id: lvm.c,v 2.220 2014/07/21 16:02:10 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -33,10 +33,6 @@
33#define MAXTAGLOOP 2000 33#define MAXTAGLOOP 2000
34 34
35 35
36/* maximum length of the conversion of a number to a string */
37#define MAXNUMBER2STR 50
38
39
40/* 36/*
41** Similar to 'tonumber', but does not attempt to convert strings and 37** Similar to 'tonumber', but does not attempt to convert strings and
42** ensure correct precision (no extra bits). Used in comparisons. 38** ensure correct precision (no extra bits). Used in comparisons.
@@ -120,32 +116,6 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
120 116
121 117
122/* 118/*
123** Convert a number object to a string
124*/
125int luaV_tostring (lua_State *L, StkId obj) {
126 if (!ttisnumber(obj))
127 return 0;
128 else {
129 char buff[MAXNUMBER2STR];
130 size_t len;
131 if (ttisinteger(obj))
132 len = lua_integer2str(buff, ivalue(obj));
133 else {
134 len = lua_number2str(buff, fltvalue(obj));
135#if !defined(LUA_COMPAT_FLOATSTRING)
136 if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
137 buff[len++] = '.';
138 buff[len++] = '0'; /* adds '.0' to result */
139 }
140#endif
141 }
142 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
143 return 1;
144 }
145}
146
147
148/*
149** Try to convert a 'for' limit to an integer, preserving the 119** Try to convert a 'for' limit to an integer, preserving the
150** semantics of the loop. 120** semantics of the loop.
151** (The following explanation assumes a non-negative step; it is valid 121** (The following explanation assumes a non-negative step; it is valid
@@ -374,7 +344,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
374 344
375 345
376/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */ 346/* macro used by 'luaV_concat' to ensure that element at 'o' is a string */
377#define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 347#define tostring(L,o) \
348 (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1)))
378 349
379/* 350/*
380** Main operation for concatenation: concat 'total' values in the stack, 351** Main operation for concatenation: concat 'total' values in the stack,
@@ -385,7 +356,7 @@ void luaV_concat (lua_State *L, int total) {
385 do { 356 do {
386 StkId top = L->top; 357 StkId top = L->top;
387 int n = 2; /* number of elements handled in this pass (at least 2) */ 358 int n = 2; /* number of elements handled in this pass (at least 2) */
388 if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) 359 if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
389 luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); 360 luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
390 else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ 361 else if (tsvalue(top-1)->len == 0) /* second operand is empty? */
391 cast_void(tostring(L, top - 2)); /* result is first operand */ 362 cast_void(tostring(L, top - 2)); /* result is first operand */