Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
socket_recv.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
21
23 /* Pops maxlen, fd; pushes data string ("" on EOF/error) */
24 Value maxv = pop_value(vm);
25 Value fdv = pop_value(vm);
26 char *out = NULL;
27#ifdef __unix__
28 if (fdv.type != VAL_INT || maxv.type != VAL_INT) {
29 fprintf(stderr, "Runtime type error: sock_recv expects (int fd, int maxlen)\n");
30 free_value(maxv);
32 push_value(vm, make_string(""));
33 break;
34 }
35 int fd = (int)fdv.i;
36 int maxlen = (int)maxv.i;
37 if (maxlen <= 0) maxlen = 4096;
38 if (maxlen > 1 << 20) maxlen = 1 << 20; /* cap at 1MB */
39 out = (char *)malloc((size_t)maxlen + 1);
40 if (out) {
41 ssize_t n = recv(fd, out, (size_t)maxlen, 0);
42 if (n <= 0) {
43 free(out);
44 out = NULL;
45 } else {
46 out[n] = '\0';
47 }
48 }
49#endif
50 free_value(maxv);
53 if (out) free(out);
54 push_value(vm, s);
55 break;
56}
Value out
Definition apop.c:38
@ OP_SOCK_RECV
Definition bytecode.h:224
Value fdv
int n
Definition insert.c:41
free(vals)
uint32_t s
Definition rol.c:31
int fd
Definition serial_open.c:92
free_value(maxv)
Tagged union representing a Fun value.
Definition value.h:68
int64_t i
Definition value.h:71
ValueType type
Definition value.h:69
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
@ VAL_INT
Definition value.h:51
#define fprintf
Definition vm.c:200