global index failed?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

global index failed?

陈果
Hi, everyone.
 
In my programme, I want to catch the index activity of the global table whenever it failed to get _G[name], so I can provide my userdata to that indexed global variable. But unfortunately I got a failure and I can't figure out the reason. Following is my code fragment:
 
    static int global_resovler(lua_State *L)
    {
   
        printf("global resolver\n");
        return 0;
    }
 
    // in the extension
    // Registers the global resolver
    lua_pushvalue(L, LUA_GLOBALSINDEX);
    lua_getmetatable(L, -1);
    
    lua_pushstring(L, "__index");
    lua_pushcfunction(L, global_resovler);
    lua_rawset(L, -3);
    lua_pushstring(L, "__newindex");
    lua_pushcfunction(L, global_resovler);
    lua_rawset(L, -3);
    lua_pop(L, 1);
 
Can anyone help me figure out why there's no output from the global_resolver when I index a global variable from the script ? Thanks!
--
Best Regards.

Chen guo
Tongji University, China
Reply | Threaded
Open this post in threaded view
|

Re: global index failed?

Greg Falcon
On 4/18/06, 陈果 <[hidden email]> wrote:
> Can anyone help me figure out why there's no output from the global_resolver
> when I index a global variable from the script ? Thanks!

My guess is that you have never defined a metatable for the globals table.

Unless some other bit of code in your program created a metatable for
the globals table, the globals table has no associated metatable.
Your call to lua_getmetatable(L, -1) would returning 0 and push
nothing on the stack.  So after this line executed, the stack would
contain only the globals table you pushed from the line prior.  The
next six lines end up setting values on the globals table, not a
metatable.

Also note if your globals table did have a metatable, then your code
fragment would not leave the stack balanced.  lua_pop(L, 1) would
remove the metatable but not the globals table pushed on by
lua_pushvalue().

Greg F

>
> Hi, everyone.
>
> In my programme, I want to catch the index activity of the global table
> whenever it failed to get _G[name], so I can provide my userdata to that
> indexed global variable. But unfortunately I got a failure and I can't
> figure out the reason. Following is my code fragment:
>
>     static int global_resovler(lua_State *L)
>     {
>
>         printf("global resolver\n");
>         return 0;
>     }
>
>     // in the extension
>     // Registers the global resolver
>     lua_pushvalue(L, LUA_GLOBALSINDEX);
>     lua_getmetatable(L, -1);
>
>     lua_pushstring(L, "__index");
>     lua_pushcfunction(L, global_resovler);
>     lua_rawset(L, -3);
>     lua_pushstring(L, "__newindex");
>     lua_pushcfunction(L, global_resovler);
>     lua_rawset(L, -3);
>     lua_pop(L, 1);
>
> --
> Best Regards.
>
> Chen guo
> Tongji University, China
Reply | Threaded
Open this post in threaded view
|

Re: global index failed?

陈果
Thank you very much. The problem is just like what you have said.
But I still wonder why the call to "lua_getmetatable(L, -1)" doesn't push nil onto the stack? I tried lua_isnil followed lua_getmetatable(L, -1) but it failed to report true.

 
在06-4-18,Greg Falcon <[hidden email]> 写道:
On 4/18/06, 陈果 <[hidden email]> wrote:
> Can anyone help me figure out why there's no output from the global_resolver
> when I index a global variable from the script ? Thanks!

My guess is that you have never defined a metatable for the globals table.

Unless some other bit of code in your program created a metatable for
the globals table, the globals table has no associated metatable.
Your call to lua_getmetatable(L, -1) would returning 0 and push
nothing on the stack.  So after this line executed, the stack would
contain only the globals table you pushed from the line prior.  The
next six lines end up setting values on the globals table, not a
metatable.

Also note if your globals table did have a metatable, then your code
fragment would not leave the stack balanced.  lua_pop(L, 1) would
remove the metatable but not the globals table pushed on by
lua_pushvalue().

Greg F

>
> Hi, everyone.
>
> In my programme, I want to catch the index activity of the global table
> whenever it failed to get _G[name], so I can provide my userdata to that
> indexed global variable. But unfortunately I got a failure and I can't
> figure out the reason. Following is my code fragment:
>

>     static int global_resovler(lua_State *L)
>     {
>
>         printf("global resolver\n");
>         return 0;
>     }
>
>     // in the extension
>     // Registers the global resolver
>     lua_pushvalue(L, LUA_GLOBALSINDEX);
>     lua_getmetatable(L, -1);
>
>     lua_pushstring(L, "__index");
>     lua_pushcfunction(L, global_resovler);
>     lua_rawset(L, -3);
>     lua_pushstring(L, "__newindex");
>     lua_pushcfunction(L, global_resovler);
>     lua_rawset(L, -3);
>     lua_pop(L, 1);
>
> --
> Best Regards.
>
> Chen guo
> Tongji University, China



--
Best Regards.

Chen guo
Tongji University, China
Mobile: 13817166264
MSN:    [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: global index failed?

Greg Falcon
On 4/18/06, 陈果 <[hidden email]> wrote:
> Thank you very much. The problem is just like what you have said.
> But I still wonder why the call to "lua_getmetatable(L, -1)" doesn't push
> nil onto the stack? I tried lua_isnil followed lua_getmetatable(L, -1) but
> it failed to report true.

lua_getmetatable does not push anything on the stack if its parameter
has no metatable.  You have to check its return value.

This is documented in the manual:
http://www.lua.org/manual/5.1/manual.html#lua_getmetatable

Greg F