6

I have been searching for quite a while now and I haven't found a way to fetch all the global variables from C++. Consider this small Lua test script.

myGlobal1 = "Global 1"
myGlobal2 = 2

function test()
  local l1=0
  print (myGlobal1,myGlobal2,l1)
end

test()

Assume you pause the execution at print (myGlobal1,myGlobal2,l1) and from C++ get all the global variables (myGlobal1 and myGlobal2). These examples are arbitrary, the global variables, from a C++ point of view, are unknown.

I have been looking at lua_getglobal() but then I need to know the name of the variable first. I looked at lua_getupvalue() but only got "_ENV" as result.

I guess I can use lua_getglobal() as soon I know the name of them, but how do I get the list of global variables (from C++)? I do have the lua_Debug structure at this point (if it is to any help)

EDIT This post wasn't originally about iterating through a table, it was about how to find the user's own globals.

However, I posted a solution to how this can be done here.

Community
  • 1
  • 1
Max Kielland
  • 5,627
  • 9
  • 60
  • 95

1 Answers1

10

Okay I solved it.

lua_pushglobaltable(L);       // Get global table
lua_pushnil(L);               // put a nil key on stack
while (lua_next(L,-2) != 0) { // key(-1) is replaced by the next key(-1) in table(-2)
  name = lua_tostring(L,-2);  // Get key(-2) name
  lua_pop(L,1);               // remove value(-1), now key on top at(-1)
}
lua_pop(L,1);                 // remove global table(-1)

When lua_next() can't find more entries the key name is popped leaving the table on top(-1).

Next problem would be to distinguish my own globals from the rest of the table entries...

Max Kielland
  • 5,627
  • 9
  • 60
  • 95
  • 1
    The [manual](http://www.lua.org/manual/5.2/manual.html#lua_next) says: "While traversing a table, do not call `lua_tolstring` directly on a key, unless you know that the key is actually a string. Recall that `lua_tolstring` may change the value at the given index; this confuses the next call to `lua_next`.". It is unlikely that you'd have a global variable whose name is not a string but it may happen. – lhf Dec 11 '13 at 17:52
  • Yes, I figured that out. I forgot to edit my post about it. I'm trying to find a better way to filter out my globals. – Max Kielland Dec 11 '13 at 18:12
  • Ask a separate question about filtering out your globals. – lhf Dec 11 '13 at 18:16
  • @lhf Yes I posted a new question ;) http://stackoverflow.com/questions/20527659/how-to-filter-out-user-defined-globals-in-lua-from-c – Max Kielland Dec 11 '13 at 18:58
  • 1
    @MaxKielland: thanks for posting this. I was experimenting with an answer and got crashes because I was passing `-1` as the index of the table to `lua_next()`. The doc for `lua_next()` (http://www.lua.org/manual/5.2/manual.html#lua_next) has an example of walking a table, but only says that "table is in the stack at index 't'". At the point of the comment the table's stack index is `-1` - so that's what I tried. For some reason it never clicked for me that I should try `-2`, which in retrospect seems like should have been an obvious thing to try. – Michael Burr Dec 11 '13 at 22:33
  • @MichaelBurr a week ago I didn't even know that Lua existed :) Yes, I have also found some documentation a bit sparse and I have done quite some trial and error cases, but I start to get some understanding of how it works now... :) I have to send many thanks to lhf for his help and patience... – Max Kielland Dec 12 '13 at 01:55