Luvref
Nvim :help pages, generated
from source
using the tree-sitter-vimdoc parser.
vim.loop.version()).INTRODUCTION
uv module, but can
be used in other Lua environments.local uv = vim.looplocal server = uv.new_tcp() server:bind(β127.0.0.1β, 1337) server:listen(128, function (err) assert(not err, err) local client = uv.new_tcp() server:accept(client) client:read_start(function (err, chunk) assert(not err, err) if chunk then client:write(chunk) else client:shutdown() client:close() end end) end) print(βTCP server listening at 127.0.0.1 port 1337β) uv.run() β an explicit run call is necessary outside of luvit
uv for
simplicity. This module consists mostly of functions with names corresponding
to their original libuv versions. For example, the libuv function
uv_tcp_bind has a luv version at uv.tcp_bind(). Currently, only one
non-function field exists: uv.constants, which is a table.uv.tcp_bind(server, host, port) can alternatively be
called as server:bind(host, port) . Note that the first argument server
becomes the object and tcp_ is removed from the function name. Method forms
are documented below where they exist.callable: a function; or a table or userdata with a __call
metamethod
buffer: a string or a sequential table of strings
threadargs: variable arguments (...) of type nil, boolean, number,
string, or userdata
CONTENTS
ERROR HANDLING
nil, err, name tuple.nil idiomatically indicates failure
err is a string with the format {name}: {message}
{name} is the error name provided internally by uv_err_name
{message} is a human-readable message provided internally by
uv_strerror
name is the same string used to construct err
fail pseudo-type.0 to indicate
success, or sometimes nothing at all. These cases are documented below.VERSION CHECKING
integerstringuv_loop_t β Event loop
loop_close(). Call this function only after the loop has
finished executing and all open handles and requests have been
closed, or it will return EBUSY.0 or failmode: string or nil (default: "default")
"default": Runs the event loop until there are no more
active and referenced handles or requests. Returns true
if uv.stop() was called and there are still active
handles or requests. Returns false in all other cases.
"once": Poll for I/O once. Note that this function
blocks if there are no pending callbacks. Returns false
when done (no active handles or requests left), or true
if more callbacks are expected (meaning you should run the
event loop again sometime in the future).
"nowait": Poll for I/O once but don't block if there are
no pending callbacks. Returns false if done (no active
handles or requests left), or true if more callbacks are
expected (meaning you should run the event loop again
sometime in the future).
boolean or failuv.run() after loading user
code, but if you use the luv bindings directly, you need to
call this after registering your initial set of event
callbacks to start the event loop.option: string
...: depends on option, see below
"block_signal": Block a signal when polling for new
events. The second argument to loop_configure() is the
signal name (as a lowercase string) or the signal number.
This operation is currently only implemented for
"sigprof" signals, to suppress unnecessary wakeups when
using a sampling profiler. Requesting other signals will
fail with EINVAL.
"metrics_idle_time": Accumulate the amount of idle time
the event loop spends in the event provider. This option
is necessary to use metrics_idle_time().
uv.loop_configure("block_signal", "sigprof")0 or failENOSYS error; it means the
loop option is not supported by the platform.nil is returned instead.string or niltrue if there are referenced active handles, active
requests, or closing handles in the loop; otherwise, false.boolean or failuv.run("nowait") to
poll in one thread and run the event loop's callbacks in
anotherinteger or nilintegerintegercallback: callable
callback will be executed with
each handle.-- Example usage of uv.walk to close all handles that
-- aren't already closing.
uv.walk(function (handle)
if not handle:is_closing() then
handle:close()
end
end)uv_req_t β Base request
uv_req_t is the base type for all libuv request types.req:cancel()uv_getaddrinfo_t, uv_getnameinfo_t and uv_work_t
requests is currently supported.0 or failreq:get_type()"fs" for uv_fs_t) and the libuv enum integer for the
request's type (uv_req_type).string, integeruv_handle_t β Base handle
uv_handle_t is the base type for all libuv handle types. All API functions
defined here work with any handle type.handle:is_active()true if the handle is active, false if it's
inactive. What "activeβ means depends on the type of handle:boolean or failhandle:is_closing()true if the handle is closing or closed, false
otherwise.boolean or failhandle:close([callback])callback: callable or nil
callback will be called
asynchronously after this call. This MUST be called on each
handle before memory is released.callback will still be deferred to the next iteration of the
event loop. It gives you a chance to free up any resources
associated with the handle.uv_connect_t or uv_write_t, are
cancelled and have their callbacks called asynchronously with
ECANCELED.handle:ref()handle:unref()handle:has_ref()true if the handle referenced, false if not.boolean or failhandle:send_buffer_size([size])size: integer or nil (default: 0)
size is omitted (or 0), this will return the current
send buffer size; otherwise, this will use size to set the
new send buffer size.integer or fail (if size is nil or 0)
0 or fail (if size is not nil and not 0)
handle:recv_buffer_size([size])size: integer or nil (default: 0)
size is omitted (or 0), this will return the current
send buffer size; otherwise, this will use size to set the
new send buffer size.integer or fail (if size is nil or 0)
0 or fail (if size is not nil and not 0)
handle:fileno()EINVAL.EBADF.integer or failhandle:get_type()"pipe" for uv_pipe_t) and the libuv enum integer for the
handle's type (uv_handle_type).string, integerREFERENCE COUNTING
uv_timer_t β Timer handle
uv_timer_t userdata or fail-- Creating a simple setTimeout wrapper
local function setTimeout(timeout, callback)
local timer = uv.new_timer()
timer:start(timeout, 0, function ()
timer:stop()
timer:close()
callback()
end)
return timer
end
β Creating a simple setInterval wrapper
local function setInterval(interval, callback)
local timer = uv.new_timer()
timer:start(interval, interval, function ()
callback()
end)
return timer
end
β And clearInterval
local function clearInterval(timer)
timer:stop()
timer:close()
end
timer:start(timeout, repeat, callback)timer: uv_timer_t userdata
timeout: integer
repeat: integer
callback: callable
timeout and repeat are in milliseconds.timeout is zero, the callback fires on the next event
loop iteration. If repeat is non-zero, the callback fires
first after timeout milliseconds and then repeatedly after
repeat milliseconds.0 or failtimer:stop()timer: uv_timer_t userdata
0 or failtimer:again()timer: uv_timer_t userdata
EINVAL.0 or failtimer:set_repeat(repeat)timer: uv_timer_t userdata
repeat: integer
timer:get_repeat()timer: uv_timer_t userdata
integertimer:get_due_in()timer: uv_timer_t userdata
integeruv_prepare_t β Prepare handle
local prepare = uv.new_prepare()
prepare:start(function()
print("Before I/O polling")
end)uv_prepare_t userdata or failprepare:start(callback)prepare: uv_prepare_t userdata
callback: callable
0 or failprepare:stop()prepare: uv_prepare_t userdata
0 or failuv_check_t β Check handle
local check = uv.new_check()
check:start(function()
print("After I/O polling")
end)uv_check_t userdata or failcheck:start(callback)check: uv_check_t userdata
callback: callable
0 or failcheck:stop()check: uv_check_t userdata
0 or failuv_idle_t β Idle handle
local idle = uv.new_idle()
idle:start(function()
print("Before I/O polling, no blocking")
end)uv_idle_t userdata or failidle:start(callback)idle: uv_idle_t userdata
callback: callable
0 or failidle:stop()idle: uv_idle_t userdata
0 or failuv_async_t β Async handle
local async
async = uv.new_async(function()
print("async operation ran")
async:close()
end)
async:send()
callback: callable or nil
...: threadargs passed to/from
uv.async_send(async, ...)
nil callback is allowed.uv_async_t userdata or failasync:send(...)async: uv_async_t userdata
...: threadargs
0 or failuv.async_send(async),
that is, not every call to it will yield an execution of the
callback. For example: if uv.async_send() is called 5 times
in a row before the callback is called, the callback will only
be called once. If uv.async_send() is called again after the
callback was called, it will be called again.uv_poll_t β Poll handle
uv_poll_t for any other purpose is not recommended;
uv_tcp_t, uv_udp_t, etc. provide an implementation that is faster and more
scalable than what can be achieved with uv_poll_t, especially on Windows.fd: integer
uv_poll_t userdata or failfd: integer
uv_poll_t userdata or failpoll:start(events, callback)poll: uv_poll_t userdata
events: string or nil (default: "rw")
callback: callable
err: nil or string
events: string or nil
events are: "r",
"w", "rw", "d", "rd", "wd", "rwd", "p", "rp",
"wp", "rwp", "dp", "rdp", "wdp", or "rwdp" where
r is READABLE, w is WRITABLE, d is DISCONNECT, and
p is PRIORITIZED. As soon as an event is detected the
callback will be called with status set to 0, and the detected
events set on the events field.0 or failuv.poll_start() on a handle that is already
active is fine. Doing so will update the events mask that is
being watched for.poll:stop()poll: uv_poll_t userdata
0 or failuv_signal_t β Signal handle
-- Create a new signal handler
local signal = uv.new_signal()
-- Define a handler function
uv.signal_start(signal, "sigint", function(signal)
print("got " .. signal .. ", shutting down")
os.exit(1)
end)uv_signal_t userdata or failsignal:start(signum, callback)signal: uv_signal_t userdata
signum: integer or string
callback: callable
signum: string
0 or fail
uv.signal_start_oneshot()
uv.signal_start_oneshot({signal}, {signum}, {callback})signal:start_oneshot(signum, callback)signal: uv_signal_t userdata
signum: integer or string
callback: callable
signum: string
0 or failsignal:stop()signal: uv_signal_t userdata
0 or failuv_process_t β Process handle
path: string
options: table (see below)
on_exit: callable
code: integer
signal: integer
local stdin = uv.new_pipe() local stdout = uv.new_pipe() local stderr = uv.new_pipe()print(βstdinβ, stdin) print(βstdoutβ, stdout) print(βstderrβ, stderr)
local handle, pid = uv.spawn(βcatβ, { stdio = {stdin, stdout, stderr} }, function(code, signal) β on exit print(βexit codeβ, code) print(βexit signalβ, signal) end)
print(βprocess openedβ, handle, pid)
uv.read_start(stdout, function(err, data) assert(not err, err) if data then print(βstdout chunkβ, stdout, data) else print(βstdout endβ, stdout) end end)
uv.read_start(stderr, function(err, data) assert(not err, err) if data then print(βstderr chunkβ, stderr, data) else print(βstderr endβ, stderr) end end)
uv.write(stdin, βHello Worldβ)
uv.shutdown(stdin, function() print(βstdin shutdownβ, stdin) uv.close(handle, function() print(βprocess closedβ, handle, pid) end) end)
options.args - Command line arguments as a list of
string. The first string should be the path to the
program. On Windows, this uses CreateProcess which
concatenates the arguments into a string. This can cause
some strange errors. (See options.verbatim below for
Windows.)
options.stdio - Set the file descriptors that will be
made available to the child process. The convention is
that the first entries are stdin, stdout, and stderr.
(Note: On Windows, file descriptors after the third are
available to the child process only if the child processes
uses the MSVCRT runtime.)
options.env - Set environment variables for the new
process.
options.cwd - Set the current working directory for the
sub-process.
options.uid - Set the child process' user id.
options.gid - Set the child process' group id.
options.verbatim - If true, do not wrap any arguments in
quotes, or perform any other escaping, when converting the
argument list into a command line string. This option is
only meaningful on Windows systems. On Unix it is silently
ignored.
options.detached - If true, spawn the child process in a
detached state - this will make it a process group leader,
and will effectively enable the child to keep running
after the parent exits. Note that the child process will
still keep the parent's event loop alive unless the parent
process calls uv.unref() on the child's process handle.
options.hide - If true, hide the subprocess console
window that would normally be created. This option is only
meaningful on Windows systems. On Unix it is silently
ignored.
options.stdio entries can take many shapes.nil placeholders means to ignore that fd in
the child process.
on_exit is called with an exit
code and signal.uv_process_t userdata, integerprocess:kill(signum)process: uv_process_t userdata
signum: integer or string
0 or failpid: integer
signum: integer or string
0 or failprocess:get_pid()process: uv_process_t userdata
integeruv_stream_t β Stream handle
uv_stream_t is an abstract type, libuv provides 3 stream implementations
in the form of uv_tcp_t, uv_pipe_t and uv_tty_t.stream:shutdown([callback])callback: callable or nil
err: nil or string
uv_shutdown_t userdata or failstream:listen(backlog, callback)backlog: integer
callback: callable
err: nil or string
backlog indicates
the number of connections the kernel might queue, same as
listen(2). When a new incoming connection is received the
callback is called.0 or failstream:accept(client_stream)0 or failserver:listen(128, function (err) local client = uv.new_tcp() server:accept(client) end)
stream:read_start(callback)callback: callable
err: nil or string
data: string or nil
data
will be nil.0 or failstream:read_start(function (err, chunk)
if err then
-- handle read error
elseif chunk then
-- handle data
else
-- handle disconnect
end
end)stream:read_stop()0 or failstream:write(data, [callback])data: buffer
callback: callable or nil
err: nil or string
data can either be a Lua string or a table of strings. If a
table is passed in, the C backend will use writev to send all
strings in a single system call.callback is for knowing when the write is
complete.uv_write_t userdata or failstream:write2(data, send_handle, [callback])data: buffer
callback: callable or nil
err: nil or string
ipc option true.uv_write_t userdata or failsend_handle must be a TCP socket or pipe, which is a
server or a connection (listening or connected state). Bound
sockets or pipes will be assumed to be servers.stream:try_write(data)data: buffer
integer or failstream:try_write2(data, send_handle)data: buffer
UV_EAGAIN.integer or failstream:is_readable()true if the stream is readable, false otherwise.booleanstream:is_writable()true if the stream is writable, false otherwise.booleanstream:set_blocking(blocking)blocking: boolean
0 or failstream:get_write_queue_size()integeruv_tcp_t β TCP handle
flags: string or nil
"unix",
"inet", "inet6", "ipx", "netlink", "x25", "ax25",
"atmpvc", "appletalk", or "packet".uv_tcp_t userdata or failtcp:open(sock)tcp: uv_tcp_t userdata
sock: integer
0 or failtcp:nodelay(enable)tcp: uv_tcp_t userdata
enable: boolean
0 or failtcp:keepalive(enable, [delay])tcp: uv_tcp_t userdata
enable: boolean
delay: integer or nil
delay is the initial delay
in seconds, ignored when enable is false.0 or failtcp:simultaneous_accepts(enable)tcp: uv_tcp_t userdata
enable: boolean
0 or failtcp:bind(host, port, [flags])tcp: uv_tcp_t userdata
host: string
port: integer
flags: table or nil
ipv6only: boolean
host should be an IP
address and not a domain name. Any flags are set with a
table with field ipv6only equal to true or false.EADDRINUSE error from either uv.tcp_bind(), uv.listen()
or uv.tcp_connect(). That is, a successful call to this
function does not guarantee that the call to uv.listen() or
uv.tcp_connect() will succeed as well.0 to let the OS assign an ephemeral port. You
can look it up later using uv.tcp_getsockname().0 or failtcp:getpeername()tcp: uv_tcp_t userdata
table or fail
ip : string
family : string
port : integer
tcp:getsockname()tcp: uv_tcp_t userdata
table or fail
ip : string
family : string
port : integer
tcp:connect(host, port, callback)tcp: uv_tcp_t userdata
host: string
port: integer
callback: callable
err: nil or string
uv_connect_t userdata or faillocal client = uv.new_tcp()
client:connect("127.0.0.1", 8080, function (err)
-- check error and carry on.
end)tcp:write_queue_size()tcp:close_reset([callback])tcp: uv_tcp_t userdata
callback: callable or nil
uv.tcp_close_reset() calls is not allowed.0 or fail
uv.socketpair()
uv.socketpair([{socktype}, [{protocol}, [{flags1}, [{flags2}]]]])socktype: string, integer or nil (default: stream)
protocol: string, integer or nil (default: 0)
flags1: table or nil
nonblock: boolean (default: false)
flags2: table or nil
nonblock: boolean (default: false)
socktype must be one of
"stream", "dgram", "raw", "rdm", or "seqpacket".protocol is set to 0 or nil, it will be automatically
chosen based on the socket's domain and type. When protocol
is specified as a string, it will be looked up using the
getprotobyname(3) function (examples: "ip", "icmp",
"tcp", "udp", etc).nonblock: Opens the specified socket handle for
OVERLAPPED or FIONBIO/`O_NONBLOCK` I/O usage. This is
recommended for handles that will be used by libuv, and not
usually recommended otherwise.
socketpair(2) with a domain of AF_UNIX.table or fail
[1, 2] : integer (file descriptor)
-- Simple read/write with tcp
local fds = uv.socketpair(nil, nil, {nonblock=true}, {nonblock=true})
local sock1 = uv.new_tcp()
sock1:open(fds[1])
local sock2 = uv.new_tcp()
sock2:open(fds[2])
sock1:write(βhelloβ)
sock2:read_start(function(err, chunk)
assert(not err, err)
print(chunk)
end)
uv_pipe_t β Pipe handle
local pipe = uv.new_pipe(false)pipe:bind(β/tmp/sock.testβ)
pipe:listen(128, function() local client = uv.new_pipe(false) pipe:accept(client) client:write(βhello!\nβ) client:close() end)
ipc: boolean or nil (default: false)
ipc argument is a boolean to
indicate if this pipe will be used for handle passing between
processes.uv_pipe_t userdata or failpipe:open(fd)pipe: uv_pipe_t userdata
fd: integer
0 or failpipe:bind(name)pipe: uv_pipe_t userdata
name: string
0 or failpipe:connect(name, [callback])pipe: uv_pipe_t userdata
name: string
callback: callable or nil
err: nil or string
uv_connect_t userdata or failpipe:getsockname()pipe: uv_pipe_t userdata
string or failpipe:getpeername()pipe: uv_pipe_t userdata
string or failpipe:pending_instances(count)pipe: uv_pipe_t userdata
count: integer
pipe:pending_count()pipe: uv_pipe_t userdata
integerpipe:pending_type()pipe: uv_pipe_t userdata
uv.pipe_pending_type() and call uv.accept(pipe, handle) .stringpipe:chmod(flags)pipe: uv_pipe_t userdata
flags: string
flags are: "r", "w", "rw", or
"wr" where r is READABLE and w is WRITABLE. This
function is blocking.0 or failread_flags: table or nil
nonblock: boolean (default: false)
write_flags: table or nil
nonblock: boolean (default: false)
write fd and read from the read fd. The resulting
handles can be passed to pipe_open, used with spawn, or
for any other purpose.nonblock: Opens the specified socket handle for
OVERLAPPED or FIONBIO/`O_NONBLOCK` I/O usage. This is
recommended for handles that will be used by libuv, and not
usually recommended otherwise.
pipe(2) with the O_CLOEXEC flag set.table or fail
read : integer (file descriptor)
write : integer (file descriptor)
-- Simple read/write with pipe_open
local fds = uv.pipe({nonblock=true}, {nonblock=true})
local read_pipe = uv.new_pipe()
read_pipe:open(fds.read)
local write_pipe = uv.new_pipe()
write_pipe:open(fds.write)
write_pipe:write(βhelloβ)
read_pipe:read_start(function(err, chunk)
assert(not err, err)
print(chunk)
end)
uv_tty_t β TTY handle
-- Simple echo program local stdin = uv.new_tty(0, true) local stdout = uv.new_tty(1, false)stdin:read_start(function (err, data) assert(not err, err) if data then stdout:write(data) else stdin:close() stdout:close() end end)
fd: integer
readable: boolean
uv_tty_t userdata or failtty:set_mode(mode)tty: uv_tty_t userdata
mode: integer
mode is a C enum with the following values:0 or failEBUSY if you call it when execution is
inside uv.tty_set_mode().0 or failtty:get_winsize()tty: uv_tty_t userdata
integer, integer or failstate: string
"supported" or
"unsupported"."supported" or "unsupported".ENOTSUP.string or failuv_udp_t β UDP handle
flags: table or nil
family: string or nil
mmsgs: integer or nil (default: 1)
family must be one of "unix", "inet",
"inet6", "ipx", "netlink", "x25", "ax25",
"atmpvc", "appletalk", or "packet".mmsgs determines the number of messages able
to be received at one time via recvmmsg(2) (the allocated
buffer will be sized to be able to fit the specified number of
max size dgrams). Only has an effect on platforms that support
recvmmsg(2).flags can also be
a string or integer. When it is a string, it will be treated
like the family key above. When it is an integer, it will be
used directly as the flags parameter when calling
uv_udp_init_ex.uv_udp_t userdata or failudp:get_send_queue_size()integerudp:get_send_queue_count()integerudp:open(fd)udp: uv_udp_t userdata
fd: integer
0 or failudp:bind(host, port, [flags])udp: uv_udp_t userdata
host: string
port: number
flags: table or nil
ipv6only: boolean
reuseaddr: boolean
flags are
set with a table with fields reuseaddr or ipv6only equal
to true or false.0 or failudp:getsockname()udp: uv_udp_t userdata
table or fail
ip : string
family : string
port : integer
udp:getpeername()udp: uv_udp_t userdata
table or fail
ip : string
family : string
port : integer
uv.udp_set_membership()
uv.udp_set_membership({udp}, {multicast_addr}, {interface_addr}, {membership})udp:set_membership(multicast_addr, interface_addr, membership)udp: uv_udp_t userdata
multicast_addr: string
interface_addr: string or nil
membership: string
multicast_addr is
multicast address to set membership for. interface_addr is
interface address. membership can be the string "leave" or
"join".0 or failuv.udp_set_source_membership()
uv.udp_set_source_membership({udp}, {multicast_addr}, {interface_addr}, {source_addr}, {membership})udp:set_source_membership(multicast_addr, interface_addr, source_addr, membership)udp: uv_udp_t userdata
multicast_addr: string
interface_addr: string or nil
source_addr: string
membership: string
multicast_addr is multicast address to set membership for.
interface_addr is interface address. source_addr is source
address. membership can be the string "leave" or "join".0 or failudp:set_multicast_loop(on)udp: uv_udp_t userdata
on: boolean
0 or failudp:set_multicast_ttl(ttl)udp: uv_udp_t userdata
ttl: integer
ttl is an integer 1 through 255.0 or failudp:set_multicast_interface(interface_addr)udp: uv_udp_t userdata
interface_addr: string
0 or failudp:set_broadcast(on)udp: uv_udp_t userdata
on: boolean
0 or failudp:set_ttl(ttl)udp: uv_udp_t userdata
ttl: integer
ttl is an integer 1 through 255.0 or failudp:send(data, host, port, callback)udp: uv_udp_t userdata
data: buffer
host: string
port: integer
callback: callable
err: nil or string
0.0.0.0 (the "all interfaces" IPv4 address) and a random
port number.uv_udp_send_t userdata or failudp:try_send(data, host, port)udp: uv_udp_t userdata
data: buffer
host: string
port: integer
integer or failudp:recv_start(callback)udp: uv_udp_t userdata
callback: callable
err: nil or string
data: string or nil
addr: table or nil
ip: string
port: integer
family: string
flags: table
partial: boolean or nil
mmsg_chunk: boolean or nil
0.0.0.0 (the
"all interfaces" IPv4 address) and a random port number.0 or failudp:recv_stop()udp: uv_udp_t userdata
0 or failudp:connect(host, port)udp: uv_udp_t userdata
host: string
port: integer
uv.udp_connect() on
an already connected handle will result in an EISCONN error.
Trying to disconnect a handle that is not connected will
return an ENOTCONN error.0 or failuv_fs_event_t β FS Event handle
uv_fs_event_t userdata or failfs_event:start(path, flags, callback)fs_event: uv_fs_event_t userdata
path: string
flags: table
watch_entry: boolean or nil (default: false)
stat: boolean or nil (default: false)
recursive: boolean or nil (default: false)
callback: callable
err: nil or string
filename: string
events: table
change: boolean or nil
rename: boolean or nil
0 or failfs_event:stop()0 or failfs_event:getpath()string or failuv_fs_poll_t β FS Poll handle
stat to detect when a file has changed
so they can work on file systems where fs event handles can't.uv_fs_poll_t userdata or failfs_poll:start(path, interval, callback)fs_event: uv_fs_event_t userdata
path: string
interval: integer
callback: callable
err: nil or string
prev: table or nil (see uv.fs_stat)
curr: table or nil (see uv.fs_stat)
path for changes every interval
milliseconds.0 or failfs_poll:stop()0 or failfs_poll:getpath()string or failFILE SYSTEM OPERATIONS
uv_fs_t userdata and asynchronously execute its callback; if an error is
encountered, the first and only argument passed to the callback will be the
err error string; if the operation completes successfully, the first
argument will be nil and the remaining arguments will be the results of the
FS call.readFile (with naive error
handling) are implemented below as an example:local function readFileSync(path) local fd = assert(uv.fs_open(path, "r", 438)) local stat = assert(uv.fs_fstat(fd)) local data = assert(uv.fs_read(fd, stat.size, 0)) assert(uv.fs_close(fd)) return data endlocal data = readFileSync(βmain.luaβ) print(βsynchronous readβ, data)
local function readFile(path, callback)
uv.fs_open(path, "r", 438, function(err, fd)
assert(not err, err)
uv.fs_fstat(fd, function(err, stat)
assert(not err, err)
uv.fs_read(fd, stat.size, 0, function(err, data)
assert(not err, err)
uv.fs_close(fd, function(err)
assert(not err, err)
return callback(data)
end)
end)
end)
end)
end
readFile(βmain.luaβ, function(data)
print(βasynchronous readβ, data)
end)
fd: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
close(2).boolean or failuv_fs_t userdatapath: string
flags: string or integer
mode: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
fd: integer or nil
open(2). Access flags may be an integer or
one of: "r", "rs", "sr", "r+", "rs+", "sr+",
"w", "wx", "xw", "w+", "wx+", "xw+", "a",
"ax", "xa", "a+", "ax+", or "`xa+`".integer or failuv_fs_t userdataCreateFileW and thus the file
is always opened in binary mode. Because of this, the
O_BINARY and O_TEXT flags are not supported.fd: integer
size: integer
offset: integer or nil
callback: callable (async version) or nil (sync
version)
err: nil or string
data: string or nil
preadv(2). Returns any data. An empty string
indicates EOF.offset is nil or omitted, it will default to -1, which
indicates 'use and update the current file offset.'offset is >= 0, the current file offset will not
be updated by the read.string or failuv_fs_t userdatapath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
unlink(2).boolean or failuv_fs_t userdatafd: integer
data: buffer
offset: integer or nil
callback: callable (async version) or nil (sync
version)
err: nil or string
bytes: integer or nil
pwritev(2). Returns the number of bytes
written.offset is nil or omitted, it will default to -1, which
indicates 'use and update the current file offset.'offset is >= 0, the current file offset will not
be updated by the write.integer or failuv_fs_t userdatapath: string
mode: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
mkdir(2).boolean or failuv_fs_t userdatatemplate: string
callback: callable (async version) or nil (sync
version)
err: nil or string
path: string or nil
mkdtemp(3).string or failuv_fs_t userdatatemplate: string
callback: callable (async version) or nil (sync
version)
err: nil or string
fd: integer or nil
path: string or nil
mkstemp(3). Returns a temporary file handle
and filename.integer, string or failuv_fs_t userdatapath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
rmdir(2).boolean or failuv_fs_t userdatapath: string
callback: callable
err: nil or string
success: uv_fs_t userdata or nil
scandir(3), with a slightly different API.
Returns a handle that the user can pass to
uv.fs_scandir_next().uv_fs_t userdata or failfs: uv_fs_t userdata
name, type pair. When there
are no more entries, nil is returned.string, string or nil or failpath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
stat: table or nil (see below)
stat(2).table or fail
dev : integer
mode : integer
nlink : integer
uid : integer
gid : integer
rdev : integer
ino : integer
size : integer
blksize : integer
blocks : integer
flags : integer
gen : integer
atime : table
sec : integer
nsec : integer
mtime : table
sec : integer
nsec : integer
ctime : table
sec : integer
nsec : integer
birthtime : table
sec : integer
nsec : integer
type : string
uv_fs_t userdatafd: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
stat: table or nil (see uv.fs_stat)
fstat(2).table or fail (see uv.fs_stat)uv_fs_t userdatafd: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
stat: table or nil (see uv.fs_stat)
lstat(2).uv_fs_t userdatapath: string
new_path: string
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
rename(2).boolean or failuv_fs_t userdatafd: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
fsync(2).boolean or failuv_fs_t userdatafd: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
fdatasync(2).boolean or failuv_fs_t userdatafd: integer
offset: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
ftruncate(2).boolean or failuv_fs_t userdataout_fd: integer
in_fd: integer
in_offset: integer
size: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
bytes: integer or nil
sendfile(2). Returns the number of
bytes written.integer or failuv_fs_t userdatapath: string
mode: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
permission: boolean or nil
access(2) on Unix. Windows uses
GetFileAttributesW(). Access mode can be an integer or a
string containing "R" or "W" or "X". Returns true or
false indicating access permission.boolean or failuv_fs_t userdatapath: string
mode: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
chmod(2).boolean or failuv_fs_t userdatafd: integer
mode: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
fchmod(2).boolean or failuv_fs_t userdatapath: string
atime: number
mtime: number
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
utime(2).boolean or failuv_fs_t userdatafd: integer
atime: number
mtime: number
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
futime(2).boolean or failuv_fs_t userdatapath: string
atime: number
mtime: number
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
lutime(2).boolean or failuv_fs_t userdatapath: string
new_path: string
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
link(2).boolean or failuv_fs_t userdatapath: string
new_path: string
flags: table, integer, or nil
dir: boolean
junction: boolean
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
symlink(2). If the flags parameter is
omitted, then the 3rd parameter will be treated as the
callback.boolean or failuv_fs_t userdatapath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
path: string or nil
readlink(2).string or failuv_fs_t userdatapath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
path: string or nil
realpath(3).string or failuv_fs_t userdatapath: string
uid: integer
gid: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
chown(2).boolean or failuv_fs_t userdatafd: integer
uid: integer
gid: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
fchown(2).boolean or failuv_fs_t userdatafd: integer
uid: integer
gid: integer
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
lchown(2).boolean or failuv_fs_t userdatapath: string
new_path: string
flags: table, integer, or nil
excl: boolean
ficlone: boolean
ficlone_force: boolean
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
flags parameter
is omitted, then the 3rd parameter will be treated as the
callback.boolean or failuv_fs_t userdatapath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
dir: luv_dir_t userdata or nil
entries: integer or nil
entries parameter
defines the maximum number of entries that should be returned
by each call to uv.fs_readdir().luv_dir_t userdata or failuv_fs_t userdatadir:readdir([callback])dir: luv_dir_t userdata
callback: callable (async version) or nil (sync
version)
err: nil or string
entries: table or nil (see below)
luv_dir_t returned by a
successful uv.fs_opendir() call. A table of data tables is
returned where the number of entries n is equal to or less
than the entries parameter used in the associated
uv.fs_opendir() call.table or fail
[1, 2, 3, ..., n] : table
name : string
type : string
uv_fs_t userdatadir:closedir([callback])dir: luv_dir_t userdata
callback: callable (async version) or nil (sync
version)
err: nil or string
success: boolean or nil
boolean or failuv_fs_t userdatapath: string
callback: callable (async version) or nil (sync
version)
err: nil or string
table or nil (see below)
statfs(2).table or nil
type : integer
bsize : integer
blocks : integer
bfree : integer
bavail : integer
files : integer
ffree : integer
THREAD POOL WORK SCHEDULING
getaddrinfo and getnameinfo requests.local function work_callback(a, b) return a + b endlocal function after_work_callback(c) print(βThe result is: β .. c) end
local work = uv.new_work(work_callback, after_work_callback)
work:queue(1, 2)
β output: βThe result is: 3β
work_callback: function
...: threadargs passed to/from
uv.queue_work(work_ctx, ...)
after_work_callback: function
...: threadargs returned from work_callback
luv_work_ctx_t (not
uv_work_t). Returns the Lua userdata wrapping it.luv_work_ctx_t userdatawork_ctx:queue(...)work_ctx: luv_work_ctx_t userdata
...: threadargs
work_callback in a new
Lua state in a thread from the threadpool with any additional
arguments from .... Values returned from work_callback are
passed to after_work_callback, which is called in the main
loop thread.boolean or failDNS UTILITY FUNCTIONS
host: string or nil
service: string or nil
hints: table or nil
family: string or integer or nil
socktype: string or integer or nil
protocol: string or integer or nil
addrconfig: boolean or nil
v4mapped: boolean or nil
all: boolean or nil
numerichost: boolean or nil
passive: boolean or nil
numericserv: boolean or nil
canonname: boolean or nil
callback: callable (async version) or nil (sync
version)
err: nil or string
addresses: table or nil (see below)
getaddrinfo(3). Either node or service may
be nil but not both.family: "unix", "inet", "inet6", "ipx",
"netlink", "x25", "ax25", "atmpvc", "appletalk",
or "packet"
socktype: "stream", "dgram", "raw", "rdm", or
"seqpacket"
protocol: will be looked up using the getprotobyname(3)
function (examples: "ip", "icmp", "tcp", "udp", etc)
table or fail
[1, 2, 3, ..., n] : table
addr : string
family : string
port : integer or nil
socktype : string
protocol : string
canonname : string or nil
uv_getaddrinfo_t userdata or failaddress: table
ip: string or nil
port: integer or nil
family: string or integer or nil
callback: callable (async version) or nil (sync
version)
err: nil or string
host: string or nil
service: string or nil
getnameinfo(3).family must be one of "unix", "inet",
"inet6", "ipx", "netlink", "x25", "ax25",
"atmpvc", "appletalk", or "packet".string, string or failuv_getnameinfo_t userdata or failTHREADING AND SYNCHRONIZATION UTILITIES
options: table or nil
stack_size: integer or nil
entry: function
...: threadargs passed to entry
luv_thread_t (not uv_thread_t).
Returns the Lua userdata wrapping it and asynchronously
executes entry, which can be either a Lua function or a Lua
function dumped to a string. Additional arguments ... are
passed to the entry function and an optional options table
may be provided. Currently accepted option fields are
stack_size.luv_thread_t userdata or failthread:equal(other_thread)thread: luv_thread_t userdata
other_thread: luv_thread_t userdata
__eq metamethod.booleanluv_thread_tthread:join()thread: luv_thread_t userdata
thread to finish executing its entry function.boolean or failmsec: integer
MISCELLANEOUS UTILITIES
string or failstring or failcwd: string
cwd.0 or failstring or failtitle: string
title.0 or failnumbernumbernumberinteger or failtable or fail
utime : table (user CPU time used)
sec : integer
usec : integer
stime : table (system CPU time used)
sec : integer
usec : integer
maxrss : integer (maximum resident set size)
ixrss : integer (integral shared memory size)
idrss : integer (integral unshared data size)
isrss : integer (integral unshared stack size)
minflt : integer (page reclaims (soft page faults))
majflt : integer (page faults (hard page faults))
nswap : integer (swaps)
inblock : integer (block input operations)
oublock : integer (block output operations)
msgsnd : integer (IPC messages sent)
msgrcv : integer (IPC messages received)
nsignals : integer (signals received)
nvcsw : integer (voluntary context switches)
nivcsw : integer (involuntary context switches)
integertable or fail
[1, 2, 3, ..., n] : table
model : string
speed : number
times : table
user : number
nice : number
sys : number
idle : number
irq : number
integerintegerid: integer
id.id: integer
id.numbernumber or fail[flags] handle-type handle-address . Flags are
R for referenced, A for active and I for internal.fd: integer
fd. Usually this will be used during
initialization to guess the type of the stdio streams.stringgettimeofday(2). Returns
the seconds and microseconds of a unix time as a pair.integer, integer or failip, family, netmask,
internal, and mac.table
[name(s)] : table
ip : string
family : string
netmask : string
internal : boolean
mac : string
ifindex: integer
if_indextoname(3).string or failifindex: integer
ifindex as a string. On all other platforms,
uv.if_indextoname() is used.string or failnumber, number, numbertable
sysname : string
release : string
version : string
machine : string
stringname: string
size: integer (default = LUAL_BUFFERSIZE)
name as
string. The internal buffer size can be set by defining
size. If omitted, LUAL_BUFFERSIZE is used. If the
environment variable exceeds the storage available in the
internal buffer, ENOBUFS is returned. If no matching
environment variable exists, ENOENT is returned.string or failname: string
value: string
name with the
string value.boolean or failboolean or failtablestring or failstring or failtable
username : string
uid : integer
gid : integer
shell : string
homedir : string
numbernumberpid: integer
pid.number or failpid: integer
priority: integer
pid. The priority range is between -20 (high priority) and
19 (low priority).boolean or faillen: integer
flags: nil (see below)
callback: callable (async version) or nil (sync
version)
err: nil or string
bytes: string or nil
len with cryptographically strong
random bytes acquired from the system CSPRNG. flags is
reserved for future extension and must currently be nil or
0 or {}.len random
bytes are available, a non-zero error value is returned or
passed to the callback. If the callback is omitted, this
function is completed synchronously.string or fail0 or failerrcode: integer
err and name in luv-error-handling) equivalent
to the given platform dependent error code: POSIX error codes
on Unix (the ones stored in errno), and Win32 error codes on
Windows (those returned by GetLastError() or
WSAGetLastError()).string, string or nilMETRICS OPERATIONS
epoll_wait). The call is
thread safe.loop_configure with
"metrics_idle_time".number