@@ -353,21 +353,22 @@ JanetStream *janet_stream(JanetHandle handle, uint32_t flags, const JanetMethod
353
353
354
354
static void janet_stream_close_impl (JanetStream * stream ) {
355
355
stream -> flags |= JANET_STREAM_CLOSED ;
356
+ int canclose = !(stream -> flags & JANET_STREAM_NOT_CLOSEABLE );
356
357
#ifdef JANET_WINDOWS
357
358
if (stream -> handle != INVALID_HANDLE_VALUE ) {
358
359
#ifdef JANET_NET
359
360
if (stream -> flags & JANET_STREAM_SOCKET ) {
360
- closesocket ((SOCKET ) stream -> handle );
361
+ if ( canclose ) closesocket ((SOCKET ) stream -> handle );
361
362
} else
362
363
#endif
363
364
{
364
- CloseHandle (stream -> handle );
365
+ if ( canclose ) CloseHandle (stream -> handle );
365
366
}
366
367
stream -> handle = INVALID_HANDLE_VALUE ;
367
368
}
368
369
#else
369
370
if (stream -> handle != -1 ) {
370
- close (stream -> handle );
371
+ if ( canclose ) close (stream -> handle );
371
372
stream -> handle = -1 ;
372
373
#ifdef JANET_EV_POLL
373
374
uint32_t i = stream -> index ;
@@ -3486,6 +3487,36 @@ JANET_CORE_FN(janet_cfun_ev_all_tasks,
3486
3487
return janet_wrap_array (array );
3487
3488
}
3488
3489
3490
+ JANET_CORE_FN (janet_cfun_to_stream ,
3491
+ "(ev/to-stream file)" ,
3492
+ "Convert a core/file to a core/stream. On POSIX operating systems, this will mark "
3493
+ "the underlying open file description as non-blocking." ) {
3494
+ janet_fixarity (argc , 1 );
3495
+ int32_t flags = 0 ;
3496
+ int32_t stream_flags = 0 ;
3497
+ FILE * file = janet_getfile (argv , 0 , & flags );
3498
+ if (flags & JANET_FILE_READ ) stream_flags |= JANET_STREAM_READABLE ;
3499
+ if (flags & JANET_FILE_WRITE ) stream_flags |= JANET_STREAM_WRITABLE ;
3500
+ if (flags & JANET_FILE_NOT_CLOSEABLE ) stream_flags |= JANET_STREAM_NOT_CLOSEABLE ;
3501
+ if (flags & JANET_FILE_CLOSED ) janet_panic ("file is closed" );
3502
+ #ifdef JANET_WINDOWS
3503
+ int fno = _fileno (file );
3504
+ int dupped_fno = _dup (fno );
3505
+ if (dupped_fno == -1 ) janet_panic (janet_strerror (errno ));
3506
+ JanetStream * stream = janet_stream (_get_osfhandle (dupped_fno ), stream_flags , NULL );
3507
+ #else
3508
+ int handle = fileno (file );
3509
+ int dupped_handle = 0 ;
3510
+ int status = 0 ;
3511
+ RETRY_EINTR (dupped_handle , dup (handle ));
3512
+ if (status == -1 ) janet_panic (janet_strerror (errno ));
3513
+ RETRY_EINTR (status , fcntl (dupped_handle , F_SETFL , O_NONBLOCK ));
3514
+ if (status == -1 ) janet_panic (janet_strerror (errno ));
3515
+ JanetStream * stream = janet_stream (dupped_handle , stream_flags , NULL );
3516
+ #endif
3517
+ return janet_wrap_abstract (stream );
3518
+ }
3519
+
3489
3520
void janet_lib_ev (JanetTable * env ) {
3490
3521
JanetRegExt ev_cfuns_ext [] = {
3491
3522
JANET_CORE_REG ("ev/give" , cfun_channel_push ),
@@ -3517,6 +3548,7 @@ void janet_lib_ev(JanetTable *env) {
3517
3548
JANET_CORE_REG ("ev/release-rlock" , janet_cfun_rwlock_read_release ),
3518
3549
JANET_CORE_REG ("ev/release-wlock" , janet_cfun_rwlock_write_release ),
3519
3550
JANET_CORE_REG ("ev/to-file" , janet_cfun_to_file ),
3551
+ JANET_CORE_REG ("ev/to-stream" , janet_cfun_to_stream ),
3520
3552
JANET_CORE_REG ("ev/all-tasks" , janet_cfun_ev_all_tasks ),
3521
3553
JANET_REG_END
3522
3554
};
0 commit comments