| 300 | } |
| 301 | |
| 302 | static uint64_t lpb_tointegerx(lua_State *L, int idx, int *isint) { |
| 303 | int neg = 0; |
| 304 | const char *s, *os; |
| 305 | #if LUA_VERSION_NUM >= 503 |
| 306 | uint64_t v = (uint64_t)lua_tointegerx(L, idx, isint); |
| 307 | if (*isint) return v; |
| 308 | #else |
| 309 | uint64_t v = 0; |
| 310 | lua_Number nv = lua_tonumberx(L, idx, isint); |
| 311 | if (*isint) { |
| 312 | if (nv < (lua_Number)INT64_MIN || nv > (lua_Number)INT64_MAX) |
| 313 | luaL_error(L, "number has no integer representation"); |
| 314 | return (uint64_t)(int64_t)nv; |
| 315 | } |
| 316 | #endif |
| 317 | if ((os = s = lua_tostring(L, idx)) == NULL) return 0; |
| 318 | while (*s == '#' || *s == '+' || *s == '-') |
| 319 | neg = (*s == '-') ^ neg, ++s; |
| 320 | if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) { |
| 321 | for (s += 2; *s != '\0'; ++s) { |
| 322 | int n = lpb_hexchar(*s); |
| 323 | if (n < 0) break; |
| 324 | v = v << 4 | n; |
| 325 | } |
| 326 | } else { |
| 327 | for (; *s != '\0'; ++s) { |
| 328 | int n = lpb_hexchar(*s); |
| 329 | if (n < 0 || n > 10) break; |
| 330 | v = v * 10 + n; |
| 331 | } |
| 332 | } |
| 333 | if (!(*isint = *s == '\0')) return 0; |
| 334 | return neg ? ~v + 1 : v; |
| 335 | } |
| 336 | |
| 337 | static uint64_t lpb_checkinteger(lua_State *L, int idx) { |
| 338 | int isint; |
no test coverage detected