@effekt-lang/effekt 0.83.0 → 0.85.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 +27 -10
- package/libraries/common/dictionary.effekt +2 -2
- package/libraries/common/effekt.effekt +49 -7
- package/libraries/common/exception.effekt +3 -3
- package/libraries/common/heap.effekt +100 -90
- package/libraries/common/io/console.effekt +3 -3
- package/libraries/common/io/error.effekt +9 -4
- package/libraries/common/io/filesystem.effekt +73 -44
- package/libraries/common/io/network.effekt +277 -96
- package/libraries/common/io/signal.effekt +5 -4
- package/libraries/common/io.effekt +44 -0
- package/libraries/common/process.effekt +118 -83
- package/libraries/common/ref.effekt +8 -5
- package/libraries/common/result.effekt +17 -1
- package/libraries/common/stream.effekt +2 -2
- package/libraries/common/string.effekt +10 -2
- package/libraries/common/stringbuffer.effekt +1 -1
- package/libraries/js/effekt_builtins.js +9 -0
- package/libraries/js/effekt_runtime.js +3 -0
- package/libraries/llvm/bytearray.c +51 -3
- package/libraries/llvm/forward-declare-c.ll +2 -0
- package/libraries/llvm/io.c +495 -182
- package/libraries/llvm/rts.ll +251 -79
- package/libraries/llvm/types.c +3 -2
- package/licenses/eclipse public license, version 2.0 - epl-2.0.html +5 -5
- package/licenses/mit license - mit-license.html +87 -49
- package/licenses/the bsd license - bsd-license.html +85 -47
- package/licenses/the mit license - mit.html +88 -50
- package/package.json +1 -1
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) {
|
|
@@ -82,6 +82,74 @@ void c_resume_int_fs(uv_fs_t* request) {
|
|
|
82
82
|
resume_Int(stack, result);
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
// Files, connections and listeners are generational handles into one table of open I/O resources.
|
|
86
|
+
// A handle is a slot of the table and the generation which the slot had when the resource was opened,
|
|
87
|
+
// so it is a plain integer, as cheap to copy as an `Int`.
|
|
88
|
+
// Closing a resource starts the next generation of its slot,
|
|
89
|
+
// so its handles no longer match, and using them raises `EBADF`, even once the slot is reused.
|
|
90
|
+
typedef struct {
|
|
91
|
+
intptr_t value; // the resource: a file descriptor, a `uv_tcp_t*`, or a `struct addrinfo*` of `c_tcp_resolve`
|
|
92
|
+
uint32_t generation; // 31 bits, so that a handle is never negative (negative results are errors)
|
|
93
|
+
uint32_t next_free; // the next free slot, while this one is free
|
|
94
|
+
} io_slot_t;
|
|
95
|
+
|
|
96
|
+
#define IO_NO_SLOT UINT32_MAX
|
|
97
|
+
|
|
98
|
+
static io_slot_t* io_slots = NULL;
|
|
99
|
+
static uint32_t io_slots_used = 0;
|
|
100
|
+
static uint32_t io_slots_capacity = 0;
|
|
101
|
+
static uint32_t io_slots_free = IO_NO_SLOT;
|
|
102
|
+
|
|
103
|
+
Int io_handle_new(intptr_t value) {
|
|
104
|
+
uint32_t slot;
|
|
105
|
+
if (io_slots_free != IO_NO_SLOT) {
|
|
106
|
+
slot = io_slots_free;
|
|
107
|
+
io_slots_free = io_slots[slot].next_free;
|
|
108
|
+
} else {
|
|
109
|
+
if (io_slots_used == io_slots_capacity) {
|
|
110
|
+
io_slots_capacity = io_slots_capacity == 0 ? 16 : 2 * io_slots_capacity;
|
|
111
|
+
io_slots = (io_slot_t*)realloc(io_slots, io_slots_capacity * sizeof(io_slot_t));
|
|
112
|
+
}
|
|
113
|
+
slot = io_slots_used++;
|
|
114
|
+
io_slots[slot].generation = 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
io_slots[slot].value = value;
|
|
118
|
+
io_slots[slot].next_free = IO_NO_SLOT;
|
|
119
|
+
return ((Int)io_slots[slot].generation << 32) | slot;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// The slot of a handle which is still open, or `NULL`.
|
|
123
|
+
io_slot_t* io_handle_slot(Int handle) {
|
|
124
|
+
if (handle < 0) return NULL;
|
|
125
|
+
|
|
126
|
+
uint32_t slot = (uint32_t)handle;
|
|
127
|
+
uint32_t generation = (uint32_t)(handle >> 32);
|
|
128
|
+
if (slot >= io_slots_used || io_slots[slot].next_free != IO_NO_SLOT || io_slots[slot].generation != generation) return NULL;
|
|
129
|
+
return &io_slots[slot];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Closes a handle which is still open, and returns its resource in `value`, or returns 0 if it was not open.
|
|
133
|
+
int io_handle_close(Int handle, intptr_t* value) {
|
|
134
|
+
io_slot_t* open = io_handle_slot(handle);
|
|
135
|
+
if (open == NULL) return 0;
|
|
136
|
+
|
|
137
|
+
*value = open->value;
|
|
138
|
+
open->generation = (open->generation + 1) & 0x7fffffff;
|
|
139
|
+
open->next_free = io_slots_free;
|
|
140
|
+
io_slots_free = (uint32_t)(open - io_slots);
|
|
141
|
+
return 1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Resumes with the handle of the opened file, or with the (negative) error.
|
|
145
|
+
void c_fs_open_cb(uv_fs_t* request) {
|
|
146
|
+
Stack stack = (Stack)request->data;
|
|
147
|
+
int64_t result = (int64_t)request->result;
|
|
148
|
+
uv_fs_req_cleanup(request);
|
|
149
|
+
free(request);
|
|
150
|
+
resume_Int(stack, result < 0 ? result : io_handle_new(result));
|
|
151
|
+
}
|
|
152
|
+
|
|
85
153
|
void c_fs_open(String path, int flags, Stack stack) {
|
|
86
154
|
|
|
87
155
|
// Convert the Effekt String to a 0-terminated string
|
|
@@ -91,7 +159,7 @@ void c_fs_open(String path, int flags, Stack stack) {
|
|
|
91
159
|
uv_fs_t* request = malloc(sizeof(uv_fs_t));
|
|
92
160
|
request->data = stack;
|
|
93
161
|
|
|
94
|
-
int result = uv_fs_open(uv_default_loop(), request, path_str, flags, 0777,
|
|
162
|
+
int result = uv_fs_open(uv_default_loop(), request, path_str, flags, 0777, c_fs_open_cb);
|
|
95
163
|
|
|
96
164
|
if (result < 0) {
|
|
97
165
|
// TODO free path_str?
|
|
@@ -131,18 +199,24 @@ void c_fs_cb(uv_fs_t* request) {
|
|
|
131
199
|
}
|
|
132
200
|
|
|
133
201
|
void c_fs_read(Int file, struct Pos buffer, Int offset, Int size, Int position, Stack stack) {
|
|
202
|
+
io_slot_t* open = io_handle_slot(file);
|
|
203
|
+
if (open == NULL) {
|
|
204
|
+
erasePositive(buffer);
|
|
205
|
+
resume_Int(stack, UV_EBADF);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
134
209
|
|
|
135
210
|
uv_buf_t buf = uv_buf_init((char*)(c_bytearray_data(buffer) + offset), size);
|
|
136
211
|
|
|
137
212
|
io_closure_t* read_closure = malloc(sizeof(io_closure_t));
|
|
138
213
|
read_closure->stack = stack;
|
|
139
214
|
read_closure->buffer = buffer;
|
|
140
|
-
read_closure->offset = offset;
|
|
141
215
|
|
|
142
216
|
uv_fs_t* request = malloc(sizeof(uv_fs_t));
|
|
143
217
|
request->data = read_closure;
|
|
144
218
|
|
|
145
|
-
int result = uv_fs_read(uv_default_loop(), request,
|
|
219
|
+
int result = uv_fs_read(uv_default_loop(), request, open->value, &buf, 1, position, c_fs_cb);
|
|
146
220
|
|
|
147
221
|
if (result < 0) {
|
|
148
222
|
uv_fs_req_cleanup(request);
|
|
@@ -153,6 +227,13 @@ void c_fs_read(Int file, struct Pos buffer, Int offset, Int size, Int position,
|
|
|
153
227
|
}
|
|
154
228
|
|
|
155
229
|
void c_fs_write(Int file, struct Pos buffer, Int offset, Int size, Int position, Stack stack) {
|
|
230
|
+
io_slot_t* open = io_handle_slot(file);
|
|
231
|
+
if (open == NULL) {
|
|
232
|
+
erasePositive(buffer);
|
|
233
|
+
resume_Int(stack, UV_EBADF);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
|
|
156
237
|
(void)size;
|
|
157
238
|
|
|
158
239
|
uv_buf_t buf = uv_buf_init((char*)(c_bytearray_data(buffer) + offset), size);
|
|
@@ -160,12 +241,11 @@ void c_fs_write(Int file, struct Pos buffer, Int offset, Int size, Int position,
|
|
|
160
241
|
io_closure_t* write_closure = malloc(sizeof(io_closure_t));
|
|
161
242
|
write_closure->stack = stack;
|
|
162
243
|
write_closure->buffer = buffer;
|
|
163
|
-
write_closure->offset = offset;
|
|
164
244
|
|
|
165
245
|
uv_fs_t* request = malloc(sizeof(uv_fs_t));
|
|
166
246
|
request->data = write_closure;
|
|
167
247
|
|
|
168
|
-
int result = uv_fs_write(uv_default_loop(), request,
|
|
248
|
+
int result = uv_fs_write(uv_default_loop(), request, open->value, &buf, 1, position, c_fs_cb);
|
|
169
249
|
|
|
170
250
|
if (result < 0) {
|
|
171
251
|
uv_fs_req_cleanup(request);
|
|
@@ -176,11 +256,17 @@ void c_fs_write(Int file, struct Pos buffer, Int offset, Int size, Int position,
|
|
|
176
256
|
}
|
|
177
257
|
|
|
178
258
|
void c_fs_close(Int file, Stack stack) {
|
|
259
|
+
intptr_t fd;
|
|
260
|
+
if (!io_handle_close(file, &fd)) {
|
|
261
|
+
resume_Int(stack, UV_EBADF);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
179
265
|
|
|
180
266
|
uv_fs_t* request = malloc(sizeof(uv_fs_t));
|
|
181
267
|
request->data = stack;
|
|
182
268
|
|
|
183
|
-
int result = uv_fs_close(uv_default_loop(), request,
|
|
269
|
+
int result = uv_fs_close(uv_default_loop(), request, fd, c_resume_int_fs);
|
|
184
270
|
|
|
185
271
|
if (result < 0) {
|
|
186
272
|
uv_fs_req_cleanup(request);
|
|
@@ -224,107 +310,241 @@ void c_tcp_connect_cb(uv_connect_t* request, int status) {
|
|
|
224
310
|
free(request);
|
|
225
311
|
resume_Int(stack, status);
|
|
226
312
|
} else {
|
|
227
|
-
|
|
313
|
+
uv_tcp_t* handle = (uv_tcp_t*)request->handle;
|
|
228
314
|
free(request);
|
|
229
|
-
resume_Int(stack, handle);
|
|
315
|
+
resume_Int(stack, io_handle_new((intptr_t)handle));
|
|
230
316
|
}
|
|
231
317
|
}
|
|
232
318
|
|
|
233
|
-
|
|
319
|
+
// Hosts are resolved in Effekt (see `io/network`), which then connects to or binds one address at a time,
|
|
320
|
+
// so the resolution only collects the addresses, as a handle to the list which `uv_getaddrinfo` returns.
|
|
321
|
+
|
|
322
|
+
void c_tcp_resolve_cb(uv_getaddrinfo_t* request, int status, struct addrinfo* addresses) {
|
|
323
|
+
Stack stack = (Stack)request->data;
|
|
324
|
+
free(request);
|
|
325
|
+
resume_Int(stack, status < 0 ? status : io_handle_new((intptr_t)addresses));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
void c_tcp_resolve(String host, Stack stack) {
|
|
234
329
|
char* host_str = c_bytearray_into_nullterminated_string(host);
|
|
235
330
|
erasePositive(host);
|
|
236
331
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
332
|
+
struct addrinfo hints;
|
|
333
|
+
memset(&hints, 0, sizeof(hints));
|
|
334
|
+
hints.ai_family = AF_UNSPEC;
|
|
335
|
+
hints.ai_socktype = SOCK_STREAM;
|
|
336
|
+
hints.ai_protocol = IPPROTO_TCP;
|
|
337
|
+
|
|
338
|
+
// A numeric host needs no resolver, so we parse it right away (`uv_getaddrinfo` without a callback is synchronous).
|
|
339
|
+
struct addrinfo numeric = hints;
|
|
340
|
+
numeric.ai_flags = AI_NUMERICHOST;
|
|
341
|
+
uv_getaddrinfo_t parsed;
|
|
342
|
+
if (uv_getaddrinfo(uv_default_loop(), &parsed, NULL, host_str, NULL, &numeric) == 0) {
|
|
242
343
|
free(host_str);
|
|
243
|
-
resume_Int(stack,
|
|
344
|
+
resume_Int(stack, io_handle_new((intptr_t)parsed.addrinfo));
|
|
244
345
|
return;
|
|
245
346
|
}
|
|
246
347
|
|
|
247
|
-
|
|
248
|
-
|
|
348
|
+
uv_getaddrinfo_t* request = malloc(sizeof(uv_getaddrinfo_t));
|
|
349
|
+
request->data = stack;
|
|
350
|
+
int result = uv_getaddrinfo(uv_default_loop(), request, c_tcp_resolve_cb, host_str, NULL, &hints);
|
|
249
351
|
|
|
250
|
-
|
|
251
|
-
result = uv_ip4_addr(host_str, port, &addr);
|
|
352
|
+
// Free the string since libuv copies it into the request
|
|
252
353
|
free(host_str);
|
|
253
354
|
|
|
355
|
+
if (result < 0) {
|
|
356
|
+
free(request);
|
|
357
|
+
resume_Int(stack, result);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// The addresses of a resolution which is still open, or `NULL` otherwise.
|
|
362
|
+
static struct addrinfo* c_tcp_resolution(Int resolution) {
|
|
363
|
+
io_slot_t* open = io_handle_slot(resolution);
|
|
364
|
+
return open == NULL ? NULL : (struct addrinfo*)open->value;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
Int c_tcp_address_count(Int resolution) {
|
|
368
|
+
Int count = 0;
|
|
369
|
+
for (struct addrinfo* address = c_tcp_resolution(resolution); address != NULL; address = address->ai_next) {
|
|
370
|
+
count++;
|
|
371
|
+
}
|
|
372
|
+
return count;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
String c_tcp_address(Int resolution, Int index) {
|
|
376
|
+
struct addrinfo* address = c_tcp_resolution(resolution);
|
|
377
|
+
for (Int i = 0; address != NULL && i < index; i++) {
|
|
378
|
+
address = address->ai_next;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// An IPv6 address keeps its zone, such as `fe80::1%lo0` (or `fe80::1%12` on Windows), much like on Node
|
|
382
|
+
char text[INET6_ADDRSTRLEN + 1 + UV_IF_NAMESIZE] = "";
|
|
383
|
+
if (address != NULL) {
|
|
384
|
+
uv_ip_name(address->ai_addr, text, sizeof(text));
|
|
385
|
+
|
|
386
|
+
struct sockaddr_in6* v6 = (struct sockaddr_in6*)address->ai_addr;
|
|
387
|
+
size_t zone_size = UV_IF_NAMESIZE;
|
|
388
|
+
char zone[UV_IF_NAMESIZE];
|
|
389
|
+
if (address->ai_family == AF_INET6 && v6->sin6_scope_id != 0
|
|
390
|
+
&& uv_if_indextoiid(v6->sin6_scope_id, zone, &zone_size) == 0) {
|
|
391
|
+
strcat(text, "%");
|
|
392
|
+
strcat(text, zone);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return c_bytearray_from_nullterminated_string(text);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
void c_tcp_resolution_release(Int resolution) {
|
|
399
|
+
intptr_t addresses;
|
|
400
|
+
if (io_handle_close(resolution, &addresses)) {
|
|
401
|
+
uv_freeaddrinfo((struct addrinfo*)addresses);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Parses an `address` as `c_tcp_address` writes it, IPv4 or IPv6, together with the given `port`.
|
|
406
|
+
static int c_tcp_parse(String address, Int port, struct sockaddr_storage* addr) {
|
|
407
|
+
char* text = c_bytearray_into_nullterminated_string(address);
|
|
408
|
+
erasePositive(address);
|
|
409
|
+
|
|
410
|
+
int result = uv_ip4_addr(text, port, (struct sockaddr_in*)addr);
|
|
411
|
+
if (result < 0) {
|
|
412
|
+
result = uv_ip6_addr(text, port, (struct sockaddr_in6*)addr);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
free(text);
|
|
416
|
+
return result;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
void c_tcp_connect(String address, Int port, Stack stack) {
|
|
420
|
+
struct sockaddr_storage addr;
|
|
421
|
+
int result = c_tcp_parse(address, port, &addr);
|
|
422
|
+
|
|
423
|
+
if (result < 0) {
|
|
424
|
+
resume_Int(stack, result);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
uv_tcp_t* tcp_handle = malloc(sizeof(uv_tcp_t));
|
|
429
|
+
result = uv_tcp_init(uv_default_loop(), tcp_handle);
|
|
430
|
+
|
|
254
431
|
if (result < 0) {
|
|
255
432
|
free(tcp_handle);
|
|
256
|
-
free(connect_req);
|
|
257
433
|
resume_Int(stack, result);
|
|
258
434
|
return;
|
|
259
435
|
}
|
|
436
|
+
tcp_handle->data = NULL; // no pending read yet (explicit `NULL` since libuv leaves it uninitialised)
|
|
437
|
+
|
|
438
|
+
uv_connect_t* connect_req = malloc(sizeof(uv_connect_t));
|
|
439
|
+
connect_req->data = stack;
|
|
260
440
|
|
|
261
441
|
result = uv_tcp_connect(connect_req, tcp_handle, (const struct sockaddr*)&addr, c_tcp_connect_cb);
|
|
262
442
|
|
|
263
443
|
if (result < 0) {
|
|
264
|
-
|
|
444
|
+
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
265
445
|
free(connect_req);
|
|
266
446
|
resume_Int(stack, result);
|
|
267
447
|
return;
|
|
268
448
|
}
|
|
269
449
|
}
|
|
270
450
|
|
|
451
|
+
typedef struct {
|
|
452
|
+
Stack stack;
|
|
453
|
+
struct Pos buffer;
|
|
454
|
+
size_t offset;
|
|
455
|
+
size_t size;
|
|
456
|
+
} tcp_read_closure_t;
|
|
457
|
+
|
|
271
458
|
void c_tcp_read_cb(uv_stream_t* stream, ssize_t bytes_read, const uv_buf_t* buf) {
|
|
272
459
|
(void)(buf);
|
|
273
|
-
|
|
460
|
+
// 0 means nothing was read this time (EAGAIN), not end of file, so keep waiting.
|
|
461
|
+
if (bytes_read == 0) return;
|
|
462
|
+
|
|
463
|
+
tcp_read_closure_t* read_closure = (tcp_read_closure_t*)stream->data;
|
|
274
464
|
Stack stack = read_closure->stack;
|
|
275
465
|
|
|
276
466
|
uv_read_stop(stream);
|
|
467
|
+
stream->data = NULL;
|
|
277
468
|
erasePositive(read_closure->buffer);
|
|
278
469
|
free(read_closure);
|
|
279
470
|
|
|
280
|
-
|
|
471
|
+
// End of file is 0, as the contract in io/network says, but other errors stay negative.
|
|
472
|
+
resume_Int(stack, bytes_read == UV_EOF ? 0 : (int64_t)bytes_read);
|
|
281
473
|
}
|
|
282
474
|
|
|
283
475
|
void c_tcp_read_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
|
284
476
|
(void)(suggested_size);
|
|
285
|
-
|
|
477
|
+
tcp_read_closure_t* read_closure = (tcp_read_closure_t*)handle->data;
|
|
478
|
+
// Exactly the requested window [offset, offset + size), io/network checks it is in bounds.
|
|
286
479
|
buf->base = (char*)c_bytearray_data(read_closure->buffer) + read_closure->offset;
|
|
287
|
-
|
|
288
|
-
buf->len = read_closure->buffer.tag;
|
|
480
|
+
buf->len = read_closure->size;
|
|
289
481
|
}
|
|
290
482
|
|
|
291
483
|
void c_tcp_read(Int handle, struct Pos buffer, Int offset, Int size, Stack stack) {
|
|
292
|
-
(
|
|
293
|
-
|
|
484
|
+
io_slot_t* open = io_handle_slot(handle);
|
|
485
|
+
if (open == NULL) {
|
|
486
|
+
erasePositive(buffer);
|
|
487
|
+
resume_Int(stack, UV_EBADF);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
uv_stream_t* stream = (uv_stream_t*)open->value;
|
|
294
491
|
|
|
295
|
-
|
|
492
|
+
// At most one read is pending on a connection, its closure is in stream->data.
|
|
493
|
+
if (stream->data != NULL) {
|
|
494
|
+
erasePositive(buffer);
|
|
495
|
+
resume_Int(stack, UV_EALREADY);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
tcp_read_closure_t* read_closure = malloc(sizeof(tcp_read_closure_t));
|
|
296
500
|
read_closure->stack = stack;
|
|
297
501
|
read_closure->buffer = buffer;
|
|
298
502
|
read_closure->offset = offset;
|
|
503
|
+
read_closure->size = size;
|
|
299
504
|
stream->data = read_closure;
|
|
300
505
|
|
|
301
506
|
int result = uv_read_start(stream, c_tcp_read_alloc_cb, c_tcp_read_cb);
|
|
302
507
|
|
|
303
508
|
if (result < 0) {
|
|
304
|
-
free(read_closure);
|
|
305
509
|
stream->data = NULL;
|
|
510
|
+
erasePositive(buffer);
|
|
511
|
+
free(read_closure);
|
|
306
512
|
resume_Int(stack, result);
|
|
307
513
|
}
|
|
308
514
|
}
|
|
309
515
|
|
|
516
|
+
typedef struct {
|
|
517
|
+
Stack stack;
|
|
518
|
+
struct Pos buffer;
|
|
519
|
+
size_t size;
|
|
520
|
+
} tcp_write_closure_t;
|
|
521
|
+
|
|
310
522
|
void c_tcp_write_cb(uv_write_t* request, int status) {
|
|
311
|
-
|
|
523
|
+
tcp_write_closure_t* write_closure = (tcp_write_closure_t*)request->data;
|
|
312
524
|
free(request);
|
|
313
525
|
erasePositive(write_closure->buffer);
|
|
314
526
|
Stack stack = write_closure->stack;
|
|
527
|
+
// `uv_write` writes everything or fails, but our contract returns the number of bytes written.
|
|
528
|
+
int64_t written = (int64_t)write_closure->size;
|
|
315
529
|
free(write_closure);
|
|
316
|
-
resume_Int(stack, (int64_t)status);
|
|
530
|
+
resume_Int(stack, status < 0 ? (int64_t)status : written);
|
|
317
531
|
}
|
|
318
532
|
|
|
319
533
|
void c_tcp_write(Int handle, struct Pos buffer, Int offset, Int size, Stack stack) {
|
|
320
|
-
|
|
534
|
+
io_slot_t* open = io_handle_slot(handle);
|
|
535
|
+
if (open == NULL) {
|
|
536
|
+
erasePositive(buffer);
|
|
537
|
+
resume_Int(stack, UV_EBADF);
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
uv_stream_t* stream = (uv_stream_t*)open->value;
|
|
321
541
|
|
|
322
542
|
uv_buf_t buf = uv_buf_init((char*)(c_bytearray_data(buffer) + offset), size);
|
|
323
543
|
|
|
324
|
-
|
|
544
|
+
tcp_write_closure_t* write_closure = malloc(sizeof(tcp_write_closure_t));
|
|
325
545
|
write_closure->stack = stack;
|
|
326
546
|
write_closure->buffer = buffer;
|
|
327
|
-
write_closure->
|
|
547
|
+
write_closure->size = size;
|
|
328
548
|
|
|
329
549
|
uv_write_t* request = malloc(sizeof(uv_write_t));
|
|
330
550
|
request->data = write_closure;
|
|
@@ -333,8 +553,9 @@ void c_tcp_write(Int handle, struct Pos buffer, Int offset, Int size, Stack stac
|
|
|
333
553
|
|
|
334
554
|
if (result < 0) {
|
|
335
555
|
free(request);
|
|
556
|
+
erasePositive(buffer);
|
|
557
|
+
free(write_closure);
|
|
336
558
|
resume_Int(stack, result);
|
|
337
|
-
free(write_closure);
|
|
338
559
|
}
|
|
339
560
|
}
|
|
340
561
|
|
|
@@ -345,40 +566,78 @@ void c_tcp_close_cb(uv_handle_t* handle) {
|
|
|
345
566
|
}
|
|
346
567
|
|
|
347
568
|
void c_tcp_close(Int handle, Stack stack) {
|
|
348
|
-
|
|
569
|
+
intptr_t value;
|
|
570
|
+
if (!io_handle_close(handle, &value)) {
|
|
571
|
+
resume_Pos(stack, Unit);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
uv_handle_t* uv_handle = (uv_handle_t*)value;
|
|
575
|
+
|
|
576
|
+
// A read still pending ends as at end of file, much like on the JS backend.
|
|
577
|
+
tcp_read_closure_t* pending = (tcp_read_closure_t*)uv_handle->data;
|
|
349
578
|
uv_handle->data = stack;
|
|
579
|
+
if (pending != NULL) {
|
|
580
|
+
uv_read_stop((uv_stream_t*)uv_handle);
|
|
581
|
+
Stack reader = pending->stack;
|
|
582
|
+
erasePositive(pending->buffer);
|
|
583
|
+
free(pending);
|
|
584
|
+
resume_Int(reader, 0);
|
|
585
|
+
}
|
|
350
586
|
uv_close(uv_handle, c_tcp_close_cb);
|
|
351
587
|
}
|
|
352
588
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
589
|
+
void c_tcp_listen_cb(uv_stream_t* server, int status);
|
|
590
|
+
|
|
591
|
+
void c_tcp_bind(String address, Int port, Stack stack) {
|
|
592
|
+
struct sockaddr_storage addr;
|
|
593
|
+
int result = c_tcp_parse(address, port, &addr);
|
|
594
|
+
|
|
595
|
+
if (result < 0) {
|
|
596
|
+
resume_Int(stack, result);
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
356
599
|
|
|
357
600
|
uv_tcp_t* tcp_handle = malloc(sizeof(uv_tcp_t));
|
|
358
|
-
|
|
601
|
+
result = uv_tcp_init(uv_default_loop(), tcp_handle);
|
|
359
602
|
|
|
360
603
|
if (result < 0) {
|
|
361
604
|
free(tcp_handle);
|
|
362
|
-
|
|
363
|
-
return
|
|
605
|
+
resume_Int(stack, result);
|
|
606
|
+
return;
|
|
364
607
|
}
|
|
365
608
|
|
|
366
|
-
|
|
367
|
-
result = uv_ip4_addr(host_str, port, &addr);
|
|
368
|
-
free(host_str);
|
|
609
|
+
tcp_handle->data = NULL; // no listen yet
|
|
369
610
|
|
|
611
|
+
result = uv_tcp_bind(tcp_handle, (const struct sockaddr*)&addr, 0);
|
|
370
612
|
if (result < 0) {
|
|
371
613
|
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
372
|
-
|
|
614
|
+
resume_Int(stack, result);
|
|
615
|
+
return;
|
|
373
616
|
}
|
|
374
617
|
|
|
375
|
-
|
|
618
|
+
// Listening already, so that the address is reserved (errors such as `EADDRINUSE` arise here),
|
|
619
|
+
// but connections are only accepted once `listen` starts (see `c_tcp_listen_cb`).
|
|
620
|
+
result = uv_listen((uv_stream_t*)tcp_handle, SOMAXCONN, c_tcp_listen_cb);
|
|
376
621
|
if (result < 0) {
|
|
377
622
|
uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
|
|
378
|
-
|
|
623
|
+
resume_Int(stack, result);
|
|
624
|
+
return;
|
|
379
625
|
}
|
|
380
626
|
|
|
381
|
-
|
|
627
|
+
resume_Int(stack, io_handle_new((intptr_t)tcp_handle));
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
Int c_tcp_port(Int listener) {
|
|
631
|
+
io_slot_t* open = io_handle_slot(listener);
|
|
632
|
+
if (open == NULL) return UV_EBADF;
|
|
633
|
+
|
|
634
|
+
struct sockaddr_storage addr;
|
|
635
|
+
int length = sizeof(addr);
|
|
636
|
+
int result = uv_tcp_getsockname((uv_tcp_t*)open->value, (struct sockaddr*)&addr, &length);
|
|
637
|
+
if (result < 0) return result;
|
|
638
|
+
|
|
639
|
+
if (addr.ss_family == AF_INET6) return ntohs(((struct sockaddr_in6*)&addr)->sin6_port);
|
|
640
|
+
return ntohs(((struct sockaddr_in*)&addr)->sin_port);
|
|
382
641
|
}
|
|
383
642
|
|
|
384
643
|
typedef struct {
|
|
@@ -386,64 +645,61 @@ typedef struct {
|
|
|
386
645
|
struct Neg handler;
|
|
387
646
|
} tcp_listen_closure_t;
|
|
388
647
|
|
|
389
|
-
|
|
648
|
+
// Accepts a connection and runs the handler of `listen` on it.
|
|
649
|
+
void c_tcp_accept(uv_stream_t* server) {
|
|
390
650
|
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
651
|
|
|
402
652
|
uv_tcp_t* client = malloc(sizeof(uv_tcp_t));
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (result < 0) {
|
|
653
|
+
if (uv_tcp_init(uv_default_loop(), client) < 0) {
|
|
406
654
|
free(client);
|
|
407
|
-
server->data = NULL;
|
|
408
|
-
free(listen_closure);
|
|
409
|
-
eraseNegative(closure_handler);
|
|
410
|
-
resume_Int(closure_stack, result);
|
|
411
655
|
return;
|
|
412
656
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
if (result < 0) {
|
|
657
|
+
client->data = NULL; // no pending read yet
|
|
658
|
+
if (uv_accept(server, (uv_stream_t*)client) < 0) {
|
|
416
659
|
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
660
|
return;
|
|
422
661
|
}
|
|
423
662
|
|
|
424
|
-
|
|
425
|
-
|
|
663
|
+
struct Neg handler = listen_closure->handler;
|
|
664
|
+
shareNegative(handler);
|
|
665
|
+
run_Int(handler, io_handle_new((intptr_t)client));
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
void c_tcp_listen_cb(uv_stream_t* server, int status) {
|
|
669
|
+
// An error while accepting concerns that one client, as opposed to the listener:
|
|
670
|
+
// listen returns only once shutdown is called (as on the JS backend), so skip the client and keep listening.
|
|
671
|
+
if (status < 0) return;
|
|
672
|
+
|
|
673
|
+
// Before `listen` starts, libuv keeps the connection alive, and stops accepting until `uv_accept` takes it.
|
|
674
|
+
if (server->data == NULL) return;
|
|
675
|
+
|
|
676
|
+
c_tcp_accept(server);
|
|
426
677
|
}
|
|
427
678
|
|
|
428
679
|
void c_tcp_listen(Int listener, struct Neg handler, Stack stack) {
|
|
429
|
-
|
|
680
|
+
io_slot_t* open = io_handle_slot(listener);
|
|
681
|
+
if (open == NULL) {
|
|
682
|
+
eraseNegative(handler);
|
|
683
|
+
resume_Int(stack, UV_EBADF);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
uv_stream_t* server = (uv_stream_t*)open->value;
|
|
430
687
|
|
|
431
688
|
tcp_listen_closure_t* listen_closure = malloc(sizeof(tcp_listen_closure_t));
|
|
432
689
|
listen_closure->stack = stack;
|
|
433
690
|
listen_closure->handler = handler;
|
|
434
691
|
server->data = listen_closure;
|
|
435
692
|
|
|
436
|
-
|
|
437
|
-
if (result < 0) {
|
|
438
|
-
free(listen_closure);
|
|
439
|
-
eraseNegative(handler);
|
|
440
|
-
resume_Int(stack, result);
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
693
|
+
c_tcp_accept(server);
|
|
443
694
|
}
|
|
444
695
|
|
|
445
696
|
void c_tcp_shutdown(Int handle, Stack stack) {
|
|
446
|
-
|
|
697
|
+
intptr_t value;
|
|
698
|
+
if (!io_handle_close(handle, &value)) {
|
|
699
|
+
resume_Pos(stack, Unit);
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
uv_handle_t* uv_handle = (uv_handle_t*)value;
|
|
447
703
|
tcp_listen_closure_t* listen_closure = (tcp_listen_closure_t*)uv_handle->data;
|
|
448
704
|
|
|
449
705
|
uv_handle->data = stack;
|
|
@@ -578,20 +834,28 @@ void c_yield(Stack stack) {
|
|
|
578
834
|
|
|
579
835
|
// Subprocesses
|
|
580
836
|
// ------------
|
|
837
|
+
//
|
|
838
|
+
// Spawn options are plain Effekt data (see the `process` module):
|
|
839
|
+
// `c_spawn` receives, per each standard stream, whether to pipe it (1) or inherit it (0), and the callback to run for it,
|
|
840
|
+
// and uses that to build libuv's options only for the duration of `uv_spawn`.
|
|
841
|
+
// The pipes belong to the process in question and are closed when it closes.
|
|
842
|
+
|
|
843
|
+
// The standard input of a spawned process, as an Effekt object holding its pipe.
|
|
844
|
+
// The pipe belongs to the process, so the eraser only frees the object.
|
|
845
|
+
typedef struct {
|
|
846
|
+
uv_stream_t* stream;
|
|
847
|
+
} writable_stream_t;
|
|
581
848
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
options->stdio = stdio;
|
|
593
|
-
options->stdio_count = 3;
|
|
594
|
-
return (struct Pos) { .tag = 0, .obj = options, };
|
|
849
|
+
void c_writable_stream_erase(void* object) { free(object); }
|
|
850
|
+
|
|
851
|
+
struct Pos c_writable_stream_new(uv_stream_t* stream) {
|
|
852
|
+
writable_stream_t* writable = (writable_stream_t*)effekt_alloc(c_writable_stream_erase, sizeof(writable_stream_t));
|
|
853
|
+
writable->stream = stream;
|
|
854
|
+
return (struct Pos) { .tag = 0, .obj = (char*)writable - sizeof(struct Header) };
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
uv_stream_t* c_writable_stream(struct Pos writable) {
|
|
858
|
+
return ((writable_stream_t*)((char*)writable.obj + sizeof(struct Header)))->stream;
|
|
595
859
|
}
|
|
596
860
|
|
|
597
861
|
static void subproc_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) {
|
|
@@ -600,86 +864,87 @@ static void subproc_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_
|
|
|
600
864
|
buf->len = suggested_size;
|
|
601
865
|
}
|
|
602
866
|
|
|
867
|
+
typedef struct subproc_proc_data subproc_proc_data_t;
|
|
868
|
+
|
|
603
869
|
typedef struct {
|
|
604
870
|
struct Neg handler;
|
|
871
|
+
subproc_proc_data_t* process;
|
|
605
872
|
} subproc_stream_cb_closure_t;
|
|
873
|
+
|
|
874
|
+
void subproc_finish(subproc_proc_data_t* pd);
|
|
875
|
+
|
|
876
|
+
// Calling a handler consumes a reference to it,
|
|
877
|
+
// so it is shared before each chunk and released once the stream ends (as in `c_tcp_listen_cb`).
|
|
606
878
|
void subproc_stream_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
|
|
607
879
|
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)(stream->data);
|
|
608
|
-
if(nread
|
|
880
|
+
if (nread > 0) {
|
|
609
881
|
struct Pos chunk = c_bytearray_construct(nread, (uint8_t*)(buf->base));
|
|
610
882
|
if (buf->base) free(buf->base);
|
|
883
|
+
shareNegative(clos->handler);
|
|
611
884
|
run_Pos(clos->handler, chunk);
|
|
612
|
-
} else {
|
|
885
|
+
} else if (nread < 0) {
|
|
886
|
+
subproc_proc_data_t* pd = clos->process;
|
|
613
887
|
uv_read_stop(stream);
|
|
614
888
|
eraseNegative(clos->handler);
|
|
615
889
|
free(clos);
|
|
890
|
+
stream->data = NULL;
|
|
616
891
|
if (buf->base) free(buf->base);
|
|
892
|
+
subproc_finish(pd);
|
|
893
|
+
} else {
|
|
894
|
+
if (buf->base) free(buf->base); // 0: nothing read this time
|
|
617
895
|
}
|
|
618
896
|
}
|
|
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
897
|
|
|
645
|
-
|
|
898
|
+
// `spawn` resumes once the process has exited and each of its piped outputs has ended,
|
|
899
|
+
// so `pending` counts these, and `status` keeps the exit status until then.
|
|
900
|
+
struct subproc_proc_data {
|
|
646
901
|
char** args;
|
|
647
902
|
Stack k;
|
|
648
|
-
|
|
649
|
-
|
|
903
|
+
uv_process_t* process;
|
|
904
|
+
uv_pipe_t* pipes[3]; // NULL iff the stream is inherited
|
|
905
|
+
int pending;
|
|
906
|
+
Int status;
|
|
907
|
+
};
|
|
908
|
+
|
|
909
|
+
static void subproc_free_args(char** args) {
|
|
910
|
+
for (int i = 0; args[i] != NULL; i++) {
|
|
911
|
+
free(args[i]);
|
|
912
|
+
}
|
|
913
|
+
free(args);
|
|
914
|
+
}
|
|
915
|
+
|
|
650
916
|
void c_close_stream_callback_exit(uv_handle_t* req) {
|
|
651
917
|
free(req);
|
|
652
918
|
}
|
|
919
|
+
|
|
653
920
|
void subproc_on_close(uv_handle_t* handle) {
|
|
654
921
|
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);
|
|
922
|
+
for (int i = 0; i < 3; i++) {
|
|
923
|
+
if (pd->pipes[i] != NULL) {
|
|
924
|
+
uv_close((uv_handle_t*)pd->pipes[i], c_close_stream_callback_exit);
|
|
925
|
+
}
|
|
664
926
|
}
|
|
665
|
-
// TODO shutdown pipes for stdio
|
|
666
|
-
free(opts->stdio);
|
|
667
|
-
free(opts);
|
|
668
927
|
free(pd);
|
|
669
928
|
free(handle);
|
|
670
929
|
}
|
|
930
|
+
|
|
931
|
+
void subproc_finish(subproc_proc_data_t* pd) {
|
|
932
|
+
pd->pending--;
|
|
933
|
+
if (pd->pending > 0) return;
|
|
934
|
+
|
|
935
|
+
Stack k = pd->k;
|
|
936
|
+
Int status = pd->status;
|
|
937
|
+
uv_close((uv_handle_t*)pd->process, subproc_on_close);
|
|
938
|
+
resume_Int(k, status);
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// The status is the exit code, or the signal times 2^32 (see `process::internal::exitStatus`).
|
|
671
942
|
void subproc_on_exit(uv_process_t* proc, int64_t exit_status, int term_signal) {
|
|
672
|
-
(void)term_signal; (void)exit_status;
|
|
673
943
|
subproc_proc_data_t* pd = (subproc_proc_data_t*)proc->data;
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
free(pd->args);
|
|
679
|
-
}
|
|
680
|
-
Stack k = pd->k;
|
|
681
|
-
uv_close((uv_handle_t*)proc, subproc_on_close);
|
|
682
|
-
resume_Int(k, exit_status);
|
|
944
|
+
subproc_free_args(pd->args);
|
|
945
|
+
pd->args = NULL;
|
|
946
|
+
pd->status = term_signal != 0 ? (Int)term_signal * 4294967296 : exit_status;
|
|
947
|
+
subproc_finish(pd);
|
|
683
948
|
}
|
|
684
949
|
|
|
685
950
|
void c_close_stream_callback(uv_shutdown_t* req, int status) {
|
|
@@ -688,8 +953,10 @@ void c_close_stream_callback(uv_shutdown_t* req, int status) {
|
|
|
688
953
|
}
|
|
689
954
|
free(req);
|
|
690
955
|
}
|
|
691
|
-
|
|
692
|
-
|
|
956
|
+
|
|
957
|
+
void c_close_stream(struct Pos writable) {
|
|
958
|
+
uv_stream_t* str = c_writable_stream(writable);
|
|
959
|
+
erasePositive(writable);
|
|
693
960
|
uv_shutdown_t* req = (uv_shutdown_t*)malloc(sizeof(uv_shutdown_t));
|
|
694
961
|
uv_shutdown(req, str, c_close_stream_callback);
|
|
695
962
|
}
|
|
@@ -698,6 +965,7 @@ typedef struct {
|
|
|
698
965
|
struct Pos buffer;
|
|
699
966
|
uv_buf_t buf;
|
|
700
967
|
} subproc_write_req_data_t;
|
|
968
|
+
|
|
701
969
|
void c_write_stream_callback(uv_write_t* req, int status) {
|
|
702
970
|
if(status != 0){
|
|
703
971
|
// TODO error handling
|
|
@@ -707,8 +975,10 @@ void c_write_stream_callback(uv_write_t* req, int status) {
|
|
|
707
975
|
free(data);
|
|
708
976
|
free(req);
|
|
709
977
|
}
|
|
710
|
-
|
|
711
|
-
|
|
978
|
+
|
|
979
|
+
void c_write_stream(struct Pos writable, struct Pos buffer) {
|
|
980
|
+
uv_stream_t* str = c_writable_stream(writable);
|
|
981
|
+
erasePositive(writable);
|
|
712
982
|
uv_write_t* req = (uv_write_t*)malloc(sizeof(uv_write_t));
|
|
713
983
|
memset(req, 0, sizeof(uv_write_t));
|
|
714
984
|
subproc_write_req_data_t* data = (subproc_write_req_data_t*)malloc(sizeof(subproc_write_req_data_t));
|
|
@@ -719,14 +989,39 @@ void c_write_stream(struct Pos stream, struct Pos buffer) {
|
|
|
719
989
|
uv_write(req, str, &(data->buf), 1, c_write_stream_callback);
|
|
720
990
|
}
|
|
721
991
|
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
992
|
+
// Connects standard stream `id` of the process `pd` to a new pipe running `handler` if `pipe` is 1, or inherits it.
|
|
993
|
+
static uv_pipe_t* subproc_stdio(subproc_proc_data_t* pd, uv_stdio_container_t* stdio, int id, Int pipe, int flags, struct Neg handler) {
|
|
994
|
+
if (!pipe) {
|
|
995
|
+
eraseNegative(handler);
|
|
996
|
+
stdio[id].flags = UV_INHERIT_FD;
|
|
997
|
+
stdio[id].data.fd = id;
|
|
998
|
+
return NULL;
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
uv_pipe_t* p = (uv_pipe_t*)malloc(sizeof(uv_pipe_t));
|
|
1002
|
+
uv_pipe_init(uv_default_loop(), p, 1);
|
|
1003
|
+
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)malloc(sizeof(subproc_stream_cb_closure_t));
|
|
1004
|
+
clos->handler = handler;
|
|
1005
|
+
clos->process = pd;
|
|
1006
|
+
p->data = clos;
|
|
1007
|
+
stdio[id].flags = UV_CREATE_PIPE | flags;
|
|
1008
|
+
stdio[id].data.stream = (uv_stream_t*)p;
|
|
1009
|
+
return p;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
// Note: The struct arguments come first (on x64, they may get split by LLVM)
|
|
1013
|
+
void c_spawn(struct Pos cmd, struct Pos args, struct Neg on_stdin, struct Neg on_stdout, struct Neg on_stderr,
|
|
1014
|
+
Int pipeStdin, Int pipeStdout, Int pipeStderr, Stack stack) {
|
|
1015
|
+
subproc_proc_data_t* pd = (subproc_proc_data_t*)malloc(sizeof(subproc_proc_data_t));
|
|
1016
|
+
|
|
1017
|
+
uv_stdio_container_t stdio[3];
|
|
1018
|
+
pd->pipes[0] = subproc_stdio(pd, stdio, 0, pipeStdin, UV_READABLE_PIPE, on_stdin);
|
|
1019
|
+
pd->pipes[1] = subproc_stdio(pd, stdio, 1, pipeStdout, UV_WRITABLE_PIPE, on_stdout);
|
|
1020
|
+
pd->pipes[2] = subproc_stdio(pd, stdio, 2, pipeStderr, UV_WRITABLE_PIPE, on_stderr);
|
|
725
1021
|
|
|
726
1022
|
// command
|
|
727
1023
|
char* cmd_s = c_bytearray_into_nullterminated_string(cmd);
|
|
728
1024
|
erasePositive(cmd);
|
|
729
|
-
opts->file = cmd_s;
|
|
730
1025
|
|
|
731
1026
|
// args
|
|
732
1027
|
int _argc = (int)((uint64_t*)args.obj)[2];
|
|
@@ -738,37 +1033,55 @@ void c_spawn(struct Pos cmd, struct Pos args, struct Pos options, Stack stack) {
|
|
|
738
1033
|
}
|
|
739
1034
|
erasePositive(args);
|
|
740
1035
|
_args[_argc + 1] = NULL;
|
|
741
|
-
|
|
742
|
-
//
|
|
743
|
-
opts
|
|
744
|
-
|
|
1036
|
+
|
|
1037
|
+
// libuv only reads the options during `uv_spawn`.
|
|
1038
|
+
uv_process_options_t opts;
|
|
1039
|
+
memset(&opts, 0, sizeof(uv_process_options_t));
|
|
1040
|
+
opts.file = cmd_s;
|
|
1041
|
+
opts.args = _args;
|
|
1042
|
+
opts.exit_cb = subproc_on_exit;
|
|
1043
|
+
opts.stdio = stdio;
|
|
1044
|
+
opts.stdio_count = 3;
|
|
1045
|
+
|
|
745
1046
|
pd->args = _args;
|
|
746
1047
|
pd->k = stack;
|
|
1048
|
+
uv_process_t* proc = (uv_process_t*)malloc(sizeof(uv_process_t));
|
|
747
1049
|
proc->data = pd;
|
|
1050
|
+
pd->process = proc;
|
|
1051
|
+
pd->pending = 1 + (pd->pipes[1] != NULL) + (pd->pipes[2] != NULL);
|
|
748
1052
|
|
|
749
1053
|
// spawn process
|
|
750
|
-
int err = uv_spawn(uv_default_loop(), proc, opts);
|
|
1054
|
+
int err = uv_spawn(uv_default_loop(), proc, &opts);
|
|
1055
|
+
|
|
1056
|
+
// The loop keeps the process even if `uv_spawn` fails
|
|
1057
|
+
if (err < 0) {
|
|
1058
|
+
for (int i = 0; i < 3; i++) {
|
|
1059
|
+
if (pd->pipes[i] != NULL) {
|
|
1060
|
+
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)(pd->pipes[i]->data);
|
|
1061
|
+
eraseNegative(clos->handler);
|
|
1062
|
+
free(clos);
|
|
1063
|
+
uv_close((uv_handle_t*)pd->pipes[i], c_close_stream_callback_exit);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
subproc_free_args(_args);
|
|
1067
|
+
free(pd);
|
|
1068
|
+
uv_close((uv_handle_t*)proc, (uv_close_cb)free);
|
|
1069
|
+
resume_Int(stack, err);
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
751
1072
|
|
|
752
|
-
if(
|
|
753
|
-
uv_stream_t* stream = (uv_stream_t*)
|
|
1073
|
+
if (pd->pipes[0] != NULL) {
|
|
1074
|
+
uv_stream_t* stream = (uv_stream_t*)pd->pipes[0];
|
|
754
1075
|
subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)(stream->data);
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
eraseNegative(clos->handler);
|
|
1076
|
+
stream->data = NULL;
|
|
1077
|
+
struct Neg handler = clos->handler;
|
|
758
1078
|
free(clos);
|
|
1079
|
+
run_Pos(handler, c_writable_stream_new(stream)); // consumes the handler's reference
|
|
759
1080
|
}
|
|
760
|
-
if(
|
|
761
|
-
uv_read_start((uv_stream_t*)
|
|
762
|
-
if(
|
|
763
|
-
uv_read_start((uv_stream_t*)
|
|
764
|
-
pd->opts = opts;
|
|
765
|
-
|
|
766
|
-
erasePositive(options);
|
|
767
|
-
if(err) {
|
|
768
|
-
printf("%s\n", uv_strerror(err));
|
|
769
|
-
free(_args);
|
|
770
|
-
free(proc);
|
|
771
|
-
}
|
|
1081
|
+
if (pd->pipes[1] != NULL)
|
|
1082
|
+
uv_read_start((uv_stream_t*)pd->pipes[1], subproc_alloc_cb, subproc_stream_cb);
|
|
1083
|
+
if (pd->pipes[2] != NULL)
|
|
1084
|
+
uv_read_start((uv_stream_t*)pd->pipes[2], subproc_alloc_cb, subproc_stream_cb);
|
|
772
1085
|
}
|
|
773
1086
|
|
|
774
1087
|
// Returns the required buffer size if the variable exists, or -1 if it does not.
|