Added some basic asyncio stuff. (0.39.12)
This commit is contained in:
parent
6a16e5fb45
commit
cb3eb0a498
9 changed files with 328 additions and 1 deletions
50
src/vm/os/fd_poll_read.c
Normal file
50
src/vm/os/fd_poll_read.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-26
|
||||
*/
|
||||
|
||||
case OP_FD_POLL_READ: {
|
||||
/* Pops timeout_ms:int, fd:int; pushes 1 if readable, 0 on timeout/EOF, -1 on error */
|
||||
Value to = pop_value(vm);
|
||||
Value fdv = pop_value(vm);
|
||||
int rc = -1;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT || to.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: fd_poll_read expects (int fd, int timeout_ms)\n");
|
||||
rc = -1;
|
||||
} else {
|
||||
int fd = (int)fdv.i;
|
||||
int timeout_ms = (int)to.i;
|
||||
struct pollfd pfd;
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
pfd.revents = 0;
|
||||
int pr = poll(&pfd, 1, timeout_ms);
|
||||
if (pr > 0) {
|
||||
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
rc = 0; /* treat as not readable / peer closed */
|
||||
} else if (pfd.revents & POLLIN) {
|
||||
rc = 1;
|
||||
} else {
|
||||
rc = 0;
|
||||
}
|
||||
} else if (pr == 0) {
|
||||
rc = 0; /* timeout */
|
||||
} else {
|
||||
rc = -1; /* error */
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)to; (void)fdv;
|
||||
#endif
|
||||
free_value(to);
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(rc));
|
||||
break;
|
||||
}
|
||||
50
src/vm/os/fd_poll_write.c
Normal file
50
src/vm/os/fd_poll_write.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-26
|
||||
*/
|
||||
|
||||
case OP_FD_POLL_WRITE: {
|
||||
/* Pops timeout_ms:int, fd:int; pushes 1 if writable, 0 on timeout, -1 on error */
|
||||
Value to = pop_value(vm);
|
||||
Value fdv = pop_value(vm);
|
||||
int rc = -1;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT || to.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: fd_poll_write expects (int fd, int timeout_ms)\n");
|
||||
rc = -1;
|
||||
} else {
|
||||
int fd = (int)fdv.i;
|
||||
int timeout_ms = (int)to.i;
|
||||
struct pollfd pfd;
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLOUT;
|
||||
pfd.revents = 0;
|
||||
int pr = poll(&pfd, 1, timeout_ms);
|
||||
if (pr > 0) {
|
||||
if (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||
rc = 0; /* treat as not writable */
|
||||
} else if (pfd.revents & POLLOUT) {
|
||||
rc = 1;
|
||||
} else {
|
||||
rc = 0;
|
||||
}
|
||||
} else if (pr == 0) {
|
||||
rc = 0; /* timeout */
|
||||
} else {
|
||||
rc = -1; /* error */
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)to; (void)fdv;
|
||||
#endif
|
||||
free_value(to);
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(rc));
|
||||
break;
|
||||
}
|
||||
40
src/vm/os/fd_set_nonblock.c
Normal file
40
src/vm/os/fd_set_nonblock.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-03-26
|
||||
*/
|
||||
|
||||
case OP_FD_SET_NONBLOCK: {
|
||||
/* Pops on:int (0/1), fd:int; pushes 1 on success, 0 on error/unsupported */
|
||||
Value onv = pop_value(vm);
|
||||
Value fdv = pop_value(vm);
|
||||
int ok = 0;
|
||||
#ifdef __unix__
|
||||
if (fdv.type != VAL_INT || onv.type != VAL_INT) {
|
||||
fprintf(stderr, "Runtime type error: fd_set_nonblock expects (int fd, int on)\n");
|
||||
ok = 0;
|
||||
} else {
|
||||
int fd = (int)fdv.i;
|
||||
int on = (int)onv.i;
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0) {
|
||||
if (on)
|
||||
flags |= O_NONBLOCK;
|
||||
else
|
||||
flags &= ~O_NONBLOCK;
|
||||
if (fcntl(fd, F_SETFL, flags) == 0) ok = 1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
(void)onv; (void)fdv;
|
||||
#endif
|
||||
free_value(onv);
|
||||
free_value(fdv);
|
||||
push_value(vm, make_int(ok ? 1 : 0));
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue