Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
fd_set_nonblock.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_set_nonblock.c
12 * @brief Implements OP_FD_SET_NONBLOCK to toggle O_NONBLOCK on a file descriptor.
13 *
14 * Behavior:
15 * - Pops on (int, 0/1) and fd (int); sets or clears O_NONBLOCK via fcntl; pushes 1 on success, 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 on:int (0/1), fd:int; pushes 1 on success, 0 on error/unsupported */
24 Value onv = pop_value(vm);
26 int ok = 0;
27#ifdef __unix__
28 if (fdv.type != VAL_INT || onv.type != VAL_INT) {
29 fprintf(stderr, "Runtime type error: fd_set_nonblock expects (int fd, int on)\n");
30 ok = 0;
31 } else {
32 int fd = (int)fdv.i;
33 int on = (int)onv.i;
34 int flags = fcntl(fd, F_GETFL, 0);
35 if (flags >= 0) {
36 if (on)
37 flags |= O_NONBLOCK;
38 else
39 flags &= ~O_NONBLOCK;
40 if (fcntl(fd, F_SETFL, flags) == 0) ok = 1;
41 }
42 }
43#else
44 (void)onv; (void)fdv;
45#endif
48 push_value(vm, make_int(ok ? 1 : 0));
49 break;
50}
@ OP_FD_SET_NONBLOCK
Definition bytecode.h:230
int ok
Definition contains.c:38
Value fdv
push_value(vm, make_int(ok ? 1 :0))
free_value(onv)
void onv
int fd
Definition serial_open.c:92
int flags
Definition stringify.c:38
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