diff options
| -rw-r--r-- | lvm.c | 21 |
1 files changed, 15 insertions, 6 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 2.226 2014/10/25 11:50:46 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.227 2014/11/02 19:19:04 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 | */ |
| @@ -30,6 +30,15 @@ | |||
| 30 | #include "lvm.h" | 30 | #include "lvm.h" |
| 31 | 31 | ||
| 32 | 32 | ||
| 33 | /* | ||
| 34 | ** You can define LUA_FLOORN2I if you want to convert floats to integers | ||
| 35 | ** by flooring them (instead of raising an error if they are not | ||
| 36 | ** integral values) | ||
| 37 | */ | ||
| 38 | #if !defined(LUA_FLOORN2I) | ||
| 39 | #define LUA_FLOORN2I 0 | ||
| 40 | #endif | ||
| 41 | |||
| 33 | 42 | ||
| 34 | /* limit for table tag-method chains (to avoid loops) */ | 43 | /* limit for table tag-method chains (to avoid loops) */ |
| 35 | #define MAXTAGLOOP 2000 | 44 | #define MAXTAGLOOP 2000 |
| @@ -80,8 +89,8 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |||
| 80 | /* | 89 | /* |
| 81 | ** try to convert a value to an integer, rounding according to 'mode': | 90 | ** try to convert a value to an integer, rounding according to 'mode': |
| 82 | ** mode == 0: accepts only integral values | 91 | ** mode == 0: accepts only integral values |
| 83 | ** mode < 0: takes the floor of the number | 92 | ** mode == 1: takes the floor of the number |
| 84 | ** mode > 0: takes the ceil of the number | 93 | ** mode == 2: takes the ceil of the number |
| 85 | */ | 94 | */ |
| 86 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { | 95 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { |
| 87 | TValue v; | 96 | TValue v; |
| @@ -91,7 +100,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { | |||
| 91 | lua_Number f = l_floor(n); | 100 | lua_Number f = l_floor(n); |
| 92 | if (n != f) { /* not an integral value? */ | 101 | if (n != f) { /* not an integral value? */ |
| 93 | if (mode == 0) return 0; /* fails if mode demands integral value */ | 102 | if (mode == 0) return 0; /* fails if mode demands integral value */ |
| 94 | else if (mode > 0) /* needs ceil? */ | 103 | else if (mode > 1) /* needs ceil? */ |
| 95 | f += 1; /* convert floor to ceil (remember: n != f) */ | 104 | f += 1; /* convert floor to ceil (remember: n != f) */ |
| 96 | } | 105 | } |
| 97 | return lua_numbertointeger(f, p); | 106 | return lua_numbertointeger(f, p); |
| @@ -113,7 +122,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { | |||
| 113 | ** try to convert a value to an integer | 122 | ** try to convert a value to an integer |
| 114 | */ | 123 | */ |
| 115 | int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | 124 | int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { |
| 116 | return tointeger_aux(obj, p, 0); | 125 | return tointeger_aux(obj, p, LUA_FLOORN2I); |
| 117 | } | 126 | } |
| 118 | 127 | ||
| 119 | 128 | ||
| @@ -135,7 +144,7 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { | |||
| 135 | static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, | 144 | static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, |
| 136 | int *stopnow) { | 145 | int *stopnow) { |
| 137 | *stopnow = 0; /* usually, let loops run */ | 146 | *stopnow = 0; /* usually, let loops run */ |
| 138 | if (!tointeger_aux(obj, p, (step < 0 ? 1 : -1))) { /* not fit in integer? */ | 147 | if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ |
| 139 | lua_Number n; /* try to convert to float */ | 148 | lua_Number n; /* try to convert to float */ |
| 140 | if (!tonumber(obj, &n)) /* cannot convert to float? */ | 149 | if (!tonumber(obj, &n)) /* cannot convert to float? */ |
| 141 | return 0; /* not a number */ | 150 | return 0; /* not a number */ |
