Loop through all Lua global variables in C++ [duplicate]
Asked Answered
J

1

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.

Jackelinejackelyn answered 11/12, 2013 at 15:25 Comment(3)
Use this remembering that globals are just indexes in _G.Antabuse
@Bartek So iterating through _G will give me all global variables?Jackelinejackelyn
@Tadeusz It is Lua5.2, as the tag shows ;)Jackelinejackelyn
J
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...

Jackelinejackelyn answered 11/12, 2013 at 17:8 Comment(6)
The manual 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.Sibship
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.Jackelinejackelyn
Ask a separate question about filtering out your globals.Sibship
@Sibship Yes I posted a new question ;) #20528159Jackelinejackelyn
@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() (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.Leonardaleonardi
@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...Jackelinejackelyn

© 2022 - 2024 — McMap. All rights reserved.