MoonBit host for the compositor component on prebuilt libwasmtime (sys, font, raster)
CI / test (push) Failing after 17s
CI / test (push) Failing after 17s
wasmhost/ runs build/compositor.wasm with wasmtime linked as the upstream C API library (fetched by setup-toolchain.sh) instead of compiling it: a cold build is about 4 s against about 2 min for host-wasm. sys and font reuse the native compositor's packages; the rasteriser is a port of host-wasm/src/raster.rs with the pixel work in C, SVG through nanosvg. Panel modules, --drm and --window come next; host-wasm stays the working host until then. wit: sys.append-file. The C API moves list<u8> as one dynamic value per byte and wasmtime caps what one call may copy, so the component writes files over 1 MiB as write-file plus appends (host-wasm implements it too). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
This commit is contained in:
26 files changed
+7592
-2
No files matched your search
@@ -0,0 +1,365 @@
|
||||
///| A component-model value as it crosses to and from wasmtime (encoding in wt_stub.c).
|
||||
pub(all) enum Val {
|
||||
Bool(Bool)
|
||||
S8(Int)
|
||||
U8(Int)
|
||||
S16(Int)
|
||||
U16(Int)
|
||||
S32(Int)
|
||||
U32(UInt)
|
||||
S64(Int64)
|
||||
U64(UInt64)
|
||||
F32(Float)
|
||||
F64(Double)
|
||||
Char(Char)
|
||||
Str(String)
|
||||
List(Array[Val])
|
||||
Record(Array[(String, Val)])
|
||||
Tuple(Array[Val])
|
||||
Variant(String, Val?)
|
||||
Enum(String)
|
||||
Option(Val?)
|
||||
Result(Bool, Val?)
|
||||
Flags(Array[String])
|
||||
/// A `list<u8>` (encoded as one blob rather than a value per byte).
|
||||
Blob(Bytes)
|
||||
} derive(Show, Eq)
|
||||
|
||||
///| A list of u8 (`list<u8>`) from bytes.
|
||||
pub fn Val::bytes(b : Bytes) -> Val {
|
||||
Blob(b)
|
||||
}
|
||||
|
||||
///| A `list<string>`.
|
||||
pub fn Val::strings(a : Array[String]) -> Val {
|
||||
List(a.map(Str(_)))
|
||||
}
|
||||
|
||||
///| Record field `name` (records are looked up by name, not position).
|
||||
pub fn Val::field(self : Val, name : String) -> Val {
|
||||
match self {
|
||||
Record(fs) => {
|
||||
for f in fs {
|
||||
if f.0 == name {
|
||||
return f.1
|
||||
}
|
||||
}
|
||||
abort("record has no field \{name}")
|
||||
}
|
||||
_ => abort("not a record: field \{name}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_int(self : Val) -> Int {
|
||||
match self {
|
||||
S8(v) | U8(v) | S16(v) | U16(v) | S32(v) => v
|
||||
U32(v) => v.reinterpret_as_int()
|
||||
S64(v) => v.to_int()
|
||||
U64(v) => v.to_int()
|
||||
Bool(b) => if b { 1 } else { 0 }
|
||||
Char(c) => c.to_int()
|
||||
_ => abort("not an integer: \{self}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_uint(self : Val) -> UInt {
|
||||
match self {
|
||||
U32(v) => v
|
||||
_ => self.to_int().reinterpret_as_uint()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_bool(self : Val) -> Bool {
|
||||
match self {
|
||||
Bool(b) => b
|
||||
_ => self.to_int() != 0
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_double(self : Val) -> Double {
|
||||
match self {
|
||||
F64(d) => d
|
||||
F32(f) => f.to_double()
|
||||
_ => self.to_int().to_double()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_string(self : Val) -> String {
|
||||
match self {
|
||||
Str(s) | Enum(s) => s
|
||||
_ => abort("not a string: \{self}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_list(self : Val) -> Array[Val] {
|
||||
match self {
|
||||
List(a) | Tuple(a) => a
|
||||
_ => abort("not a list: \{self}")
|
||||
}
|
||||
}
|
||||
|
||||
///| A `list<u8>` as bytes.
|
||||
pub fn Val::to_bytes(self : Val) -> Bytes {
|
||||
if self is Blob(b) {
|
||||
return b
|
||||
}
|
||||
let a = self.to_list()
|
||||
let out = FixedArray::make(a.length(), b'\x00')
|
||||
for i, v in a {
|
||||
out[i] = v.to_int().to_byte()
|
||||
}
|
||||
Bytes::from_fixedarray(out)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Val::to_option(self : Val) -> Val? {
|
||||
match self {
|
||||
Option(o) => o
|
||||
_ => abort("not an option: \{self}")
|
||||
}
|
||||
}
|
||||
|
||||
// ---- encoding ----
|
||||
|
||||
///|
|
||||
fn put_u32(buf : @buffer.Buffer, v : Int) -> Unit {
|
||||
buf.write_int_le(v)
|
||||
}
|
||||
|
||||
///|
|
||||
fn put_str(buf : @buffer.Buffer, s : String) -> Unit {
|
||||
let b = @utf8.encode(s)
|
||||
put_u32(buf, b.length())
|
||||
buf.write_bytes(b)
|
||||
}
|
||||
|
||||
///|
|
||||
fn kind(v : Val) -> Byte {
|
||||
match v {
|
||||
Bool(_) => 0
|
||||
S8(_) => 1
|
||||
U8(_) => 2
|
||||
S16(_) => 3
|
||||
U16(_) => 4
|
||||
S32(_) => 5
|
||||
U32(_) => 6
|
||||
S64(_) => 7
|
||||
U64(_) => 8
|
||||
F32(_) => 9
|
||||
F64(_) => 10
|
||||
Char(_) => 11
|
||||
Str(_) => 12
|
||||
List(_) => 13
|
||||
Record(_) => 14
|
||||
Tuple(_) => 15
|
||||
Variant(_, _) => 16
|
||||
Enum(_) => 17
|
||||
Option(_) => 18
|
||||
Result(_, _) => 19
|
||||
Flags(_) => 20
|
||||
Blob(_) => 0x80
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn enc(buf : @buffer.Buffer, v : Val) -> Unit {
|
||||
buf.write_byte(kind(v))
|
||||
match v {
|
||||
Bool(b) => buf.write_byte(if b { b'\x01' } else { b'\x00' })
|
||||
S8(x) | U8(x) => buf.write_byte(x.to_byte())
|
||||
S16(x) | U16(x) => {
|
||||
buf.write_byte(x.to_byte())
|
||||
buf.write_byte((x >> 8).to_byte())
|
||||
}
|
||||
S32(x) => buf.write_int_le(x)
|
||||
U32(x) => buf.write_uint_le(x)
|
||||
S64(x) => buf.write_int64_le(x)
|
||||
U64(x) => buf.write_uint64_le(x)
|
||||
F32(x) => buf.write_float_le(x)
|
||||
F64(x) => buf.write_double_le(x)
|
||||
Char(c) => buf.write_int_le(c.to_int())
|
||||
Str(s) | Enum(s) => put_str(buf, s)
|
||||
List(a) | Tuple(a) => {
|
||||
put_u32(buf, a.length())
|
||||
for x in a {
|
||||
enc(buf, x)
|
||||
}
|
||||
}
|
||||
Record(fs) => {
|
||||
put_u32(buf, fs.length())
|
||||
for f in fs {
|
||||
put_str(buf, f.0)
|
||||
enc(buf, f.1)
|
||||
}
|
||||
}
|
||||
Variant(name, p) => {
|
||||
put_str(buf, name)
|
||||
match p {
|
||||
Some(x) => {
|
||||
buf.write_byte(b'\x01')
|
||||
enc(buf, x)
|
||||
}
|
||||
None => buf.write_byte(b'\x00')
|
||||
}
|
||||
}
|
||||
Option(p) =>
|
||||
match p {
|
||||
Some(x) => {
|
||||
buf.write_byte(b'\x01')
|
||||
enc(buf, x)
|
||||
}
|
||||
None => buf.write_byte(b'\x00')
|
||||
}
|
||||
Result(ok, p) => {
|
||||
buf.write_byte(if ok { b'\x01' } else { b'\x00' })
|
||||
match p {
|
||||
Some(x) => {
|
||||
buf.write_byte(b'\x01')
|
||||
enc(buf, x)
|
||||
}
|
||||
None => buf.write_byte(b'\x00')
|
||||
}
|
||||
}
|
||||
Flags(names) => {
|
||||
put_u32(buf, names.length())
|
||||
for n in names {
|
||||
put_str(buf, n)
|
||||
}
|
||||
}
|
||||
Blob(b) => {
|
||||
put_u32(buf, b.length())
|
||||
buf.write_bytes(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Encode a message (a list of values: arguments or results).
|
||||
pub fn encode(vals : Array[Val]) -> Bytes {
|
||||
let buf = @buffer.new(size_hint=64)
|
||||
put_u32(buf, vals.length())
|
||||
for v in vals {
|
||||
enc(buf, v)
|
||||
}
|
||||
buf.to_bytes()
|
||||
}
|
||||
|
||||
// ---- decoding ----
|
||||
|
||||
///|
|
||||
struct Reader {
|
||||
b : Bytes
|
||||
mut pos : Int
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::u8(self : Reader) -> Int {
|
||||
let v = self.b[self.pos].to_int()
|
||||
self.pos += 1
|
||||
v
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::u32(self : Reader) -> UInt {
|
||||
let p = self.pos
|
||||
let v = self.b[p].to_uint() |
|
||||
(self.b[p + 1].to_uint() << 8) |
|
||||
(self.b[p + 2].to_uint() << 16) |
|
||||
(self.b[p + 3].to_uint() << 24)
|
||||
self.pos += 4
|
||||
v
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::u64(self : Reader) -> UInt64 {
|
||||
let lo = self.u32().to_uint64()
|
||||
let hi = self.u32().to_uint64()
|
||||
lo | (hi << 32)
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::str(self : Reader) -> String {
|
||||
let n = self.u32().reinterpret_as_int()
|
||||
let s = @utf8.decode_lossy(self.b[self.pos:self.pos + n])
|
||||
self.pos += n
|
||||
s
|
||||
}
|
||||
|
||||
///|
|
||||
fn Reader::val(self : Reader) -> Val {
|
||||
match self.u8() {
|
||||
0 => Bool(self.u8() != 0)
|
||||
1 => {
|
||||
let x = self.u8()
|
||||
S8(if x >= 128 { x - 256 } else { x })
|
||||
}
|
||||
2 => U8(self.u8())
|
||||
3 => {
|
||||
let x = self.u8() | (self.u8() << 8)
|
||||
S16(if x >= 32768 { x - 65536 } else { x })
|
||||
}
|
||||
4 => U16(self.u8() | (self.u8() << 8))
|
||||
5 => S32(self.u32().reinterpret_as_int())
|
||||
6 => U32(self.u32())
|
||||
7 => S64(self.u64().reinterpret_as_int64())
|
||||
8 => U64(self.u64())
|
||||
9 => F32(Float::reinterpret_from_uint(self.u32()))
|
||||
10 => F64(self.u64().reinterpret_as_double())
|
||||
11 => Char(Int::unsafe_to_char(self.u32().reinterpret_as_int()))
|
||||
12 => Str(self.str())
|
||||
13 | 15 as k => {
|
||||
let n = self.u32().reinterpret_as_int()
|
||||
let a = Array::new(capacity=n)
|
||||
for _ in 0..<n {
|
||||
a.push(self.val())
|
||||
}
|
||||
if k == 13 {
|
||||
List(a)
|
||||
} else {
|
||||
Tuple(a)
|
||||
}
|
||||
}
|
||||
14 => {
|
||||
let n = self.u32().reinterpret_as_int()
|
||||
let a = Array::new(capacity=n)
|
||||
for _ in 0..<n {
|
||||
let name = self.str()
|
||||
a.push((name, self.val()))
|
||||
}
|
||||
Record(a)
|
||||
}
|
||||
16 => {
|
||||
let name = self.str()
|
||||
Variant(name, if self.u8() != 0 { Some(self.val()) } else { None })
|
||||
}
|
||||
17 => Enum(self.str())
|
||||
18 => Option(if self.u8() != 0 { Some(self.val()) } else { None })
|
||||
19 => {
|
||||
let ok = self.u8() != 0
|
||||
Result(ok, if self.u8() != 0 { Some(self.val()) } else { None })
|
||||
}
|
||||
20 => {
|
||||
let n = self.u32().reinterpret_as_int()
|
||||
Flags(Array::makei(n, _ => self.str()))
|
||||
}
|
||||
0x80 => {
|
||||
let n = self.u32().reinterpret_as_int()
|
||||
let b = self.b[self.pos:self.pos + n].to_owned()
|
||||
self.pos += n
|
||||
Blob(b)
|
||||
}
|
||||
k => abort("unknown value kind \{k}")
|
||||
}
|
||||
}
|
||||
|
||||
///| Decode a message.
|
||||
pub fn decode(b : Bytes) -> Array[Val] {
|
||||
let r = { b, pos: 0 }
|
||||
let n = r.u32().reinterpret_as_int()
|
||||
Array::makei(n, _ => r.val())
|
||||
}
|
||||
Reference in new issue
Block a user