Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
fd_poll_write.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 2026 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
10/**
11 * @file fd_poll_write.c
12 * @brief Implements OP_FD_POLL_WRITE to check if a file descriptor is writable.
13 *
14 * Behavior:
15 * - Pops timeout_ms (int) and fd (int); waits up to timeout for writability; pushes 1 if writable, 0 otherwise.
16 * - On non-UNIX platforms, returns 0 (unsupported).
17 *
18 * Errors:
19 * - If types are wrong, prints an error and returns 0.
20 */
21
23 /* Pops timeout_ms:int, fd:int; pushes 1 if writable, 0 on timeout, -1 on error */
24 Value to = pop_value(vm);
26 int rc = -1;
27#ifdef __unix__
28 if (fdv.type != VAL_INT || to.type != VAL_INT) {
29 fprintf(stderr, "Runtime type error: fd_poll_write expects (int fd, int timeout_ms)\n");
30 rc = -1;
31 } else {
32 int fd = (int)fdv.i;
33 int timeout_ms = (int)to.i;
34 struct pollfd pfd;
35 pfd.fd = fd;
36 pfd.events = POLLOUT;
37 pfd.revents = 0;
38 int pr = poll(&pfd, 1, timeout_ms);
39 if (pr > 0) {
40 if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
41 rc = 0; /* treat as not writable */
42 } else if (pfd.revents & POLLOUT) {
43 rc = 1;
44 } else {
45 rc = 0;
46 }
47 } else if (pr == 0) {
48 rc = 0; /* timeout */
49 } else {
50 rc = -1; /* error */
51 }
52 }
53#else
54 (void)to; (void)fdv;
55#endif
59 break;
60}
@ OP_FD_POLL_WRITE
Definition bytecode.h:232
CURLcode rc
Definition download.c:71
void to
Value fdv
free_value(to)
push_value(vm, make_int(rc))
int fd
Definition serial_open.c:92
Tagged union representing a Fun value.
Definition value.h:68
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
#define fprintf
Definition vm.c:200