1
0
mirror of https://github.com/janet-lang/janet synced 2026-07-05 02:42:42 +00:00

expose os/isatty, fix floating point math

This commit is contained in:
Noam Preil
2025-12-10 23:35:24 +00:00
parent 2f69678f2f
commit 98bbe9f474
4 changed files with 36 additions and 8 deletions
+4 -2
View File
@@ -51,9 +51,11 @@ int system_test() {
assert(janet_equals(janet_wrap_number(1.4), janet_wrap_number(1.4)));
assert(janet_equals(janet_wrap_number(3.14159265), janet_wrap_number(3.14159265)));
#ifdef NAN
#ifndef JANET_PLAN9
assert(janet_checktype(janet_wrap_number(NAN), JANET_NUMBER));
#ifdef JANET_PLAN9
// Plan 9 traps NaNs by default; disable that.
setfcr(0);
#endif
assert(janet_checktype(janet_wrap_number(NAN), JANET_NUMBER));
#else
assert(janet_checktype(janet_wrap_number(0.0 / 0.0), JANET_NUMBER));
#endif
+22 -2
View File
@@ -29,6 +29,11 @@
#include <stdlib.h>
#ifdef JANET_PLAN9
#include <unistd.h>
#endif
#ifndef JANET_REDUCED_OS
#include <time.h>
@@ -288,7 +293,20 @@ JANET_CORE_FN(os_exit,
return janet_wrap_nil();
}
#ifndef JANET_REDUCED_OS
#ifdef JANET_PLAN9
JANET_CORE_FN(os_isatty,
"(os/isatty &opt file)",
"Returns true if `file` is a terminal. If `file` is not specified, "
"it will default to standard output.") {
janet_arity(argc, 0, 1);
FILE *f = (argc == 1) ? janet_getfile(argv, 0, NULL) : stdout;
int fd = fileno(f);
if (fd == -1) janet_panic(janet_strerror(errno));
return janet_wrap_boolean(isatty(fd));
}
#elif !defined(JANET_REDUCED_OS)
JANET_CORE_FN(os_cpu_count,
"(os/cpu-count &opt dflt)",
@@ -2847,7 +2865,9 @@ void janet_lib_os(JanetTable *env) {
JANET_CORE_REG("os/which", os_which),
JANET_CORE_REG("os/arch", os_arch),
JANET_CORE_REG("os/compiler", os_compiler),
#ifndef JANET_REDUCED_OS
#ifdef JANET_PLAN9
JANET_CORE_REG("os/isatty", os_isatty),
#elif !defined(JANET_REDUCED_OS)
/* misc (un-sandboxed) */
JANET_CORE_REG("os/cpu-count", os_cpu_count),
-4
View File
@@ -305,10 +305,6 @@ extern "C" {
#define JANET_STACK_MAX 0x7fffffff
#endif
#ifdef JANET_PLAN9
#undef NAN
#endif
/* Use nanboxed values - uses 8 bytes per value instead of 12 or 16.
* To turn of nanboxing, for debugging purposes or for certain
* architectures (Nanboxing only tested on x86 and x64), comment out
+10
View File
@@ -309,7 +309,9 @@ static int curpos(void) {
char buf[32];
int cols, rows;
unsigned int i = 0;
#ifndef JANET_PLAN9
if (write_console("\x1b[6n", 4) != 4) return -1;
#endif
while (i < sizeof(buf) - 1) {
if (read_console(buf + i, 1) != 1) break;
if (buf[i] == 'R') break;
@@ -327,6 +329,10 @@ static int getcols(void) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return (int)(csbi.srWindow.Right - csbi.srWindow.Left + 1);
#elif defined(JANET_PLAN9)
//FIXME(noam): this can probably read /dev/window to get window size, and
//divide by the active font width, since plan9 requires fixed-width glyphs.
return 80;
#else
struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
@@ -1133,6 +1139,10 @@ int main(int argc, char **argv) {
JanetArray *args;
JanetTable *env;
#ifdef JANET_PLAN9
setfcr(0);
#endif
#ifdef _WIN32
setup_console_output();
#endif