1
0
mirror of https://github.com/janet-lang/janet synced 2025-11-21 09:44:49 +00:00

Add os/pipe and os/open.

ev/pipe -> os/pipe, and os/open is a wrapper
around the open system call.
This commit is contained in:
Calvin Rose
2020-11-15 09:56:19 -06:00
parent d199c817dc
commit cdcb774dc8
4 changed files with 252 additions and 63 deletions

View File

@@ -1254,7 +1254,7 @@ JanetAsyncStatus ev_machine_read(JanetListenerState *s, JanetAsyncEvent event) {
status = ReadFile(s->stream->handle, state->chunk_buf, chunk_size, NULL, &state->overlapped);
if (!status && (ERROR_IO_PENDING != WSAGetLastError())) {
if (WSAGetLastError() == ERROR_BROKEN_PIPE) {
janet_schedule(s->fiber, janet_wrap_nil());
janet_schedule(s->fiber, janet_wrap_nil());
} else {
janet_cancel(s->fiber, janet_ev_lasterr());
}
@@ -1678,60 +1678,6 @@ Janet janet_cfun_stream_write(int32_t argc, Janet *argv) {
janet_await();
}
/* For a pipe ID */
#ifdef JANET_WINDOWS
static volatile long PipeSerialNumber;
#endif
static Janet cfun_ev_pipe(int32_t argc, Janet *argv) {
(void) argv;
janet_fixarity(argc, 0);
#ifdef JANET_WINDOWS
/*
* On windows, the built in CreatePipe function doesn't support overlapped IO
* so we lift from the windows source code and modify for our own version.
*/
JanetHandle rhandle, whandle;
UCHAR PipeNameBuffer[MAX_PATH];
sprintf(PipeNameBuffer,
"\\\\.\\Pipe\\JanetExeAnon.%08x.%08x",
GetCurrentProcessId(),
InterlockedIncrement(&PipeSerialNumber));
rhandle = CreateNamedPipeA(
PipeNameBuffer,
PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_BYTE | PIPE_WAIT,
1, /* Number of pipes */
4096, /* Out buffer size */
4096, /* In buffer size */
120 * 1000, /* Timeout in ms */
NULL);
if (!rhandle) janet_panicv(janet_ev_lasterr());
whandle = CreateFileA(
PipeNameBuffer,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (whandle == INVALID_HANDLE_VALUE) {
Janet x = janet_ev_lasterr();
CloseHandle(rhandle);
janet_panicv(x);
}
JanetStream *reader = janet_stream(rhandle, JANET_STREAM_READABLE, NULL);
JanetStream *writer = janet_stream(whandle, JANET_STREAM_WRITABLE, NULL);
#else
int fds[2];
if (pipe(fds)) janet_panicv(janet_ev_lasterr());
JanetStream *reader = janet_stream(fds[0], JANET_STREAM_READABLE, NULL);
JanetStream *writer = janet_stream(fds[1], JANET_STREAM_WRITABLE, NULL);
#endif
Janet tup[2] = {janet_wrap_abstract(reader), janet_wrap_abstract(writer)};
return janet_wrap_tuple(janet_tuple_n(tup, 2));
}
static const JanetReg ev_cfuns[] = {
{
"ev/call", cfun_ev_call,
@@ -1826,13 +1772,6 @@ static const JanetReg ev_cfuns[] = {
"completes. Takes an optional timeout in seconds, after which will return nil. "
"Returns nil, or raises an error if the write failed.")
},
{
"ev/pipe", cfun_ev_pipe,
JDOC("(ev/pipe)\n\n"
"Create a readable stream and a writable stream that are connected. Returns a two element "
"tuple where the first element is a readable stream and the second element is the writable "
"stream.")
},
{NULL, NULL, NULL}
};