LUA_API int lua_getn (lua_State *L, int index) { StkId t; const TValue *value; int n; lua_lock(L); t = index2adr(L, index); api_check(L, ttype(t) == LUA_TTABLE); value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n")); /* = t.n */ if (ttype(value) == LUA_TNUMBER) { n = (int)value; } else { Node *nd; Table *a = hvalue(t); lua_Number max = 0; int i; i = a->sizearray; while (i--) { if (ttype(&a->array[i]) != LUA_TNIL) break; } max = i+1; i = sizenode(a); nd = a->node; while (i--) { if (ttype(gkey(nd)) == LUA_TNUMBER && ttype(gval(nd)) != LUA_TNIL && nvalue(gkey(nd)) > max) max = nvalue(gkey(nd)); nd++; } lua_number2int(n, max); } lua_unlock(L); return n; }
LUA_API const lua_WChar *lua_tolwstring (lua_State *L, int idx, size_t *len) { StkId o = index2adr(L, idx); if (!ttiswstring(o)) { lua_lock(L); /* `luaV_tostring' may create a new string */ if (!luaV_towstring(L, o)) { /* conversion failed? */ if (len != NULL) *len = 0; lua_unlock(L); return NULL; } luaC_checkGC(L); o = index2adr(L, idx); /* previous call may reallocate the stack */ lua_unlock(L); } if (len != NULL) *len = twsvalue(o)->len; return wsvalue(o); }
static Fdelete (lua_State *L) { filedata *f = (filedata *)luaL_checkuserdata(L, 1); if(f->close) luaL_error(L,"attempt to use a deleted file"); if (create_mem_zone(1,f->bf.name,f->taillezone + 24,DELETE_ZONE)<0 ) luaL_error(L,"not enough memory for file"); globalrefresh++; f->close =1; StkId o = index2adr(L,1); setnilvalue(o); return 0; }
LUA_API void lua_replace (lua_State *L, int idx) { lua_lock(L); /* explicit test for incompatible code */ if ( idx == LUA_ENVIRONINDEX && !hascallingenv( L ) ) { luaG_runerror(L, "no calling environment"); } api_checknelems(L, 1); { rtStack_t& rtStack = L->rtStack; rtStack.Lock( L ); StkId stackTop = L->GetCurrentStackFrame().TopMutable( L, L->rtStack ); if ( idx == LUA_ENVIRONINDEX ) { Closure *func = curr_func(L); const TValue *val = stackTop; func->env = gcvalue(val); luaC_barrier(L, func, val); } else if ( idx == LUA_STORAGEINDEX ) { const TValue *val = stackTop; setgcvalue( L, &L->storage, gcvalue( val ) ); luaC_barrier( L, L, val ); } else { ValueAddress o = index2adr(L, idx); api_checkvalidindex(L, o); setobj( L, o, stackTop ); if ( idx < LUA_GLOBALSINDEX ) /* function upvalue? */ { luaC_barrier(L, curr_func(L), stackTop); } } popstack( L, 1 ); rtStack.Unlock( L ); } lua_unlock(L); }