Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update luv to support latest libuv #262

Merged
merged 2 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add some new api
1. add LUV_UV_VERSION_GEQ macro to check libuv version when compile
2. add uv.signal_start_oneshot, uv.os_getenv, uv.os_setenv, uv.os_unsetenv, uv.os_gethostname, uv.translate_sys_error
  • Loading branch information
zhaozg committed Jun 24, 2017
commit 6962ace0593eb8c0e08bda626a803230b0d2692f
14 changes: 14 additions & 0 deletions src/luv.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ static const luaL_Reg luv_functions[] = {
// signal.c
{"new_signal", luv_new_signal},
{"signal_start", luv_signal_start},
#if LUV_UV_VERSION_GEQ(1, 12, 0)
{"signal_start_oneshot", luv_signal_start_oneshot},
#endif
{"signal_stop", luv_signal_stop},

// process.c
Expand Down Expand Up @@ -251,6 +254,12 @@ static const luaL_Reg luv_functions[] = {
{"print_all_handles", luv_print_all_handles},
{"print_active_handles", luv_print_active_handles},
#endif
#if LUV_UV_VERSION_GEQ(1, 12, 0)
{"os_getenv", luv_os_getenv},
{"os_setenv", luv_os_setenv},
{"os_unsetenv", luv_os_unsetenv},
{"os_gethostname", luv_os_gethostname},
#endif

// thread.c
{"new_thread", luv_new_thread},
Expand All @@ -263,6 +272,11 @@ static const luaL_Reg luv_functions[] = {
{"new_work", luv_new_work},
{"queue_work", luv_queue_work},

// util.c
#if LUV_UV_VERSION_GEQ(1, 10, 0)
{"translate_sys_error", luv_translate_sys_error},
#endif

{NULL, NULL}
};

Expand Down
50 changes: 49 additions & 1 deletion src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ static int luv_setgid(lua_State* L){
return 0;
}

#ifndef _WIN32
static int luv_print_all_handles(lua_State* L){
uv_print_all_handles(luv_loop(L), stderr);
return 0;
Expand All @@ -364,4 +363,53 @@ static int luv_print_active_handles(lua_State* L){
}
#endif

#if LUV_UV_VERSION_GEQ(1, 12, 0)
static int luv_os_getenv(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
size_t size = luaL_optinteger(L, 2, LUAL_BUFFERSIZE);
char *buff = malloc(size);
int ret = uv_os_getenv(name, buff, &size);
if (ret == 0) {
lua_pushlstring(L, buff, size);
ret = 1;
} else
ret = luv_error(L, ret);
free(buff);
return ret;
}

static int luv_os_setenv(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
const char* value = luaL_checkstring(L, 2);
int ret = uv_os_setenv(name, value);
if (ret == 0)
return luv_error(L, ret);
else
lua_pushboolean(L, 1);
return 1;
}

static int luv_os_unsetenv(lua_State* L) {
const char* name = luaL_checkstring(L, 1);
int ret = uv_os_unsetenv(name);
if (ret == 0)
return luv_error(L, ret);
else
lua_pushboolean(L, 1);
return 1;
}

static int luv_os_gethostname(lua_State* L) {
char hostname[PATH_MAX];
size_t size = sizeof(hostname);
int ret = uv_os_gethostname(hostname, &size);
if (ret == 0) {
lua_pushlstring(L, hostname, size);
ret = 1;
}
else
ret = luv_error(L, ret);
return ret;
}

#endif
25 changes: 25 additions & 0 deletions src/signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ static int luv_signal_start(lua_State* L) {
return 1;
}

#if LUV_UV_VERSION_GEQ(1, 12, 0)
static int luv_signal_start_oneshot(lua_State* L) {
uv_signal_t* handle = luv_check_signal(L, 1);
int signum, ret;
if (lua_isnumber(L, 2)) {
signum = lua_tointeger(L, 2);
}
else if (lua_isstring(L, 2)) {
signum = luv_sig_string_to_num(luaL_checkstring(L, 2));
luaL_argcheck(L, signum, 2, "Invalid Signal name");
}
else {
return luaL_argerror(L, 2, "Missing Signal name");
}

if (!lua_isnoneornil(L, 3)) {
luv_check_callback(L, (luv_handle_t*)handle->data, LUV_SIGNAL, 3);
}
ret = uv_signal_start_oneshot(handle, luv_signal_cb, signum);
if (ret < 0) return luv_error(L, ret);
lua_pushinteger(L, ret);
return 1;
}
#endif

static int luv_signal_stop(lua_State* L) {
uv_signal_t* handle = luv_check_signal(L, 1);
int ret = uv_signal_stop(handle);
Expand Down
13 changes: 13 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ static void luv_status(lua_State* L, int status) {
lua_pushnil(L);
}
}

#if LUV_UV_VERSION_GEQ(1, 10, 0)
static int luv_translate_sys_error(lua_State* L) {
int status = luaL_checkinteger(L, 1);
status = uv_translate_sys_error(status);
if (status < 0) {
luv_error(L, status);
lua_remove(L, -3);
return 2;
}
return 0;
}
#endif
3 changes: 3 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include "luv.h"

#define LUV_UV_VERSION_GEQ(major, minor, patch) \
(((major)<<16 | (minor)<<8 | (patch)) <= UV_VERSION_HEX)

void luv_stack_dump(lua_State* L, const char* name);
static int luv_error(lua_State* L, int ret);
static void luv_status(lua_State* L, int status);
Expand Down