@effekt-lang/effekt 0.83.0 → 0.84.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/effekt +0 -0
- package/libraries/chez/common/effekt_primitives.ss +9 -0
- package/libraries/common/array.effekt +3 -1
- package/libraries/common/bench.effekt +0 -5
- package/libraries/common/bytearray.effekt +12 -6
- package/libraries/common/effekt.effekt +49 -7
- package/libraries/common/heap.effekt +100 -90
- package/libraries/common/io/console.effekt +3 -3
- package/libraries/common/io/error.effekt +4 -4
- package/libraries/common/io/filesystem.effekt +42 -32
- package/libraries/common/io/network.effekt +170 -83
- package/libraries/common/io/signal.effekt +5 -4
- package/libraries/common/io.effekt +8 -0
- package/libraries/common/process.effekt +88 -82
- package/libraries/common/ref.effekt +8 -5
- package/libraries/js/effekt_builtins.js +9 -0
- package/libraries/llvm/bytearray.c +3 -3
- package/libraries/llvm/io.c +201 -135
- package/libraries/llvm/rts.ll +231 -79
- package/libraries/llvm/types.c +3 -2
- package/licenses/eclipse public license, version 2.0 - epl-2.0.html +3 -3
- package/licenses/mit license - mit-license.html +79 -32
- package/licenses/the bsd license - bsd-license.html +79 -32
- package/licenses/the mit license - mit.html +79 -32
- package/package.json +1 -1
|
@@ -14,16 +14,16 @@
|
|
|
14
14
|
* | Rc | Eraser | Contents ... |
|
|
15
15
|
* +--------------+--------------+
|
|
16
16
|
*
|
|
17
|
-
* The eraser
|
|
17
|
+
* The eraser only frees the object.
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
void
|
|
21
|
+
void c_bytearray_erase(void *object) { free(object); }
|
|
22
22
|
|
|
23
23
|
struct Pos c_bytearray_new(const Int size) {
|
|
24
24
|
void *objPtr = malloc(sizeof(struct Header) + size);
|
|
25
25
|
struct Header *headerPtr = objPtr;
|
|
26
|
-
*headerPtr = (struct Header) { .rc = 0, .eraser =
|
|
26
|
+
*headerPtr = (struct Header) { .rc = 0, .eraser = c_bytearray_erase, };
|
|
27
27
|
return (struct Pos) {
|
|
28
28
|
.tag = size,
|
|
29
29
|
.obj = objPtr,
|
package/libraries/llvm/io.c
CHANGED
|
@@ -66,10 +66,10 @@ double c_io_random(void) {
|
|
|
66
66
|
// TODO
|
|
67
67
|
// - pooling of request objects (benchmark first!)
|
|
68
68
|
|
|
69
|
+
// `c_fs_cb`'s data: the stack to resume in and the buffer to release.
|
|
69
70
|
typedef struct {
|
|
70
71
|
Stack stack;
|
|
71
72
|
struct Pos buffer;
|
|
72
|
-
size_t offset;
|
|
73
73
|
} io_closure_t;
|
|
74
74
|
|
|
75
75
|
void c_resume_int_fs(uv_fs_t* request) {
|
|
@@ -137,7 +137,6 @@ void c_fs_read(Int file, struct Pos buffer, Int offset, Int size, Int position,
|
|
|
137
137
|
io_closure_t* read_closure = malloc(sizeof(io_closure_t));
|
|
138
138
|
read_closure->stack = stack;
|
|
139
139
|
read_closure->buffer = buffer;
|
|
140
|
-
read_closure->offset = offset;
|
|
141
140
|
|
|
142
141
|
uv_fs_t* request = malloc(sizeof(uv_fs_t));
|
|
143
142
|
request->data = read_closure;
|
|
@@ -160,7 +159,6 @@ void c_fs_write(Int file, struct Pos buffer, Int offset, Int size, Int position,
|
|
|
160
159
|
io_closure_t* write_closure = malloc(sizeof(io_closure_t));
|
|
161
160
|
write_closure->stack = stack;
|
|
162
161
|
write_closure->buffer = buffer;
|
|
163
|
-
write_closure->offset = offset;
|
|
164
162
|
|
|
165
163
|
uv_fs_t* request = malloc(sizeof(uv_fs_t));
|
|
166
164
|
request->data = write_closure;
|
|
@@ -243,6 +241,7 @@ void c_tcp_connect(String host, Int port, Stack stack) {
|
|
|
243
241
|
resume_Int(stack, result);
|
|
244
242
|
return;
|
|
245
243
|
}
|
|
244
|
+
tcp_handle->data = NULL; // no pending read yet (explicit `NULL` since libuv leaves it uninitialised)
|
|
246
245
|
|
|
247
246
|
uv_connect_t* connect_req = malloc(sizeof(uv_connect_t));
|
|
248
247
|
connect_req->data = stack;
|
|
@@ -252,7 +251,7 @@ void c_tcp_connect(String host, Int port, Stack stack) {
|
|
|
252
251
|
free(host_str);
|
|
253
252
|
|
|
254
253
|
if (result < 0) {
|
|
255
|
-
|
|
254
|
+
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
256
255
|
free(connect_req);
|
|
257
256
|
resume_Int(stack, result);
|
|
258
257
|
return;
|
|
@@ -261,59 +260,87 @@ void c_tcp_connect(String host, Int port, Stack stack) {
|
|
|
261
260
|
result = uv_tcp_connect(connect_req, tcp_handle, (const struct sockaddr*)&addr, c_tcp_connect_cb);
|
|
262
261
|
|
|
263
262
|
if (result < 0) {
|
|
264
|
-
|
|
263
|
+
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
265
264
|
free(connect_req);
|
|
266
265
|
resume_Int(stack, result);
|
|
267
266
|
return;
|
|
268
267
|
}
|
|
269
268
|
}
|
|
270
269
|
|
|
270
|
+
typedef struct {
|
|
271
|
+
Stack stack;
|
|
272
|
+
struct Pos buffer;
|
|
273
|
+
size_t offset;
|
|
274
|
+
size_t size;
|
|
275
|
+
} tcp_read_closure_t;
|
|
276
|
+
|
|
271
277
|
void c_tcp_read_cb(uv_stream_t* stream, ssize_t bytes_read, const uv_buf_t* buf) {
|
|
272
278
|
(void)(buf);
|
|
273
|
-
|
|
279
|
+
// 0 means nothing was read this time (EAGAIN), not end of file, so keep waiting.
|
|
280
|
+
if (bytes_read == 0) return;
|
|
281
|
+
|
|
282
|
+
tcp_read_closure_t* read_closure = (tcp_read_closure_t*)stream->data;
|
|
274
283
|
Stack stack = read_closure->stack;
|
|
275
284
|
|
|
276
285
|
uv_read_stop(stream);
|
|
286
|
+
stream->data = NULL;
|
|
277
287
|
erasePositive(read_closure->buffer);
|
|
278
288
|
free(read_closure);
|
|
279
289
|
|
|
280
|
-
|
|
290
|
+
// End of file is 0, as the contract in io/network says, but other errors stay negative.
|
|
291
|
+
resume_Int(stack, bytes_read == UV_EOF ? 0 : (int64_t)bytes_read);
|
|
281
292
|
}
|
|
282
293
|
|
|
283
294
|
void c_tcp_read_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
|
284
295
|
(void)(suggested_size);
|
|
285
|
-
|
|
296
|
+
tcp_read_closure_t* read_closure = (tcp_read_closure_t*)handle->data;
|
|
297
|
+
// Exactly the requested window [offset, offset + size), io/network checks it is in bounds.
|
|
286
298
|
buf->base = (char*)c_bytearray_data(read_closure->buffer) + read_closure->offset;
|
|
287
|
-
|
|
288
|
-
buf->len = read_closure->buffer.tag;
|
|
299
|
+
buf->len = read_closure->size;
|
|
289
300
|
}
|
|
290
301
|
|
|
291
302
|
void c_tcp_read(Int handle, struct Pos buffer, Int offset, Int size, Stack stack) {
|
|
292
|
-
(void)size;
|
|
293
303
|
uv_stream_t* stream = (uv_stream_t*)handle;
|
|
294
304
|
|
|
295
|
-
|
|
305
|
+
// At most one read is pending on a connection, its closure is in stream->data.
|
|
306
|
+
if (stream->data != NULL) {
|
|
307
|
+
erasePositive(buffer);
|
|
308
|
+
resume_Int(stack, UV_EALREADY);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
tcp_read_closure_t* read_closure = malloc(sizeof(tcp_read_closure_t));
|
|
296
313
|
read_closure->stack = stack;
|
|
297
314
|
read_closure->buffer = buffer;
|
|
298
315
|
read_closure->offset = offset;
|
|
316
|
+
read_closure->size = size;
|
|
299
317
|
stream->data = read_closure;
|
|
300
318
|
|
|
301
319
|
int result = uv_read_start(stream, c_tcp_read_alloc_cb, c_tcp_read_cb);
|
|
302
320
|
|
|
303
321
|
if (result < 0) {
|
|
304
|
-
free(read_closure);
|
|
305
322
|
stream->data = NULL;
|
|
323
|
+
erasePositive(buffer);
|
|
324
|
+
free(read_closure);
|
|
306
325
|
resume_Int(stack, result);
|
|
307
326
|
}
|
|
308
327
|
}
|
|
309
328
|
|
|
329
|
+
typedef struct {
|
|
330
|
+
Stack stack;
|
|
331
|
+
struct Pos buffer;
|
|
332
|
+
size_t size;
|
|
333
|
+
} tcp_write_closure_t;
|
|
334
|
+
|
|
310
335
|
void c_tcp_write_cb(uv_write_t* request, int status) {
|
|
311
|
-
|
|
336
|
+
tcp_write_closure_t* write_closure = (tcp_write_closure_t*)request->data;
|
|
312
337
|
free(request);
|
|
313
338
|
erasePositive(write_closure->buffer);
|
|
314
339
|
Stack stack = write_closure->stack;
|
|
340
|
+
// `uv_write` writes everything or fails, but our contract returns the number of bytes written.
|
|
341
|
+
int64_t written = (int64_t)write_closure->size;
|
|
315
342
|
free(write_closure);
|
|
316
|
-
resume_Int(stack, (int64_t)status);
|
|
343
|
+
resume_Int(stack, status < 0 ? (int64_t)status : written);
|
|
317
344
|
}
|
|
318
345
|
|
|
319
346
|
void c_tcp_write(Int handle, struct Pos buffer, Int offset, Int size, Stack stack) {
|
|
@@ -321,10 +348,10 @@ void c_tcp_write(Int handle, struct Pos buffer, Int offset, Int size, Stack stac
|
|
|
321
348
|
|
|
322
349
|
uv_buf_t buf = uv_buf_init((char*)(c_bytearray_data(buffer) + offset), size);
|
|
323
350
|
|
|
324
|
-
|
|
351
|
+
tcp_write_closure_t* write_closure = malloc(sizeof(tcp_write_closure_t));
|
|
325
352
|
write_closure->stack = stack;
|
|
326
353
|
write_closure->buffer = buffer;
|
|
327
|
-
write_closure->
|
|
354
|
+
write_closure->size = size;
|
|
328
355
|
|
|
329
356
|
uv_write_t* request = malloc(sizeof(uv_write_t));
|
|
330
357
|
request->data = write_closure;
|
|
@@ -333,8 +360,9 @@ void c_tcp_write(Int handle, struct Pos buffer, Int offset, Int size, Stack stac
|
|
|
333
360
|
|
|
334
361
|
if (result < 0) {
|
|
335
362
|
free(request);
|
|
363
|
+
erasePositive(buffer);
|
|
364
|
+
free(write_closure);
|
|
336
365
|
resume_Int(stack, result);
|
|
337
|
-
free(write_closure);
|
|
338
366
|
}
|
|
339
367
|
}
|
|
340
368
|
|
|
@@ -346,11 +374,23 @@ void c_tcp_close_cb(uv_handle_t* handle) {
|
|
|
346
374
|
|
|
347
375
|
void c_tcp_close(Int handle, Stack stack) {
|
|
348
376
|
uv_handle_t* uv_handle = (uv_handle_t*)handle;
|
|
377
|
+
|
|
378
|
+
// A read still pending ends as at end of file, much like on the JS backend.
|
|
379
|
+
tcp_read_closure_t* pending = (tcp_read_closure_t*)uv_handle->data;
|
|
349
380
|
uv_handle->data = stack;
|
|
381
|
+
if (pending != NULL) {
|
|
382
|
+
uv_read_stop((uv_stream_t*)uv_handle);
|
|
383
|
+
Stack reader = pending->stack;
|
|
384
|
+
erasePositive(pending->buffer);
|
|
385
|
+
free(pending);
|
|
386
|
+
resume_Int(reader, 0);
|
|
387
|
+
}
|
|
350
388
|
uv_close(uv_handle, c_tcp_close_cb);
|
|
351
389
|
}
|
|
352
390
|
|
|
353
|
-
|
|
391
|
+
void c_tcp_listen_cb(uv_stream_t* server, int status);
|
|
392
|
+
|
|
393
|
+
void c_tcp_bind(String host, Int port, Stack stack) {
|
|
354
394
|
char* host_str = c_bytearray_into_nullterminated_string(host);
|
|
355
395
|
erasePositive(host);
|
|
356
396
|
|
|
@@ -360,25 +400,46 @@ Int c_tcp_bind(String host, Int port) {
|
|
|
360
400
|
if (result < 0) {
|
|
361
401
|
free(tcp_handle);
|
|
362
402
|
free(host_str);
|
|
363
|
-
|
|
403
|
+
resume_Int(stack, result);
|
|
404
|
+
return;
|
|
364
405
|
}
|
|
365
406
|
|
|
407
|
+
tcp_handle->data = NULL; // no listen yet
|
|
408
|
+
|
|
366
409
|
struct sockaddr_in addr;
|
|
367
410
|
result = uv_ip4_addr(host_str, port, &addr);
|
|
368
411
|
free(host_str);
|
|
369
412
|
|
|
370
413
|
if (result < 0) {
|
|
371
414
|
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
372
|
-
|
|
415
|
+
resume_Int(stack, result);
|
|
416
|
+
return;
|
|
373
417
|
}
|
|
374
418
|
|
|
375
419
|
result = uv_tcp_bind(tcp_handle, (const struct sockaddr*)&addr, 0);
|
|
376
420
|
if (result < 0) {
|
|
377
421
|
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
378
|
-
|
|
422
|
+
resume_Int(stack, result);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Listening already, so that the address is reserved (errors such as `EADDRINUSE` arise here),
|
|
427
|
+
// but connections are only accepted once `listen` starts (see `c_tcp_listen_cb`).
|
|
428
|
+
result = uv_listen((uv_stream_t*)tcp_handle, SOMAXCONN, c_tcp_listen_cb);
|
|
429
|
+
if (result < 0) {
|
|
430
|
+
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
431
|
+
resume_Int(stack, result);
|
|
432
|
+
return;
|
|
379
433
|
}
|
|
380
434
|
|
|
381
|
-
|
|
435
|
+
resume_Int(stack, (int64_t)tcp_handle);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
Int c_tcp_port(Int listener) {
|
|
439
|
+
struct sockaddr_in addr;
|
|
440
|
+
int length = sizeof(addr);
|
|
441
|
+
int result = uv_tcp_getsockname((uv_tcp_t*)listener, (struct sockaddr*)&addr, &length);
|
|
442
|
+
return result < 0 ? result : ntohs(addr.sin_port);
|
|
382
443
|
}
|
|
383
444
|
|
|
384
445
|
typedef struct {
|
|
@@ -386,43 +447,35 @@ typedef struct {
|
|
|
386
447
|
struct Neg handler;
|
|
387
448
|
} tcp_listen_closure_t;
|
|
388
449
|
|
|
389
|
-
|
|
450
|
+
// Accepts a connection and runs the handler of `listen` on it.
|
|
451
|
+
void c_tcp_accept(uv_stream_t* server) {
|
|
390
452
|
tcp_listen_closure_t* listen_closure = (tcp_listen_closure_t*)server->data;
|
|
391
|
-
Stack closure_stack = listen_closure->stack;
|
|
392
|
-
struct Neg closure_handler = listen_closure->handler;
|
|
393
|
-
|
|
394
|
-
if (status < 0) {
|
|
395
|
-
server->data = NULL;
|
|
396
|
-
free(listen_closure);
|
|
397
|
-
eraseNegative(closure_handler);
|
|
398
|
-
resume_Int(closure_stack, status);
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
453
|
|
|
402
454
|
uv_tcp_t* client = malloc(sizeof(uv_tcp_t));
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (result < 0) {
|
|
455
|
+
if (uv_tcp_init(uv_default_loop(), client) < 0) {
|
|
406
456
|
free(client);
|
|
407
|
-
server->data = NULL;
|
|
408
|
-
free(listen_closure);
|
|
409
|
-
eraseNegative(closure_handler);
|
|
410
|
-
resume_Int(closure_stack, result);
|
|
411
457
|
return;
|
|
412
458
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
if (result < 0) {
|
|
459
|
+
client->data = NULL; // no pending read yet
|
|
460
|
+
if (uv_accept(server, (uv_stream_t*)client) < 0) {
|
|
416
461
|
uv_close((uv_handle_t*)client, (uv_close_cb)free);
|
|
417
|
-
server->data = NULL;
|
|
418
|
-
free(listen_closure);
|
|
419
|
-
eraseNegative(closure_handler);
|
|
420
|
-
resume_Int(closure_stack, result);
|
|
421
462
|
return;
|
|
422
463
|
}
|
|
423
464
|
|
|
424
|
-
|
|
425
|
-
|
|
465
|
+
struct Neg handler = listen_closure->handler;
|
|
466
|
+
shareNegative(handler);
|
|
467
|
+
run_Int(handler, (int64_t)client);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
void c_tcp_listen_cb(uv_stream_t* server, int status) {
|
|
471
|
+
// An error while accepting concerns that one client, as opposed to the listener:
|
|
472
|
+
// listen returns only once shutdown is called (as on the JS backend), so skip the client and keep listening.
|
|
473
|
+
if (status < 0) return;
|
|
474
|
+
|
|
475
|
+
// Before `listen` starts, libuv keeps the connection alive, and stops accepting until `uv_accept` takes it.
|
|
476
|
+
if (server->data == NULL) return;
|
|
477
|
+
|
|
478
|
+
c_tcp_accept(server);
|
|
426
479
|
}
|
|
427
480
|
|
|
428
481
|
void c_tcp_listen(Int listener, struct Neg handler, Stack stack) {
|
|
@@ -433,13 +486,7 @@ void c_tcp_listen(Int listener, struct Neg handler, Stack stack) {
|
|
|
433
486
|
listen_closure->handler = handler;
|
|
434
487
|
server->data = listen_closure;
|
|
435
488
|
|
|
436
|
-
|
|
437
|
-
if (result < 0) {
|
|
438
|
-
free(listen_closure);
|
|
439
|
-
eraseNegative(handler);
|
|
440
|
-
resume_Int(stack, result);
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
489
|
+
c_tcp_accept(server);
|
|
443
490
|
}
|
|
444
491
|
|
|
445
492
|
void c_tcp_shutdown(Int handle, Stack stack) {
|
|
@@ -578,20 +625,28 @@ void c_yield(Stack stack) {
|
|
|
578
625
|
|
|
579
626
|
// Subprocesses
|
|
580
627
|
// ------------
|
|
628
|
+
//
|
|
629
|
+
// Spawn options are plain Effekt data (see the `process` module):
|
|
630
|
+
// `c_spawn` receives, per each standard stream, whether to pipe it (1) or inherit it (0), and the callback to run for it,
|
|
631
|
+
// and uses that to build libuv's options only for the duration of `uv_spawn`.
|
|
632
|
+
// The pipes belong to the process in question and are closed when it closes.
|
|
633
|
+
|
|
634
|
+
// The standard input of a spawned process, as an Effekt object holding its pipe.
|
|
635
|
+
// The pipe belongs to the process, so the eraser only frees the object.
|
|
636
|
+
typedef struct {
|
|
637
|
+
uv_stream_t* stream;
|
|
638
|
+
} writable_stream_t;
|
|
639
|
+
|
|
640
|
+
void c_writable_stream_erase(void* object) { free(object); }
|
|
581
641
|
|
|
582
|
-
struct Pos
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
stdio[2].flags = UV_INHERIT_FD;
|
|
591
|
-
stdio[2].data.fd = 2;
|
|
592
|
-
options->stdio = stdio;
|
|
593
|
-
options->stdio_count = 3;
|
|
594
|
-
return (struct Pos) { .tag = 0, .obj = options, };
|
|
642
|
+
struct Pos c_writable_stream_new(uv_stream_t* stream) {
|
|
643
|
+
writable_stream_t* writable = (writable_stream_t*)effekt_alloc(c_writable_stream_erase, sizeof(writable_stream_t));
|
|
644
|
+
writable->stream = stream;
|
|
645
|
+
return (struct Pos) { .tag = 0, .obj = (char*)writable - sizeof(struct Header) };
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
uv_stream_t* c_writable_stream(struct Pos writable) {
|
|
649
|
+
return ((writable_stream_t*)((char*)writable.obj + sizeof(struct Header)))->stream;
|
|
595
650
|
}
|
|
596
651
|
|
|
597
652
|
static void subproc_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
|
@@ -603,73 +658,50 @@ static void subproc_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_
|
|
|
603
658
|
typedef struct {
|
|
604
659
|
struct Neg handler;
|
|
605
660
|
} subproc_stream_cb_closure_t;
|
|
661
|
+
|
|
662
|
+
// Calling a handler consumes a reference to it,
|
|
663
|
+
// so it is shared before each chunk and released once the stream ends (as in `c_tcp_listen_cb`).
|
|
606
664
|
void subproc_stream_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
|
|
607
665
|
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)(stream->data);
|
|
608
|
-
if(nread
|
|
666
|
+
if (nread > 0) {
|
|
609
667
|
struct Pos chunk = c_bytearray_construct(nread, (uint8_t*)(buf->base));
|
|
610
668
|
if (buf->base) free(buf->base);
|
|
669
|
+
shareNegative(clos->handler);
|
|
611
670
|
run_Pos(clos->handler, chunk);
|
|
612
|
-
} else {
|
|
671
|
+
} else if (nread < 0) {
|
|
613
672
|
uv_read_stop(stream);
|
|
614
673
|
eraseNegative(clos->handler);
|
|
615
674
|
free(clos);
|
|
675
|
+
stream->data = NULL;
|
|
616
676
|
if (buf->base) free(buf->base);
|
|
677
|
+
} else {
|
|
678
|
+
if (buf->base) free(buf->base); // 0: nothing read this time
|
|
617
679
|
}
|
|
618
680
|
}
|
|
619
|
-
struct Pos subproc_options_on_stream(struct Pos opts, size_t id, uv_stdio_flags flags, struct Neg callback) {
|
|
620
|
-
uv_process_options_t* options = (uv_process_options_t*)opts.obj;
|
|
621
|
-
|
|
622
|
-
options->stdio[id].flags = UV_CREATE_PIPE | flags;
|
|
623
|
-
uv_pipe_t* pipe = (uv_pipe_t*)malloc(sizeof(uv_pipe_t));
|
|
624
|
-
|
|
625
|
-
uv_pipe_init(uv_default_loop(), pipe, 1);
|
|
626
|
-
options->stdio[id].data.stream = (uv_stream_t*)pipe;
|
|
627
|
-
|
|
628
|
-
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)malloc(sizeof(subproc_stream_cb_closure_t));
|
|
629
|
-
clos->handler = callback;
|
|
630
|
-
pipe->data = clos;
|
|
631
|
-
|
|
632
|
-
return opts;
|
|
633
|
-
}
|
|
634
|
-
struct Pos c_spawn_options_on_stdout(struct Pos opts, struct Neg callback) {
|
|
635
|
-
return subproc_options_on_stream(opts, 1, UV_WRITABLE_PIPE, callback);
|
|
636
|
-
}
|
|
637
|
-
struct Pos c_spawn_options_on_stderr(struct Pos opts, struct Neg callback) {
|
|
638
|
-
return subproc_options_on_stream(opts, 2, UV_WRITABLE_PIPE, callback);
|
|
639
|
-
}
|
|
640
|
-
struct Pos c_spawn_options_pipe_stdin(struct Pos opts, struct Neg callback) {
|
|
641
|
-
return subproc_options_on_stream(opts, 0, UV_READABLE_PIPE, callback);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
681
|
|
|
645
682
|
typedef struct {
|
|
646
683
|
char** args;
|
|
647
684
|
Stack k;
|
|
648
|
-
|
|
685
|
+
uv_pipe_t* pipes[3]; // NULL iff the stream is inherited
|
|
649
686
|
} subproc_proc_data_t;
|
|
687
|
+
|
|
650
688
|
void c_close_stream_callback_exit(uv_handle_t* req) {
|
|
651
689
|
free(req);
|
|
652
690
|
}
|
|
691
|
+
|
|
653
692
|
void subproc_on_close(uv_handle_t* handle) {
|
|
654
693
|
subproc_proc_data_t* pd = (subproc_proc_data_t*)((uv_process_t*)handle)->data;
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
if(opts->stdio[1].flags & UV_CREATE_PIPE){
|
|
660
|
-
uv_close((uv_handle_t*)(opts->stdio[1].data.stream), c_close_stream_callback_exit);
|
|
661
|
-
}
|
|
662
|
-
if(opts->stdio[2].flags & UV_CREATE_PIPE){
|
|
663
|
-
uv_close((uv_handle_t*)(opts->stdio[2].data.stream), c_close_stream_callback_exit);
|
|
694
|
+
for (int i = 0; i < 3; i++) {
|
|
695
|
+
if (pd->pipes[i] != NULL) {
|
|
696
|
+
uv_close((uv_handle_t*)pd->pipes[i], c_close_stream_callback_exit);
|
|
697
|
+
}
|
|
664
698
|
}
|
|
665
|
-
// TODO shutdown pipes for stdio
|
|
666
|
-
free(opts->stdio);
|
|
667
|
-
free(opts);
|
|
668
699
|
free(pd);
|
|
669
700
|
free(handle);
|
|
670
701
|
}
|
|
702
|
+
|
|
671
703
|
void subproc_on_exit(uv_process_t* proc, int64_t exit_status, int term_signal) {
|
|
672
|
-
(void)term_signal;
|
|
704
|
+
(void)term_signal;
|
|
673
705
|
subproc_proc_data_t* pd = (subproc_proc_data_t*)proc->data;
|
|
674
706
|
if (pd->args) {
|
|
675
707
|
for(int i = 0; pd->args[i] != NULL; i++) {
|
|
@@ -688,8 +720,10 @@ void c_close_stream_callback(uv_shutdown_t* req, int status) {
|
|
|
688
720
|
}
|
|
689
721
|
free(req);
|
|
690
722
|
}
|
|
691
|
-
|
|
692
|
-
|
|
723
|
+
|
|
724
|
+
void c_close_stream(struct Pos writable) {
|
|
725
|
+
uv_stream_t* str = c_writable_stream(writable);
|
|
726
|
+
erasePositive(writable);
|
|
693
727
|
uv_shutdown_t* req = (uv_shutdown_t*)malloc(sizeof(uv_shutdown_t));
|
|
694
728
|
uv_shutdown(req, str, c_close_stream_callback);
|
|
695
729
|
}
|
|
@@ -698,6 +732,7 @@ typedef struct {
|
|
|
698
732
|
struct Pos buffer;
|
|
699
733
|
uv_buf_t buf;
|
|
700
734
|
} subproc_write_req_data_t;
|
|
735
|
+
|
|
701
736
|
void c_write_stream_callback(uv_write_t* req, int status) {
|
|
702
737
|
if(status != 0){
|
|
703
738
|
// TODO error handling
|
|
@@ -707,8 +742,10 @@ void c_write_stream_callback(uv_write_t* req, int status) {
|
|
|
707
742
|
free(data);
|
|
708
743
|
free(req);
|
|
709
744
|
}
|
|
710
|
-
|
|
711
|
-
|
|
745
|
+
|
|
746
|
+
void c_write_stream(struct Pos writable, struct Pos buffer) {
|
|
747
|
+
uv_stream_t* str = c_writable_stream(writable);
|
|
748
|
+
erasePositive(writable);
|
|
712
749
|
uv_write_t* req = (uv_write_t*)malloc(sizeof(uv_write_t));
|
|
713
750
|
memset(req, 0, sizeof(uv_write_t));
|
|
714
751
|
subproc_write_req_data_t* data = (subproc_write_req_data_t*)malloc(sizeof(subproc_write_req_data_t));
|
|
@@ -719,14 +756,38 @@ void c_write_stream(struct Pos stream, struct Pos buffer) {
|
|
|
719
756
|
uv_write(req, str, &(data->buf), 1, c_write_stream_callback);
|
|
720
757
|
}
|
|
721
758
|
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
759
|
+
// Connects standard stream `id` to a new pipe running `handler` if `pipe` is 1, or inherits it.
|
|
760
|
+
static uv_pipe_t* subproc_stdio(uv_stdio_container_t* stdio, int id, Int pipe, int flags, struct Neg handler) {
|
|
761
|
+
if (!pipe) {
|
|
762
|
+
eraseNegative(handler);
|
|
763
|
+
stdio[id].flags = UV_INHERIT_FD;
|
|
764
|
+
stdio[id].data.fd = id;
|
|
765
|
+
return NULL;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
uv_pipe_t* p = (uv_pipe_t*)malloc(sizeof(uv_pipe_t));
|
|
769
|
+
uv_pipe_init(uv_default_loop(), p, 1);
|
|
770
|
+
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)malloc(sizeof(subproc_stream_cb_closure_t));
|
|
771
|
+
clos->handler = handler;
|
|
772
|
+
p->data = clos;
|
|
773
|
+
stdio[id].flags = UV_CREATE_PIPE | flags;
|
|
774
|
+
stdio[id].data.stream = (uv_stream_t*)p;
|
|
775
|
+
return p;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// Note: The struct arguments come first (on x64, they may get split by LLVM)
|
|
779
|
+
void c_spawn(struct Pos cmd, struct Pos args, struct Neg on_stdin, struct Neg on_stdout, struct Neg on_stderr,
|
|
780
|
+
Int pipeStdin, Int pipeStdout, Int pipeStderr, Stack stack) {
|
|
781
|
+
subproc_proc_data_t* pd = (subproc_proc_data_t*)malloc(sizeof(subproc_proc_data_t));
|
|
782
|
+
|
|
783
|
+
uv_stdio_container_t stdio[3];
|
|
784
|
+
pd->pipes[0] = subproc_stdio(stdio, 0, pipeStdin, UV_READABLE_PIPE, on_stdin);
|
|
785
|
+
pd->pipes[1] = subproc_stdio(stdio, 1, pipeStdout, UV_WRITABLE_PIPE, on_stdout);
|
|
786
|
+
pd->pipes[2] = subproc_stdio(stdio, 2, pipeStderr, UV_WRITABLE_PIPE, on_stderr);
|
|
725
787
|
|
|
726
788
|
// command
|
|
727
789
|
char* cmd_s = c_bytearray_into_nullterminated_string(cmd);
|
|
728
790
|
erasePositive(cmd);
|
|
729
|
-
opts->file = cmd_s;
|
|
730
791
|
|
|
731
792
|
// args
|
|
732
793
|
int _argc = (int)((uint64_t*)args.obj)[2];
|
|
@@ -738,32 +799,37 @@ void c_spawn(struct Pos cmd, struct Pos args, struct Pos options, Stack stack) {
|
|
|
738
799
|
}
|
|
739
800
|
erasePositive(args);
|
|
740
801
|
_args[_argc + 1] = NULL;
|
|
741
|
-
|
|
742
|
-
//
|
|
743
|
-
opts
|
|
744
|
-
|
|
802
|
+
|
|
803
|
+
// libuv only reads the options during `uv_spawn`.
|
|
804
|
+
uv_process_options_t opts;
|
|
805
|
+
memset(&opts, 0, sizeof(uv_process_options_t));
|
|
806
|
+
opts.file = cmd_s;
|
|
807
|
+
opts.args = _args;
|
|
808
|
+
opts.exit_cb = subproc_on_exit;
|
|
809
|
+
opts.stdio = stdio;
|
|
810
|
+
opts.stdio_count = 3;
|
|
811
|
+
|
|
745
812
|
pd->args = _args;
|
|
746
813
|
pd->k = stack;
|
|
814
|
+
uv_process_t* proc = (uv_process_t*)malloc(sizeof(uv_process_t));
|
|
747
815
|
proc->data = pd;
|
|
748
816
|
|
|
749
817
|
// spawn process
|
|
750
|
-
int err = uv_spawn(uv_default_loop(), proc, opts);
|
|
818
|
+
int err = uv_spawn(uv_default_loop(), proc, &opts);
|
|
751
819
|
|
|
752
|
-
if(
|
|
753
|
-
uv_stream_t* stream = (uv_stream_t*)
|
|
820
|
+
if (pd->pipes[0] != NULL) {
|
|
821
|
+
uv_stream_t* stream = (uv_stream_t*)pd->pipes[0];
|
|
754
822
|
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)(stream->data);
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
eraseNegative(clos->handler);
|
|
823
|
+
stream->data = NULL;
|
|
824
|
+
struct Neg handler = clos->handler;
|
|
758
825
|
free(clos);
|
|
826
|
+
run_Pos(handler, c_writable_stream_new(stream)); // consumes the handler's reference
|
|
759
827
|
}
|
|
760
|
-
if(
|
|
761
|
-
uv_read_start((uv_stream_t*)
|
|
762
|
-
if(
|
|
763
|
-
uv_read_start((uv_stream_t*)
|
|
764
|
-
pd->opts = opts;
|
|
828
|
+
if (pd->pipes[1] != NULL)
|
|
829
|
+
uv_read_start((uv_stream_t*)pd->pipes[1], subproc_alloc_cb, subproc_stream_cb);
|
|
830
|
+
if (pd->pipes[2] != NULL)
|
|
831
|
+
uv_read_start((uv_stream_t*)pd->pipes[2], subproc_alloc_cb, subproc_stream_cb);
|
|
765
832
|
|
|
766
|
-
erasePositive(options);
|
|
767
833
|
if(err) {
|
|
768
834
|
printf("%s\n", uv_strerror(err));
|
|
769
835
|
free(_args);
|