Faster busy-client frames: read only the buffer's bytes, cache the wallpaper, cheaper strokes
CI / test (push) Failing after 18s

Profiling a full-screen terminal at 1080p (about 2 fps):
- snapshot_shm_buffer asked the host to copy the pool from byte 0 to the end of the buffer; foot
  puts its buffers deep in a ~135 MB pool, so every commit moved ~135 MB (~185 ms). New
  sys.mmap-read-at(fd, offset, len) copies just the buffer: step 200 ms -> 5 ms per frame.
- The wallpaper (gradient + discs) is rendered once and copied back each frame while unchanged;
  it is split from the desktop card, which changes every second.
- stroke_rounded_rect walked every pixel of the window to draw a 1px outline; it now only
  draws the border strips (exact per-pixel path kept for the corner bands).
- Window shadows only blend the strips that stick out from under the window.
Rasteriser: ~256 ms -> ~93 ms per full-screen frame. Tests compare strokes and fills with the
old per-pixel code, and check the wallpaper cache.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 04:01:25 -08:00
1 parent 70ad87f2d2
commit 66bf293085
21 files changed
+302 -136

No files matched your search

@@ -20,6 +20,10 @@ pub fn wasmExportRun(p0 : Int, p1 : Int) -> Int {
} }
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///| ///|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32) #|(func (param i32) (param i32) (result i32)
@@ -27,10 +31,6 @@ extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#| local.get 1 call $moonbit.init_array16 #| local.get 1 call $moonbit.init_array16
#| local.get 0) #| local.get 0)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///| ///|
extern "wasm" fn mbt_ffi_free(position : Int) = extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref) #|(func (param i32) local.get 0 call $moonbit.decref)
+4 -1
View File
@@ -470,7 +470,10 @@ pub fn MoonPlasmaCompositor::compose_frame(
panel_ops.append(self.switcher_ops()) panel_ops.append(self.switcher_ops())
} }
self.run_shell_actions() self.run_shell_actions()
scene.background_ops = bg_ops // The wallpaper (the first ops) is cached by the renderer; the desktop card after it changes.
let n_wall = if self.shell.wallpaper_len <= bg_ops.length() { self.shell.wallpaper_len } else { 0 }
scene.wallpaper_ops = Array::makei(n_wall, i => bg_ops[i])
scene.background_ops = Array::makei(bg_ops.length() - n_wall, i => bg_ops[n_wall + i])
scene.panel_ops = panel_ops scene.panel_ops = panel_ops
let damaged = self.damage.take_damage(self.output_w, self.output_h).length() > 0 let damaged = self.damage.take_damage(self.output_w, self.output_h).length() > 0
let sig : FrameSig = { let sig : FrameSig = {
+50
View File
@@ -76,3 +76,53 @@ test "blitting a client buffer matches per-pixel blending, even partly off scree
} }
} }
} }
///| The old per-pixel stroke, as the reference for the span-based one.
fn reference_stroke(dst : Pixmap, rect : @scene.Rect, color : @scene.Rgba, width : Int, radius : Int, clip : @scene.Rect?) -> Unit {
let x0 = if rect.x < 0 { 0 } else { rect.x }
let y0 = if rect.y < 0 { 0 } else { rect.y }
let x1 = if rect.x + rect.w > dst.width { dst.width } else { rect.x + rect.w }
let y1 = if rect.y + rect.h > dst.height { dst.height } else { rect.y + rect.h }
let inner_x = rect.x + width
let inner_y = rect.y + width
let inner_w = rect.w - width * 2
let inner_h = rect.h - width * 2
let inner_r = if radius > width { radius - width } else { 0 }
for y = y0; y < y1; y = y + 1 {
for x = x0; x < x1; x = x + 1 {
let in_outer = in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius)
let in_inner = inner_w > 0 && inner_h > 0 && in_rounded_rect(x, y, inner_x, inner_y, inner_w, inner_h, inner_r)
if in_outer && !in_inner {
blend_pixel(dst, x, y, color.r.to_int(), color.g.to_int(), color.b.to_int(), color.a.to_int(), clip)
}
}
}
}
///|
test "span-based strokes match the per-pixel reference" {
let rects = [(0, 0, 40, 30), (5, 7, 33, 21), (-6, -4, 30, 20), (30, 20, 40, 30), (2, 2, 5, 5), (3, 3, 2, 9), (-10, 15, 80, 8), (10, 2, 20, 44)]
let colors = [@scene.Rgba::new(200, 40, 90, 255), @scene.Rgba::new(10, 200, 120, 128)]
let clips : Array[@scene.Rect?] = [None, Some(@scene.Rect::new(8, 6, 25, 20)), Some(@scene.Rect::new(-5, -5, 20, 20))]
for rc in rects {
for radius in [0, 1, 4, 9, 40] {
for width in [1, 2, 3, 7] {
for col in colors {
for clip in clips {
let a = Pixmap::new(64, 48)
let b = Pixmap::new(64, 48)
for i = 0; i < a.data.length(); i = i + 1 {
let v = ((i * 41 + 5) % 251).to_byte()
a.data[i] = v
b.data[i] = v
}
let r = @scene.Rect::new(rc.0, rc.1, rc.2, rc.3)
stroke_rounded_rect(a, r, col, width, radius, clip)
reference_stroke(b, r, col, width, radius, clip)
assert_eq(a.data, b.data)
}
}
}
}
}
}
+45 -18
View File
@@ -252,17 +252,29 @@ fn stroke_rounded_rect(
let inner_w = rect.w - width * 2 let inner_w = rect.w - width * 2
let inner_h = rect.h - width * 2 let inner_h = rect.h - width * 2
let inner_r = if radius > width { radius - width } else { 0 } let inner_r = if radius > width { radius - width } else { 0 }
// Visible columns and rows after the clip, for the span path below.
let (cx0, cx1, cy0, cy1) = match clip {
Some(c) => (c.x, c.x + c.w, c.y, c.y + c.h)
None => (0, dst.width, 0, dst.height)
}
// Rows where the inner rectangle is full width (clear of its rounded corners) only have the
// two side strips to draw; every pixel between them is the interior. The other rows (the top
// and bottom bands) take the exact per-pixel test.
let mid_a = inner_y + inner_r
let mid_b = inner_y + inner_h - inner_r
for y = y0; y < y1; y = y + 1 { for y = y0; y < y1; y = y + 1 {
if inner_w > 0 && inner_h > 0 && y >= mid_a && y < mid_b {
if y >= cy0 && y < cy1 {
let l0 = if x0 > cx0 { x0 } else { cx0 }
let l1 = if inner_x < x1 { inner_x } else { x1 }
blend_span(dst, y, l0, if l1 < cx1 { l1 } else { cx1 }, cr, cg, cb, ca)
let r0 = if inner_x + inner_w > x0 { inner_x + inner_w } else { x0 }
blend_span(dst, y, if r0 > cx0 { r0 } else { cx0 }, if x1 < cx1 { x1 } else { cx1 }, cr, cg, cb, ca)
}
continue
}
for x = x0; x < x1; x = x + 1 { for x = x0; x < x1; x = x + 1 {
let in_outer = in_rounded_rect( let in_outer = in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius)
x,
y,
rect.x,
rect.y,
rect.w,
rect.h,
radius,
)
let in_inner = inner_w > 0 && let in_inner = inner_w > 0 &&
inner_h > 0 && inner_h > 0 &&
in_rounded_rect(x, y, inner_x, inner_y, inner_w, inner_h, inner_r) in_rounded_rect(x, y, inner_x, inner_y, inner_w, inner_h, inner_r)
@@ -732,11 +744,26 @@ pub fn blit_shm_surface(
} }
} }
///| The last rendered wallpaper and the ops it was drawn from.
let wallpaper_cache : Ref[(Array[@scene.DrawOp], Pixmap)?] = { val: None }
///| ///|
pub fn render_scene(scene : @scene.SceneGraph) -> Pixmap { pub fn render_scene(scene : @scene.SceneGraph) -> Pixmap {
let pixmap = Pixmap::new(scene.output_w, scene.output_h) let pixmap = Pixmap::new(scene.output_w, scene.output_h)
// 1. Background layer // 1. Background layer: the wallpaper is rendered once and copied while it is unchanged.
if scene.wallpaper_ops.length() > 0 {
match wallpaper_cache.val {
Some((ops, cached)) if cached.width == pixmap.width && cached.height == pixmap.height && ops == scene.wallpaper_ops =>
pixmap.data.unsafe_blit(0, cached.data, 0, cached.data.length())
_ => {
draw_ops(pixmap, scene.wallpaper_ops, 0, 0)
let copy = Pixmap::new(pixmap.width, pixmap.height)
copy.data.unsafe_blit(0, pixmap.data, 0, pixmap.data.length())
wallpaper_cache.val = Some((scene.wallpaper_ops, copy))
}
}
}
draw_ops(pixmap, scene.background_ops, 0, 0) draw_ops(pixmap, scene.background_ops, 0, 0)
// 2. Windows layer (bottom-to-top) // 2. Windows layer (bottom-to-top)
@@ -748,14 +775,14 @@ pub fn render_scene(scene : @scene.SceneGraph) -> Pixmap {
let outer_w = win.geometry.w + left + right let outer_w = win.geometry.w + left + right
let outer_h = win.geometry.h + top + bottom let outer_h = win.geometry.h + top + bottom
// Subtle drop shadow // Subtle drop shadow. The window covers most of it, so only the strips that stick out
fill_rounded_rect( // (right and bottom) are blended instead of the whole shadow rectangle.
pixmap, let shadow = @scene.Rect::new(outer_x + 4, outer_y + 6, outer_w, outer_h)
@scene.Rect::new(outer_x + 4, outer_y + 6, outer_w, outer_h), let shadow_col = @scene.Rgba::new(0, 0, 0, 80)
@scene.Rgba::new(0, 0, 0, 80), fill_rounded_rect(pixmap, shadow, shadow_col, 8, Some(@scene.Rect::new(outer_x + outer_w, outer_y, 4, outer_h + 6)))
8, fill_rounded_rect(pixmap, shadow, shadow_col, 8, Some(@scene.Rect::new(outer_x, outer_y + outer_h, outer_w + 4, 6)))
None, // The title bar is slightly translucent when unfocused, so keep the shadow under it.
) fill_rounded_rect(pixmap, shadow, shadow_col, 8, Some(@scene.Rect::new(outer_x, outer_y, outer_w, top + 2)))
// Breeze SSD decoration draw-ops // Breeze SSD decoration draw-ops
draw_ops(pixmap, win.deco_ops, outer_x, outer_y) draw_ops(pixmap, win.deco_ops, outer_x, outer_y)
+21
View File
@@ -58,3 +58,24 @@ test "software renderer alpha blending, rounded rect, shm blit, and png encoding
assert_eq(ppm[0], b'\x50') // 'P' assert_eq(ppm[0], b'\x50') // 'P'
assert_eq(ppm[1], b'\x36') // '6' assert_eq(ppm[1], b'\x36') // '6'
} }
///|
test "a cached wallpaper renders the same pixels, and a changed one is redrawn" {
let mk = fn(col : @scene.Rgba) {
let sg = @scene.SceneGraph::new(64, 48)
sg.wallpaper_ops = [@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, 64, 48), col, 0)]
sg.background_ops = [@scene.DrawOp::FillRect(@scene.Rect::new(4, 4, 10, 10), @scene.Rgba::new(255, 0, 0, 255), 0)]
sg
}
let a = render_scene(mk(@scene.Rgba::new(10, 20, 30, 255)))
let b = render_scene(mk(@scene.Rgba::new(10, 20, 30, 255))) // served from the cache
assert_eq(a.data, b.data)
assert_eq(b.pixel(30, 30).unwrap().g.to_int(), 20)
assert_eq(b.pixel(6, 6).unwrap().r.to_int(), 255) // what is drawn over it still shows
let c = render_scene(mk(@scene.Rgba::new(90, 80, 70, 255)))
assert_eq(c.pixel(30, 30).unwrap().g.to_int(), 80)
// Changing the size does not reuse the old pixels.
let sg = @scene.SceneGraph::new(32, 32)
sg.wallpaper_ops = [@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, 32, 32), @scene.Rgba::new(10, 20, 30, 255), 0)]
assert_eq(render_scene(sg).width, 32)
}
+3
View File
@@ -311,6 +311,8 @@ pub fn DamageTracker::take_damage(
pub(all) struct SceneGraph { pub(all) struct SceneGraph {
output_w : Int output_w : Int
output_h : Int output_h : Int
/// The static wallpaper, drawn first; the renderer keeps its pixels between frames.
mut wallpaper_ops : Array[DrawOp]
mut background_ops : Array[DrawOp] mut background_ops : Array[DrawOp]
mut windows : Array[WindowSceneNode] mut windows : Array[WindowSceneNode]
mut panel_ops : Array[DrawOp] mut panel_ops : Array[DrawOp]
@@ -324,6 +326,7 @@ pub fn SceneGraph::new(output_w : Int, output_h : Int) -> SceneGraph {
{ {
output_w, output_w,
output_h, output_h,
wallpaper_ops: [],
background_ops: [], background_ops: [],
windows: [], windows: [],
panel_ops: [], panel_ops: [],
+4
View File
@@ -196,6 +196,8 @@ pub(all) struct DesktopShell {
mut tokens : @theme.Tokens mut tokens : @theme.Tokens
mut screen_w : Int mut screen_w : Int
/// Desktop card position (once moved) and drag state. /// Desktop card position (once moved) and drag state.
/// How many of the background ops are the static wallpaper (the rest is the desktop card).
mut wallpaper_len : Int
mut desk_set : Bool mut desk_set : Bool
mut desk_x : Int mut desk_x : Int
mut desk_y : Int mut desk_y : Int
@@ -273,6 +275,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
win_geoms: {}, win_geoms: {},
tokens: conv_theme(@scene.ThemeTokens::moon_night()), tokens: conv_theme(@scene.ThemeTokens::moon_night()),
screen_w: 0, screen_w: 0,
wallpaper_len: 0,
desk_set: false, desk_set: false,
desk_x: 0, desk_x: 0,
desk_y: 0, desk_y: 0,
@@ -763,6 +766,7 @@ pub fn DesktopShell::render(
} }
let bg : Array[@scene.DrawOp] = [] let bg : Array[@scene.DrawOp] = []
translate(@libshell.render_background(@ui.size(w, h), tokens), 0, 0, bg) translate(@libshell.render_background(@ui.size(w, h), tokens), 0, 0, bg)
self.wallpaper_len = bg.length()
let (dx, dy) = self.desk_pos() let (dx, dy) = self.desk_pos()
render_desktop_widget(dx, dy, tokens, now(), bg) render_desktop_widget(dx, dy, tokens, now(), bg)
+13
View File
@@ -146,6 +146,19 @@ pub fn pwrite_bytes(fd : Int, buf : Bytes, offset : Int) -> Int {
c_pwrite_bytes(fd, buf, buf.length(), offset) c_pwrite_bytes(fd, buf, buf.length(), offset)
} }
///|
pub fn mmap_read_at(fd : Int, offset : Int, len : Int) -> Bytes {
let raw = c_mmap_read(fd, offset + len)
if raw.length() < offset + len {
return Bytes::make(0, b'\x00')
}
if offset == 0 && raw.length() == len {
raw
} else {
raw[offset:offset + len].to_bytes()
}
}
///| ///|
pub fn mmap_read(fd : Int, size : Int) -> Bytes { pub fn mmap_read(fd : Int, size : Int) -> Bytes {
c_mmap_read(fd, size) c_mmap_read(fd, size)
+5
View File
@@ -103,6 +103,11 @@ pub fn mmap_read(fd : Int, size : Int) -> Bytes {
from_fixed(@hostsys.mmap_read(fd, size)) from_fixed(@hostsys.mmap_read(fd, size))
} }
///| `len` bytes of the file behind `fd` starting at `offset` (a client buffer within its pool).
pub fn mmap_read_at(fd : Int, offset : Int, len : Int) -> Bytes {
from_fixed(@hostsys.mmap_read_at(fd, offset, len))
}
///| ///|
pub fn read_file_bytes(path : String) -> Bytes { pub fn read_file_bytes(path : String) -> Bytes {
from_fixed(@hostsys.read_file(path)) from_fixed(@hostsys.read_file(path))
+4 -10
View File
@@ -385,17 +385,11 @@ fn snapshot_shm_buffer(
if needed <= 0 || buf.width <= 0 || buf.height <= 0 { if needed <= 0 || buf.width <= 0 || buf.height <= 0 {
return None return None
} }
let raw = @sys.mmap_read(pool.fd, needed) // Only this buffer's bytes: a pool can be hundreds of MB while a buffer is a few.
if raw.length() < needed {
return None
}
let byte_len = buf.stride * buf.height let byte_len = buf.stride * buf.height
// The buffer's bytes within the pool: a slice copy, not a byte-by-byte loop (a full-screen let slice = @sys.mmap_read_at(pool.fd, buf.offset, byte_len)
// buffer is 8 MB and this runs on every commit). if slice.length() < byte_len {
let slice = if buf.offset == 0 && raw.length() == byte_len { return None
raw
} else {
raw[buf.offset:buf.offset + byte_len].to_bytes()
} }
Some({ Some({
width: buf.width, width: buf.width,
+21
View File
@@ -519,6 +519,27 @@ impl Host for State {
} }
} }
fn mmap_read_at(&mut self, fd: i32, offset: i32, len: i32) -> Vec<u8> {
if fd < 0 || offset < 0 || len <= 0 {
return Vec::new();
}
let end = offset as i64 + len as i64;
unsafe {
let mut st: libc::stat = std::mem::zeroed();
if libc::fstat(fd, &mut st) < 0 || (st.st_size as i64) < end {
return Vec::new();
}
// Mapping a large pool only reserves address space; just the pages copied are touched.
let p = libc::mmap(std::ptr::null_mut(), end as usize, libc::PROT_READ, libc::MAP_SHARED, fd, 0);
if p == libc::MAP_FAILED {
return Vec::new();
}
let out = std::slice::from_raw_parts((p as *const u8).add(offset as usize), len as usize).to_vec();
libc::munmap(p, end as usize);
out
}
}
fn read_file(&mut self, path: String) -> Vec<u8> { fn read_file(&mut self, path: String) -> Vec<u8> {
std::fs::read(path).unwrap_or_default() std::fs::read(path).unwrap_or_default()
} }
+15 -15
View File
@@ -25,21 +25,10 @@ fn wasmImportClose() = "moonplasma:host/display@0.1.0" "close"
extern "wasm" fn mbt_ffi_loadf64(offset : Int) -> Double = extern "wasm" fn mbt_ffi_loadf64(offset : Int) -> Double =
#|(func (param i32) (result f64) local.get 0 f64.load) #|(func (param i32) (result f64) local.get 0 f64.load)
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///| ///|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int = extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u) #|(func (param i32) (result i32) local.get 0 i32.load8_u)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///| ///|
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] = extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#|(func (param i32) (param i32) (result i32) #|(func (param i32) (param i32) (result i32)
@@ -47,12 +36,23 @@ extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#| local.get 1 call $moonbit.init_array8 #| local.get 1 call $moonbit.init_array8
#| local.get 0) #| local.get 0)
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
///| ///|
#owned(bytes) #owned(bytes)
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int = extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
#|(func (param i32) (result i32) local.get 0) #|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
@@ -40,32 +40,32 @@ fn mbt_ffi_loadf64(offset : Int) -> Double {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_load8_u(offset : Int) -> Int { fn mbt_ffi_load8_u(offset : Int) -> Int {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] { fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_free(position : Int) -> Unit { fn mbt_ffi_free(position : Int) -> Unit {
abort("wasm-only") abort("wasm-only")
} }
///| ///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only") abort("wasm-only")
} }
+7 -7
View File
@@ -11,13 +11,6 @@ fn wasmImportRasterise(p0 : Int, p1 : Int, p2 : Int, p3 : Int) = "moonplasma:ho
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int = extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0) #|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///| ///|
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] = extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#|(func (param i32) (param i32) (result i32) #|(func (param i32) (param i32) (result i32)
@@ -25,6 +18,13 @@ extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#| local.get 1 call $moonbit.init_array8 #| local.get 1 call $moonbit.init_array8
#| local.get 0) #| local.get 0)
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///| ///|
extern "wasm" fn mbt_ffi_free(position : Int) = extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref) #|(func (param i32) local.get 0 call $moonbit.decref)
@@ -16,12 +16,12 @@ fn mbt_ffi_str2ptr(str : String) -> Int {
} }
///| ///|
fn mbt_ffi_malloc(size : Int) -> Int { fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
abort("wasm-only") abort("wasm-only")
} }
///| ///|
fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] { fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only") abort("wasm-only")
} }
+26 -26
View File
@@ -45,34 +45,10 @@ fn wasmImportPickerStart() -> Int = "moonplasma:host/plasmoids@0.1.0" "picker-st
///| ///|
fn wasmImportPickerPoll(p0 : Int) = "moonplasma:host/plasmoids@0.1.0" "picker-poll" fn wasmImportPickerPoll(p0 : Int) = "moonplasma:host/plasmoids@0.1.0" "picker-poll"
///|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32)
#| local.get 0
#| local.get 1 call $moonbit.init_array16
#| local.get 0)
///| ///|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) = extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8) #|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
///|
extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 =
#|(func (param i32) (result i64) local.get 0 i64.load)
///|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
///|
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///| ///|
extern "wasm" fn mbt_ffi_free(position : Int) = extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref) #|(func (param i32) local.get 0 call $moonbit.decref)
@@ -81,6 +57,10 @@ extern "wasm" fn mbt_ffi_free(position : Int) =
extern "wasm" fn mbt_ffi_store64(offset : Int, value : Int64) = extern "wasm" fn mbt_ffi_store64(offset : Int, value : Int64) =
#|(func (param i32) (param i64) local.get 0 local.get 1 i64.store) #|(func (param i32) (param i64) local.get 0 local.get 1 i64.store)
///|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
///| ///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int = extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32) #|(func (param i32) (result i32) (local i32)
@@ -88,12 +68,32 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#| local.tee 1 i32.const 0 call $moonbit.init_array8 #| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1) #| local.get 1)
///|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///| ///|
#owned(bytes) #owned(bytes)
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int = extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
#|(func (param i32) (result i32) local.get 0) #|(func (param i32) (result i32) local.get 0)
///| ///|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) = extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store) #|(func (param i32) (param i32) (result i32)
#| local.get 0
#| local.get 1 call $moonbit.init_array16
#| local.get 0)
///|
extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 =
#|(func (param i32) (result i64) local.get 0 i64.load)
///|
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
@@ -75,36 +75,11 @@ fn wasmImportPickerPoll(p0 : Int) -> Unit {
abort("wasm-only host import") abort("wasm-only host import")
} }
///|
fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_store8(offset : Int, value : Int) -> Unit { fn mbt_ffi_store8(offset : Int, value : Int) -> Unit {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_load64(offset : Int) -> Int64 {
abort("wasm-only")
}
///|
fn mbt_ffi_load8_u(offset : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_str2ptr(str : String) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_free(position : Int) -> Unit { fn mbt_ffi_free(position : Int) -> Unit {
abort("wasm-only") abort("wasm-only")
@@ -115,17 +90,42 @@ fn mbt_ffi_store64(offset : Int, value : Int64) -> Unit {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_store32(offset : Int, value : Int) -> Unit {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_malloc(size : Int) -> Int { fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_load8_u(offset : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int {
abort("wasm-only") abort("wasm-only")
} }
///| ///|
fn mbt_ffi_store32(offset : Int, value : Int) -> Unit { fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String {
abort("wasm-only")
}
///|
fn mbt_ffi_load64(offset : Int) -> Int64 {
abort("wasm-only")
}
///|
fn mbt_ffi_str2ptr(str : String) -> Int {
abort("wasm-only") abort("wasm-only")
} }
+13 -10
View File
@@ -33,6 +33,9 @@ fn wasmImportPwrite(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma
///| ///|
fn wasmImportMmapRead(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "mmap-read" fn wasmImportMmapRead(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "mmap-read"
///|
fn wasmImportMmapReadAt(p0 : Int, p1 : Int, p2 : Int, p3 : Int) = "moonplasma:host/sys@0.1.0" "mmap-read-at"
///| ///|
fn wasmImportReadFile(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "read-file" fn wasmImportReadFile(p0 : Int, p1 : Int, p2 : Int) = "moonplasma:host/sys@0.1.0" "read-file"
@@ -72,13 +75,6 @@ fn wasmImportHasExe(p0 : Int, p1 : Int) -> Int = "moonplasma:host/sys@0.1.0" "ha
///| ///|
fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "spawn" fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int = "moonplasma:host/sys@0.1.0" "spawn"
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#|(func (param i32) (result i32) (local i32)
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///| ///|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String = extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32) #|(func (param i32) (param i32) (result i32)
@@ -94,6 +90,11 @@ extern "wasm" fn mbt_ffi_free(position : Int) =
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int = extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load) #|(func (param i32) (result i32) local.get 0 i32.load)
///|
#owned(bytes)
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
#|(func (param i32) (result i32) local.get 0)
///| ///|
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] = extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#|(func (param i32) (param i32) (result i32) #|(func (param i32) (param i32) (result i32)
@@ -107,7 +108,9 @@ extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0) #|(func (param i32) (result i32) local.get 0)
///| ///|
#owned(bytes) extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int = #|(func (param i32) (result i32) (local i32)
#|(func (param i32) (result i32) local.get 0) #| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
+11 -6
View File
@@ -55,6 +55,11 @@ fn wasmImportMmapRead(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import") abort("wasm-only host import")
} }
///|
fn wasmImportMmapReadAt(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Unit {
abort("wasm-only host import")
}
///| ///|
fn wasmImportReadFile(p0 : Int, p1 : Int, p2 : Int) -> Unit { fn wasmImportReadFile(p0 : Int, p1 : Int, p2 : Int) -> Unit {
abort("wasm-only host import") abort("wasm-only host import")
@@ -120,11 +125,6 @@ fn wasmImportSpawn(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
abort("wasm-only host import") abort("wasm-only host import")
} }
///|
fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String { fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String {
abort("wasm-only") abort("wasm-only")
@@ -140,6 +140,11 @@ fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only") abort("wasm-only")
} }
///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int {
abort("wasm-only")
}
///| ///|
fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] { fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
abort("wasm-only") abort("wasm-only")
@@ -151,6 +156,6 @@ fn mbt_ffi_str2ptr(str : String) -> Int {
} }
///| ///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int { fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only") abort("wasm-only")
} }
+14
View File
@@ -115,6 +115,20 @@ pub fn mmap_read(fd : Int, size : Int) -> FixedArray[Byte] {
mbt_ffi_free(return_area) mbt_ffi_free(return_area)
return ret return ret
}
///|
/// Copy `len` bytes starting at `offset` of the file behind `fd` (a client's buffer inside its
/// shared pool; a pool can be far larger than any one buffer, so never read from byte 0).
pub fn mmap_read_at(fd : Int, offset : Int, len : Int) -> FixedArray[Byte] {
let return_area = mbt_ffi_malloc(8)
wasmImportMmapReadAt(fd, offset, len, return_area);
let result = mbt_ffi_ptr2bytes(mbt_ffi_load32((return_area) + 0), mbt_ffi_load32((return_area) + 4))
let ret = result
mbt_ffi_free(return_area)
return ret
} }
///| ///|
pub fn read_file(path : String) -> FixedArray[Byte] { pub fn read_file(path : String) -> FixedArray[Byte] {
+3
View File
@@ -29,6 +29,9 @@ interface sys {
pwrite: func(fd: s32, data: list<u8>, offset: s32) -> s32; pwrite: func(fd: s32, data: list<u8>, offset: s32) -> s32;
/// Copy `size` bytes of the file behind `fd` (mapped read-only by the host). /// Copy `size` bytes of the file behind `fd` (mapped read-only by the host).
mmap-read: func(fd: s32, size: s32) -> list<u8>; mmap-read: func(fd: s32, size: s32) -> list<u8>;
/// Copy `len` bytes starting at `offset` of the file behind `fd` (a client's buffer inside its
/// shared pool; a pool can be far larger than any one buffer, so never read from byte 0).
mmap-read-at: func(fd: s32, offset: s32, len: s32) -> list<u8>;
read-file: func(path: string) -> list<u8>; read-file: func(path: string) -> list<u8>;
write-file: func(path: string, data: list<u8>) -> s32; write-file: func(path: string, data: list<u8>) -> s32;