lua-users home
lua-l archive

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

I'm modifying the 'luaT_gettmbyobj()' function within ltm.c (5.1w4) in order to provide a couple of metamethod-like functions that are in fact fixed within the core.

The problem is.. how exactly to do that?

I have the lua_CFunction pointers, of course, but creating a closure without touching the stack (if I touch the stack, well, BOOOM...) Any ideas from harder-than-hard core programmers welcome.


const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
  Table *mt;
  switch (ttype(o)) {
    case LUA_TTABLE:
      mt = hvalue(o)->metatable;
      mt = uvalue(o)->metatable;
      if (ttisenum(o)) {
        lua_CFunction f= (event==TM_CALL) ? enum_call :
                         (event==TM_INDEX) ? enum_index :
                         (event==TM_NEWINDEX) ? enum_newindex : NULL;
        if (f)
// 'f' is the C Function pointer, but we need to make it a closure.
fprintf( stderr, "*** ENUM CALL NOT IMPLEMENTED RIGHT !!! ***\n" );

lua_pushcclosure( L, f, 0 /*no upvalues*/ ); // SHOULDN*T BE A PUSH!

            fprintf( stderr, "returning %p\n", f );
            fprintf( stderr, "tt %d", ttype(L->top-1) );

            return L->top-1;     // TBD: is this right?
      mt = NULL;
  return (mt ?  luaH_getstr(mt, G(L)->tmname[event]) : &luaO_nilobject);