static int luv_has_ref(lua_State* L) { uv_handle_t* handle = luv_check_handle(L, 1); int ret = uv_has_ref(handle); if (ret < 0) return luv_error(L, ret); lua_pushboolean(L, ret); return 1; }
static int luv_fileno(lua_State* L) { uv_handle_t* handle = luv_check_handle(L, 1); uv_os_fd_t fd; int ret = uv_fileno(handle, &fd); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, (LUA_INTEGER)fd); return 1; }
// Show the libuv type instead of generic "userdata" static int luv_handle_tostring(lua_State* L) { uv_handle_t* handle = luv_check_handle(L, 1); switch (handle->type) { #define XX(uc, lc) case UV_##uc: lua_pushfstring(L, "uv_"#lc"_t: %p", handle); break; UV_HANDLE_TYPE_MAP(XX) #undef XX default: lua_pushfstring(L, "uv_handle_t: %p", handle); break; } return 1; }
static int luv_close(lua_State* L) { uv_handle_t* handle = luv_check_handle(L, 1); if (uv_is_closing(handle)) { luaL_error(L, "handle %p is already closing", handle); } if (!lua_isnoneornil(L, 2)) { luv_check_callback(L, handle->data, LUV_CLOSED, 2); } uv_close(handle, luv_close_cb); return 0; }
static int luv_thread_arg_set(lua_State* L, luv_thread_arg_t* args, int idx, int top, int flag) { int i; idx = idx > 0 ? idx : 1; i = idx; while (i <= top && i <= LUV_THREAD_MAXNUM_ARG + idx) { luv_val_t *arg = args->argv + i - idx; arg->type = lua_type(L, i); switch (arg->type) { case LUA_TNIL: break; case LUA_TBOOLEAN: arg->val.boolean = lua_toboolean(L, i); break; case LUA_TNUMBER: arg->val.num = lua_tonumber(L, i); break; case LUA_TLIGHTUSERDATA: arg->val.userdata = lua_touserdata(L, i); break; case LUA_TSTRING: { const char* p = lua_tolstring(L, i, &arg->val.str.len); arg->val.str.base = malloc(arg->val.str.len); if (arg->val.str.base == NULL) { perror("out of memory"); return 0; } memcpy((void*)arg->val.str.base, p, arg->val.str.len); break; } case LUA_TUSERDATA: if (flag == 1) { arg->val.userdata = luv_check_handle(L, i); break; } default: fprintf(stderr, "Error: thread arg not support type '%s' at %d", luaL_typename(L, arg->type), i); exit(-1); break; } i++; } args->argc = i - idx; return args->argc; }
static int luv_recv_buffer_size(lua_State* L) { uv_handle_t* handle = luv_check_handle(L, 1); int value; int ret; if (lua_isnoneornil(L, 2)) { value = 0; } else { value = luaL_checkinteger(L, 2); } ret = uv_recv_buffer_size(handle, &value); if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }
static int luv_unref(lua_State* L) { uv_handle_t* handle = luv_check_handle(L, 1); uv_unref(handle); return 0; }