@effekt-lang/effekt 0.84.0 → 0.86.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.
@@ -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, c_resume_int_fs);
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,6 +199,13 @@ 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
 
@@ -141,7 +216,7 @@ void c_fs_read(Int file, struct Pos buffer, Int offset, Int size, Int position,
141
216
  uv_fs_t* request = malloc(sizeof(uv_fs_t));
142
217
  request->data = read_closure;
143
218
 
144
- int result = uv_fs_read(uv_default_loop(), request, file, &buf, 1, position, c_fs_cb);
219
+ int result = uv_fs_read(uv_default_loop(), request, open->value, &buf, 1, position, c_fs_cb);
145
220
 
146
221
  if (result < 0) {
147
222
  uv_fs_req_cleanup(request);
@@ -152,6 +227,13 @@ void c_fs_read(Int file, struct Pos buffer, Int offset, Int size, Int position,
152
227
  }
153
228
 
154
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
+
155
237
  (void)size;
156
238
 
157
239
  uv_buf_t buf = uv_buf_init((char*)(c_bytearray_data(buffer) + offset), size);
@@ -163,7 +245,7 @@ void c_fs_write(Int file, struct Pos buffer, Int offset, Int size, Int position,
163
245
  uv_fs_t* request = malloc(sizeof(uv_fs_t));
164
246
  request->data = write_closure;
165
247
 
166
- int result = uv_fs_write(uv_default_loop(), request, file, &buf, 1, position, c_fs_cb);
248
+ int result = uv_fs_write(uv_default_loop(), request, open->value, &buf, 1, position, c_fs_cb);
167
249
 
168
250
  if (result < 0) {
169
251
  uv_fs_req_cleanup(request);
@@ -174,11 +256,17 @@ void c_fs_write(Int file, struct Pos buffer, Int offset, Int size, Int position,
174
256
  }
175
257
 
176
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
+
177
265
 
178
266
  uv_fs_t* request = malloc(sizeof(uv_fs_t));
179
267
  request->data = stack;
180
268
 
181
- int result = uv_fs_close(uv_default_loop(), request, file, c_resume_int_fs);
269
+ int result = uv_fs_close(uv_default_loop(), request, fd, c_resume_int_fs);
182
270
 
183
271
  if (result < 0) {
184
272
  uv_fs_req_cleanup(request);
@@ -222,41 +310,134 @@ void c_tcp_connect_cb(uv_connect_t* request, int status) {
222
310
  free(request);
223
311
  resume_Int(stack, status);
224
312
  } else {
225
- int64_t handle = (int64_t)request->handle;
313
+ uv_tcp_t* handle = (uv_tcp_t*)request->handle;
226
314
  free(request);
227
- resume_Int(stack, handle);
315
+ resume_Int(stack, io_handle_new((intptr_t)handle));
228
316
  }
229
317
  }
230
318
 
231
- void c_tcp_connect(String host, Int port, Stack stack) {
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) {
232
329
  char* host_str = c_bytearray_into_nullterminated_string(host);
233
330
  erasePositive(host);
234
331
 
235
- uv_tcp_t* tcp_handle = malloc(sizeof(uv_tcp_t));
236
- int result = uv_tcp_init(uv_default_loop(), tcp_handle);
237
-
238
- if (result < 0) {
239
- free(tcp_handle);
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) {
240
343
  free(host_str);
241
- resume_Int(stack, result);
344
+ resume_Int(stack, io_handle_new((intptr_t)parsed.addrinfo));
242
345
  return;
243
346
  }
244
- tcp_handle->data = NULL; // no pending read yet (explicit `NULL` since libuv leaves it uninitialised)
245
347
 
246
- uv_connect_t* connect_req = malloc(sizeof(uv_connect_t));
247
- connect_req->data = stack;
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);
248
351
 
249
- struct sockaddr_in addr;
250
- result = uv_ip4_addr(host_str, port, &addr);
352
+ // Free the string since libuv copies it into the request
251
353
  free(host_str);
252
354
 
253
355
  if (result < 0) {
254
- uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
255
- free(connect_req);
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) {
256
424
  resume_Int(stack, result);
257
425
  return;
258
426
  }
259
427
 
428
+ uv_tcp_t* tcp_handle = malloc(sizeof(uv_tcp_t));
429
+ result = uv_tcp_init(uv_default_loop(), tcp_handle);
430
+
431
+ if (result < 0) {
432
+ free(tcp_handle);
433
+ resume_Int(stack, result);
434
+ return;
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;
440
+
260
441
  result = uv_tcp_connect(connect_req, tcp_handle, (const struct sockaddr*)&addr, c_tcp_connect_cb);
261
442
 
262
443
  if (result < 0) {
@@ -300,7 +481,13 @@ void c_tcp_read_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* b
300
481
  }
301
482
 
302
483
  void c_tcp_read(Int handle, struct Pos buffer, Int offset, Int size, Stack stack) {
303
- uv_stream_t* stream = (uv_stream_t*)handle;
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;
304
491
 
305
492
  // At most one read is pending on a connection, its closure is in stream->data.
306
493
  if (stream->data != NULL) {
@@ -344,7 +531,13 @@ void c_tcp_write_cb(uv_write_t* request, int status) {
344
531
  }
345
532
 
346
533
  void c_tcp_write(Int handle, struct Pos buffer, Int offset, Int size, Stack stack) {
347
- uv_stream_t* stream = (uv_stream_t*)handle;
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;
348
541
 
349
542
  uv_buf_t buf = uv_buf_init((char*)(c_bytearray_data(buffer) + offset), size);
350
543
 
@@ -373,7 +566,12 @@ void c_tcp_close_cb(uv_handle_t* handle) {
373
566
  }
374
567
 
375
568
  void c_tcp_close(Int handle, Stack stack) {
376
- uv_handle_t* uv_handle = (uv_handle_t*)handle;
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;
377
575
 
378
576
  // A read still pending ends as at end of file, much like on the JS backend.
379
577
  tcp_read_closure_t* pending = (tcp_read_closure_t*)uv_handle->data;
@@ -390,32 +588,26 @@ void c_tcp_close(Int handle, Stack stack) {
390
588
 
391
589
  void c_tcp_listen_cb(uv_stream_t* server, int status);
392
590
 
393
- void c_tcp_bind(String host, Int port, Stack stack) {
394
- char* host_str = c_bytearray_into_nullterminated_string(host);
395
- erasePositive(host);
396
-
397
- uv_tcp_t* tcp_handle = malloc(sizeof(uv_tcp_t));
398
- int result = uv_tcp_init(uv_default_loop(), tcp_handle);
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);
399
594
 
400
595
  if (result < 0) {
401
- free(tcp_handle);
402
- free(host_str);
403
596
  resume_Int(stack, result);
404
597
  return;
405
598
  }
406
599
 
407
- tcp_handle->data = NULL; // no listen yet
408
-
409
- struct sockaddr_in addr;
410
- result = uv_ip4_addr(host_str, port, &addr);
411
- free(host_str);
600
+ uv_tcp_t* tcp_handle = malloc(sizeof(uv_tcp_t));
601
+ result = uv_tcp_init(uv_default_loop(), tcp_handle);
412
602
 
413
603
  if (result < 0) {
414
- uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
604
+ free(tcp_handle);
415
605
  resume_Int(stack, result);
416
606
  return;
417
607
  }
418
608
 
609
+ tcp_handle->data = NULL; // no listen yet
610
+
419
611
  result = uv_tcp_bind(tcp_handle, (const struct sockaddr*)&addr, 0);
420
612
  if (result < 0) {
421
613
  uv_close((uv_handle_t*)tcp_handle, (uv_close_cb)free);
@@ -432,14 +624,20 @@ void c_tcp_bind(String host, Int port, Stack stack) {
432
624
  return;
433
625
  }
434
626
 
435
- resume_Int(stack, (int64_t)tcp_handle);
627
+ resume_Int(stack, io_handle_new((intptr_t)tcp_handle));
436
628
  }
437
629
 
438
630
  Int c_tcp_port(Int listener) {
439
- struct sockaddr_in addr;
631
+ io_slot_t* open = io_handle_slot(listener);
632
+ if (open == NULL) return UV_EBADF;
633
+
634
+ struct sockaddr_storage addr;
440
635
  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);
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);
443
641
  }
444
642
 
445
643
  typedef struct {
@@ -464,7 +662,7 @@ void c_tcp_accept(uv_stream_t* server) {
464
662
 
465
663
  struct Neg handler = listen_closure->handler;
466
664
  shareNegative(handler);
467
- run_Int(handler, (int64_t)client);
665
+ run_Int(handler, io_handle_new((intptr_t)client));
468
666
  }
469
667
 
470
668
  void c_tcp_listen_cb(uv_stream_t* server, int status) {
@@ -479,7 +677,13 @@ void c_tcp_listen_cb(uv_stream_t* server, int status) {
479
677
  }
480
678
 
481
679
  void c_tcp_listen(Int listener, struct Neg handler, Stack stack) {
482
- uv_stream_t* server = (uv_stream_t*)listener;
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;
483
687
 
484
688
  tcp_listen_closure_t* listen_closure = malloc(sizeof(tcp_listen_closure_t));
485
689
  listen_closure->stack = stack;
@@ -490,7 +694,12 @@ void c_tcp_listen(Int listener, struct Neg handler, Stack stack) {
490
694
  }
491
695
 
492
696
  void c_tcp_shutdown(Int handle, Stack stack) {
493
- uv_handle_t* uv_handle = (uv_handle_t*)handle;
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;
494
703
  tcp_listen_closure_t* listen_closure = (tcp_listen_closure_t*)uv_handle->data;
495
704
 
496
705
  uv_handle->data = stack;
@@ -655,10 +864,15 @@ static void subproc_alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_
655
864
  buf->len = suggested_size;
656
865
  }
657
866
 
867
+ typedef struct subproc_proc_data subproc_proc_data_t;
868
+
658
869
  typedef struct {
659
870
  struct Neg handler;
871
+ subproc_proc_data_t* process;
660
872
  } subproc_stream_cb_closure_t;
661
873
 
874
+ void subproc_finish(subproc_proc_data_t* pd);
875
+
662
876
  // Calling a handler consumes a reference to it,
663
877
  // so it is shared before each chunk and released once the stream ends (as in `c_tcp_listen_cb`).
664
878
  void subproc_stream_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
@@ -669,21 +883,35 @@ void subproc_stream_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
669
883
  shareNegative(clos->handler);
670
884
  run_Pos(clos->handler, chunk);
671
885
  } else if (nread < 0) {
886
+ subproc_proc_data_t* pd = clos->process;
672
887
  uv_read_stop(stream);
673
888
  eraseNegative(clos->handler);
674
889
  free(clos);
675
890
  stream->data = NULL;
676
891
  if (buf->base) free(buf->base);
892
+ subproc_finish(pd);
677
893
  } else {
678
894
  if (buf->base) free(buf->base); // 0: nothing read this time
679
895
  }
680
896
  }
681
897
 
682
- typedef struct {
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 {
683
901
  char** args;
684
902
  Stack k;
903
+ uv_process_t* process;
685
904
  uv_pipe_t* pipes[3]; // NULL iff the stream is inherited
686
- } subproc_proc_data_t;
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
+ }
687
915
 
688
916
  void c_close_stream_callback_exit(uv_handle_t* req) {
689
917
  free(req);
@@ -700,18 +928,23 @@ void subproc_on_close(uv_handle_t* handle) {
700
928
  free(handle);
701
929
  }
702
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`).
703
942
  void subproc_on_exit(uv_process_t* proc, int64_t exit_status, int term_signal) {
704
- (void)term_signal;
705
943
  subproc_proc_data_t* pd = (subproc_proc_data_t*)proc->data;
706
- if (pd->args) {
707
- for(int i = 0; pd->args[i] != NULL; i++) {
708
- free(pd->args[i]);
709
- }
710
- free(pd->args);
711
- }
712
- Stack k = pd->k;
713
- uv_close((uv_handle_t*)proc, subproc_on_close);
714
- 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);
715
948
  }
716
949
 
717
950
  void c_close_stream_callback(uv_shutdown_t* req, int status) {
@@ -756,8 +989,8 @@ void c_write_stream(struct Pos writable, struct Pos buffer) {
756
989
  uv_write(req, str, &(data->buf), 1, c_write_stream_callback);
757
990
  }
758
991
 
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) {
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) {
761
994
  if (!pipe) {
762
995
  eraseNegative(handler);
763
996
  stdio[id].flags = UV_INHERIT_FD;
@@ -769,6 +1002,7 @@ static uv_pipe_t* subproc_stdio(uv_stdio_container_t* stdio, int id, Int pipe, i
769
1002
  uv_pipe_init(uv_default_loop(), p, 1);
770
1003
  subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)malloc(sizeof(subproc_stream_cb_closure_t));
771
1004
  clos->handler = handler;
1005
+ clos->process = pd;
772
1006
  p->data = clos;
773
1007
  stdio[id].flags = UV_CREATE_PIPE | flags;
774
1008
  stdio[id].data.stream = (uv_stream_t*)p;
@@ -781,9 +1015,9 @@ void c_spawn(struct Pos cmd, struct Pos args, struct Neg on_stdin, struct Neg on
781
1015
  subproc_proc_data_t* pd = (subproc_proc_data_t*)malloc(sizeof(subproc_proc_data_t));
782
1016
 
783
1017
  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);
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);
787
1021
 
788
1022
  // command
789
1023
  char* cmd_s = c_bytearray_into_nullterminated_string(cmd);
@@ -813,10 +1047,29 @@ void c_spawn(struct Pos cmd, struct Pos args, struct Neg on_stdin, struct Neg on
813
1047
  pd->k = stack;
814
1048
  uv_process_t* proc = (uv_process_t*)malloc(sizeof(uv_process_t));
815
1049
  proc->data = pd;
1050
+ pd->process = proc;
1051
+ pd->pending = 1 + (pd->pipes[1] != NULL) + (pd->pipes[2] != NULL);
816
1052
 
817
1053
  // spawn process
818
1054
  int err = uv_spawn(uv_default_loop(), proc, &opts);
819
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
+ }
1072
+
820
1073
  if (pd->pipes[0] != NULL) {
821
1074
  uv_stream_t* stream = (uv_stream_t*)pd->pipes[0];
822
1075
  subproc_stream_cb_closure_t* clos = (subproc_stream_cb_closure_t*)(stream->data);
@@ -829,12 +1082,6 @@ void c_spawn(struct Pos cmd, struct Pos args, struct Neg on_stdin, struct Neg on
829
1082
  uv_read_start((uv_stream_t*)pd->pipes[1], subproc_alloc_cb, subproc_stream_cb);
830
1083
  if (pd->pipes[2] != NULL)
831
1084
  uv_read_start((uv_stream_t*)pd->pipes[2], subproc_alloc_cb, subproc_stream_cb);
832
-
833
- if(err) {
834
- printf("%s\n", uv_strerror(err));
835
- free(_args);
836
- free(proc);
837
- }
838
1085
  }
839
1086
 
840
1087
  // Returns the required buffer size if the variable exists, or -1 if it does not.
@@ -99,11 +99,11 @@
99
99
 
100
100
  ; Foreign imports
101
101
 
102
- declare ptr @malloc(i64)
102
+ declare ptr @malloc(i64 zeroext)
103
103
  declare void @free(ptr)
104
- declare ptr @realloc(ptr, i64)
105
- declare noalias ptr @calloc(i64, i64)
106
- declare ptr @memcpy(ptr, ptr, i64)
104
+ declare ptr @realloc(ptr, i64 zeroext)
105
+ declare noalias ptr @calloc(i64 zeroext, i64 zeroext)
106
+ declare ptr @memcpy(ptr, ptr, i64 zeroext)
107
107
  declare i64 @llvm.ctlz.i64 (i64 , i1)
108
108
  declare i64 @llvm.cttz.i64 (i64 , i1)
109
109
  declare i64 @llvm.fshr.i64(i64, i64, i64)
@@ -120,7 +120,7 @@ declare double @log1p(double)
120
120
  ; Intrinsic versions of the following two only added in LLVM 19
121
121
  declare double @atan(double)
122
122
  declare double @tan(double)
123
- declare void @exit(i32)
123
+ declare void @exit(i32 signext)
124
124
  declare void @llvm.assume(i1)
125
125
 
126
126
 
@@ -221,6 +221,26 @@ define void @eraseNegative(%Neg %val) alwaysinline {
221
221
  ret void
222
222
  }
223
223
 
224
+ ; Make sure the given bytearray is unique (if rc=0 then keep it, otherwise copy it because it's shared).
225
+ ; Note that we do the check here as opposed to in C since LTO does not inline C functions on `%Pos` on MacOS.
226
+ define %Pos @uniqueByteArray(%Pos %array) alwaysinline {
227
+ %object = extractvalue %Pos %array, 1
228
+ %isNull = icmp eq %Object %object, null
229
+ br i1 %isNull, label %done, label %next
230
+
231
+ next:
232
+ %objectReferenceCount = getelementptr %Header, ptr %object, i64 0, i32 0
233
+ %referenceCount = load %ReferenceCount, ptr %objectReferenceCount, !alias.scope !14, !noalias !24, !range !41
234
+ switch %ReferenceCount %referenceCount, label %copy [%ReferenceCount 0, label %done]
235
+
236
+ copy:
237
+ %copied = call %Pos @c_bytearray_copy_shared(%Pos %array)
238
+ ret %Pos %copied
239
+
240
+ done:
241
+ ret %Pos %array
242
+ }
243
+
224
244
 
225
245
  ; User-facing C-helpers for GC
226
246
 
@@ -788,7 +808,7 @@ define private ptr @frameDescriptorEntry(%StackPointer %header, i64 %index) alwa
788
808
  %returnAddressPointer = getelementptr %FrameHeader, %StackPointer %header, i64 0, i32 0
789
809
  %returnAddress = load %ReturnAddress, ptr %returnAddressPointer, !alias.scope !12, !noalias !22
790
810
  %offsetPointer = getelementptr i64, ptr %returnAddress, i64 %index
791
- %offset = load i64, ptr %offsetPointer
811
+ %offset = load i64, ptr %offsetPointer, align 1
792
812
  %entry = getelementptr i8, ptr %returnAddress, i64 %offset
793
813
  ret ptr %entry
794
814
  }