lua-users home
lua-l archive

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

>Why is the metatable at upvalueindex(1)?
>In: "im:ColorAllocate(255, 255, 255)" the 'gettable' event is called
and it
>calls 'ColorAllocate' in the 'gettable' table. But in wich moment
>the metatable in upvalueindex(1)? Is that something that the gettable

The methods are closures that get bound to a reference to the metatable
just before I stuff them into the gettable table.

  #define set_closure(L,n,f,index) \
      (lua_pushvalue(L, index), \              <-- push metatable onto
top of stack  
       lua_pushcclosure(L, f, 1), \            <-- make closure right
       lua_pushstring(L, n), \                 <-- push name of method
       lua_insert(L, -2), /* swap name with closure */ \
       lua_settable(L, -3))                    <-- stuff into gettable

  static void set_closures (lua_State *L, const luaL_reg *l, int n, int
      int i;
      for (i=0; i<n; i++)
        set_closure(L, l[i].name, l[i].func, index);

  static const luaL_reg image_methods[] = {
    {"ColorAllocate", image_color_allocate},
    {"Line",          image_line},
    {"PNG",           image_png},

  metatable = my_newtable(L);  /* metatable */
  methods   = my_newtable(L);  /* Image methods */    <-- gettable table
  set_closures(L, image_methods, NUMOF(image_methods), metatable);

- Peter