diff --git a/src/blua/lapi.c b/src/blua/lapi.c index 990e17b6e..420e4e20b 100644 --- a/src/blua/lapi.c +++ b/src/blua/lapi.c @@ -641,6 +641,25 @@ LUA_API void lua_getfenv (lua_State *L, int idx) { */ +static void auxsetstr (lua_State *L, TValue *t, const char *k) { + TValue key; + TValue *aux; + TString *str = luaS_new(L, k); + api_checknelems(L, 1); + api_checkvalidindex(L, t); + if (luaV_fastset(L, t, str, aux, luaH_setstr, L->top)) { + setobj2t(L, cast(TValue *, aux), L->top - 1); + L->top--; /* pop value */ + } + else { + setsvalue2s(L, &key, str); + luaV_finishset(L, t, &key, L->top - 1, aux); + L->top--; /* pop value */ + } + lua_unlock(L); +} + + LUA_API void lua_settable (lua_State *L, int idx) { StkId t; lua_lock(L); @@ -654,16 +673,8 @@ LUA_API void lua_settable (lua_State *L, int idx) { LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { - StkId t; - TValue key; lua_lock(L); - api_checknelems(L, 1); - t = index2adr(L, idx); - api_checkvalidindex(L, t); - setsvalue(L, &key, luaS_new(L, k)); - luaV_settable(L, t, &key, L->top - 1); - L->top--; /* pop value */ - lua_unlock(L); + auxsetstr(L, index2adr(L, idx), k); } diff --git a/src/blua/lvm.c b/src/blua/lvm.c index 64e670d2f..484329825 100644 --- a/src/blua/lvm.c +++ b/src/blua/lvm.c @@ -132,31 +132,45 @@ void luaV_finishget (lua_State *L, TValue *t, TValue *key, StkId val, luaG_runerror(L, "gettable chain too long; possible loop"); } -void luaV_settable (lua_State *L, TValue *t, TValue *key, StkId val) { - int loop; + +/* +** Main function for table assignment (invoking metamethods if needed). +** Compute 't[key] = val' +*/ +void luaV_finishset (lua_State *L, TValue *t, TValue *key, + StkId val, TValue *oldval) { + int loop; /* counter to avoid infinite loops */ for (loop = 0; loop < MAXTAGLOOP; loop++) { - TValue *tm = NULL; - if (ttistable(t)) { /* `t' is a table? */ + TValue *tm; + if (oldval != NULL) { + lua_assert(ttistable(t) && ttisnil(oldval)); Table *h = hvalue(t); - TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ + /* must check the metamethod */ /* oldval is nil=> look for newindex, oldval is not nil => look for usedindex */ if (!((ttisnil(oldval) && ((tm = fasttm(L, h->metatable, TM_NEWINDEX)) != NULL)) || ((!ttisnil(oldval)) && ((tm = fasttm(L, h->metatable, TM_USEDINDEX)) != NULL)))) { - setobj2t(L, oldval, val); + /* no metamethod and (now) there is an entry with given key */ + setobj2t(L, cast(TValue *, oldval), val); luaC_barriert(L, h, val); return; } - /* else will try the tag method */ + /* else will try the metamethod */ } - else if (l_unlikely(ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))) - luaG_typeerror(L, t, "index"); + else { /* not a table; check metamethod */ + if (l_unlikely(ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX)))) + luaG_typeerror(L, t, "index"); + } + /* try the metamethod */ if (ttisfunction(tm)) { callTM(L, tm, t, key, val); return; } - t = tm; /* else repeat with `tm' */ + t = tm; /* else repeat assignment over 'tm' */ + if (luaV_fastset(L, t, key, oldval, luaH_set, val)) + return; /* done */ + /* else loop */ } - luaG_runerror(L, "loop in settable"); + luaG_runerror(L, "settable chain too long; possible loop"); } diff --git a/src/blua/lvm.h b/src/blua/lvm.h index 2ffc80ace..2f65098f0 100644 --- a/src/blua/lvm.h +++ b/src/blua/lvm.h @@ -44,6 +44,32 @@ else luaV_finishget(L,t,k,v,aux); } +/* +** Fast track for set table. If 't' is a table and 't[k]' is not nil, +** call GC barrier, do a raw 't[k]=v', and return true; otherwise, +** return false with 'slot' equal to NULL (if 't' is not a table) or +** 'nil'. (This is needed by 'luaV_finishget'.) Note that, if the macro +** returns true, there is no need to 'invalidateTMcache', because the +** call is not creating a new entry. +*/ +#define luaV_fastset(L,t,k,slot,f,v) \ + (!ttistable(t) \ + ? (slot = NULL, 0) \ + : (slot = f(L, hvalue(t), k), \ + ttisnil(slot) ? 0 \ + : (__extension__ ({ \ + if (valiswhite(v) && isblack(obj2gco(hvalue(t)))) \ + luaC_barrierback(L, hvalue(t)); \ + setobj2t(L, cast(TValue *,slot), v); \ + 1; \ + })))) + + +#define luaV_settable(L,t,k,v) { TValue *slot; \ + if (!luaV_fastset(L,t,k,slot,luaH_set,v)) \ + luaV_finishset(L,t,k,v,slot); } + + 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); @@ -51,8 +77,8 @@ 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_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_finishset (lua_State *L, TValue *t, TValue *key, + StkId val, TValue *oldval); LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);