mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-02-12 17:24:09 +00:00
FFI: Change tonumber(cdata) semantics. Return nil for non-numbers.
This commit is contained in:
parent
a48058a791
commit
22946f268b
@ -197,22 +197,21 @@ LJLIB_ASM(tonumber) LJLIB_REC(.)
|
||||
#if LJ_HASFFI
|
||||
if (tviscdata(o)) {
|
||||
CTState *cts = ctype_cts(L);
|
||||
if (LJ_DUALNUM) {
|
||||
CType *ct = ctype_raw(cts, cdataV(o)->typeid);
|
||||
if (ctype_isinteger_or_bool(ct->info)) {
|
||||
int64_t i;
|
||||
lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT64), (uint8_t *)&i, o, 0);
|
||||
if ((ct->size == 8 && (ct->info & CTF_UNSIGNED)) ?
|
||||
(uint64_t)i <= 0x7fffffffu : checki32(i)) {
|
||||
setintV(L->base-1, (int32_t)i);
|
||||
CType *ct = lj_ctype_rawref(cts, cdataV(o)->typeid);
|
||||
if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
|
||||
if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
|
||||
if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) &&
|
||||
ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) {
|
||||
int32_t i;
|
||||
lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0);
|
||||
setintV(L->base-1, i);
|
||||
return FFH_RES(1);
|
||||
} /* else: retry and convert to double. */
|
||||
}
|
||||
}
|
||||
lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE),
|
||||
(uint8_t *)&(L->base-1)->n, o, 0);
|
||||
return FFH_RES(1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
const char *p = strdata(lj_lib_checkstr(L, 1));
|
||||
|
@ -1138,10 +1138,18 @@ void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd)
|
||||
void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd)
|
||||
{
|
||||
CTState *cts = ctype_ctsG(J2G(J));
|
||||
IRType st = crec_ct2irt(ctype_raw(cts, cdataV(&rd->argv[0])->typeid));
|
||||
CTypeID did = (st >= IRT_I8 && st <= IRT_INT) ? CTID_INT32 : CTID_DOUBLE;
|
||||
CType *d = ctype_get(cts, did);
|
||||
CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->typeid);
|
||||
if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
|
||||
if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
|
||||
if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 &&
|
||||
!(ct->size == 4 && (ct->info & CTF_UNSIGNED)))
|
||||
d = ctype_get(cts, CTID_INT32);
|
||||
else
|
||||
d = ctype_get(cts, CTID_DOUBLE);
|
||||
J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]);
|
||||
} else {
|
||||
J->base[0] = TREF_NIL;
|
||||
}
|
||||
}
|
||||
|
||||
#undef IR
|
||||
|
Loading…
Reference in New Issue
Block a user