From 953f36c96e8aad6627132d2ecb29e865162bfce5 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Thu, 25 Apr 2024 00:36:19 +0200 Subject: socket: make socket.send() accept non-string data Signed-off-by: Jo-Philipp Wich --- lib/socket.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/socket.c b/lib/socket.c index ed0e819..7f4dfe7 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -2431,8 +2431,10 @@ uc_socket_inst_connect(uc_vm_t *vm, size_t nargs) * * @function module:socket.socket#send * - * @param {string} data - * The data to be sent through the socket. + * @param {*} data + * The data to be sent through the socket. String data is sent as-is, any other + * type is implicitly converted to a string first before being sent on the + * socket. * * @param {number} [flags] * Optional flags that modify the behavior of the send operation. @@ -2469,11 +2471,12 @@ uc_socket_inst_send(uc_vm_t *vm, size_t nargs) struct sockaddr_storage ss = { 0 }; struct sockaddr *sa = NULL; socklen_t salen = 0; + char *buf = NULL; ssize_t ret; int sockfd; args_get(vm, nargs, &sockfd, - "data", UC_STRING, false, &data, + "data", UC_NULL, false, &data, "flags", UC_INTEGER, true, &flags, "address", UC_NULL, true, &addr); @@ -2484,10 +2487,16 @@ uc_socket_inst_send(uc_vm_t *vm, size_t nargs) sa = (struct sockaddr *)&ss; } + if (ucv_type(data) != UC_STRING) + buf = ucv_to_string(vm, data); + ret = sendto(sockfd, - ucv_string_get(data), ucv_string_length(data), + buf ? buf : ucv_string_get(data), + buf ? strlen(buf) : ucv_string_length(data), (flags ? ucv_int64_get(flags) : 0) | MSG_NOSIGNAL, sa, salen); + free(buf); + if (ret == -1) err_return(errno, "send()"); -- cgit v1.2.3