Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ev/to-file for synchronous resource operations #1533

Merged
merged 7 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Raise error if using ev/to-file on MinGW
  • Loading branch information
pyrmont committed Dec 15, 2024
commit 1a24d4fc86617de3faa3bd920d31931da9e0f877
19 changes: 9 additions & 10 deletions src/core/ev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3309,10 +3309,7 @@ static JanetFile *get_file_for_stream(JanetStream *stream) {
FILE *f = _fdopen(fd_dup, fmt);
if (NULL == f) {
/* the stream isn't duplicated so this would close the stream
/* _close(fd); */
return NULL;
}
if (setvbuf(f, NULL, _IONBF, 0)) {
* _close(fd); */
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
#else
Expand All @@ -3323,24 +3320,26 @@ static JanetFile *get_file_for_stream(JanetStream *stream) {
close(newHandle);
return NULL;
}
if (setvbuf(f, NULL, _IONBF, 0)) {
close(newHandle);
return NULL;
}
#endif
return janet_makejfile(f, flags);
}

JANET_CORE_FN(janet_cfun_to_file,
"(ev/to-file)",
"Create core/file copy of the stream. This value can be used "
"when blocking IO behavior is needed. Buffering is turned off "
"for the file.") {
"when blocking IO behavior is needed.") {
janet_fixarity(argc, 1);
#ifdef JANET_MINGW
(void) argc;
(void) argv;
janet_panic("ev/to-file not supported on MinGW");
return janet_wrap_nil();
#else
JanetStream *stream = janet_getabstract(argv, 0, &janet_stream_type);
JanetFile *iof = get_file_for_stream(stream);
if (iof == NULL) janet_panic("cannot make file from stream");
return janet_wrap_abstract(iof);
#endif
}

JANET_CORE_FN(janet_cfun_ev_all_tasks,
Expand Down
10 changes: 7 additions & 3 deletions test/suite-ev.janet
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,13 @@
# Now do our telnet chat
(def bob (net/connect test-host test-port :stream))
(expect-read bob "Whats your name?\n")
(def fbob (ev/to-file bob))
(file/write fbob "bob")
(:close fbob)
(if (= :mingw (os/which))
(net/write bob "bob")
(do
(def fbob (ev/to-file bob))
(file/write fbob "bob")
(file/flush fbob)
(:close fbob)))
(expect-read bob "Welcome bob\n")
(def alice (net/connect test-host test-port))
(expect-read alice "Whats your name?\n")
Expand Down
Loading