lua-users home
lua-l archive

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


Hi,

Ignacio Burgueño wrote:
Michael Bauroth wrote:
Hi,

can eventually someone point me to a short example for how to use __index and __newIndex (eventually in context Lunar) to access C++ methods like properties?


Attached is part of what I use. Take a look at the functions 'thunk_index', 'thunk_newindex' and the place were those are registered.

Basically, you when your table is indexed, you llok for a matching method and if none you look for a matching property. There are some files missing from my example, but you essentially, the code you'd want to rip is here.

Example of use:
[snip]

LCB_IMPL_SET(A, value) {
    // stack pos #1 is 'self'
    const char* theValue = luaL_checkString(L, 2);

Value is at #3, instead of #2 ?

I made a quick & dirty update to get value from #1.


static int thunk_newindex(lua_State* L) {
  // stack: userdata, clave, valor
  T* obj = check(L, 1);  // get 'self', or if you prefer, 'this'
  lua_pushvalue(L, 2);	// stack: userdata, clave, valor, clave
  lua_rawget(L, lua_upvalueindex(1));	// upvalue 1 = tabla con setters
  if(!lua_isnil(L, -1)) {
    // stack: userdata, clave, valor, setter
    RegType* p = static_cast<RegType*>(lua_touserdata(L, -1));
    //hd... lua_pop(L, 1);	// stack: userdata, clave, valor
    lua_remove(L, 1);lua_remove(L, 1);
    return (obj->*(p->mfunc))(L);  // call member function
  }
  else {...
}


[snip]

Hope that helps.


It helped me, thanks!
(English comments would be better, though.. :) )

Regards,
Ignacio Burgueño



--
Regards,
Hakki Dogusan