Always fetch the promise JSValue and ID when we allocate one. Make it impossible that we've freed it before we return it. Hopefully fixes leaks. Definitely not worse for performance.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3758 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2022-01-14 03:05:37 +00:00
parent 27c2f27708
commit 0bcc7d8c59
5 changed files with 43 additions and 44 deletions

View File

@ -259,8 +259,7 @@ JSValue _socket_startTls(JSContext* context, JSValueConst this_val, int argc, JS
{
tf_tls_session_start_connect(socket->_tls);
}
socket->_startTlsPromise = tf_task_allocate_promise(socket->_task);
JSValue result = tf_task_get_promise(socket->_task, socket->_startTlsPromise);
JSValue result = tf_task_allocate_promise(socket->_task, &socket->_startTlsPromise);
_socket_processOutgoingTls(socket);
return result;
}
@ -349,8 +348,7 @@ JSValue _socket_bind(JSContext* context, JSValueConst this_val, int argc, JSValu
};
data->resolver.data = data;
data->socket = socket;
data->promise = tf_task_allocate_promise(socket->_task);
JSValue promise = tf_task_allocate_promise(socket->_task, &data->promise);
int result = uv_getaddrinfo(tf_task_get_loop(socket->_task), &data->resolver, _socket_onResolvedForBind, node, port, &hints);
if (result != 0)
{
@ -359,7 +357,7 @@ JSValue _socket_bind(JSContext* context, JSValueConst this_val, int argc, JSValu
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(tf_task_get_context(socket->_task), error));
free(data);
}
return tf_task_get_promise(socket->_task, data->promise);
return promise;
}
void _socket_onResolvedForBind(uv_getaddrinfo_t* resolver, int status, struct addrinfo* result)
@ -397,8 +395,6 @@ JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSV
strncpy(socket->_peerName, node, sizeof(socket->_peerName) - 1);
promiseid_t promise = tf_task_allocate_promise(socket->_task);
socket_resolve_data_t* data = malloc(sizeof(socket_resolve_data_t));
memset(data, 0, sizeof(*data));
struct addrinfo hints = {
@ -408,20 +404,19 @@ JSValue _socket_connect(JSContext* context, JSValueConst this_val, int argc, JSV
};
data->resolver.data = data;
data->socket = socket;
data->promise = promise;
JSValue promise = tf_task_allocate_promise(socket->_task, &data->promise);
int result = uv_getaddrinfo(tf_task_get_loop(socket->_task), &data->resolver, _socket_onResolvedForConnect, node, port, &hints);
if (result != 0)
{
char error[256];
snprintf(error, sizeof(error), "uv_getaddrinfo: %s", uv_strerror(result));
tf_task_reject_promise(socket->_task, promise, JS_ThrowInternalError(context, "%s", error));
tf_task_reject_promise(socket->_task, data->promise, JS_ThrowInternalError(context, "%s", error));
free(data);
}
JS_FreeCString(context, node);
JS_FreeCString(context, port);
return tf_task_get_promise(socket->_task, promise);
return promise;
}
void _socket_onResolvedForConnect(uv_getaddrinfo_t* resolver, int status, struct addrinfo* result)
@ -515,8 +510,8 @@ JSValue _socket_accept(JSContext* context, JSValueConst this_val, int argc, JSVa
socket_t* client = _socket_create_internal(context);
client->_direction = kAccept;
promiseid_t promise = tf_task_allocate_promise(socket->_task);
JSValue result = tf_task_get_promise(socket->_task, promise);
promiseid_t promise;
JSValue result = tf_task_allocate_promise(socket->_task, &promise);
int status = uv_accept((uv_stream_t*)&socket->_socket, (uv_stream_t*)&client->_socket);
if (status == 0)
{
@ -537,8 +532,7 @@ JSValue _socket_close(JSContext* context, JSValueConst this_val, int argc, JSVal
socket_t* socket = JS_GetOpaque(this_val, _classId);
if (socket->_closePromise == -1)
{
socket->_closePromise = tf_task_allocate_promise(socket->_task);
JSValue result = tf_task_get_promise(socket->_task, socket->_closePromise);
JSValue result = tf_task_allocate_promise(socket->_task, &socket->_closePromise);
_socket_close_internal(socket);
return result;
}
@ -548,8 +542,8 @@ JSValue _socket_close(JSContext* context, JSValueConst this_val, int argc, JSVal
JSValue _socket_shutdown(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
socket_t* socket = JS_GetOpaque(this_val, _classId);
promiseid_t promise = tf_task_allocate_promise(socket->_task);
JSValue result = tf_task_get_promise(socket->_task, promise);
promiseid_t promise = -1;
JSValue result = tf_task_allocate_promise(socket->_task, &promise);
if (socket->_tls)
{
_socket_processTlsShutdown(socket, promise);
@ -610,8 +604,8 @@ JSValue _socket_read(JSContext* context, JSValueConst this_val, int argc, JSValu
socket_t* socket = JS_GetOpaque(this_val, _classId);
socket->_onRead = JS_DupValue(context, argv[0]);
int result = uv_read_start((uv_stream_t*)&socket->_socket, _socket_allocateBuffer, _socket_onRead);
promiseid_t promise = tf_task_allocate_promise(socket->_task);
JSValue read_result = tf_task_get_promise(socket->_task, promise);
promiseid_t promise = -1;
JSValue read_result = tf_task_allocate_promise(socket->_task, &promise);
if (result != 0)
{
char error[256];
@ -822,8 +816,8 @@ static int _socket_write_tls(socket_t* socket, promiseid_t promise, const char*
JSValue _socket_write(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
socket_t* socket = JS_GetOpaque(this_val, _classId);
promiseid_t promise = tf_task_allocate_promise(socket->_task);
JSValue write_result = tf_task_get_promise(socket->_task, promise);
promiseid_t promise = -1;
JSValue write_result = tf_task_allocate_promise(socket->_task, &promise);
if (!JS_IsUndefined(argv[0]))
{
if (socket->_tls)