Pushing CBaseEntity into a table

Does anyone know a simpler way to push a CBaseEntity into a lua table from a module?
I profiled the code and I didn’t like what I saw.
I am currently using:
[lua]
ILuaObject* GetEntity(CBaseEntity *pEnt)
{
ILuaObject *entityFunc = gLua->GetGlobal(“Entity”);

gLua->Push(entityFunc);
	gLua->Push((float)pEnt->entindex()); // Apparently you can't push an int
gLua->Call(1, 1);

entityFunc->UnReference();

return gLua->GetReturn(0);

}

// In the lua function
ILuaObject* EntTable = gLua->GetNewTable();
for(int i = 0; i < pCount; i++)
{
CBaseEntity pEntity = pEntities;
if(pEntity)
{
ILuaObject* oEntity = GetEntity(pEntity);
if(oEntity)
{
if(!oEntity->isNil())
{
EntTable->SetMember(i + 1, oEntity);
}
oEntity->UnReference();
}
}
else
{
EntTable->SetMember(i + 1, false);
}
}
gLua->Push(EntTable);
[/lua]

[lua]ILuaObject* EntTable = gLua->GetNewTable();
for(int i = 0; i < pCount; i++)
{
CBaseEntity pEntity = pEntities;
if(pEntity)
{
gLua->PushUserData(gLua->GetMetaTable(“Entity”, GLua::TYPE_ENTITY), pEntity);
//I’m not sure about the metatable name, but it’s Entity in _R, so…
EntTable->SetMember(i + 1, gLua->GetObject());
}
else
{
EntTable->SetMember(i + 1, false);
}
}[/lua]

It’s untested, but should work.

Pretty sure it’s a CBaseHandle, which is harder to get at.

entity->GetRefEHandle()