mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-02-08 07:34:07 +00:00
Give expected results for negative non-base-10 numbers in tonumber().
This was undefined in Lua 5.1, but it's defined in 5.2.
This commit is contained in:
parent
fe651bf6e2
commit
f3cf0d6e15
@ -287,18 +287,27 @@ LJLIB_ASM(tonumber) LJLIB_REC(.)
|
|||||||
} else {
|
} else {
|
||||||
const char *p = strdata(lj_lib_checkstr(L, 1));
|
const char *p = strdata(lj_lib_checkstr(L, 1));
|
||||||
char *ep;
|
char *ep;
|
||||||
|
unsigned int neg = 0;
|
||||||
unsigned long ul;
|
unsigned long ul;
|
||||||
if (base < 2 || base > 36)
|
if (base < 2 || base > 36)
|
||||||
lj_err_arg(L, 2, LJ_ERR_BASERNG);
|
lj_err_arg(L, 2, LJ_ERR_BASERNG);
|
||||||
ul = strtoul(p, &ep, base);
|
while (lj_char_isspace((unsigned char)(*p))) p++;
|
||||||
if (p != ep) {
|
if (*p == '-') { p++; neg = 1; } else if (*p == '+') { p++; }
|
||||||
while (lj_char_isspace((unsigned char)(*ep))) ep++;
|
if (lj_char_isalnum((unsigned char)(*p))) {
|
||||||
if (*ep == '\0') {
|
ul = strtoul(p, &ep, base);
|
||||||
if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u))
|
if (p != ep) {
|
||||||
setintV(L->base-1-LJ_FR2, (int32_t)ul);
|
while (lj_char_isspace((unsigned char)(*ep))) ep++;
|
||||||
else
|
if (*ep == '\0') {
|
||||||
setnumV(L->base-1-LJ_FR2, (lua_Number)ul);
|
if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u+neg)) {
|
||||||
return FFH_RES(1);
|
if (neg) ul = -ul;
|
||||||
|
setintV(L->base-1-LJ_FR2, (int32_t)ul);
|
||||||
|
} else {
|
||||||
|
lua_Number n = (lua_Number)ul;
|
||||||
|
if (neg) n = -n;
|
||||||
|
setnumV(L->base-1-LJ_FR2, n);
|
||||||
|
}
|
||||||
|
return FFH_RES(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user