Use moonbitlang/async for process and file IO

Process execution and file access go through @process and @fs. The spinner
is a background task in a task group, and the C stub keeps only getenv,
exit, isatty, symlink and stderr output. orbit info reads installed state
only, and it records and restores package descriptions. just install adds
Orbit's bin directory to ~/.bashrc once.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Haiku 5.5 committed 2026-11-26 22:40:18 -08:00
1 parent 65e5dc966f
commit ec21287549
11 files changed
+236 -213

No files matched your search

+18 -86
View File
@@ -1,17 +1,10 @@
// Native glue for orbit: process execution, file IO and environment access.
// Every string crosses the boundary as UTF-8 `Bytes` (not NUL-terminated).
// Arguments are never decref'd here: if the compiler passes them owned we leak
// a few bytes in a short-lived CLI, which is harmless; if it passes them
// borrowed, decref'ing would be a use-after-free.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include "moonbit.h"
static int last_status = 0;
static char *to_cstr(moonbit_bytes_t b) {
int32_t n = Moonbit_array_length(b);
char *s = (char *)malloc((size_t)n + 1);
@@ -27,84 +20,6 @@ static moonbit_bytes_t from_buf(const char *buf, size_t len) {
return out;
}
static int decode_status(int r) {
if (r == -1) return -1;
if (WIFEXITED(r)) return WEXITSTATUS(r);
return 128;
}
int32_t orbit_run(moonbit_bytes_t cmd) {
char *c = to_cstr(cmd);
fflush(stdout);
int r = system(c);
free(c);
return decode_status(r);
}
moonbit_bytes_t orbit_capture(moonbit_bytes_t cmd) {
char *c = to_cstr(cmd);
fflush(stdout);
FILE *p = popen(c, "r");
free(c);
if (!p) {
last_status = -1;
return from_buf("", 0);
}
size_t cap = 4096, len = 0;
char *buf = (char *)malloc(cap);
size_t got;
while ((got = fread(buf + len, 1, cap - len, p)) > 0) {
len += got;
if (len == cap) {
cap *= 2;
buf = (char *)realloc(buf, cap);
if (!buf) abort();
}
}
last_status = decode_status(pclose(p));
moonbit_bytes_t out = from_buf(buf, len);
free(buf);
return out;
}
int32_t orbit_last_status(void) { return last_status; }
moonbit_bytes_t orbit_read_file(moonbit_bytes_t path) {
char *p = to_cstr(path);
FILE *f = fopen(p, "rb");
free(p);
if (!f) {
last_status = -1;
return from_buf("", 0);
}
size_t cap = 4096, len = 0, got;
char *buf = (char *)malloc(cap);
while ((got = fread(buf + len, 1, cap - len, f)) > 0) {
len += got;
if (len == cap) {
cap *= 2;
buf = (char *)realloc(buf, cap);
if (!buf) abort();
}
}
fclose(f);
last_status = 0;
moonbit_bytes_t out = from_buf(buf, len);
free(buf);
return out;
}
int32_t orbit_write_file(moonbit_bytes_t path, moonbit_bytes_t data) {
char *p = to_cstr(path);
FILE *f = fopen(p, "wb");
free(p);
if (!f) return -1;
int32_t n = Moonbit_array_length(data);
size_t w = fwrite(data, 1, (size_t)n, f);
int rc = fclose(f);
return (w == (size_t)n && rc == 0) ? 0 : -1;
}
moonbit_bytes_t orbit_getenv(moonbit_bytes_t name) {
char *n = to_cstr(name);
const char *v = getenv(n);
@@ -117,3 +32,20 @@ void orbit_exit(int32_t code) {
fflush(stdout);
exit(code);
}
int32_t orbit_symlink(moonbit_bytes_t target, moonbit_bytes_t linkpath) {
char *t = to_cstr(target);
char *l = to_cstr(linkpath);
unlink(l);
int rc = symlink(t, l);
free(t);
free(l);
return rc;
}
void orbit_print_err(moonbit_bytes_t msg) {
char *m = to_cstr(msg);
fputs(m, stderr);
fflush(stderr);
free(m);
}