following code is not running, i get 0 when i print(lua_getstack()). i do not want to change the code's reasoning, i need it as simple as it seems to do wht i need. Any suggestion?
int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
const char *luaCode = "function jj() print('Hello from Lua!') end";
if (luaL_dostring(L, luaCode) != LUA_OK) {
fprintf(stderr, "Error executing Lua code: %s\n", lua_tostring(L, -1));
lua_close(L);
return 1;
}
// Call a Lua function to populate the stack
lua_getglobal(L, "jj");
lua_pcall(L, 0, 0, 0);
// Try to get stack information
lua_Debug ar;
int gg = lua_getstack(L, 0, &ar);
if (gg == 0) {
fprintf(stderr, "No stack information available\n");
} else {
printf("Stack level: %d\n", 0);
printf(" Function: %s\n", ar.name ? ar.name : "(no name)");
printf(" Source: %s\n", ar.short_src);
printf(" Line: %d\n", ar.currentline);
}
lua_close(L);
return 0;
}
After
lua_pcallreturns,jjis no longer on the call stack.You can use
lua_getinfo:Starting the
whatstring with>pops a function from the stack, and fills out the debug structure with information based on the rest of the characters in thewhatstring (see:lua_Debugfor details).Or set a hook with
lua_sethook. Here is a hook that runs every time a function is called:Whereas
lua_getstackis for collecting the activation record for a currently active function call (which you can then use withlua_getinfoto collect debug information).