diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-21 09:54:26 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-21 09:54:26 -0300 |
commit | 465b47489910c8660f7d8f9869ffacdbb2d5d292 (patch) | |
tree | 3da1e80d05496bf48515dd71c55f14cdcb252ed1 | |
parent | 6353d619a594b5dfbef5dad10af9d4c051878466 (diff) | |
download | lua-465b47489910c8660f7d8f9869ffacdbb2d5d292.tar.gz lua-465b47489910c8660f7d8f9869ffacdbb2d5d292.tar.bz2 lua-465b47489910c8660f7d8f9869ffacdbb2d5d292.zip |
small reorganization of 'luaV_flttointeger'/'luaV_tointeger'
-rw-r--r-- | lcode.c | 6 | ||||
-rw-r--r-- | ltable.c | 9 | ||||
-rw-r--r-- | lvm.c | 48 | ||||
-rw-r--r-- | lvm.h | 10 |
4 files changed, 39 insertions, 34 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 2.154 2018/02/15 15:34:29 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.155 2018/02/17 19:20:00 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -605,9 +605,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) { | |||
605 | 605 | ||
606 | 606 | ||
607 | static int floatI (lua_Number f, lua_Integer *fi) { | 607 | static int floatI (lua_Number f, lua_Integer *fi) { |
608 | TValue v; | 608 | return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi)); |
609 | setfltvalue(&v, f); | ||
610 | return (luaV_flttointeger(&v, fi, 0) && fitsBx(*fi)); | ||
611 | } | 609 | } |
612 | 610 | ||
613 | 611 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 2.131 2018/01/28 15:13:26 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 2.132 2018/02/19 20:06:56 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -559,12 +559,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { | |||
559 | TValue aux; | 559 | TValue aux; |
560 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); | 560 | if (ttisnil(key)) luaG_runerror(L, "table index is nil"); |
561 | else if (ttisfloat(key)) { | 561 | else if (ttisfloat(key)) { |
562 | lua_Number f = fltvalue(key); | ||
562 | lua_Integer k; | 563 | lua_Integer k; |
563 | if (luaV_flttointeger(key, &k, 0)) { /* does index fit in an integer? */ | 564 | if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */ |
564 | setivalue(&aux, k); | 565 | setivalue(&aux, k); |
565 | key = &aux; /* insert it as an integer */ | 566 | key = &aux; /* insert it as an integer */ |
566 | } | 567 | } |
567 | else if (luai_numisnan(fltvalue(key))) | 568 | else if (luai_numisnan(f)) |
568 | luaG_runerror(L, "table index is NaN"); | 569 | luaG_runerror(L, "table index is NaN"); |
569 | } | 570 | } |
570 | mp = mainpositionTV(t, key); | 571 | mp = mainpositionTV(t, key); |
@@ -669,7 +670,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
669 | case LUA_TNIL: return luaO_nilobject; | 670 | case LUA_TNIL: return luaO_nilobject; |
670 | case LUA_TNUMFLT: { | 671 | case LUA_TNUMFLT: { |
671 | lua_Integer k; | 672 | lua_Integer k; |
672 | if (luaV_flttointeger(key, &k, 0)) /* index is an integral? */ | 673 | if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ |
673 | return luaH_getint(t, k); /* use specialized version */ | 674 | return luaH_getint(t, k); /* use specialized version */ |
674 | /* else... */ | 675 | /* else... */ |
675 | } /* FALLTHROUGH */ | 676 | } /* FALLTHROUGH */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.341 2018/02/17 19:20:00 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.342 2018/02/19 20:06:56 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 | */ |
@@ -90,36 +90,42 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |||
90 | ** mode == 1: takes the floor of the number | 90 | ** mode == 1: takes the floor of the number |
91 | ** mode == 2: takes the ceil of the number | 91 | ** mode == 2: takes the ceil of the number |
92 | */ | 92 | */ |
93 | int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) { | 93 | int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { |
94 | if (!ttisfloat(obj)) | 94 | lua_Number f = l_floor(n); |
95 | return 0; | 95 | if (n != f) { /* not an integral value? */ |
96 | else { | 96 | if (mode == 0) return 0; /* fails if mode demands integral value */ |
97 | lua_Number n = fltvalue(obj); | 97 | else if (mode > 1) /* needs ceil? */ |
98 | lua_Number f = l_floor(n); | 98 | f += 1; /* convert floor to ceil (remember: n != f) */ |
99 | if (n != f) { /* not an integral value? */ | ||
100 | if (mode == 0) return 0; /* fails if mode demands integral value */ | ||
101 | else if (mode > 1) /* needs ceil? */ | ||
102 | f += 1; /* convert floor to ceil (remember: n != f) */ | ||
103 | } | ||
104 | return lua_numbertointeger(f, p); | ||
105 | } | 99 | } |
100 | return lua_numbertointeger(f, p); | ||
106 | } | 101 | } |
107 | 102 | ||
108 | 103 | ||
109 | /* | 104 | /* |
110 | ** try to convert a value to an integer. ("Fast track" is handled | 105 | ** try to convert a value to an integer, rounding according to 'mode', |
111 | ** by macro 'tointeger'.) | 106 | ** without string coercion. |
107 | ** ("Fast track" handled by macro 'tointegerns'.) | ||
112 | */ | 108 | */ |
113 | int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { | 109 | int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { |
114 | TValue v; | 110 | if (ttisfloat(obj)) |
115 | if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) | 111 | return luaV_flttointeger(fltvalue(obj), p, mode); |
116 | obj = &v; /* change string to its corresponding number */ | 112 | else if (ttisinteger(obj)) { |
117 | if (ttisinteger(obj)) { | ||
118 | *p = ivalue(obj); | 113 | *p = ivalue(obj); |
119 | return 1; | 114 | return 1; |
120 | } | 115 | } |
121 | else | 116 | else |
122 | return luaV_flttointeger(obj, p, mode); | 117 | return 0; |
118 | } | ||
119 | |||
120 | |||
121 | /* | ||
122 | ** try to convert a value to an integer. | ||
123 | */ | ||
124 | int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { | ||
125 | TValue v; | ||
126 | if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) | ||
127 | obj = &v; /* change string to its corresponding number */ | ||
128 | return luaV_tointegerns(obj, p, mode); | ||
123 | } | 129 | } |
124 | 130 | ||
125 | 131 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 2.48 2017/11/29 13:02:17 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 2.49 2018/02/19 20:06:56 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 | */ |
@@ -50,13 +50,12 @@ | |||
50 | 50 | ||
51 | /* convert an object to an integer (including string coercion) */ | 51 | /* convert an object to an integer (including string coercion) */ |
52 | #define tointeger(o,i) \ | 52 | #define tointeger(o,i) \ |
53 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) | 53 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I)) |
54 | 54 | ||
55 | 55 | ||
56 | /* convert an object to an integer (without string coercion) */ | 56 | /* convert an object to an integer (without string coercion) */ |
57 | #define tointegerns(o,i) \ | 57 | #define tointegerns(o,i) \ |
58 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) \ | 58 | (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I)) |
59 | : luaV_flttointeger(o,i,LUA_FLOORN2I)) | ||
60 | 59 | ||
61 | 60 | ||
62 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) | 61 | #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) |
@@ -106,7 +105,8 @@ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); | |||
106 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); | 105 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); |
107 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); | 106 | LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); |
108 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); | 107 | LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode); |
109 | LUAI_FUNC int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode); | 108 | LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode); |
109 | LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode); | ||
110 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, | 110 | LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, |
111 | StkId val, const TValue *slot); | 111 | StkId val, const TValue *slot); |
112 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, | 112 | LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, |