LUALIB_API int luaopen_luv (lua_State *L) { uv_loop_t* loop; int ret; loop = lua_newuserdata(L, sizeof(*loop)); ret = uv_loop_init(loop); if (ret < 0) { return luaL_error(L, "%s: %s\n", uv_err_name(ret), uv_strerror(ret)); } // Tell the state how to find the loop. lua_pushstring(L, "uv_loop"); lua_insert(L, -2); lua_rawset(L, LUA_REGISTRYINDEX); // Tell the loop how to find the state. loop->data = L; luv_req_init(L); luv_handle_init(L); luaL_newlib(L, luv_functions); luv_constants(L); lua_setfield(L, -2, "constants"); return 1; }
LUALIB_API int luaopen_luv (lua_State *L) { uv_loop_t* loop; int ret; // Setup the uv_loop meta table for a proper __gc luaL_newmetatable(L, "uv_loop.meta"); lua_pushstring(L, "__gc"); lua_pushcfunction(L, loop_gc); lua_settable(L, -3); loop = lua_newuserdata(L, sizeof(*loop)); ret = uv_loop_init(loop); if (ret < 0) { return luaL_error(L, "%s: %s\n", uv_err_name(ret), uv_strerror(ret)); } // setup the metatable for __gc luaL_getmetatable(L, "uv_loop.meta"); lua_setmetatable(L, -2); // Tell the state how to find the loop. lua_pushstring(L, "uv_loop"); lua_insert(L, -2); lua_rawset(L, LUA_REGISTRYINDEX); lua_pop(L, 1); // Tell the loop how to find the state. loop->data = L; luv_req_init(L); luv_handle_init(L); luv_thread_init(L); luv_work_init(L); luaL_newlib(L, luv_functions); luv_constants(L); lua_setfield(L, -2, "constants"); return 1; }