GCtab *lj_lib_checktab(lua_State *L, int narg) { TValue *o = L->base + narg-1; if (!(o < L->top && tvistab(o))) lj_err_argt(L, narg, LUA_TTABLE); return tabV(o); }
GCtab *lj_lib_checktabornil(lua_State *L, int narg) { TValue *o = L->base + narg-1; if (o < L->top) { if (tvistab(o)) return tabV(o); else if (tvisnil(o)) return NULL; } lj_err_arg(L, narg, LJ_ERR_NOTABN); return NULL; /* unreachable */ }
/* Helper for LEN. __len metamethod. */ TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o) { cTValue *mo = lj_meta_lookup(L, o, MM_len); if (tvisnil(mo)) { if (LJ_52 && tvistab(o)) tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<<MM_len); else lj_err_optype(L, o, LJ_ERR_OPLEN); return NULL; } return mmcall(L, lj_cont_ra, mo, o, LJ_52 ? o : niltv(L)); }
/* -- Luajittex needs this one because it overloads loadfile -- */ LUALIB_API int RESERVED_load_aux_JIT(lua_State *L, int status, int envarg) { if (status == 0) { if (tvistab(L->base+envarg-1)) { GCfunc *fn = funcV(L->top-1); GCtab *t = tabV(L->base+envarg-1); setgcref(fn->c.env, obj2gco(t)); lj_gc_objbarrier(L, fn, t); } return 1; } else { setnilV(L->top-2); return 2; } }
/* Lookup metamethod for object. */ cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm) { GCtab *mt; if (tvistab(o)) mt = tabref(tabV(o)->metatable); else if (tvisudata(o)) mt = tabref(udataV(o)->metatable); else mt = tabref(basemt_obj(G(L), o)); if (mt) { cTValue *mo = lj_tab_getstr(mt, mmname_str(G(L), mm)); if (mo) return mo; } return niltv(L); }
ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev) { global_State *g = G(L); GCstr *s = lj_str_newlit(L, LJ_VMEVENTS_REGKEY); cTValue *tv = lj_tab_getstr(tabV(registry(L)), s); if (tvistab(tv)) { int hash = VMEVENT_HASH(ev); tv = lj_tab_getint(tabV(tv), hash); if (tv && tvisfunc(tv)) { lj_state_checkstack(L, LUA_MINSTACK); setfuncV(L, L->top++, funcV(tv)); return savestack(L, L->top); } } g->vmevmask &= ~VMEVENT_MASK(ev); /* No handler: cache this fact. */ return 0; }
/* Helper for LEN. __len metamethod. */ TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o) { cTValue *mo = lj_meta_lookup(L, o, MM_len); if (tvisnil(mo)) { #ifdef LUAJIT_ENABLE_LUA52COMPAT if (tvistab(o)) tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<<MM_len); else #endif lj_err_optype(L, o, LJ_ERR_OPLEN); return NULL; } #ifdef LUAJIT_ENABLE_LUA52COMPAT return mmcall(L, lj_cont_ra, mo, o, o); #else return mmcall(L, lj_cont_ra, mo, o, niltv(L)); #endif }
static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize) { if (libname) { luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); lua_getfield(L, -1, libname); if (!tvistab(L->top-1)) { L->top--; if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL) lj_err_callerv(L, LJ_ERR_BADMODN, libname); settabV(L, L->top, tabV(L->top-1)); L->top++; lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ } L->top--; settabV(L, L->top-1, tabV(L->top)); } else { lua_createtable(L, 0, hsize); } return tabV(L->top-1); }
/* Helper for TSET*. __newindex chain and metamethod. */ TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k) { TValue tmp; int loop; for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { cTValue *mo; if (LJ_LIKELY(tvistab(o))) { GCtab *t = tabV(o); cTValue *tv = lj_tab_get(L, t, k); if (LJ_LIKELY(!tvisnil(tv))) { t->nomm = 0; /* Invalidate negative metamethod cache. */ lj_gc_anybarriert(L, t); return (TValue *)tv; } else if (!(mo = lj_meta_fast(L, tabref(t->metatable), MM_newindex))) { t->nomm = 0; /* Invalidate negative metamethod cache. */ lj_gc_anybarriert(L, t); if (tv != niltv(L)) return (TValue *)tv; if (tvisnil(k)) lj_err_msg(L, LJ_ERR_NILIDX); else if (tvisint(k)) { setnumV(&tmp, (lua_Number)intV(k)); k = &tmp; } else if (tvisnum(k) && tvisnan(k)) lj_err_msg(L, LJ_ERR_NANIDX); return lj_tab_newkey(L, t, k); } } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_newindex))) { lj_err_optype(L, o, LJ_ERR_OPINDEX); return NULL; /* unreachable */ } if (tvisfunc(mo)) { L->top = mmcall(L, lj_cont_nop, mo, o, k); /* L->top+2 = v filled in by caller. */ return NULL; /* Trigger metamethod call. */ } copyTV(L, &tmp, mo); o = &tmp; } lj_err_msg(L, LJ_ERR_SETLOOP); return NULL; /* unreachable */ }
/* Helper for TGET*. __index chain and metamethod. */ cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k) { int loop; for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { cTValue *mo; if (LJ_LIKELY(tvistab(o))) { GCtab *t = tabV(o); cTValue *tv = lj_tab_get(L, t, k); if (!tvisnil(tv) || !(mo = lj_meta_fast(L, tabref(t->metatable), MM_index))) return tv; } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_index))) { lj_err_optype(L, o, LJ_ERR_OPINDEX); return NULL; /* unreachable */ } if (tvisfunc(mo)) { L->top = mmcall(L, lj_cont_ra, mo, o, k); return NULL; /* Trigger metamethod call. */ } o = mo; } lj_err_msg(L, LJ_ERR_GETLOOP); return NULL; /* unreachable */ }