lua-users home
lua-l archive

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


Hi,

I am using LUNAR(http://lua-users.org/wiki/CppBindingWithLunar) for exposing my C++ objects to LUA.  

The following is the C Portion of the code. CLObject is one class. CLNumList  is another class which are being discussed here.

#define NUMLIST "CLNumList"

typedef struct {CLNumList*pT;} CLNL;
typedef struct {CLObject *pT;} CLO;


class CLObject {
public:

// Constants
static const char className[];
static Luna< CLObject >::RegType methods[];
..
int GetType(lua_State *L);//(unsigned char&cType);
int GetListofNumbers(lua_State *L);

CObject* real_object; //this is the actual pointer to the C++ class.

};
class CLNumList{
public:

// Constants
static const char className[];
static Luna< CLNumList >::RegType methods[];
..
        
int GetFirst(lua_State *L);
int GetNext(lua_State *L);
int GetInteger(lua_State *L);

int GetIntegerCount(lua_State *L);

CNumList* real_object; //this is the actual pointer to the C++ class.

} ;

The following is .cpp code
...
CLObject :GetListofNumbers(lua_State *L)//
{
                CLNumList*o = new CLNumList(L);
CLNL*ud = static_cast< CLNL*>(lua_newuserdata(L, sizeof(CLNumList)));  // create new userdata
real_object->GetListofNumbers(*o->real_object); //CLObject
ud->pT = o;
lua_pushlightuserdata(L, ud);
luaL_getmetatable(L, NUMLIST );
lua_setmetatable(L, -2);
return 1; 
}


-- The following is the LUA calling scenario.

CLObjectInstance = CLObjectI:new()
local clnumlist1 = CLObjectInstance: GetListofNumbers()

local total =  clnumlist1:GetIntegerCount()
local value=0
for lpCount = 1, total do
if(lpCount==1) then 
          value = clnumlist1GetFirst()
else
          value = clnumlist1GetNext()
end
end

--now if i access the following method using the CLObjectInstance, then LUA throws this error.
CLObjectInstance:GetType() --throws attempt tp call method GetType a nil value.

Could any one shed light on this issue? . 

Thanks and Regards.