From 7ff545bd2e47418157671f07b31abc63666df9bf Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sun, 11 Aug 2024 18:43:10 -0500 Subject: [PATCH] Stop out better configuration. --- meson.build | 1 + meson_options.txt | 1 + src/conf/janetconf.h | 1 + src/core/corelib.c | 5 ++++- src/core/ev.c | 2 -- src/core/filewatch.c | 48 ++++++++++++++++++++++++++++++++++++++++---- src/core/util.h | 5 ++--- src/include/janet.h | 5 +++++ 8 files changed, 58 insertions(+), 10 deletions(-) diff --git a/meson.build b/meson.build index 276b2904..fd76ed05 100644 --- a/meson.build +++ b/meson.build @@ -79,6 +79,7 @@ conf.set('JANET_EV_NO_KQUEUE', not get_option('kqueue')) conf.set('JANET_NO_INTERPRETER_INTERRUPT', not get_option('interpreter_interrupt')) conf.set('JANET_NO_FFI', not get_option('ffi')) conf.set('JANET_NO_FFI_JIT', not get_option('ffi_jit')) +conf.set('JANET_NO_FILEWATCH', not get_option('filewatch')) conf.set('JANET_NO_CRYPTORAND', not get_option('cryptorand')) if get_option('os_name') != '' conf.set('JANET_OS_NAME', get_option('os_name')) diff --git a/meson_options.txt b/meson_options.txt index f6730593..7b9b33af 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -22,6 +22,7 @@ option('kqueue', type : 'boolean', value : true) option('interpreter_interrupt', type : 'boolean', value : true) option('ffi', type : 'boolean', value : true) option('ffi_jit', type : 'boolean', value : true) +option('filewatch', type : 'boolean', value : true) option('recursion_guard', type : 'integer', min : 10, max : 8000, value : 1024) option('max_proto_depth', type : 'integer', min : 10, max : 8000, value : 200) diff --git a/src/conf/janetconf.h b/src/conf/janetconf.h index 92dc56cb..ad9d27c7 100644 --- a/src/conf/janetconf.h +++ b/src/conf/janetconf.h @@ -29,6 +29,7 @@ /* #define JANET_NO_NET */ /* #define JANET_NO_INT_TYPES */ /* #define JANET_NO_EV */ +/* #define JANET_NO_FILEWATCH */ /* #define JANET_NO_REALPATH */ /* #define JANET_NO_SYMLINKS */ /* #define JANET_NO_UMASK */ diff --git a/src/core/corelib.c b/src/core/corelib.c index a59013a9..da1dfc5c 100644 --- a/src/core/corelib.c +++ b/src/core/corelib.c @@ -456,7 +456,7 @@ JANET_CORE_FN(janet_core_range, } JanetArray *array = janet_array(int_count); for (int32_t i = 0; i < int_count; i++) { - array->data[i] = janet_wrap_number(start + (double) i * step); + array->data[i] = janet_wrap_number((double) start + (double) i * step); } array->count = int_count; return janet_wrap_array(array); @@ -1127,6 +1127,9 @@ static void janet_load_libs(JanetTable *env) { #endif #ifdef JANET_EV janet_lib_ev(env); +#ifdef JANET_FILEWATCH + janet_lib_filewatch(env); +#endif #endif #ifdef JANET_NET janet_lib_net(env); diff --git a/src/core/ev.c b/src/core/ev.c index 5c885893..9a13b583 100644 --- a/src/core/ev.c +++ b/src/core/ev.c @@ -3287,8 +3287,6 @@ void janet_lib_ev(JanetTable *env) { janet_register_abstract_type(&janet_channel_type); janet_register_abstract_type(&janet_mutex_type); janet_register_abstract_type(&janet_rwlock_type); - - janet_lib_filewatch(env); } #endif diff --git a/src/core/filewatch.c b/src/core/filewatch.c index 7dd13ccf..d90394e4 100644 --- a/src/core/filewatch.c +++ b/src/core/filewatch.c @@ -27,6 +27,7 @@ #endif #ifdef JANET_EV +#ifdef JANET_FILEWATCH typedef struct { JanetTable *watch_descriptors; @@ -36,9 +37,9 @@ typedef struct { } JanetWatcher; #ifdef JANET_LINUX + #include #include -#endif typedef struct { const char *name; @@ -63,7 +64,8 @@ static const JanetWatchFlagName watcher_flags_linux[] = { {"q-overflow", IN_Q_OVERFLOW}, {"unmount", IN_UNMOUNT}, }; -static uint32_t decode_inotify_flags(const Janet *options, int32_t n) { + +static uint32_t decode_watch_flags(const Janet *options, int32_t n) { uint32_t flags = 0; for (int32_t i = 0; i < n; i++) { if (!(janet_checktype(options[i], JANET_KEYWORD))) { @@ -218,6 +220,43 @@ static void janet_watcher_listen(JanetWatcher *watcher) { janet_async_start(watcher->stream, JANET_ASYNC_LISTEN_READ, watcher_callback_read, watcher); } +#else + +/* Default implementation */ + +static uint32_t decode_watch_flags(const Janet *options, int32_t n) { + (void) options; + (void) n; + return 0; +} + +static void janet_watcher_init(JanetWatcher *watcher, JanetChannel *channel, uint32_t default_flags) { + (void) watcher; + (void) channel; + (void) default_flags; + janet_panic("nyi"); +} + +static void janet_watcher_add(JanetWatcher *watcher, const char *path, uint32_t flags) { + (void) watcher; + (void) flags; + (void) path; + janet_panic("nyi"); +} + +static void janet_watcher_remove(JanetWatcher *watcher, const char *path) { + (void) watcher; + (void) path; + janet_panic("nyi"); +} + +static void janet_watcher_listen(JanetWatcher *watcher) { + (void) watcher; + janet_panic("nyi"); +} + +#endif + /* C Functions */ static int janet_filewatch_mark(void *p, size_t s) { @@ -242,7 +281,7 @@ JANET_CORE_FN(cfun_filewatch_make, janet_arity(argc, 1, -1); JanetChannel *channel = janet_getchannel(argv, 0); JanetWatcher *watcher = janet_abstract(&janet_filewatch_at, sizeof(JanetWatcher)); - uint32_t default_flags = decode_inotify_flags(argv + 1, argc - 1); + uint32_t default_flags = decode_watch_flags(argv + 1, argc - 1); janet_watcher_init(watcher, channel, default_flags); return janet_wrap_abstract(watcher); } @@ -253,7 +292,7 @@ JANET_CORE_FN(cfun_filewatch_add, janet_arity(argc, 2, -1); JanetWatcher *watcher = janet_getabstract(argv, 0, &janet_filewatch_at); const char *path = janet_getcstring(argv, 1); - uint32_t flags = watcher->default_flags | decode_inotify_flags(argv + 2, argc - 2); + uint32_t flags = watcher->default_flags | decode_watch_flags(argv + 2, argc - 2); janet_watcher_add(watcher, path, flags); return argv[0]; } @@ -290,3 +329,4 @@ void janet_lib_filewatch(JanetTable *env) { } #endif +#endif diff --git a/src/core/util.h b/src/core/util.h index ecdcdc5a..be4b6466 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -190,9 +190,6 @@ void janet_lib_debug(JanetTable *env); #ifdef JANET_PEG void janet_lib_peg(JanetTable *env); #endif -#ifdef JANET_TYPED_ARRAY -void janet_lib_typed_array(JanetTable *env); -#endif #ifdef JANET_INT_TYPES void janet_lib_inttypes(JanetTable *env); #endif @@ -204,8 +201,10 @@ extern const JanetAbstractType janet_address_type; void janet_lib_ev(JanetTable *env); void janet_ev_mark(void); int janet_make_pipe(JanetHandle handles[2], int mode); +#ifdef JANET_FILEWATCH void janet_lib_filewatch(JanetTable *env); #endif +#endif #ifdef JANET_FFI void janet_lib_ffi(JanetTable *env); #endif diff --git a/src/include/janet.h b/src/include/janet.h index 41d10c88..db31478c 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -210,6 +210,11 @@ extern "C" { #define JANET_EV #endif +/* Enable or disable the filewatch/ module */ +#if !defined(JANET_NO_FILEWATCH) +#define JANET_FILEWATCH +#endif + /* Enable or disable networking */ #if defined(JANET_EV) && !defined(JANET_NO_NET) && !defined(__EMSCRIPTEN__) #define JANET_NET