Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
socket_close.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
10/**
11 * @file socket_close.c
12 * @brief Implements OP_SOCK_CLOSE to close a socket file descriptor.
13 *
14 * Behavior:
15 * - Pops fd (int) and closes it; pushes 1 on success, 0 on failure.
16 * - On non-UNIX platforms, always pushes 0 (unsupported).
17 *
18 * Errors:
19 * - If argument type is wrong, prints an error and pushes 0.
20 */
21
23 /* Pops fd; returns 1/0 */
24 Value fdv = pop_value(vm);
25 int ok = 0;
26#ifdef __unix__
27 if (fdv.type != VAL_INT) {
28 fprintf(stderr, "Runtime type error: sock_close expects (int fd)\n");
30 push_value(vm, make_int(0));
31 break;
32 }
33 int fd = (int)fdv.i;
34 ok = (close(fd) == 0) ? 1 : 0;
35#endif
38 break;
39}
@ OP_SOCK_CLOSE
Definition bytecode.h:225
int ok
Definition contains.c:38
Value fdv
int fd
Definition serial_open.c:92
free_value(fdv)
push_value(vm, make_int(ok))
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