Replace Rust host-native with 100% pure-MoonBit Wayland compositor
CI / test (push) Successful in 36s
CI / test (push) Successful in 36s
This commit is contained in:
1 parent
17a8a46cfd
commit
ae4f3fa93c
31 files changed
+3647
-4962
No files matched your search
@@ -0,0 +1,9 @@
|
||||
import {
|
||||
"moonbitlang/core/builtin",
|
||||
}
|
||||
|
||||
supported_targets = "-all+native"
|
||||
|
||||
options(
|
||||
"native-stub": [ "sys_stub.c" ],
|
||||
)
|
||||
@@ -0,0 +1,172 @@
|
||||
///| Minimal POSIX OS primitives for the pure-MoonBit Wayland compositor.
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_unix_listen(path : Bytes) -> Int = "mp_unix_listen"
|
||||
|
||||
///|
|
||||
extern "C" fn c_unix_accept(listen_fd : Int) -> Int = "mp_unix_accept"
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_unix_connect(path : Bytes) -> Int = "mp_unix_connect"
|
||||
|
||||
///|
|
||||
extern "C" fn c_close_fd(fd : Int) -> Int = "mp_close_fd"
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_unlink(path : Bytes) -> Int = "mp_unlink"
|
||||
|
||||
///|
|
||||
extern "C" fn c_poll_read(fd : Int, timeout_ms : Int) -> Int = "mp_poll_read"
|
||||
|
||||
///|
|
||||
#borrow(buf, out_fd)
|
||||
extern "C" fn c_recvmsg_fd(
|
||||
fd : Int,
|
||||
buf : FixedArray[Byte],
|
||||
max_len : Int,
|
||||
out_fd : FixedArray[Int],
|
||||
) -> Int = "mp_recvmsg_fd"
|
||||
|
||||
///|
|
||||
#borrow(buf)
|
||||
extern "C" fn c_sendmsg_fd(
|
||||
fd : Int,
|
||||
buf : Bytes,
|
||||
len : Int,
|
||||
send_fd : Int,
|
||||
) -> Int = "mp_sendmsg_fd"
|
||||
|
||||
///|
|
||||
#borrow(name)
|
||||
extern "C" fn c_memfd_create(name : Bytes, size : Int) -> Int = "mp_memfd_create"
|
||||
|
||||
///|
|
||||
#borrow(buf)
|
||||
extern "C" fn c_pwrite_bytes(
|
||||
fd : Int,
|
||||
buf : Bytes,
|
||||
len : Int,
|
||||
offset : Int,
|
||||
) -> Int = "mp_pwrite_bytes"
|
||||
|
||||
///|
|
||||
extern "C" fn c_mmap_read(fd : Int, size : Int) -> Bytes = "mp_mmap_read"
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_read_file(path : Bytes) -> Bytes = "mp_read_file"
|
||||
|
||||
///|
|
||||
#borrow(path, buf)
|
||||
extern "C" fn c_write_file(path : Bytes, buf : Bytes, len : Int) -> Int = "mp_write_file"
|
||||
|
||||
///|
|
||||
extern "C" fn c_sleep_ms(ms : Int) -> Unit = "mp_sleep_ms"
|
||||
|
||||
///|
|
||||
extern "C" fn c_getpid() -> Int = "mp_getpid"
|
||||
|
||||
///| Encode ASCII/UTF-8 String into Bytes for C paths/names.
|
||||
pub fn str_to_utf8_bytes(s : String) -> Bytes {
|
||||
let buf : Array[Byte] = []
|
||||
for ch in s {
|
||||
let code = ch.to_int()
|
||||
if code < 0x80 {
|
||||
buf.push(code.to_byte())
|
||||
} else if code < 0x800 {
|
||||
buf.push((0xC0 | (code >> 6)).to_byte())
|
||||
buf.push((0x80 | (code & 0x3F)).to_byte())
|
||||
} else if code < 0x10000 {
|
||||
buf.push((0xE0 | (code >> 12)).to_byte())
|
||||
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
|
||||
buf.push((0x80 | (code & 0x3F)).to_byte())
|
||||
} else {
|
||||
buf.push((0xF0 | (code >> 18)).to_byte())
|
||||
buf.push((0x80 | ((code >> 12) & 0x3F)).to_byte())
|
||||
buf.push((0x80 | ((code >> 6) & 0x3F)).to_byte())
|
||||
buf.push((0x80 | (code & 0x3F)).to_byte())
|
||||
}
|
||||
}
|
||||
Bytes::from_array(buf)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn unix_listen(path : String) -> Int {
|
||||
c_unix_listen(str_to_utf8_bytes(path))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn unix_accept(listen_fd : Int) -> Int {
|
||||
c_unix_accept(listen_fd)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn unix_connect(path : String) -> Int {
|
||||
c_unix_connect(str_to_utf8_bytes(path))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn close_fd(fd : Int) -> Unit {
|
||||
ignore(c_close_fd(fd))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn unlink(path : String) -> Unit {
|
||||
ignore(c_unlink(str_to_utf8_bytes(path)))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
|
||||
c_poll_read(fd, timeout_ms)
|
||||
}
|
||||
|
||||
///| Receive bytes and optional SCM_RIGHTS fd. Returns (bytes_read, received_fd).
|
||||
/// bytes_read == -1 means EAGAIN; -2 means error; 0 means EOF.
|
||||
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
|
||||
let out_fd : FixedArray[Int] = FixedArray::make(1, -1)
|
||||
let n = c_recvmsg_fd(fd, buf, max_len, out_fd)
|
||||
(n, out_fd[0])
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn sendmsg_fd(fd : Int, buf : Bytes, send_fd : Int) -> Int {
|
||||
c_sendmsg_fd(fd, buf, buf.length(), send_fd)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn memfd_create(name : String, size : Int) -> Int {
|
||||
c_memfd_create(str_to_utf8_bytes(name), size)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
|
||||
c_pwrite_bytes(fd, buf, buf.length(), offset)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn mmap_read(fd : Int, size : Int) -> Bytes {
|
||||
c_mmap_read(fd, size)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn read_file_bytes(path : String) -> Bytes {
|
||||
c_read_file(str_to_utf8_bytes(path))
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn write_file_bytes(path : String, data : Bytes) -> Int {
|
||||
c_write_file(str_to_utf8_bytes(path), data, data.length())
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn sleep_ms(ms : Int) -> Unit {
|
||||
c_sleep_ms(ms)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn getpid() -> Int {
|
||||
c_getpid()
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "moonbit.h"
|
||||
|
||||
static void bytes_to_cstr(moonbit_bytes_t b, char *out, size_t cap) {
|
||||
int32_t len = Moonbit_array_length(b);
|
||||
size_t n = (size_t)len < cap - 1 ? (size_t)len : cap - 1;
|
||||
memcpy(out, b, n);
|
||||
out[n] = '\0';
|
||||
}
|
||||
|
||||
static int set_nonblock_cloexec(int fd) {
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0) {
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
int fdflags = fcntl(fd, F_GETFD, 0);
|
||||
if (fdflags >= 0) {
|
||||
fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_unix_listen(moonbit_bytes_t path_bytes) {
|
||||
char path[108];
|
||||
bytes_to_cstr(path_bytes, path, sizeof(path));
|
||||
unlink(path);
|
||||
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) return -1;
|
||||
set_nonblock_cloexec(fd);
|
||||
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
if (listen(fd, 16) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_unix_accept(int32_t listen_fd) {
|
||||
int fd = accept(listen_fd, NULL, NULL);
|
||||
if (fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
set_nonblock_cloexec(fd);
|
||||
return fd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_unix_connect(moonbit_bytes_t path_bytes) {
|
||||
char path[108];
|
||||
bytes_to_cstr(path_bytes, path, sizeof(path));
|
||||
|
||||
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
struct sockaddr_un addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sun_family = AF_UNIX;
|
||||
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
|
||||
|
||||
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_close_fd(int32_t fd) {
|
||||
if (fd < 0) return 0;
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_unlink(moonbit_bytes_t path_bytes) {
|
||||
char path[512];
|
||||
bytes_to_cstr(path_bytes, path, sizeof(path));
|
||||
return unlink(path);
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_poll_read(int32_t fd, int32_t timeout_ms) {
|
||||
struct pollfd pfd;
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
pfd.revents = 0;
|
||||
int r = poll(&pfd, 1, timeout_ms);
|
||||
if (r < 0) return -1;
|
||||
if (r == 0) return 0;
|
||||
return (pfd.revents & (POLLIN | POLLHUP | POLLERR)) ? 1 : 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t *out_fd) {
|
||||
out_fd[0] = -1;
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
int32_t n = max_len < cap ? max_len : cap;
|
||||
if (n <= 0) return 0;
|
||||
|
||||
struct iovec iov;
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = (size_t)n;
|
||||
|
||||
char cmsg_buf[CMSG_SPACE(sizeof(int) * 4)];
|
||||
memset(cmsg_buf, 0, sizeof(cmsg_buf));
|
||||
|
||||
struct msghdr msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
msg.msg_control = cmsg_buf;
|
||||
msg.msg_controllen = sizeof(cmsg_buf);
|
||||
|
||||
ssize_t recvd = recvmsg(fd, &msg, 0);
|
||||
if (recvd < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) return -1;
|
||||
return -2;
|
||||
}
|
||||
|
||||
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
|
||||
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
||||
int *fds = (int *)CMSG_DATA(cmsg);
|
||||
size_t num_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
||||
if (num_fds > 0) {
|
||||
out_fd[0] = fds[0];
|
||||
for (size_t i = 1; i < num_fds; i++) {
|
||||
close(fds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (int32_t)recvd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_sendmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t send_fd) {
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
int32_t n = len < cap ? len : cap;
|
||||
if (n <= 0) return 0;
|
||||
|
||||
struct iovec iov;
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = (size_t)n;
|
||||
|
||||
char cmsg_buf[CMSG_SPACE(sizeof(int))];
|
||||
memset(cmsg_buf, 0, sizeof(cmsg_buf));
|
||||
|
||||
struct msghdr msg;
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
msg.msg_iov = &iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
if (send_fd >= 0) {
|
||||
msg.msg_control = cmsg_buf;
|
||||
msg.msg_controllen = sizeof(cmsg_buf);
|
||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
cmsg->cmsg_level = SOL_SOCKET;
|
||||
cmsg->cmsg_type = SCM_RIGHTS;
|
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
||||
memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(int));
|
||||
}
|
||||
|
||||
size_t total = 0;
|
||||
while (total < (size_t)n) {
|
||||
iov.iov_base = buf + total;
|
||||
iov.iov_len = (size_t)n - total;
|
||||
ssize_t sent = sendmsg(fd, &msg, MSG_NOSIGNAL);
|
||||
if (sent < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
struct pollfd pfd = { .fd = fd, .events = POLLOUT, .revents = 0 };
|
||||
poll(&pfd, 1, 50);
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
total += (size_t)sent;
|
||||
// Only send ancillary data on the first sendmsg call
|
||||
msg.msg_control = NULL;
|
||||
msg.msg_controllen = 0;
|
||||
}
|
||||
return (int32_t)total;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_memfd_create(moonbit_bytes_t name_bytes, int32_t size) {
|
||||
char name[64];
|
||||
bytes_to_cstr(name_bytes, name, sizeof(name));
|
||||
int fd = memfd_create(name, MFD_CLOEXEC);
|
||||
if (fd < 0) return -1;
|
||||
if (size > 0 && ftruncate(fd, (off_t)size) < 0) {
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_pwrite_bytes(int32_t fd, moonbit_bytes_t buf, int32_t len, int32_t offset) {
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
int32_t n = len < cap ? len : cap;
|
||||
ssize_t w = pwrite(fd, buf, (size_t)n, (off_t)offset);
|
||||
return w < 0 ? -1 : (int32_t)w;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
moonbit_bytes_t mp_mmap_read(int32_t fd, int32_t size) {
|
||||
if (fd < 0 || size <= 0) {
|
||||
return moonbit_make_bytes_raw(0);
|
||||
}
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0 || st.st_size < (off_t)size) {
|
||||
return moonbit_make_bytes_raw(0);
|
||||
}
|
||||
void *ptr = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (ptr == MAP_FAILED) {
|
||||
return moonbit_make_bytes_raw(0);
|
||||
}
|
||||
moonbit_bytes_t out = moonbit_make_bytes_raw(size);
|
||||
memcpy(out, ptr, (size_t)size);
|
||||
munmap(ptr, (size_t)size);
|
||||
return out;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
moonbit_bytes_t mp_read_file(moonbit_bytes_t path_bytes) {
|
||||
char path[512];
|
||||
bytes_to_cstr(path_bytes, path, sizeof(path));
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return moonbit_make_bytes_raw(0);
|
||||
fseek(f, 0, SEEK_END);
|
||||
long sz = ftell(f);
|
||||
if (sz <= 0) {
|
||||
fclose(f);
|
||||
return moonbit_make_bytes_raw(0);
|
||||
}
|
||||
fseek(f, 0, SEEK_SET);
|
||||
moonbit_bytes_t out = moonbit_make_bytes_raw((int32_t)sz);
|
||||
size_t rd = fread(out, 1, (size_t)sz, f);
|
||||
fclose(f);
|
||||
if (rd != (size_t)sz) {
|
||||
return moonbit_make_bytes_raw(0);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_write_file(moonbit_bytes_t path_bytes, moonbit_bytes_t buf, int32_t len) {
|
||||
char path[512];
|
||||
bytes_to_cstr(path_bytes, path, sizeof(path));
|
||||
int32_t cap = Moonbit_array_length(buf);
|
||||
int32_t n = len < cap ? len : cap;
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return -1;
|
||||
size_t wr = fwrite(buf, 1, (size_t)n, f);
|
||||
fclose(f);
|
||||
return wr == (size_t)n ? 0 : -1;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
void mp_sleep_ms(int32_t ms) {
|
||||
if (ms > 0) {
|
||||
usleep((useconds_t)ms * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_getpid(void) {
|
||||
return (int32_t)getpid();
|
||||
}
|
||||
Reference in new issue
Block a user