static TInt socket_open(lua_State* L) { lua_pushstring(L, reinterpret_cast<const char*>(KSocketServ().Ptr())); lua_gettable(L, LUA_REGISTRYINDEX); TInt handle = luaL_checkint(L, -1); // Remove handle from stack lua_pop(L, 1); RSocketServ s; s.SetHandle(handle); TUint addrFamily = to_addr_family(luaL_optstring(L, 1, "inet")); TUint sockType = to_sock_type(luaL_optstring(L, 2, "stream")); TUint protocol = to_protocol(luaL_optstring(L, 3, "tcp")); // Allocate socket using user data so that we can return it from the function, // and have it collected using __gc CActiveSocket** socket = static_cast<CActiveSocket**>(lua_newuserdata(L, sizeof(CActiveSocket*))); TRAPD(err, *socket = CActiveSocket::NewL(s, addrFamily, sockType, protocol)); if (err != KErrNone) { // Raise massive error! } // Set up metadata table lua_pushstring(L, reinterpret_cast<const char*>(KSocketType().Ptr())); lua_gettable(L, LUA_REGISTRYINDEX); lua_setmetatable(L, 1); return 1; }