summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2024-04-25 00:36:19 +0200
committerJo-Philipp Wich <jo@mein.io>2024-04-25 00:36:19 +0200
commit953f36c96e8aad6627132d2ecb29e865162bfce5 (patch)
tree8dccc4f91378db96d940a7ea0a7996622d1ac96d /lib
parenta33d16a8649306a08fe21f3f693c55ad7cf97d1d (diff)
socket: make socket.send() accept non-string data
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib')
-rw-r--r--lib/socket.c17
1 files 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()");