Skip to content

Commit ae03877

Browse files
Merge pull request #713 from luvit/docs
Clean up and expand docs for constants/options
2 parents 258831e + 55c8cb9 commit ae03877

File tree

2 files changed

+154
-47
lines changed

2 files changed

+154
-47
lines changed

docs.md

+152-47
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ within the context of luv's Lua API. Low-level implementation details and
8282
unexposed C functions and types are not documented here except for when they
8383
are relevant to behavior seen in the Lua module.
8484

85+
- [Constants][]
8586
- [Error handling][]
8687
- [Version checking][]
8788
- [`uv_loop_t`][] — Event loop
@@ -109,14 +110,109 @@ are relevant to behavior seen in the Lua module.
109110
- [Miscellaneous utilities][]
110111
- [Metrics operations][]
111112

113+
## Constants
114+
115+
[constants]: #constants
116+
117+
As a Lua library, luv supports and encourages the use of lowercase strings to
118+
represent options. For example:
119+
```lua
120+
-- signal start with string input
121+
uv.signal_start("sigterm", function(signame)
122+
print(signame) -- string output: "sigterm"
123+
end)
124+
```
125+
126+
However, luv also superficially exposes libuv constants in a Lua table at
127+
`uv.constants` where its keys are uppercase constant names and their associated
128+
values are integers defined internally by libuv. The values from this table may
129+
be supported as function arguments, but their use may not change the output
130+
type. For example:
131+
132+
```lua
133+
-- signal start with integer input
134+
uv.signal_start(uv.constants.SIGTERM, function(signame)
135+
print(signame) -- string output: "sigterm"
136+
end)
137+
```
138+
139+
The uppercase constants defined in `uv.constants` that have associated
140+
lowercase option strings are listed below.
141+
142+
### Address Families
143+
144+
- `AF_UNIX`: "unix"
145+
- `AF_INET`: "inet"
146+
- `AF_INET6`: "inet6"
147+
- `AF_IPX`: "ipx"
148+
- `AF_NETLINK`: "netlink"
149+
- `AF_X25`: "x25"
150+
- `AF_AX25`: "as25"
151+
- `AF_ATMPVC`: "atmpvc"
152+
- `AF_APPLETALK`: "appletalk"
153+
- `AF_PACKET`: "packet"
154+
155+
### Signals
156+
157+
- `SIGHUP`: "sighup"
158+
- `SIGINT`: "sigint"
159+
- `SIGQUIT`: "sigquit"
160+
- `SIGILL`: "sigill"
161+
- `SIGTRAP`: "sigtrap"
162+
- `SIGABRT`: "sigabrt"
163+
- `SIGIOT`: "sigiot"
164+
- `SIGBUS`: "sigbus"
165+
- `SIGFPE`: "sigfpe"
166+
- `SIGKILL`: "sigkill"
167+
- `SIGUSR1`: "sigusr1"
168+
- `SIGSEGV`: "sigsegv"
169+
- `SIGUSR2`: "sigusr2"
170+
- `SIGPIPE`: "sigpipe"
171+
- `SIGALRM`: "sigalrm"
172+
- `SIGTERM`: "sigterm"
173+
- `SIGCHLD`: "sigchld"
174+
- `SIGSTKFLT`: "sigstkflt"
175+
- `SIGCONT`: "sigcont"
176+
- `SIGSTOP`: "sigstop"
177+
- `SIGTSTP`: "sigtstp"
178+
- `SIGBREAK`: "sigbreak"
179+
- `SIGTTIN`: "sigttin"
180+
- `SIGTTOU`: "sigttou"
181+
- `SIGURG`: "sigurg"
182+
- `SIGXCPU`: "sigxcpu"
183+
- `SIGXFSZ`: "sigxfsz"
184+
- `SIGVTALRM`: "sigvtalrm"
185+
- `SIGPROF`: "sigprof"
186+
- `SIGWINCH`: "sigwinch"
187+
- `SIGIO`: "sigio"
188+
- `SIGPOLL`: "sigpoll"
189+
- `SIGLOST`: "siglost"
190+
- `SIGPWR`: "sigpwr"
191+
- `SIGSYS`: "sigsys"
192+
193+
### Socket Types
194+
195+
- `SOCK_STREAM`: "stream"
196+
- `SOCK_DGRAM`: "dgram"
197+
- `SOCK_SEQPACKET`: "seqpacket"
198+
- `SOCK_RAW`: "raw"
199+
- `SOCK_RDM`: "rdm"
200+
201+
### TTY Modes
202+
203+
- `TTY_MODE_NORMAL`: "normal"
204+
- `TTY_MODE_RAW`: "raw"
205+
- `TTY_MODE_IO`: "io"
206+
112207
## Error Handling
113208

114209
[Error handling]: #error-handling
115210

116-
In libuv, errors are negative numbered constants; however, while those errors are exposed through `uv.errno`,
117-
the functions used to handle them are not exposed to luv users. Instead, if an
118-
internal error is encountered, the luv function will return to the caller an
119-
assertable `nil, err, name` tuple.
211+
In libuv, errors are represented by negative numbered constants. While these
212+
constants are made available in the `uv.errno` table, they are not returned by
213+
luv funtions and the libuv functions used to handle them are not exposed.
214+
Instead, if an internal error is encountered, the failing luv function will
215+
return to the caller an assertable `nil, err, name` tuple:
120216

121217
- `nil` idiomatically indicates failure
122218
- `err` is a string with the format `{name}: {message}`
@@ -130,10 +226,8 @@ When a function is called successfully, it will return either a value that is
130226
relevant to the operation of the function, or the integer `0` to indicate
131227
success, or sometimes nothing at all. These cases are documented below.
132228

133-
### `uv.errno`
134-
135-
A table value which exposes error constants as a map, where the key is the error name (without the `UV_` prefix) and its value is a negative number.
136-
See Libuv's [Error constants](https://docs.libuv.org/en/v1.x/errors.html#error-constants) page for further details.
229+
Below is a list of known error names and error strings. See libuv's
230+
[error constants][] page for an original source.
137231

138232
- `E2BIG`: argument list too long.
139233
- `EACCES`: permission denied.
@@ -1094,8 +1188,8 @@ Reception of some signals is emulated on Windows:
10941188
-- Create a new signal handler
10951189
local signal = uv.new_signal()
10961190
-- Define a handler function
1097-
uv.signal_start(signal, "sigint", function(signal)
1098-
print("got " .. signal .. ", shutting down")
1191+
uv.signal_start(signal, "sigint", function(signame)
1192+
print("got " .. signame .. ", shutting down")
10991193
os.exit(1)
11001194
end)
11011195
```
@@ -1107,32 +1201,36 @@ it.
11071201

11081202
**Returns:** `uv_signal_t userdata` or `fail`
11091203

1110-
### `uv.signal_start(signal, signum, callback)`
1204+
### `uv.signal_start(signal, signame, callback)`
11111205

1112-
> method form `signal:start(signum, callback)`
1206+
> method form `signal:start(signame, callback)`
11131207
11141208
**Parameters:**
11151209
- `signal`: `uv_signal_t userdata`
1116-
- `signum`: `integer` or `string`
1210+
- `signame`: `string` or `integer`
11171211
- `callback`: `callable`
1118-
- `signum`: `string`
1212+
- `signame`: `string`
11191213

11201214
Start the handle with the given callback, watching for the given signal.
11211215

1216+
See [Constants][] for supported `signame` input and output values.
1217+
11221218
**Returns:** `0` or `fail`
11231219

1124-
### `uv.signal_start_oneshot(signal, signum, callback)`
1220+
### `uv.signal_start_oneshot(signal, signame, callback)`
11251221

1126-
> method form `signal:start_oneshot(signum, callback)`
1222+
> method form `signal:start_oneshot(signame, callback)`
11271223
11281224
**Parameters:**
11291225
- `signal`: `uv_signal_t userdata`
1130-
- `signum`: `integer` or `string`
1226+
- `signame`: `string` or `integer`
11311227
- `callback`: `callable`
1132-
- `signum`: `string`
1228+
- `signame`: `string`
11331229

11341230
Same functionality as `uv.signal_start()` but the signal handler is reset the moment the signal is received.
11351231

1232+
See [Constants][] for supported `signame` input and output values.
1233+
11361234
**Returns:** `0` or `fail`
11371235

11381236
### `uv.signal_stop(signal)`
@@ -1274,28 +1372,32 @@ When the child process exits, `on_exit` is called with an exit code and signal.
12741372

12751373
**Returns:** `uv_process_t userdata`, `integer`
12761374

1277-
### `uv.process_kill(process, signum)`
1375+
### `uv.process_kill(process, signame)`
12781376

1279-
> method form `process:kill(signum)`
1377+
> method form `process:kill(signame)`
12801378
12811379
**Parameters:**
12821380
- `process`: `uv_process_t userdata`
1283-
- `signum`: `integer` or `string` or `nil` (default: `sigterm`)
1381+
- `signame`: `string` or `integer` or `nil` (default: `sigterm`)
12841382

12851383
Sends the specified signal to the given process handle. Check the documentation
12861384
on `uv_signal_t` for signal support, specially on Windows.
12871385

1386+
See [Constants][] for supported `signame` input values.
1387+
12881388
**Returns:** `0` or `fail`
12891389

1290-
### `uv.kill(pid, signum)`
1390+
### `uv.kill(pid, signame)`
12911391

12921392
**Parameters:**
12931393
- `pid`: `integer`
1294-
- `signum`: `integer` or `string` or `nil` (default: `sigterm`)
1394+
- `signame`: `string` or `integer` or `nil` (default: `sigterm`)
12951395

12961396
Sends the specified signal to the given PID. Check the documentation on
12971397
`uv_signal_t` for signal support, specially on Windows.
12981398

1399+
See [Constants][] for supported `signame` input values.
1400+
12991401
**Returns:** `0` or `fail`
13001402

13011403
### `uv.process_get_pid(process)`
@@ -1549,11 +1651,12 @@ TCP handles are used to represent both TCP streams and servers.
15491651
### `uv.new_tcp([flags])`
15501652

15511653
**Parameters:**
1552-
- `flags`: `string` or `nil`
1654+
- `flags`: `string` or `integer` or `nil`
15531655

15541656
Creates and initializes a new `uv_tcp_t`. Returns the Lua userdata wrapping it.
1555-
Flags may be a family string: `"unix"`, `"inet"`, `"inet6"`, `"ipx"`,
1556-
`"netlink"`, `"x25"`, `"ax25"`, `"atmpvc"`, `"appletalk"`, or `"packet"`.
1657+
1658+
If set, `flags` must be a valid address family. See [Constants][] for supported
1659+
address family input values.
15571660

15581661
**Returns:** `uv_tcp_t userdata` or `fail`
15591662

@@ -1649,6 +1752,8 @@ later using `uv.tcp_getsockname()`.
16491752

16501753
Get the address of the peer connected to the handle.
16511754

1755+
See [Constants][] for supported address `family` output values.
1756+
16521757
**Returns:** `table` or `fail`
16531758
- `ip` : `string`
16541759
- `family` : `string`
@@ -1663,6 +1768,8 @@ Get the address of the peer connected to the handle.
16631768

16641769
Get the current address to which the handle is bound.
16651770

1771+
See [Constants][] for supported address `family` output values.
1772+
16661773
**Returns:** `table` or `fail`
16671774
- `ip` : `string`
16681775
- `family` : `string`
@@ -1723,8 +1830,7 @@ and `uv.tcp_close_reset()` calls is not allowed.
17231830

17241831
Create a pair of connected sockets with the specified properties. The resulting handles can be passed to `uv.tcp_open`, used with `uv.spawn`, or for any other purpose.
17251832

1726-
When specified as a string, `socktype` must be one of `"stream"`, `"dgram"`, `"raw"`,
1727-
`"rdm"`, or `"seqpacket"`.
1833+
See [Constants][] for supported `socktype` input values.
17281834

17291835
When `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).
17301836

@@ -2059,16 +2165,11 @@ This function is not thread safe on systems that don’t support ioctl TIOCGPTN
20592165
20602166
**Parameters:**
20612167
- `tty`: `uv_tty_t userdata`
2062-
- `mode`: `integer`
2168+
- `mode`: `string` or `integer`
20632169

20642170
Set the TTY using the specified terminal mode.
20652171

2066-
Parameter `mode` is a C enum with the following values:
2067-
2068-
- 0 - UV_TTY_MODE_NORMAL: Initial/normal terminal mode
2069-
- 1 - UV_TTY_MODE_RAW: Raw input mode (On Windows, ENABLE_WINDOW_INPUT is
2070-
also enabled)
2071-
- 2 - UV_TTY_MODE_IO: Binary-safe I/O mode for IPC (Unix-only)
2172+
See [Constants][] for supported TTY mode input values.
20722173

20732174
**Returns:** `0` or `fail`
20742175

@@ -2135,9 +2236,7 @@ UDP handles encapsulate UDP communication for both clients and servers.
21352236
Creates and initializes a new `uv_udp_t`. Returns the Lua userdata wrapping
21362237
it. The actual socket is created lazily.
21372238

2138-
When specified, `family` must be one of `"unix"`, `"inet"`, `"inet6"`,
2139-
`"ipx"`, `"netlink"`, `"x25"`, `"ax25"`, `"atmpvc"`, `"appletalk"`, or
2140-
`"packet"`.
2239+
See [Constants][] for supported address `family` input values.
21412240

21422241
When specified, `mmsgs` determines the number of messages able to be received
21432242
at one time via `recvmmsg(2)` (the allocated buffer will be sized to be able
@@ -2386,6 +2485,8 @@ Prepare for receiving data. If the socket has not previously been bound with
23862485
`uv.udp_bind()` it is bound to `0.0.0.0` (the "all interfaces" IPv4 address)
23872486
and a random port number.
23882487

2488+
See [Constants][] for supported address `family` output values.
2489+
23892490
**Returns:** `0` or `fail`
23902491

23912492
### `uv.udp_recv_stop(udp)`
@@ -3270,12 +3371,14 @@ called in the main loop thread.
32703371
Equivalent to `getaddrinfo(3)`. Either `node` or `service` may be `nil` but not
32713372
both.
32723373

3273-
Valid hint strings for the keys that take a string:
3274-
- `family`: `"unix"`, `"inet"`, `"inet6"`, `"ipx"`,
3275-
`"netlink"`, `"x25"`, `"ax25"`, `"atmpvc"`, `"appletalk"`, or `"packet"`
3276-
- `socktype`: `"stream"`, `"dgram"`, `"raw"`,
3277-
`"rdm"`, or `"seqpacket"`
3278-
- `protocol`: will be looked up using the `getprotobyname(3)` function (examples: `"ip"`, `"icmp"`, `"tcp"`, `"udp"`, etc)
3374+
See [Constants][] for supported address `family` input and output values.
3375+
3376+
See [Constants][] for supported `socktype` input and output values.
3377+
3378+
When `protocol` is set to 0 or nil, it will be automatically chosen based on the
3379+
socket's domain and type. When `protocol` is specified as a string, it will be
3380+
looked up using the `getprotobyname(3)` function. Examples: `"ip"`, `"icmp"`,
3381+
`"tcp"`, `"udp"`, etc.
32793382

32803383
**Returns (sync version):** `table` or `fail`
32813384
- `[1, 2, 3, ..., n]` : `table`
@@ -3302,8 +3405,7 @@ Valid hint strings for the keys that take a string:
33023405

33033406
Equivalent to `getnameinfo(3)`.
33043407

3305-
When specified, `family` must be one of `"unix"`, `"inet"`, `"inet6"`, `"ipx"`,
3306-
`"netlink"`, `"x25"`, `"ax25"`, `"atmpvc"`, `"appletalk"`, or `"packet"`.
3408+
See [Constants][] for supported address `family` input values.
33073409

33083410
**Returns (sync version):** `string, string` or `fail`
33093411

@@ -3413,7 +3515,7 @@ the number to correspond with the table keys used in `uv.thread_getaffinity` and
34133515
34143516
**Parameters:**
34153517
- `thread`: `luv_thread_t userdata`
3416-
- `priority`: ``number`
3518+
- `priority`: `number`
34173519

34183520
Sets the specified thread's scheduling priority setting. It requires elevated
34193521
privilege to set specific priorities on some platforms.
@@ -3731,6 +3833,8 @@ table. Each table key is the name of the interface while each associated value
37313833
is an array of address information where fields are `ip`, `family`, `netmask`,
37323834
`internal`, and `mac`.
37333835

3836+
See [Constants][] for supported address `family` output values.
3837+
37343838
**Returns:** `table`
37353839
- `[name(s)]` : `table`
37363840
- `ip` : `string`
@@ -3954,3 +4058,4 @@ metrics counters.
39544058
[libuv]: https://github.com/libuv/libuv
39554059
[libuv documentation page]: http://docs.libuv.org/
39564060
[libuv API documentation]: http://docs.libuv.org/en/v1.x/api.html
4061+
[error constants]: https://docs.libuv.org/en/v1.x/errors.html#error-constants

src/constants.c

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ static int luv_constants(lua_State* L) {
321321
lua_pushinteger(L, SIGSYS);
322322
lua_setfield(L, -2, "SIGSYS");
323323
#endif
324+
325+
// Other
324326
#if LUV_UV_VERSION_GEQ(1, 40, 0)
325327
lua_pushinteger(L, UV_UDP_MMSG_FREE);
326328
lua_setfield(L, -2, "UDP_MMSG_FREE");

0 commit comments

Comments
 (0)