Problems with lua_getlocal() and lua_getupvalue()

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Problems with lua_getlocal() and lua_getupvalue()

Tom Miles-2
Problems with lua_getlocal() and lua_getupvalue()

Hi there,

I've been lurking on the list for a while, and now I've finally plucked up the courage to ask a couple of questions.

I've been tasked to do some work on an existing lua5.0 implementation in our code base.  Part of that task is to do better error handling (or at least, better error reporting).  To this end, I have written an error handler function to show as much information as I can :)  All of my code works, except I cannot retrieve any information about the locals, and upvalues.

My test script is as follows:

--

function scriptbreaker(monkey)
        local funky = "Funky "

        local a = function() NilFunction(); return funky..monkey end

        print(a())
end

scriptbreaker("Gibbon")

--

Pertinant parts of my error handler are:

lua_Debug debug_info;
int level = 1;
while (lua_getstack(state, level++, &debug_info) == 1)
{
        std::stringstream vars, upvals;
        get_local_vars(lua, debug_info, vars);
        if (lua_getinfo(state, "Slnfu", &debug_info) == 1)
        {
                get_upvalues(lua, upvals);
        }
        // Do stuff with the string streams here...
}

void get_local_vars(lua_State * lua, lua_Debug & debug_info, std::stringstream & stream)
{
        const char * name = null;
        int i = 1;
        while ((name = lua_getlocal(lua, &debug_info, i++)) != NULL)
        {
                stream << name << " " << lua << endl;
        }
}

void get_upvalues(lua_State * lua, std::stringstream & stream)
{
        const char * name = null;
        int i = 1;
        int top = lua_gettop(lua);
        if (lua_isfunction(lua, top))
        {
                while ((name = lua_getupvalue(lua, top, i++)) != NULL)
                {
                        stream << name << " " << lua << endl;
                }
        }
}

I have a streaming operator that reads the value from a lua_State which I haven't shown here.  This isn't really important though as I never get into the while loops.  I realise that in the example script, there are no up values anyway, but even when there are, and debug_info.nups > 0, then the functions always return NULL.  Am I being really "noobish" here, and missing something obvious or is there something odd going on?

Thanks for any help,

Tom