aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/lapi.c b/lapi.c
index 0dd005c0..998bac0a 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.225 2002/12/04 17:28:27 roberto Exp roberto $ 2** $Id: lapi.c,v 1.226 2002/12/04 17:38:31 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -866,3 +866,49 @@ LUA_API int lua_pushupvalues (lua_State *L) {
866} 866}
867 867
868 868
869static const char *aux_upvalue (lua_State *L, int funcindex, int n,
870 TObject **val) {
871 Closure *f;
872 StkId fi = luaA_index(L, funcindex);
873 if (!ttisfunction(fi)) return NULL;
874 f = clvalue(fi);
875 if (n > f->l.nupvalues) return NULL;
876 if (f->c.isC) {
877 *val = &f->c.upvalue[n-1];
878 return "";
879 }
880 else {
881 *val = f->l.upvals[n-1]->v;
882 return getstr(f->l.p->upvalues[n-1]);
883 }
884}
885
886
887LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) {
888 const char *name;
889 TObject *val;
890 lua_lock(L);
891 name = aux_upvalue(L, funcindex, n, &val);
892 if (name) {
893 setobj2s(L->top, val);
894 api_incr_top(L);
895 }
896 lua_unlock(L);
897 return name;
898}
899
900
901LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
902 const char *name;
903 TObject *val;
904 lua_lock(L);
905 api_checknelems(L, 1);
906 name = aux_upvalue(L, funcindex, n, &val);
907 if (name) {
908 L->top--;
909 setobj(val, L->top); /* write barrier */
910 }
911 lua_unlock(L);
912 return name;
913}
914