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
110 lines
2.5 KiB
MoonBit
110 lines
2.5 KiB
MoonBit
///| Running a component with wasmtime (its C API, linked as a prebuilt library; see wt_stub.c).
|
|
///
|
|
/// The host registers its functions by interface with `define`, each under an id; calls from the
|
|
/// component reach the one handler given to `set_handler` as (id, arguments).
|
|
|
|
///|
|
|
extern "C" fn c_set_dispatch(f : FuncRef[(Int, Bytes) -> Bytes]) = "wt_set_dispatch"
|
|
|
|
///|
|
|
extern "C" fn c_last_error() -> Bytes = "wt_last_error"
|
|
|
|
///|
|
|
extern "C" fn c_init() -> Int = "wt_init"
|
|
|
|
///|
|
|
#borrow(path)
|
|
extern "C" fn c_load(path : Bytes) -> Int = "wt_load"
|
|
|
|
///|
|
|
#borrow(iface, names)
|
|
extern "C" fn c_define(iface : Bytes, names : Bytes, first_id : Int) -> Int = "wt_define"
|
|
|
|
///|
|
|
extern "C" fn c_instantiate() -> Int = "wt_instantiate"
|
|
|
|
///|
|
|
#borrow(iface, name, args)
|
|
extern "C" fn c_call(iface : Bytes, name : Bytes, args : Bytes) -> Bytes = "wt_call"
|
|
|
|
///| What a host function returns: its results (zero or one value).
|
|
let handler : Ref[(Int, Array[Val]) -> Array[Val]] = Ref(fn(id, _) {
|
|
abort("no handler for host function \{id}")
|
|
})
|
|
|
|
///|
|
|
fn dispatch(id : Int, args : Bytes) -> Bytes {
|
|
encode((handler.val)(id, decode(args)))
|
|
}
|
|
|
|
///|
|
|
fn last_error() -> String {
|
|
@utf8.decode_lossy(c_last_error())
|
|
}
|
|
|
|
///|
|
|
fn cstr(s : String) -> Bytes {
|
|
@utf8.encode(s)
|
|
}
|
|
|
|
///| Create the engine and load the component at `path`.
|
|
pub fn load(path : String) -> Result[Unit, String] {
|
|
if c_init() != 0 {
|
|
return Err(last_error())
|
|
}
|
|
c_set_dispatch(fn(id, args) { dispatch(id, args) })
|
|
if c_load(cstr(path)) != 0 {
|
|
return Err(last_error())
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
///| The function that serves every host import.
|
|
pub fn set_handler(f : (Int, Array[Val]) -> Array[Val]) -> Unit {
|
|
handler.val = f
|
|
}
|
|
|
|
///| Define functions `names` of interface `iface` ("" for root imports) with ids `first_id`,
|
|
/// `first_id + 1`, ...
|
|
pub fn define_funcs(
|
|
iface : String,
|
|
names : Array[String],
|
|
first_id : Int,
|
|
) -> Result[Unit, String] {
|
|
let joined = names.iter().fold(init="", fn(acc, n) {
|
|
if acc == "" {
|
|
n
|
|
} else {
|
|
acc + "\n" + n
|
|
}
|
|
})
|
|
if c_define(cstr(iface), cstr(joined), first_id) != 0 {
|
|
Err(last_error())
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn instantiate() -> Result[Unit, String] {
|
|
if c_instantiate() != 0 {
|
|
Err(last_error())
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
///| Call export `name` (of interface `iface`, "" for a root export).
|
|
pub fn call(
|
|
iface : String,
|
|
name : String,
|
|
args : Array[Val],
|
|
) -> Result[Array[Val], String] {
|
|
let out = c_call(cstr(iface), cstr(name), encode(args))
|
|
if out.length() == 0 {
|
|
Err(last_error())
|
|
} else {
|
|
Ok(decode(out))
|
|
}
|
|
}
|