** Try to convert a 'for' limit to an integer, preserving the ** semantics of the loop. ** (The following explanation assumes a non-negative step; it is valid ** for negative steps mutatis mutandis.) ** If the limit can be converted to an integer, rounding down, that is ** it. ** Otherwise, check whether the limit can be converted to a number. If ** the number is too large, it is OK to set the li
| 133 | ** case the LUA_MININTEGER limit would still run the loop once. |
| 134 | */ |
| 135 | static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, |
| 136 | int *stopnow) { |
| 137 | *stopnow = 0; /* usually, let loops run */ |
| 138 | if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ |
| 139 | lua_Number n; /* try to convert to float */ |
| 140 | if (!tonumber(obj, &n)) /* cannot convert to float? */ |
| 141 | return 0; /* not a number */ |
| 142 | if (luai_numlt(0, n)) { /* if true, float is larger than max integer */ |
| 143 | *p = LUA_MAXINTEGER; |
| 144 | if (step < 0) *stopnow = 1; |
| 145 | } |
| 146 | else { /* float is smaller than min integer */ |
| 147 | *p = LUA_MININTEGER; |
| 148 | if (step >= 0) *stopnow = 1; |
| 149 | } |
| 150 | } |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* |
no test coverage detected