lua-users home
lua-l archive

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



Many thanks Eric
Your solution works splendid.

Just for information I had to
make two small changes to make it work for me.

"int CCC::getInstance(lua_State* L )" changed to
"static int CCC::getInstance(lua_State* L )"

and

"{ "getInstance", CCC::getInstance },"  change to
"{ "getInstance", &CCC::getInstance },"

Now its works exactly as I want to.
Again. Many thanks Eric.

Roland






Roland Strålberg

At home
rstralberg@msn.com
http://www.rstralberg.com

At work
roland.stralberg@microbit.se
http://www.microbit.se




From:  "Eric Chou" <ericchou.cpp@gmail.com>
Reply-To:  Lua list <lua@bazar2.conectiva.com.br>
To:  "Lua list" <lua@bazar2.conectiva.com.br>
Subject: Re: Newbie: How to make static pointers avaiblabe in Lua scriptfrom C++
Date:  Mon, 27 Feb 2006 10:53:57 +0800


You can't use lunar::Register() to wrap the static member function. because, each call to registered member function, actually call the Lunar::thunk() with upvalue, which needs a userdata setted metatable on the bottom of the stack. You can read Lunar source code for detail.


/** Lua test code

a = CCC:getInstance()  --CCC is a table, not userdata
a.print( "Hello" ) ; -- a is lightuserdata, haven't been set metatable.

*/
so , above code has errors.



I have done this work as follow :)

Add this function to Lunar:
static void Lunar<T>::register_functions(lua_State *L)
{
           luaL_openlib(L, T::className, T::functions, 0);
           lua_pop(L, 1);

}

each class which has static member function you want to wrap add :
static luaL_reg functions[];

As you example, you can write:
luaL_reg CCC::functions[] = {
   { "getInstance", CCC::getInstance },

   {0, 0}
};

Then you can use lunar<CCC>::register_functions(pLuaState) to wrap static member function in CCC::functions array.

CCC::getInstance code:
int CCC::getInstance(lua_State* L )

{
Lunar<CCC>::push( L, Instance() ) ; // must use this method to push lightuser in order to set metatable .
return 1 ;
}

Lua test code:
a = CCC:getInstance() -- after call, a is a userdata already seted metatable

a:print( "Hello" )  -- must use ':', or a.print( a, "Hello" )


Maybe these above can help you.



On 2/27/06, Roland Strålberg
<rstralberg@msn.com> wrote:
Hi
After have trying and reading and trying I still cant get following to work

(its a singleton)
I'm using Lunar.h. Sorry for this long post but I really need some starting
help.

class CCC
{
public:
 static CCC* Instance() ;
 void Print( const char* pText ) {  }


 CCC() {}
 ~CCC() {}
private:
 static CCC* m_pInstance ;

// Lua declaration
public:
static const char className[];
static Lunar<CCC>::RegType methods[];

CCC( lua_State* ) ;
int getInstance( lua_State* ) ;

int print( lua_State* ) ;
} ;

CCC* CCC::m_pInstance = 0 ;
CCC* CCC::Instance()
{
 if ( m_pInstance == 0 )
   m_pInstance = new CCC ;
 return m_pInstance ;
}

// Lua implenation

const char CCC::className[] = "CCC" ;

Lunar<CCC>::RegType CCC::methods[] =
{
 { "getInstance", &CCC::getInstance },
 { "print", &CCC::print},
 { 0, 0 }

} ;

CCC::CCC( lua_State* L )
{
}

int CCC::getInstance(lua_State* L )
{
 lua_pushlightuserdata( L, Instance() ) ;
 return 1 ;
}

int CCC::print(lua_State* L )
{
const char* p = luaL_checkstring( L, -1 ) ;

Print( p ) ;
return 0 ;
}

// CCC is registered somewhere else with
// Lunar<CCC>::Register( pLuaState ) ;

/** Lua test code

a = CCC:getInstance()
a.print( "Hello" ) ;


*/

I can compile without errors
but when i run my program and executes the lua.file
loaded by luaL_loadfile( L, "test.lua" ), and then lua_pcall( L, 0,
LUA_MULTRET, 0 )
i get this error when a = CCC:getInstance() is executed in script


"calling 'getInstance' on bad self (CCC expected, got table)'"

I would be really thankful for any help.
Roland Strålberg

mailto:rstralberg@msn.com

http://www.rstralberg.com






--
Best Regards
Eric

Mailto: EricChou.Cpp@gmail.com

MSN:  storm_joe@hotmail.com