aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-12-14 16:32:26 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-12-14 16:32:26 -0200
commit151dc5cc5ffd57aa7a9f448f323baa57571f33b2 (patch)
treeb7d22b057ec16d9aa3dd5be6a1ac6362150b4b18 /lauxlib.c
parent0b6cfea005ff719bd7210cd1873e19c7ce4c069b (diff)
downloadlua-151dc5cc5ffd57aa7a9f448f323baa57571f33b2.tar.gz
lua-151dc5cc5ffd57aa7a9f448f323baa57571f33b2.tar.bz2
lua-151dc5cc5ffd57aa7a9f448f323baa57571f33b2.zip
traverse loaded modules (instead of globals) for a name for a function +
removes prefix '_G.' from names (if present)
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/lauxlib.c b/lauxlib.c
index abe57656..4197f6bf 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.277 2014/12/10 11:31:32 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.278 2014/12/13 17:47:58 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -48,7 +48,7 @@ static int findfield (lua_State *L, int objidx, int level) {
48 lua_pushnil(L); /* start 'next' loop */ 48 lua_pushnil(L); /* start 'next' loop */
49 while (lua_next(L, -2)) { /* for each pair in table */ 49 while (lua_next(L, -2)) { /* for each pair in table */
50 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */ 50 if (lua_type(L, -2) == LUA_TSTRING) { /* ignore non-string keys */
51 if (level == 1 && lua_rawequal(L, objidx, -1)) { /* found object? */ 51 if (lua_rawequal(L, objidx, -1)) { /* found object? */
52 lua_pop(L, 1); /* remove value (but keep name) */ 52 lua_pop(L, 1); /* remove value (but keep name) */
53 return 1; 53 return 1;
54 } 54 }
@@ -66,12 +66,20 @@ static int findfield (lua_State *L, int objidx, int level) {
66} 66}
67 67
68 68
69/*
70** Search for a name for a function in all loaded modules
71** (registry._LOADED).
72*/
69static int pushglobalfuncname (lua_State *L, lua_Debug *ar) { 73static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
70 int top = lua_gettop(L); 74 int top = lua_gettop(L);
71 lua_getinfo(L, "f", ar); /* push function */ 75 lua_getinfo(L, "f", ar); /* push function */
72 lua_pushglobaltable(L); 76 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
73 /* try first global names, and then global.name names */ 77 if (findfield(L, top + 1, 2)) {
74 if (findfield(L, top + 1, 1) || findfield(L, top + 1, 2)) { 78 const char *name = lua_tostring(L, -1);
79 if (strncmp(name, "_G.", 3) == 0) { /* name start with '_G.'? */
80 lua_pushstring(L, name + 3); /* push name without prefix */
81 lua_remove(L, -2); /* remove original name */
82 }
75 lua_copy(L, -1, top + 1); /* move name to proper place */ 83 lua_copy(L, -1, top + 1); /* move name to proper place */
76 lua_pop(L, 2); /* remove pushed values */ 84 lua_pop(L, 2); /* remove pushed values */
77 return 1; 85 return 1;