0

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;
  }
2
  • The correct tag is lua-api Commented Feb 4, 2024 at 10:30
  • Probably there is no Lua stack because you have already exited lua_pcall. Commented Feb 4, 2024 at 10:33

1 Answer 1

0

After lua_pcall returns, jj is no longer on the call stack.

You can use lua_getinfo:

int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);

Starting the what string with > pops a function from the stack, and fills out the debug structure with information based on the rest of the characters in the what string (see: lua_Debug for details).

#include <lauxlib.h>                                                        
#include <lua.h>                                                           
#include <lualib.h>                                                         
#include <stdio.h>                                                          
                                                                           
int main(void)                                           
{                                                                           
    lua_State *L = luaL_newstate();                                        
    luaL_openlibs(L);                                             
                                                                             
    const char *luaCode = "function jj() print('Hello from Lua!') end";       
    luaL_dostring(L, luaCode);                                                                                         
    lua_getglobal(L, "jj");                                                   
                                                                              
    lua_Debug ar;                                                          

    if (lua_getinfo(L, ">Sn", &ar)) {
        printf("Function (%s): %s\n", ar.what, ar.name ? ar.name : "(no name)");
        printf("Source: %s\n", ar.short_src);
        printf("Line: %d\n", ar.linedefined);
    }
              
    lua_close(L);
}
Function (Lua): (no name)
Source: [string "function jj() print('Hello from Lua!') end"]
Line: 1

Or set a hook with lua_sethook. Here is a hook that runs every time a function is called:

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdio.h>

static void hook(lua_State *L, lua_Debug *ar)
{
    if (LUA_HOOKCALL == ar->event && lua_getinfo(L, "Snl", ar)) {
        printf("Function (%s): %s\n", ar->what, ar->name ? ar->name : "(no name)");
        printf("Source: %s\n", ar->short_src);
        printf("Line: %d\n", ar->currentline);
    }
}

int main(void)
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    const char *luaCode = "function jj() print('Hello from Lua!') end";
    luaL_dostring(L, luaCode);
    lua_sethook(L, hook, LUA_MASKCALL, 0);
    lua_getglobal(L, "jj");
    lua_pcall(L, 0, 0, 0);
    lua_close(L);
}
Function (Lua): (no name)
Source: [string "function jj() print('Hello from Lua!') end"]
Line: 1
Function (C): print
Source: [C]
Line: -1
Hello from Lua!

Whereas lua_getstack is for collecting the activation record for a currently active function call (which you can then use with lua_getinfo to collect debug information).

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdio.h>

static int trace(lua_State *L)
{
    lua_Debug ar;

    if (lua_getstack(L, 1, &ar) && lua_getinfo(L, "Snl", &ar)) {
        printf("Function (%s): %s\n", ar.what, ar.name ? ar.name : "(no name)");
        printf("Source: %s\n", ar.short_src);
        printf("Line: %d\n", ar.currentline);
    }

    return 0;
}

int main(void)
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    const char *luaCode = "function jj()\nprint('Hello from Lua!')\ntrace()\nend";
    luaL_dostring(L, luaCode);

    lua_register(L, "trace", trace);

    lua_getglobal(L, "jj");
    lua_pcall(L, 0, 0, 0);
    lua_close(L);
}
Hello from Lua!
Function (Lua): (no name)
Source: [string "function jj()..."]
Line: 3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.