diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-12-19 09:11:55 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-12-19 09:11:55 -0200 |
| commit | dd8edecae19251a3b89502416a2e7f8e75b2bd20 (patch) | |
| tree | 10c94c2a2f89f534ca9a5b9b2591008bda8eae17 /lapi.c | |
| parent | 28021c5c663702a1b1890f0e5d280c36be8a6c16 (diff) | |
| download | lua-dd8edecae19251a3b89502416a2e7f8e75b2bd20.tar.gz lua-dd8edecae19251a3b89502416a2e7f8e75b2bd20.tar.bz2 lua-dd8edecae19251a3b89502416a2e7f8e75b2bd20.zip | |
new functions to manipulate upvales (get/setupvalue)
Diffstat (limited to 'lapi.c')
| -rw-r--r-- | lapi.c | 48 |
1 files changed, 47 insertions, 1 deletions
| @@ -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 | ||
| 869 | static 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 | |||
| 887 | LUA_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 | |||
| 901 | LUA_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 | |||
