1 /*
2 * Copyright 2014 The Luvit Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 #include "private.h"
18
luv_newuserdata(lua_State * L,size_t sz)19 static void* luv_newuserdata(lua_State* L, size_t sz) {
20 void* handle = malloc(sz);
21 if (handle) {
22 *(void**)lua_newuserdata(L, sizeof(void*)) = handle;
23 }
24 return handle;
25 }
26
luv_checkudata(lua_State * L,int ud,const char * tname)27 static void* luv_checkudata(lua_State* L, int ud, const char* tname) {
28 return *(void**) luaL_checkudata(L, ud, tname);
29 }
30
luv_check_handle(lua_State * L,int index)31 static uv_handle_t* luv_check_handle(lua_State* L, int index) {
32 int isHandle;
33 uv_handle_t* handle;
34 void *udata;
35 if (!(udata = lua_touserdata(L, index))) { goto fail; }
36 if (!(handle = *(uv_handle_t**) udata)) { goto fail; }
37 if (!handle->data) { goto fail; }
38 lua_getfield(L, LUA_REGISTRYINDEX, "uv_handle");
39 lua_getmetatable(L, index < 0 ? index - 1 : index);
40 lua_rawget(L, -2);
41 isHandle = lua_toboolean(L, -1);
42 lua_pop(L, 2);
43 if (isHandle) { return handle; }
44 fail: luaL_argerror(L, index, "Expected uv_handle userdata");
45 return NULL;
46 }
47
48 // Show the libuv type instead of generic "userdata"
luv_handle_tostring(lua_State * L)49 static int luv_handle_tostring(lua_State* L) {
50 uv_handle_t* handle = luv_check_handle(L, 1);
51 switch (handle->type) {
52 #define XX(uc, lc) case UV_##uc: lua_pushfstring(L, "uv_"#lc"_t: %p", handle); break;
53 UV_HANDLE_TYPE_MAP(XX)
54 #undef XX
55 default: lua_pushfstring(L, "uv_handle_t: %p", handle); break;
56 }
57 return 1;
58 }
59
luv_is_active(lua_State * L)60 static int luv_is_active(lua_State* L) {
61 uv_handle_t* handle = luv_check_handle(L, 1);
62 int ret = uv_is_active(handle);
63 if (ret < 0) return luv_error(L, ret);
64 lua_pushboolean(L, ret);
65 return 1;
66 }
67
luv_is_closing(lua_State * L)68 static int luv_is_closing(lua_State* L) {
69 uv_handle_t* handle = luv_check_handle(L, 1);
70 int ret = uv_is_closing(handle);
71 if (ret < 0) return luv_error(L, ret);
72 lua_pushboolean(L, ret);
73 return 1;
74 }
75
luv_close_cb(uv_handle_t * handle)76 static void luv_close_cb(uv_handle_t* handle) {
77 lua_State* L;
78 luv_handle_t* data = (luv_handle_t*)handle->data;
79 if (!data) return;
80 L = data->ctx->L;
81 luv_call_callback(L, data, LUV_CLOSED, 0);
82 luv_unref_handle(L, data);
83 }
84
luv_close(lua_State * L)85 static int luv_close(lua_State* L) {
86 uv_handle_t* handle = luv_check_handle(L, 1);
87 if (uv_is_closing(handle)) {
88 luaL_error(L, "handle %p is already closing", handle);
89 }
90 if (!lua_isnoneornil(L, 2)) {
91 luv_check_callback(L, (luv_handle_t*)handle->data, LUV_CLOSED, 2);
92 }
93 uv_close(handle, luv_close_cb);
94 return 0;
95 }
96
luv_handle_free(uv_handle_t * handle)97 static void luv_handle_free(uv_handle_t* handle) {
98 luv_handle_t* data = (luv_handle_t*)handle->data;
99 if (data) {
100 if (data->extra_gc)
101 data->extra_gc(data->extra);
102 free(data);
103 }
104 free(handle);
105 }
106
luv_gc_cb(uv_handle_t * handle)107 static void luv_gc_cb(uv_handle_t* handle) {
108 luv_close_cb(handle);
109 luv_handle_free(handle);
110 }
111
luv_handle_gc(lua_State * L)112 static int luv_handle_gc(lua_State* L) {
113 uv_handle_t** udata = (uv_handle_t**)lua_touserdata(L, 1);
114 uv_handle_t* handle = *udata;
115
116 // Only cleanup if the handle hasn't been cleaned up yet.
117 if (handle) {
118 if (!uv_is_closing(handle)) {
119 // If the handle is not closed yet, close it first before freeing memory.
120 uv_close(handle, luv_gc_cb);
121 }
122 else {
123 // Otherwise, free the memory right away.
124 luv_handle_free(handle);
125 }
126 // Mark as cleaned up by wiping the dangling pointer.
127 *udata = NULL;
128 }
129
130 return 0;
131 }
132
luv_ref(lua_State * L)133 static int luv_ref(lua_State* L) {
134 uv_handle_t* handle = luv_check_handle(L, 1);
135 uv_ref(handle);
136 return 0;
137 }
138
luv_unref(lua_State * L)139 static int luv_unref(lua_State* L) {
140 uv_handle_t* handle = luv_check_handle(L, 1);
141 uv_unref(handle);
142 return 0;
143 }
144
luv_has_ref(lua_State * L)145 static int luv_has_ref(lua_State* L) {
146 uv_handle_t* handle = luv_check_handle(L, 1);
147 int ret = uv_has_ref(handle);
148 if (ret < 0) return luv_error(L, ret);
149 lua_pushboolean(L, ret);
150 return 1;
151 }
152
luv_send_buffer_size(lua_State * L)153 static int luv_send_buffer_size(lua_State* L) {
154 uv_handle_t* handle = luv_check_handle(L, 1);
155 int value = luaL_optinteger(L, 2, 0);
156 int ret;
157 if (value == 0) { // get
158 ret = uv_send_buffer_size(handle, &value);
159 if (ret < 0) return luv_error(L, ret);
160 lua_pushinteger(L, value);
161 return 1;
162 } else { // set
163 ret = uv_send_buffer_size(handle, &value);
164 return luv_result(L, ret);
165 }
166 }
167
luv_recv_buffer_size(lua_State * L)168 static int luv_recv_buffer_size(lua_State* L) {
169 uv_handle_t* handle = luv_check_handle(L, 1);
170 int value = luaL_optinteger(L, 2, 0);
171 int ret;
172 if (value == 0) { // get
173 ret = uv_recv_buffer_size(handle, &value);
174 if (ret < 0) return luv_error(L, ret);
175 lua_pushinteger(L, value);
176 return 1;
177 } else { // set
178 ret = uv_recv_buffer_size(handle, &value);
179 return luv_result(L, ret);
180 }
181 }
182
luv_fileno(lua_State * L)183 static int luv_fileno(lua_State* L) {
184 uv_handle_t* handle = luv_check_handle(L, 1);
185 uv_os_fd_t fd;
186 int ret = uv_fileno(handle, &fd);
187 if (ret < 0) return luv_error(L, ret);
188 lua_pushinteger(L, (LUA_INTEGER)(ptrdiff_t)fd);
189 return 1;
190 }
191
192 #if LUV_UV_VERSION_GEQ(1, 19, 0)
luv_handle_get_type(lua_State * L)193 static int luv_handle_get_type(lua_State* L) {
194 uv_handle_t* handle = luv_check_handle(L, 1);
195 uv_handle_type type = uv_handle_get_type(handle);
196 const char* type_name = uv_handle_type_name(type);
197 lua_pushstring(L, type_name);
198 lua_pushinteger(L, type);
199 return 2;
200 }
201 #endif
202