FFI: Allow cdata indexing with integer cdata.

This commit is contained in:
Mike Pall 2011-01-26 21:12:54 +01:00
parent 0622ad9adc
commit de0781d610

View File

@ -71,6 +71,7 @@ CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,
{ {
uint8_t *p = (uint8_t *)cdataptr(cd); uint8_t *p = (uint8_t *)cdataptr(cd);
CType *ct = ctype_get(cts, cd->typeid); CType *ct = ctype_get(cts, cd->typeid);
ptrdiff_t idx;
/* Resolve reference for cdata object. */ /* Resolve reference for cdata object. */
if (ctype_isref(ct->info)) { if (ctype_isref(ct->info)) {
@ -88,8 +89,8 @@ collect_attrib:
lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */ lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */
if (tvisnum(key)) { /* Numeric key. */ if (tvisnum(key)) { /* Numeric key. */
ptrdiff_t idx = LJ_64 ? (ptrdiff_t)numV(key) : idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key));
(ptrdiff_t)lj_num2int(numV(key)); integer_key:
if (ctype_ispointer(ct->info)) { if (ctype_ispointer(ct->info)) {
CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */ CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */
if (sz != CTSIZE_INVALID) { if (sz != CTSIZE_INVALID) {
@ -101,6 +102,15 @@ collect_attrib:
return ct; return ct;
} }
} }
} else if (tviscdata(key)) { /* Integer cdata key. */
GCcdata *cdk = cdataV(key);
CType *ctk = ctype_raw(cts, cdk->typeid);
if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk);
if (ctype_isinteger(ctk->info)) {
lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk,
(uint8_t *)&idx, cdataptr(cdk), 0);
goto integer_key;
}
} else if (tvisstr(key)) { /* String key. */ } else if (tvisstr(key)) { /* String key. */
GCstr *name = strV(key); GCstr *name = strV(key);
if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */