diff --git a/docs.md b/docs.md index 1b5e40cc..6a1852ef 100644 --- a/docs.md +++ b/docs.md @@ -1943,24 +1943,32 @@ read_pipe:read_start(function(err, chunk) print(chunk) end) ``` + ### `uv.pipe_bind2(pipe, name, [flags])` > method form `pipe:bind2(name, [flags])` **Parameters:** + - `pipe`: `uv_pipe_t userdata` - `name`: `string` -- `flags`: `integer`(default: 0) +- `flags`: `integer` or `table` or `nil`(default: 0) Bind the pipe to a file path (Unix) or a name (Windows). -`Flags` must be zero or `uv.constants.PIPE_NO_TRUNCATE`. Returns `EINVAL` for unsupported flags without performing the bind operation. +`Flags`: + +- If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`. +- If `type(flags)` is `table`, it must be `{}` or `{ no_trunate = true|false }`. +- If `type(flags)` is `nil`, it use default value `0`. +- Returns `EINVAL` for unsupported flags without performing the bind operation. Supports Linux abstract namespace sockets. namelen must include the leading '\0' byte but not the trailing nul byte. **Returns:** `0` or `fail` **Note**: + 1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, typically between 92 and 108 bytes. 2. New in version 1.46.0. @@ -1970,21 +1978,28 @@ typically between 92 and 108 bytes. > method form `pipe:connect2(name, [flags], [callback])` **Parameters:** + - `pipe`: `uv_pipe_t userdata` - `name`: `string` -- `flags`: `integer`(default: 0) +- `flags`: `integer` or `table` or `nil`(default: 0) - `callback`: `callable` or `nil` - `err`: `nil` or `string` Connect to the Unix domain socket or the named pipe. -`Flags` must be zero or `uv.constants.PIPE_NO_TRUNCATE`. Returns `EINVAL` for unsupported flags without performing the bind operation. +`Flags`: + +- If `type(flags)` is `number`, it must be `0` or `uv.constants.PIPE_NO_TRUNCATE`. +- If `type(flags)` is `table`, it must be `{}` or `{ no_trunate = true|false }`. +- If `type(flags)` is `nil`, it use default value `0`. +- Returns `EINVAL` for unsupported flags without performing the bind operation. Supports Linux abstract namespace sockets. namelen must include the leading nul byte but not the trailing nul byte. **Returns:** `uv_connect_t userdata` or `fail` **Note**: + 1. Paths on Unix get truncated to sizeof(sockaddr_un.sun_path) bytes, typically between 92 and 108 bytes. 2. New in version 1.46.0. diff --git a/src/pipe.c b/src/pipe.c index 4d48a58a..8664f1a5 100644 --- a/src/pipe.c +++ b/src/pipe.c @@ -64,11 +64,27 @@ static int luv_pipe_connect(lua_State* L) { #if LUV_UV_VERSION_GEQ(1,46,0) +static int luv_pipe_optflags(lua_State *L, int i, unsigned int flags) { + // flags param can be nil, an integer, or a table + if (lua_type(L, i) == LUA_TNUMBER || lua_isnoneornil(L, i)) { + flags = (unsigned int)luaL_optinteger(L, i, flags); + } + else if (lua_type(L, i) == LUA_TTABLE) { + lua_getfield(L, i, "no_truncate"); + if (lua_toboolean(L, -1)) flags |= UV_PIPE_NO_TRUNCATE; + lua_pop(L, 1); + } + else { + return luaL_argerror(L, i, "expected nil, integer, or table"); + } + return flags; +} + static int luv_pipe_bind2(lua_State* L) { size_t namelen; uv_pipe_t* handle = luv_check_pipe(L, 1); const char* name = luaL_checklstring(L, 2, &namelen); - unsigned int flags = luaL_optinteger(L, 3, 0); + unsigned int flags = luv_pipe_optflags(L, 3, 0); int ret = uv_pipe_bind2(handle, name, namelen, flags); return luv_result(L, ret); } @@ -78,7 +94,7 @@ static int luv_pipe_connect2(lua_State* L) { luv_ctx_t* ctx = luv_context(L); uv_pipe_t* handle = luv_check_pipe(L, 1); const char* name = luaL_checklstring(L, 2, &namelen); - unsigned int flags = luaL_optinteger(L, 3, 0); + unsigned int flags = luv_pipe_optflags(L, 3, 0); int ref = luv_check_continuation(L, 4); uv_connect_t* req = (uv_connect_t*)lua_newuserdata(L, uv_req_size(UV_CONNECT)); req->data = luv_setup_req(L, ctx, ref);