lua-users home
lua-l archive

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


simon wrote:

Howdy,

Hi!


I'm having a few problems getting around MSVC calling
conventions when registering a function with lua.

[...]


It's a member function, being registered in a
constructor.

Do I read member function ?

Not being _cdecl should not result in an error, but trying to register a class method to a script is impossible. Class member functions use the _thiscall calling convention, which requires the this-pointer of the class to be passed as invisible first argument. Because Lua cannot know the this-pointer it doesn't support registering class methods.

The best would be to make your method static and save the this-pointer inside the lua_State:
class CMemberCall {
 public:
   CMemberCall(lua_State *pLuaState) {
     int nTag;

     // Save the this-pointer inside the LuaState
     nTag = lua_newtag(pLuaState);
     lua_pushusertag(pLuaState, this, nTag);
     lua_setglobal(pLuaState, "_RPGScript_Owner");

     // Register our Foo-Proxy
     lua_Register(pLuaState, "Foo", _LuaFoo);
   }
   void Foo(void) {
     printf("Foo!");
   }

   static int _LuaFoo(void) {
     CMemberCall *pThis = /* Retrieve this pointer from LuaState */
     pThis->Foo();
   }
}

Sorry, I recently read about setglobal() and tag methods being removed, so the above code to save the this-pointer inside the lua_State has to be modified a bit.

Anyway, I think this is about what the tool toLua does.



Thanks and regards,
Simon Haines


Ciao,
-Markus-