lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Oops, sorry, this turned out to be a bug in my code. Solved now.

On 16.08.2019 at 22:56 Andreas Falkenhahn wrote:

> I wrote this C function to print the names of all local variables:

>     void dumplocals(lua_State *L) {

>         lua_Debug ar;
>         int level = 0, i;
>         const char *name;

>         if(lua_getstack(L, level, &ar) == 0) {
>             printf("Error\n");
>             return;
>         }

>         i = 0;
>         while((name = lua_getlocal(L, &ar, i + 1)) != NULL) {
>             printf("%s\n", name);
>             lua_pop(L, 1);
>             i++;
>         }
>     } 

> And this is the Lua code:

>     do
>        local a,b,c=1,2,3
>        print(a,b,c)
>        testfunc()
>     end

> Note that testfunc() is a C function. Now I'd like to print the
> names of all local variables when testfunc() returns. That's why I
> have added a call to dumplocals() to lvm.c/luaV_execute() in the
> OP_CALL/OP_TAILCALL branch as follows:

>         firstResult = luaD_precall(L, ra);
>         if (firstResult) {
>           if (firstResult > L->top) {  /* yield? */
>             lua_assert(L->ci->state == (CI_C | CI_YIELD));
>             (L->ci - 1)->u.l.savedpc = pc;
>             (L->ci - 1)->state = CI_SAVEDPC;
>             return NULL;
>           }
>           /* it was a C function (`precall' called it); adjust results */
>           luaD_poscall(L, nresults, firstResult);
>           if (nresults >= 0) L->top = L->ci->top;

>           // !!! THIS IS THE ONLY LINE I ADDED !!!
>           dumplocals(L);
>         } 

> Note that I'm still on Lua 5.0.

> Now I'd expect dumplocals() to print

>     a
>     b
>     c

> But it doesn't do that. Instead, lua_getlocal() in dumplocals()
> always returns NULL although there should be three local variables
> a,b,c. So I don't really understand why this doesn't work. Can anybody explain?



-- 
Best regards,
 Andreas Falkenhahn                            mailto:andreas@falkenhahn.com