适用于 lua 的 libuv 原生绑定
libuv bindings for luajit and lua 5.1/ 5.2/ 5.3/ 5.4.
This library makes libuv available to lua scripts. It was made for the luvit project but should usable from nearly any lua project.
The library can be used by multiple threads at once. Each thread is assumed to load the library from a different lua_State. Luv will create a unique uv_loop_t for each state. You can't share uv handles between states/loops.
local uv = require('luv')
-- Create a handle to a uv_timer_t
local timer = uv.new_timer()
-- This will wait 1000ms and then continue inside the callback
timer:start(1000, 0, function ()
-- timer here is the value we passed in before from new_timer.
print("Awake!")
-- You must always close your uv handles or you'll leak memory
-- We can't depend on the GC since it doesn't know enough about libuv.
timer:close()
end)
print("Sleeping")
-- uv.run will block and wait for all events to run.
-- When there are no longer any active handles, it will return
uv.run()Here is an example of an TCP echo server
…More examples can be found in the examples and tests folders.
Luv is available on Luarocks here. It can be installed via:
luarocks install luvNote: To require luv using require 'uv' (to maintain compatibility with how luv is required in luvi) create a uv.lua with the contents:
return require 'luv'To build, first install your compiler tools.
On linux this probably means gcc and make. On Ubuntu, the build-essential
package is good for this.
On OSX, you probably want XCode which comes with clang and make and friends.
For windows the free Visual Studio Express works. If you get the 2013 edition,
make sure to get the Windows Desktop edition. The Windows version doesn't
include a working C compiler. Make sure to run all of setup including getting a
free license.
Now install Cmake. The version in brew on OSX or most Linux package managers
is good. The version on Travis CI is too old and so I use a PPA there. On
windows use the installer and make sure to add cmake to your command prompt
path.
If you haven't already, install git and make sure it's in your path. This comes with XCode on OSX. On Linux it's in your package manager. For windows, use the installer at . Make sure it's available to your windows command prompt.
Now open a terminal and clone the code. For windows I recommend the special developer command prompt that came with Visual Studio.
git clone https://github.com/luvit/luv.git --recursive
cd luvOn windows I wrote a small batch file that runs the correct cmake commands and copies the output files for easy access.
C:\Code\luv> msvcbuild.bat
C:\Code\luv> luajit tests\run.luaOn unix systems, use the Makefile.
~/Code/luv> make testThis will build luv as a module library. Module libraries are plugins that are not linked into other targets.
By default luv is linked with LuaJIT 2.1.0-beta3. If you rather like to link luv with PUC Lua 5.4 you can run make with:
~/Code/luv> WITH_LUA_ENGINE=Lua makeIf you want to build luv as a static library run make with:
~/Code/luv> BUILD_MODULE=OFF BUILD_STATIC_LIBS=ON makeThis will create a static library libluv_a.a.
If you want to build luv as a shared library run make with:
~/Code/luv> BUILD_MODULE=OFF BUILD_SHARED_LIBS=ON makeThis will create a shared library libluv.so.
By default the build system will build luv with the supplied dependencies. These are:
However, if your target system has already one or more of these dependencies
installed you can link luv against them.
The default shared library name for libuv is libuv. To link against it use:
~/Code/luv> WITH_SHARED_LIBUV=ON makeThe default shared library name for LuaJIT is libluajit-5.1. To link against
it use:
~/Code/luv> LUA_BUILD_TYPE=System makeThe default shared library name for Lua 5.x is liblua5.x. To link against
it use:
~/Code/luv> LUA_BUILD_TYPE=System WITH_LUA_ENGINE=Lua make暂无开放 Issues,或尚未同步最近议题。