Skip to content

Commit

Permalink
Merge pull request #393 from squeek502/new-tcp-udp
Browse files Browse the repository at this point in the history
Make new_tcp/new_udp first argument more user-friendly/consistent
  • Loading branch information
squeek502 authored Oct 9, 2019
2 parents 54b0f76 + 4573edf commit 0082126
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ static int luv_new_tcp(lua_State* L) {
ret = uv_tcp_init(ctx->loop, handle);
}
else {
ret = uv_tcp_init_ex(ctx->loop, handle, luaL_checkinteger(L, 1));
unsigned int flags;
if (lua_isnumber(L, 1)) {
flags = lua_tointeger(L, 1);
}
else if (lua_isstring(L, 1)) {
const char* family = lua_tostring(L, 1);
flags = luv_af_string_to_num(family);
if (!flags) {
luaL_argerror(L, 1, lua_pushfstring(L, "invalid or unknown address family: '%s'", family));
}
}
else {
luaL_argerror(L, 1, "expected string or integer");
}
ret = uv_tcp_init_ex(ctx->loop, handle, flags);
}
if (ret < 0) {
lua_pop(L, 1);
Expand Down
16 changes: 15 additions & 1 deletion src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ static int luv_new_udp(lua_State* L) {
ret = uv_udp_init(ctx->loop, handle);
}
else {
ret = uv_udp_init_ex(ctx->loop, handle, luaL_checkinteger(L, 1));
unsigned int flags;
if (lua_isnumber(L, 1)) {
flags = lua_tointeger(L, 1);
}
else if (lua_isstring(L, 1)) {
const char* family = lua_tostring(L, 1);
flags = luv_af_string_to_num(family);
if (!flags) {
luaL_argerror(L, 1, lua_pushfstring(L, "invalid or unknown address family: '%s'", family));
}
}
else {
luaL_argerror(L, 1, "expected string or integer");
}
ret = uv_udp_init_ex(ctx->loop, handle, flags);
}
if (ret < 0) {
lua_pop(L, 1);
Expand Down

0 comments on commit 0082126

Please sign in to comment.