lua-users home
lua-l archive

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


On 22 October 2011 12:55, Patrick Rapin <toupie300@gmail.com> wrote:
> I just had a crash with Lua 5.2 and it took me a little to understand why.
> The problem is with the following instruction:
>
>  lua_getglobal(L, lua_tostring(L, -1));
>
> With Lua 5.1, this is no problem. The macro expands to
>
>  lua_getfield(L, LUA_GLOBALSINDEX,  lua_tostring(L, -1))
>
> But on Lua 5.2, the macro now expands to:
>
>  (lua_pushglobaltable(L), lua_getfield(L, -1, ( lua_tostring(L,
> -1))), lua_remove(L, -2))
>
> And when the lua_tostring is evaluated, the stack has changed !
>
> I propose to rewrite the macro with something like:
>
> #define lua_getglobal(L,s) \
>        do { const char* S=s; lua_pushglobaltable(L); lua_getfield(L, -1, S);
> lua_remove(L, -2); } while(0)
>
>

Yes this is correct as you are using a relative rather than absolute
address and your proposed solution looks good for relative indexes,
although I would question the do while block. The do while block scope
is recommended by numerous FAQs, books et al, yet with a high enough
compiler warning level and the wrong compiler it will generate user
warnings in code. *

* The warning will be something like conditional expression is
constant IIRC and you can use a compiler pragma or empty for loop to
remove the warnings yet it looks ugly as sin.

Liam