📄 lapi.c
字号:
TValue key;
pve_lock(L);
t = index2adr(L, idx);
api_checkvalidindex(L, t);
setsvalue(L, &key, pveS_new(L, k));
pveV_gettable(L, t, &key, L->top);
api_incr_top(L);
pve_unlock(L);
}
pve_API void pve_rawget (pve_State *L, int idx) {
StkId t;
pve_lock(L);
t = index2adr(L, idx);
api_check(L, ttistable(t));
setobj2s(L, L->top - 1, pveH_get(hvalue(t), L->top - 1));
pve_unlock(L);
}
pve_API void pve_rawgeti (pve_State *L, int idx, int n) {
StkId o;
pve_lock(L);
o = index2adr(L, idx);
api_check(L, ttistable(o));
setobj2s(L, L->top, pveH_getnum(hvalue(o), n));
api_incr_top(L);
pve_unlock(L);
}
pve_API void pve_createtable (pve_State *L, int narray, int nrec) {
pve_lock(L);
pveC_checkGC(L);
sethvalue(L, L->top, pveH_new(L, narray, nrec));
api_incr_top(L);
pve_unlock(L);
}
pve_API int pve_getmetatable (pve_State *L, int objindex) {
const TValue *obj;
Table *mt = NULL;
int res;
pve_lock(L);
obj = index2adr(L, objindex);
switch (ttype(obj)) {
case pve_TTABLE:
mt = hvalue(obj)->metatable;
break;
case pve_TUSERDATA:
mt = uvalue(obj)->metatable;
break;
default:
mt = G(L)->mt[ttype(obj)];
break;
}
if (mt == NULL)
res = 0;
else {
sethvalue(L, L->top, mt);
api_incr_top(L);
res = 1;
}
pve_unlock(L);
return res;
}
pve_API void pve_getfenv (pve_State *L, int idx) {
StkId o;
pve_lock(L);
o = index2adr(L, idx);
api_checkvalidindex(L, o);
switch (ttype(o)) {
case pve_TFUNCTION:
sethvalue(L, L->top, clvalue(o)->c.env);
break;
case pve_TUSERDATA:
sethvalue(L, L->top, uvalue(o)->env);
break;
case pve_TTHREAD:
setobj2s(L, L->top, gt(thvalue(o)));
break;
default:
setnilvalue(L->top);
break;
}
api_incr_top(L);
pve_unlock(L);
}
/*
** set functions (stack -> pve)
*/
pve_API void pve_settable (pve_State *L, int idx) {
StkId t;
pve_lock(L);
api_checknelems(L, 2);
t = index2adr(L, idx);
api_checkvalidindex(L, t);
pveV_settable(L, t, L->top - 2, L->top - 1);
L->top -= 2; /* pop index and value */
pve_unlock(L);
}
pve_API void pve_setfield (pve_State *L, int idx, const char *k) {
StkId t;
TValue key;
pve_lock(L);
api_checknelems(L, 1);
t = index2adr(L, idx);
api_checkvalidindex(L, t);
setsvalue(L, &key, pveS_new(L, k));
pveV_settable(L, t, &key, L->top - 1);
L->top--; /* pop value */
pve_unlock(L);
}
pve_API void pve_rawset (pve_State *L, int idx) {
StkId t;
pve_lock(L);
api_checknelems(L, 2);
t = index2adr(L, idx);
api_check(L, ttistable(t));
setobj2t(L, pveH_set(L, hvalue(t), L->top-2), L->top-1);
pveC_barriert(L, hvalue(t), L->top-1);
L->top -= 2;
pve_unlock(L);
}
pve_API void pve_rawseti (pve_State *L, int idx, int n) {
StkId o;
pve_lock(L);
api_checknelems(L, 1);
o = index2adr(L, idx);
api_check(L, ttistable(o));
setobj2t(L, pveH_setnum(L, hvalue(o), n), L->top-1);
pveC_barriert(L, hvalue(o), L->top-1);
L->top--;
pve_unlock(L);
}
pve_API int pve_setmetatable (pve_State *L, int objindex) {
TValue *obj;
Table *mt;
pve_lock(L);
api_checknelems(L, 1);
obj = index2adr(L, objindex);
api_checkvalidindex(L, obj);
if (ttisnil(L->top - 1))
mt = NULL;
else {
api_check(L, ttistable(L->top - 1));
mt = hvalue(L->top - 1);
}
switch (ttype(obj)) {
case pve_TTABLE: {
hvalue(obj)->metatable = mt;
if (mt)
pveC_objbarriert(L, hvalue(obj), mt);
break;
}
case pve_TUSERDATA: {
uvalue(obj)->metatable = mt;
if (mt)
pveC_objbarrier(L, rawuvalue(obj), mt);
break;
}
default: {
G(L)->mt[ttype(obj)] = mt;
break;
}
}
L->top--;
pve_unlock(L);
return 1;
}
pve_API int pve_setfenv (pve_State *L, int idx) {
StkId o;
int res = 1;
pve_lock(L);
api_checknelems(L, 1);
o = index2adr(L, idx);
api_checkvalidindex(L, o);
api_check(L, ttistable(L->top - 1));
switch (ttype(o)) {
case pve_TFUNCTION:
clvalue(o)->c.env = hvalue(L->top - 1);
break;
case pve_TUSERDATA:
uvalue(o)->env = hvalue(L->top - 1);
break;
case pve_TTHREAD:
sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1));
break;
default:
res = 0;
break;
}
pveC_objbarrier(L, gcvalue(o), hvalue(L->top - 1));
L->top--;
pve_unlock(L);
return res;
}
/*
** `load' and `call' functions (run pve code)
*/
#define adjustresults(L,nres) \
{ if (nres == pve_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; }
#define checkresults(L,na,nr) \
api_check(L, (nr) == pve_MULTRET || (L->ci->top - L->top >= (nr) - (na)))
pve_API void pve_call (pve_State *L, int nargs, int nresults) {
StkId func;
pve_lock(L);
api_checknelems(L, nargs+1);
checkresults(L, nargs, nresults);
func = L->top - (nargs+1);
pveD_call(L, func, nresults);
adjustresults(L, nresults);
pve_unlock(L);
}
/*
** Execute a protected call.
*/
struct CallS { /* data to `f_call' */
StkId func;
int nresults;
};
static void f_call (pve_State *L, void *ud) {
struct CallS *c = cast(struct CallS *, ud);
pveD_call(L, c->func, c->nresults);
}
pve_API int pve_pcall (pve_State *L, int nargs, int nresults, int errfunc) {
struct CallS c;
int status;
ptrdiff_t func;
pve_lock(L);
api_checknelems(L, nargs+1);
checkresults(L, nargs, nresults);
if (errfunc == 0)
func = 0;
else {
StkId o = index2adr(L, errfunc);
api_checkvalidindex(L, o);
func = savestack(L, o);
}
c.func = L->top - (nargs+1); /* function to be called */
c.nresults = nresults;
status = pveD_pcall(L, f_call, &c, savestack(L, c.func), func);
adjustresults(L, nresults);
pve_unlock(L);
return status;
}
/*
** Execute a protected C call.
*/
struct CCallS { /* data to `f_Ccall' */
pve_CFunction func;
void *ud;
};
static void f_Ccall (pve_State *L, void *ud) {
struct CCallS *c = cast(struct CCallS *, ud);
Closure *cl;
cl = pveF_newCclosure(L, 0, getcurrenv(L));
cl->c.f = c->func;
setclvalue(L, L->top, cl); /* push function */
api_incr_top(L);
setpvalue(L->top, c->ud); /* push only argument */
api_incr_top(L);
pveD_call(L, L->top - 2, 0);
}
pve_API int pve_cpcall (pve_State *L, pve_CFunction func, void *ud) {
struct CCallS c;
int status;
pve_lock(L);
c.func = func;
c.ud = ud;
status = pveD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0);
pve_unlock(L);
return status;
}
pve_API int pve_load (pve_State *L, pve_Reader reader, void *data,
const char *chunkname) {
ZIO z;
int status;
pve_lock(L);
if (!chunkname) chunkname = "?";
pveZ_init(L, &z, reader, data);
status = pveD_protectedparser(L, &z, chunkname);
pve_unlock(L);
return status;
}
pve_API int pve_dump (pve_State *L, pve_Writer writer, void *data) {
int status;
TValue *o;
pve_lock(L);
api_checknelems(L, 1);
o = L->top - 1;
if (isLfunction(o))
status = pveU_dump(L, clvalue(o)->l.p, writer, data, 0);
else
status = 1;
pve_unlock(L);
return status;
}
pve_API int pve_status (pve_State *L) {
return L->status;
}
/*
** Garbage-collection function
*/
pve_API int pve_gc (pve_State *L, int what, int data) {
int res = 0;
global_State *g;
pve_lock(L);
g = G(L);
switch (what) {
case pve_GCSTOP: {
g->GCthreshold = MAX_LUMEM;
break;
}
case pve_GCRESTART: {
g->GCthreshold = g->totalbytes;
break;
}
case pve_GCCOLLECT: {
pveC_fullgc(L);
break;
}
case pve_GCCOUNT: {
/* GC values are expressed in Kbytes: #bytes/2^10 */
res = cast_int(g->totalbytes >> 10);
break;
}
case pve_GCCOUNTB: {
res = cast_int(g->totalbytes & 0x3ff);
break;
}
case pve_GCSTEP: {
lu_mem a = (cast(lu_mem, data) << 10);
if (a <= g->totalbytes)
g->GCthreshold = g->totalbytes - a;
else
g->GCthreshold = 0;
while (g->GCthreshold <= g->totalbytes)
pveC_step(L);
if (g->gcstate == GCSpause) /* end of cycle? */
res = 1; /* signal it */
break;
}
case pve_GCSETPAUSE: {
res = g->gcpause;
g->gcpause = data;
break;
}
case pve_GCSETSTEPMUL: {
res = g->gcstepmul;
g->gcstepmul = data;
break;
}
default: res = -1; /* invalid option */
}
pve_unlock(L);
return res;
}
/*
** miscellaneous functions
*/
pve_API int pve_error (pve_State *L) {
pve_lock(L);
api_checknelems(L, 1);
pveG_errormsg(L);
pve_unlock(L);
return 0; /* to avoid warnings */
}
pve_API int pve_next (pve_State *L, int idx) {
StkId t;
int more;
pve_lock(L);
t = index2adr(L, idx);
api_check(L, ttistable(t));
more = pveH_next(L, hvalue(t), L->top - 1);
if (more) {
api_incr_top(L);
}
else /* no more elements */
L->top -= 1; /* remove key */
pve_unlock(L);
return more;
}
pve_API void pve_concat (pve_State *L, int n) {
pve_lock(L);
api_checknelems(L, n);
if (n >= 2) {
pveC_checkGC(L);
pveV_concat(L, n, cast_int(L->top - L->base) - 1);
L->top -= (n-1);
}
else if (n == 0) { /* push empty string */
setsvalue2s(L, L->top, pveS_newlstr(L, "", 0));
api_incr_top(L);
}
/* else n == 1; nothing to do */
pve_unlock(L);
}
pve_API pve_Alloc pve_getallocf (pve_State *L, void **ud) {
pve_Alloc f;
pve_lock(L);
if (ud) *ud = G(L)->ud;
f = G(L)->frealloc;
pve_unlock(L);
return f;
}
pve_API void pve_setallocf (pve_State *L, pve_Alloc f, void *ud) {
pve_lock(L);
G(L)->ud = ud;
G(L)->frealloc = f;
pve_unlock(L);
}
pve_API void *pve_newuserdata (pve_State *L, size_t size) {
Udata *u;
pve_lock(L);
pveC_checkGC(L);
u = pveS_newudata(L, size, getcurrenv(L));
setuvalue(L, L->top, u);
api_incr_top(L);
pve_unlock(L);
return u + 1;
}
static const char *aux_upvalue (StkId fi, int n, TValue **val) {
Closure *f;
if (!ttisfunction(fi)) return NULL;
f = clvalue(fi);
if (f->c.isC) {
if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
*val = &f->c.upvalue[n-1];
return "";
}
else {
Proto *p = f->l.p;
if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
*val = f->l.upvals[n-1]->v;
return getstr(p->upvalues[n-1]);
}
}
pve_API const char *pve_getupvalue (pve_State *L, int funcindex, int n) {
const char *name;
TValue *val;
pve_lock(L);
name = aux_upvalue(index2adr(L, funcindex), n, &val);
if (name) {
setobj2s(L, L->top, val);
api_incr_top(L);
}
pve_unlock(L);
return name;
}
pve_API const char *pve_setupvalue (pve_State *L, int funcindex, int n) {
const char *name;
TValue *val;
StkId fi;
pve_lock(L);
fi = index2adr(L, funcindex);
api_checknelems(L, 1);
name = aux_upvalue(fi, n, &val);
if (name) {
L->top--;
setobj(L, val, L->top);
pveC_barrier(L, clvalue(fi), L->top);
}
pve_unlock(L);
return name;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -