Fun
0.41.5
The programming language that makes You have fun
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
os
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
22
case
OP_SOCK_RECV
: {
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);
31
free_value
(
fdv
);
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);
51
free_value
(
fdv
);
52
Value
s
=
make_string
(
out
?
out
:
""
);
53
if
(
out
)
free
(
out
);
54
push_value(vm,
s
);
55
break
;
56
}
out
Value out
Definition
apop.c:38
OP_SOCK_RECV
@ OP_SOCK_RECV
Definition
bytecode.h:224
fdv
Value fdv
Definition
fd_poll_read.c:25
n
int n
Definition
insert.c:41
free
free(vals)
s
uint32_t s
Definition
rol.c:31
fd
int fd
Definition
serial_open.c:92
free_value
free_value(maxv)
Value
Tagged union representing a Fun value.
Definition
value.h:68
Value::i
int64_t i
Definition
value.h:71
Value::type
ValueType type
Definition
value.h:69
make_string
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition
value.c:95
VAL_INT
@ VAL_INT
Definition
value.h:51
fprintf
#define fprintf
Definition
vm.c:200
Generated on
for Fun by
1.16.1