1
0
mirror of https://github.com/janet-lang/janet synced 2024-10-18 16:05:47 +00:00

Expose janet_channel_make and janet_channel_make_threaded

This commit is contained in:
Calvin Rose 2024-08-14 17:34:48 -05:00
parent 7ff545bd2e
commit af2eb06298
5 changed files with 28 additions and 4 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Add experimental `filewatch/` module for listening to file system changes.
- Add `bundle/who-is` to query which bundle a file on disk was installed by. - Add `bundle/who-is` to query which bundle a file on disk was installed by.
- Add `geomean` function - Add `geomean` function
- Add `:R` and `:W` flags to `os/pipe` to create blocking pipes on Posix and Windows systems. - Add `:R` and `:W` flags to `os/pipe` to create blocking pipes on Posix and Windows systems.

View File

@ -4387,10 +4387,11 @@
`Shorthand for adding scripts during an install. Scripts will be installed to `Shorthand for adding scripts during an install. Scripts will be installed to
(string (dyn *syspath*) "/bin") by default and will be set to be executable.` (string (dyn *syspath*) "/bin") by default and will be set to be executable.`
[manifest src &opt dest chmod-mode] [manifest src &opt dest chmod-mode]
(default dest (string "bin" (sep) (->> src (string/split "/") last))) (default dest (->> src (string/split "/") last))
(def bin-dest (string "bin" (sep) dest))
(default chmod-mode 8r755) (default chmod-mode 8r755)
(os/mkdir (string (dyn *syspath*) (sep) "bin")) (os/mkdir (string (dyn *syspath*) (sep) "bin"))
(bundle/add-file manifest src dest chmod-mode)) (bundle/add-file manifest src bin-dest chmod-mode))
(defn bundle/update-all (defn bundle/update-all
"Reinstall all bundles" "Reinstall all bundles"

View File

@ -988,6 +988,20 @@ int janet_channel_take(JanetChannel *channel, Janet *out) {
return janet_channel_pop(channel, out, 2); return janet_channel_pop(channel, out, 2);
} }
JanetChannel *janet_channel_make(uint32_t limit) {
janet_assert(limit <= INT32_MAX, "bad limit");
JanetChannel *channel = janet_abstract(&janet_channel_type, sizeof(JanetChannel));
janet_chan_init(channel, (int32_t) limit, 0);
return channel;
}
JanetChannel *janet_channel_make_threaded(uint32_t limit) {
janet_assert(limit <= INT32_MAX, "bad limit");
JanetChannel *channel = janet_abstract_threaded(&janet_channel_type, sizeof(JanetChannel));
janet_chan_init(channel, (int32_t) limit, 0);
return channel;
}
/* Channel Methods */ /* Channel Methods */
JANET_CORE_FN(cfun_channel_push, JANET_CORE_FN(cfun_channel_push,

View File

@ -89,6 +89,9 @@ static void janet_watcher_init(JanetWatcher *watcher, JanetChannel *channel, uin
do { do {
fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
} while (fd == -1 && errno == EINTR); } while (fd == -1 && errno == EINTR);
if (fd == -1) {
janet_panicv(janet_ev_lasterr());
}
watcher->watch_descriptors = janet_table(0); watcher->watch_descriptors = janet_table(0);
watcher->channel = channel; watcher->channel = channel;
watcher->default_flags = default_flags; watcher->default_flags = default_flags;
@ -101,6 +104,9 @@ static void janet_watcher_add(JanetWatcher *watcher, const char *path, uint32_t
do { do {
result = inotify_add_watch(watcher->stream->handle, path, flags); result = inotify_add_watch(watcher->stream->handle, path, flags);
} while (result == -1 && errno == EINTR); } while (result == -1 && errno == EINTR);
if (result == -1) {
janet_panicv(janet_ev_lasterr());
}
Janet name = janet_cstringv(path); Janet name = janet_cstringv(path);
Janet wd = janet_wrap_integer(result); Janet wd = janet_wrap_integer(result);
janet_table_put(watcher->watch_descriptors, name, wd); janet_table_put(watcher->watch_descriptors, name, wd);
@ -117,7 +123,7 @@ static void janet_watcher_remove(JanetWatcher *watcher, const char *path) {
result = inotify_rm_watch(watcher->stream->handle, watch_handle); result = inotify_rm_watch(watcher->stream->handle, watch_handle);
} while (result != -1 && errno == EINTR); } while (result != -1 && errno == EINTR);
if (result == -1) { if (result == -1) {
janet_panicf("%s", janet_strerror(errno)); janet_panicv(janet_ev_lasterr());
} }
} }
@ -277,7 +283,7 @@ static const JanetAbstractType janet_filewatch_at = {
JANET_CORE_FN(cfun_filewatch_make, JANET_CORE_FN(cfun_filewatch_make,
"(filewatch/make channel &opt default-flags)", "(filewatch/make channel &opt default-flags)",
"Create a new filewatcher.") { "Create a new filewatcher that will give events to a channel channel.") {
janet_arity(argc, 1, -1); janet_arity(argc, 1, -1);
JanetChannel *channel = janet_getchannel(argv, 0); JanetChannel *channel = janet_getchannel(argv, 0);
JanetWatcher *watcher = janet_abstract(&janet_filewatch_at, sizeof(JanetWatcher)); JanetWatcher *watcher = janet_abstract(&janet_filewatch_at, sizeof(JanetWatcher));

View File

@ -1450,6 +1450,8 @@ JANET_API int32_t janet_abstract_incref(void *abst);
JANET_API int32_t janet_abstract_decref(void *abst); JANET_API int32_t janet_abstract_decref(void *abst);
/* Expose channel utilities */ /* Expose channel utilities */
JanetChannel *janet_channel_make(uint32_t limit);
JanetChannel *janet_channel_make_threaded(uint32_t limit);
JanetChannel *janet_getchannel(const Janet *argv, int32_t n); JanetChannel *janet_getchannel(const Janet *argv, int32_t n);
JanetChannel *janet_optchannel(const Janet *argv, int32_t argc, int32_t n, JanetChannel *dflt); JanetChannel *janet_optchannel(const Janet *argv, int32_t argc, int32_t n, JanetChannel *dflt);
JANET_API int janet_channel_give(JanetChannel *channel, Janet x); JANET_API int janet_channel_give(JanetChannel *channel, Janet x);