diff --git a/src/blua/lapi.c b/src/blua/lapi.c index 6a33983fe..990e17b6e 100644 --- a/src/blua/lapi.c +++ b/src/blua/lapi.c @@ -519,6 +519,23 @@ LUA_API int lua_pushthread (lua_State *L) { ** get functions (Lua -> stack) */ +static void auxgetstr (lua_State *L, TValue *t, const char *k) { + TValue *aux; + TValue key; + TString *str = luaS_new(L, k); + api_checkvalidindex(L, t); + if (luaV_fastget(L, t, str, aux, luaH_getstr)) { + setobj2s(L, L->top, aux); + api_incr_top(L); + } + else { + setsvalue(L, &key, str); + luaV_finishget(L, t, L->top, L->top, aux); + api_incr_top(L); + } + lua_unlock(L); +} + LUA_API void lua_gettable (lua_State *L, int idx) { StkId t; @@ -531,15 +548,8 @@ LUA_API void lua_gettable (lua_State *L, int idx) { LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { - StkId t; - TValue key; lua_lock(L); - t = index2adr(L, idx); - api_checkvalidindex(L, t); - setsvalue(L, &key, luaS_new(L, k)); - luaV_gettable(L, t, &key, L->top); - api_incr_top(L); - lua_unlock(L); + auxgetstr(L, index2adr(L, idx), k); } diff --git a/src/blua/lvm.c b/src/blua/lvm.c index 7afbbd572..64e670d2f 100644 --- a/src/blua/lvm.c +++ b/src/blua/lvm.c @@ -105,32 +105,33 @@ static void callTM (lua_State *L, const TValue *f, const TValue *p1, } -void luaV_gettable (lua_State *L, TValue *t, TValue *key, StkId val) { - int loop; +/* +** Complete a table access: if 't' is a table, 'tm' has its metamethod; +** otherwise, 'tm' is NULL. +*/ +void luaV_finishget (lua_State *L, TValue *t, TValue *key, StkId val, + TValue *tm) { + int loop; /* counter to avoid infinite loops */ + lua_assert(tm != NULL || !ttistable(t)); for (loop = 0; loop < MAXTAGLOOP; loop++) { - TValue *tm; - if (ttistable(t)) { /* `t' is a table? */ - Table *h = hvalue(t); - const TValue *res = luaH_get(h, key); /* do a primitive get */ - if (!ttisnil(res) || /* result is no nil? */ - (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ - setobj2s(L, val, res); - return; - } - /* else will try the tag method */ + if (tm == NULL) { /* no metamethod (from a table)? */ + if (l_unlikely(ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))) + luaG_typeerror(L, t, "index"); /* no metamethod */ } - else if (l_unlikely(ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX)))) - luaG_typeerror(L, t, "index"); - if (ttisfunction(tm)) { - callTMres(L, val, tm, t, key); + if (ttisfunction(tm)) { /* metamethod is a function */ + callTMres(L, val, tm, t, key); /* call it */ return; } - t = tm; /* else repeat with `tm' */ + t = tm; /* else repeat access over 'tm' */ + if (luaV_fastget(L,t,key,tm,luaH_get)) { /* try fast track */ + setobj2s(L, val, tm); /* done */ + return; + } + /* else repeat */ } - luaG_runerror(L, "loop in gettable"); + luaG_runerror(L, "gettable chain too long; possible loop"); } - void luaV_settable (lua_State *L, TValue *t, TValue *key, StkId val) { int loop; for (loop = 0; loop < MAXTAGLOOP; loop++) { diff --git a/src/blua/lvm.h b/src/blua/lvm.h index f9226ee5d..2ffc80ace 100644 --- a/src/blua/lvm.h +++ b/src/blua/lvm.h @@ -22,12 +22,35 @@ (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) +/* +** fast track for 'gettable': 1 means 'aux' points to resulted value; +** 0 means 'aux' is metamethod (if 't' is a table) or NULL. 'f' is +** the raw get function to use. +*/ +#define luaV_fastget(L,t,k,aux,f) \ + (!ttistable(t) \ + ? (aux = NULL, 0) /* not a table; 'aux' is NULL and result is 0 */ \ + : (aux = f(hvalue(t), k), /* else, do raw access */ \ + !ttisnil(aux) ? 1 /* result not nil? 'aux' has it */ \ + : (aux = fasttm(L, hvalue(t)->metatable, TM_INDEX), /* get metamethod */\ + aux != NULL ? 0 /* has metamethod? must call it */ \ + : (aux = luaO_nilobject, 1)))) /* else, final result is nil */ + +/* +** standard implementation for 'gettable' +*/ +#define luaV_gettable(L,t,k,v) { TValue *aux; \ + if (luaV_fastget(L,t,k,aux,luaH_get)) { setobj2s(L, v, aux); } \ + else luaV_finishget(L,t,k,v,aux); } + + + LUAI_FUNC int luaV_lessthan (lua_State *L, TValue *l, TValue *r); LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); -LUAI_FUNC void luaV_gettable (lua_State *L, TValue *t, TValue *key, - StkId val); +LUAI_FUNC void luaV_finishget (lua_State *L, TValue *t, TValue *key, + StkId val, TValue *tm); LUAI_FUNC void luaV_settable (lua_State *L, TValue *t, TValue *key, StkId val); LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);