Closed sigmareaver closed 1 year ago
It seems that the solution is to use the built-in for_each
function that comes with the state/table/where you need it.
However, this lists everything that's been previously loaded in and not just the items in the script that I desire. Running the script with an empty environment doesn't seem to do much either. I don't know if Sol has a solution for this, but my solution was to use lua_setupvalue()
. Here is the example code:
auto result = m_lua_state.load(R"STR(
function Func1() print("Hello") end
function Func2() print("World") end
function Func3() print("Foo") end
function Func4() print("Bar") end
)STR");
m_lua_state.create_named_table("MyEnv");
lua_getglobal(m_lua_state.lua_state(), "MyEnv");
if (!lua_setupvalue(m_lua_state.lua_state(), -2, 1))
std::cout << "lua_setupvalue() failed!\n";
sol::table MyEnv = m_lua_state["MyEnv"];
if (result.valid() && result().valid()) { // If loading code was success and if executing code was success
std::cout << "Printing MyEnv...\n";
MyEnv.for_each(
[](std::pair<sol::object, sol::object> kv_pair) {
auto type = kv_pair.second.get_type();
std::cout << " " << kv_pair.first.as<std::string>() << ": " << (int)type << "\n";
}
);
}
And here is the output:
Printing MyEnv...
Func2: 6
Func1: 6
Func3: 6
Func4: 6
I am using LuaJIT. I would like to iterate over the state and print out the names of all functions loaded in a script.
I have tried the above code, as well as changing
lua.begin()
andlua.end()
toglobals.begin()
andglobals.end()
where globals is a local table oflua.globals()
.After it iterates through once, maybe twice, it crashes on
it++
. The stack being:The crash is a null pointer access violation, due to
Table* t
being NULL.Am I doing something wrong? Or is there a workaround? How can I get a list of functions in a script?