From dd8edecae19251a3b89502416a2e7f8e75b2bd20 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 19 Dec 2002 09:11:55 -0200 Subject: new functions to manipulate upvales (get/setupvalue) --- lapi.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'lapi.c') diff --git a/lapi.c b/lapi.c index 0dd005c0..998bac0a 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.225 2002/12/04 17:28:27 roberto Exp roberto $ +** $Id: lapi.c,v 1.226 2002/12/04 17:38:31 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -866,3 +866,49 @@ LUA_API int lua_pushupvalues (lua_State *L) { } +static const char *aux_upvalue (lua_State *L, int funcindex, int n, + TObject **val) { + Closure *f; + StkId fi = luaA_index(L, funcindex); + if (!ttisfunction(fi)) return NULL; + f = clvalue(fi); + if (n > f->l.nupvalues) return NULL; + if (f->c.isC) { + *val = &f->c.upvalue[n-1]; + return ""; + } + else { + *val = f->l.upvals[n-1]->v; + return getstr(f->l.p->upvalues[n-1]); + } +} + + +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { + const char *name; + TObject *val; + lua_lock(L); + name = aux_upvalue(L, funcindex, n, &val); + if (name) { + setobj2s(L->top, val); + api_incr_top(L); + } + lua_unlock(L); + return name; +} + + +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { + const char *name; + TObject *val; + lua_lock(L); + api_checknelems(L, 1); + name = aux_upvalue(L, funcindex, n, &val); + if (name) { + L->top--; + setobj(val, L->top); /* write barrier */ + } + lua_unlock(L); + return name; +} + -- cgit v1.2.3-55-g6feb