Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
008f2f04bc | ||
|
|
de8abfaa26 | ||
|
|
e0bde1d150 |
No files matched your search
@@ -6,13 +6,6 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
branches: [main]
|
branches: [main]
|
||||||
|
|
||||||
# Several quick pushes to main used to start several full builds at once; on the shared
|
|
||||||
# runner that starved the cluster node of memory (2026-11-26 freeze). Keep one run per ref
|
|
||||||
# and cancel the older one.
|
|
||||||
concurrency:
|
|
||||||
group: ci-${{ github.ref }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
@@ -27,16 +20,8 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
|
curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash
|
||||||
echo "$HOME/.moon/bin" >> "$GITHUB_PATH"
|
echo "$HOME/.moon/bin" >> "$GITHUB_PATH"
|
||||||
# Fetch the package registry index, or registry deps (jqlssg/moonedn) aren't found.
|
|
||||||
"$HOME/.moon/bin/moon" update
|
|
||||||
|
|
||||||
- name: Install native compositor build dependencies
|
- name: Install wasm-tools, wit-bindgen, wasmtime
|
||||||
run: |
|
|
||||||
apt-get update -q
|
|
||||||
apt-get install -y -q --no-install-recommends libseat-dev libdrm-dev libinput-dev libudev-dev libxkbcommon-dev \
|
|
||||||
libpam0g-dev pamtester ffmpeg
|
|
||||||
|
|
||||||
- name: Install wasm-tools, wit-bindgen, wasmtime, wasmer C API
|
|
||||||
run: scripts/setup-toolchain.sh
|
run: scripts/setup-toolchain.sh
|
||||||
|
|
||||||
- name: Install host dependencies (jco)
|
- name: Install host dependencies (jco)
|
||||||
|
|||||||
@@ -5,12 +5,3 @@ node_modules/
|
|||||||
.context/
|
.context/
|
||||||
.mooncakes/
|
.mooncakes/
|
||||||
*.log
|
*.log
|
||||||
host-native/target/
|
|
||||||
.pixi/
|
|
||||||
|
|
||||||
# moon rewrites these for whichever target was built last, so they differ run to run. A few
|
|
||||||
# (app, gfx, renderer, scene) were committed early and stay tracked; the rest are not.
|
|
||||||
compositor/*/pkg.generated.mbti
|
|
||||||
wasmhost/*/pkg.generated.mbti
|
|
||||||
__pycache__/
|
|
||||||
assets/fonts/NotoColorEmoji.ttf
|
|
||||||
@@ -1,241 +1,88 @@
|
|||||||
# Chandra
|
# moon-plasma
|
||||||
|
|
||||||
A modular, KDE-Plasma-inspired Wayland desktop where **every part is a WebAssembly component**
|
A modular, KDE-Plasma-inspired desktop environment where **every part is a
|
||||||
(Component Model, WIT interfaces) written in [MoonBit](https://www.moonbitlang.com), on top of a
|
WebAssembly component** (Component Model, WIT interfaces) written in
|
||||||
deliberately small host. The compositor itself (Wayland server, window manager, scene, shell) is a
|
[MoonBit](https://www.moonbitlang.com), on top of a deliberately tiny trusted host.
|
||||||
component, and so is every panel module: the clock, the dock (task manager), the launcher, the settings.
|
|
||||||
|
|
||||||
Chandra was called moon-plasma until 2026-11-25. The code, the WIT packages (`chandra:*`), the
|

|
||||||
session (`chandra-session`) and the paths (`~/.config/chandra`) all use the new name; an old
|
|
||||||
`~/.config/moon-plasma/panel.edn` and `state.edn` are still read when the new ones don't exist yet.
|
|
||||||
|
|
||||||
Repository: <https://git.site.latha.org/experiments/chandra>
|
*Headless-Chromium capture of the M0 browser host: the shell, the launcher and the clock
|
||||||
|
are three separate MoonBit components.*
|
||||||

|
|
||||||
|
|
||||||
*A frame rendered headless by `build/wasmhost --gfx-out`: the wallpaper, the desktop card and every
|
|
||||||
panel button are drawn by wasm components.*
|
|
||||||
|
|
||||||
## Vision
|
## Vision
|
||||||
|
|
||||||
Plasma's strength is its decomposition (shell, widgets, window manager, runner, settings modules,
|
Plasma's strength is its decomposition (shell, widgets, window manager, runner,
|
||||||
notifications, tray). Chandra keeps that decomposition and makes each seam a **typed WIT
|
settings modules, notifications, tray). moon-plasma keeps that decomposition and
|
||||||
interface** with **deny-by-default capabilities**:
|
makes each seam a **typed WIT interface** with **deny-by-default capabilities**:
|
||||||
|
|
||||||
* the **host** only owns the wasm engine, devices (sockets, files, DRM/KMS, libinput), the
|
* the **host** only owns engine/lifecycle, devices, the capability broker and the
|
||||||
capability broker and the rasteriser;
|
render-list rasteriser;
|
||||||
* **everything else is a component**: swap the clock for another implementation, swap the shell,
|
* **everything else is a component**: swap the clock for another implementation,
|
||||||
change the theme, without rebuilding the rest;
|
swap the whole shell, change the theme, without rebuilding the rest;
|
||||||
* components draw by returning a **render list** (`list<draw-op>`), never pixels;
|
* components draw by returning a **render-list** (`list<draw-op>`), never pixels;
|
||||||
* components can't touch anything their imports and the desktop config don't grant.
|
* components can't touch anything their imports + the desktop config don't grant.
|
||||||
|
|
||||||
Design: [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) (Plasma to component mapping, permissions,
|
Read [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) (Plasma→component mapping, host
|
||||||
versioning) and [docs/compositor-design.md](docs/compositor-design.md) (the compositor and its
|
decision, rendering, input, permissions, versioning, roadmap M0–M8) and
|
||||||
hosts). Toolchain notes: [docs/TOOLCHAIN.md](docs/TOOLCHAIN.md). Dark mode and the portal: [docs/dark-mode.md](docs/dark-mode.md). Running as root and run0:
|
[docs/TOOLCHAIN.md](docs/TOOLCHAIN.md) (what works today, exact versions).
|
||||||
[docs/run0.md](docs/run0.md). Kiosk mode (`--kiosk CMD`, one fullscreen app, locked down): [docs/kiosk.md](docs/kiosk.md). Picking up a new build without logging out (`just reload`, `chandra-ctl reload`): [docs/reloading.md](docs/reloading.md). Chandra as the kiosk shell of the gamerig streaming rigs (feasibility, gaps, plan): [docs/kiosk-gamerig.md](docs/kiosk-gamerig.md).
|
|
||||||
|
|
||||||
## How it runs
|
## Status: M0 (proof of concept)
|
||||||
|
|
||||||
```
|
Works today:
|
||||||
build/compositor.wasm the compositor, a wasm component (components/compositor, code in compositor/)
|
|
||||||
build/wasmhost the host that runs it: MoonBit, linked to the prebuilt wasmtime C API
|
|
||||||
build/components/*.wasm panel modules, loaded by the host and drawn by the compositor
|
|
||||||
```
|
|
||||||
|
|
||||||
The **MoonBit host** ([wasmhost/](wasmhost/README.md)) is the only host. It links
|
* WIT package `moonplasma:desktop@0.1.0` (`wit/`): worlds `widget`, `clock-widget`,
|
||||||
`libwasmtime` from the upstream C API release instead of compiling wasmtime, so a cold build takes
|
`launcher-widget`, `panel`, `shell`.
|
||||||
about 4 seconds. (An earlier host in Rust on the wasmtime crate, `host-wasm/`, was removed on
|
* Four MoonBit components, each an independently built `.wasm` component of 24–33 KB:
|
||||||
2026-11-26.) A **native build** of the same
|
`clock-digital`, `clock-binary` (drop-in replacement), `launcher` (kickoff-like with
|
||||||
compositor code (no wasm, `just run-native`) is used by the test suite.
|
keyboard search), `shell` (desktop background + bottom panel layout + theme tokens).
|
||||||
|
* Host (`host/`): Node + browser, jco-transpiled components, capability grants,
|
||||||
|
popup/focus input routing, hot-swap, fault isolation, Canvas2D + ASCII renderers.
|
||||||
|
* Tests: 16 MoonBit unit tests (pure logic), 13 host integration tests that
|
||||||
|
instantiate the real components, WIT conformance (`wasm-tools component targets`),
|
||||||
|
generated-bindings freshness check, `wasmtime compile` of every component.
|
||||||
|
|
||||||
The compositor runs **nested**, as a window on your current Wayland session, as a **login
|
Stubbed / not done (see ARCHITECTURE §10): no window manager/compositor, search runner,
|
||||||
session** on DRM/KMS with libinput (`--drm`, through libseat/logind), or **on an X server**
|
notifications, settings, tray, session, shortcuts, clipboard, files; `launch` only logs;
|
||||||
(`--x11`: a window under an X window manager, or the whole screen without one), for NVIDIA's
|
host does input hit-testing; fixed-advance text metrics, no real fonts/icons; native
|
||||||
proprietary driver on Xorg ([docs/x11.md](docs/x11.md)).
|
wasmtime host is M3; no CPU/fuel limits under jco.
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|
||||||
Requirements: Linux x86_64, [MoonBit](https://www.moonbitlang.com/download),
|
Requirements: Linux x86_64, Node ≥ 20, [MoonBit](https://www.moonbitlang.com/download).
|
||||||
[pixi](https://pixi.sh) (it provides `just`, tcc and seatd/libseat), and the dev headers for
|
|
||||||
libdrm, libinput, libudev and libxkbcommon (on Debian/Ubuntu:
|
|
||||||
`apt install libdrm-dev libinput-dev libudev-dev libxkbcommon-dev libseat-dev`).
|
|
||||||
Screen recording needs `ffmpeg` on `PATH`.
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export PATH="$HOME/.moon/bin:$PATH"
|
export PATH="$HOME/.moon/bin:$PATH"
|
||||||
moon update # fetch mooncakes dependencies
|
scripts/setup-toolchain.sh # wasm-tools, wit-bindgen, wasmtime -> ./.tools (no root)
|
||||||
scripts/setup-toolchain.sh # wasm-tools, wit-bindgen, wasmtime + its C API -> ./.tools (no root)
|
(cd host && npm ci) # jco
|
||||||
|
scripts/build.sh # MoonBit -> components -> build/components, build/jco
|
||||||
|
scripts/test.sh # everything CI runs
|
||||||
|
|
||||||
just run # build compositor.wasm, wasmhost and the modules; open in a window
|
node host/src/cli.mjs --time 2026-11-24T14:07:00 --click-launcher # terminal render
|
||||||
just client # start foot inside it (or: just client konsole)
|
node host/serve.mjs 8080 # browser host: http://127.0.0.1:8080/
|
||||||
|
node host/serve.mjs & # then try ?config=desktop.binary-clock.json or ?theme=moon-day
|
||||||
```
|
```
|
||||||
|
|
||||||
| Recipe | What it does |
|
Swap the clock **without rebuilding anything else**:
|
||||||
|---|---|
|
|
||||||
| `just run` | Nested in a window on your Wayland session, MoonBit host |
|
|
||||||
| `just run-drm` | As a login session on the real screen (from a text console, as yourself) |
|
|
||||||
| `just run-x11` | On the X server in `$DISPLAY` (NVIDIA on Xorg; the whole screen when no window manager runs) |
|
|
||||||
| `just install` | Install everything: the "Chandra" display-manager session, "Chandra (X11)" for Xorg, and face unlock (asks for root once). `--link` symlinks into this checkout (later just rebuild), `--native` picks the native compositor, `--pam` turns face unlock on, `--no-face` skips it |
|
|
||||||
| `just uninstall` | Remove it all again (`--purge` also deletes enrolled faces) |
|
|
||||||
| `just reload` | Reload the running session's panel modules from disk (`chandra-ctl reload [NAME]`) |
|
|
||||||
| `just run-native`, `run-drm-native` | The native (non-wasm) build |
|
|
||||||
| `just compositor-wasm`, `wasmhost`, `modules` | Build one piece |
|
|
||||||
| `just test` | Native compositor tests and lib tests |
|
|
||||||
| `just frame` | Render one composed frame to `_build/e2e_frame.png` |
|
|
||||||
| `just remote-build` | Build on a sprites.dev sprite (`CHANDRA_SPRITE`) and pull the artifacts back |
|
|
||||||
|
|
||||||
`build/wasmhost build/compositor.wasm --socket PATH` takes `--window` (nested), `--drm` (login
|
```bash
|
||||||
session), `--x11` (on Xorg; `--fullscreen` under a window manager, `--login-session` to act as the
|
node host/src/cli.mjs --config config/desktop.binary-clock.json
|
||||||
desktop session), `--theme PATH`, `--no-restore`, `--profile`, `--size WxH` and `--gfx-out frame.png`
|
scripts/build.sh clock-binary # builds just that component
|
||||||
(render to a PNG, headless; add `--serve` to keep refreshing it). `WASMHOST_PROFILE=1` prints the
|
# in the browser console: await moonPlasma.swap('clock', 'clock-binary')
|
||||||
time spent in each host function every 5 seconds; `WASMHOST_TRACE=1` logs every host call.
|
```
|
||||||
No GPU? Chandra detects it and asks less of the CPU; `CHANDRA_RENDER=software|hardware` or
|
|
||||||
Settings > Session > Lighter animations overrides it, and `CHANDRA_FRAME_TRACE=1` logs frame times
|
|
||||||
([docs/rendering.md](docs/rendering.md)).
|
|
||||||
|
|
||||||
`CHANDRA_DEBUG_CONFIGURE=1` (any host) logs each window's configures, acks and committed buffer
|
Screenshots: see the end of [docs/TOOLCHAIN.md](docs/TOOLCHAIN.md).
|
||||||
sizes, for an app that draws at the wrong size.
|
|
||||||
|
|
||||||
The installed session logs to `~/.local/state/chandra/session.log`. The session before is kept as
|
|
||||||
`session.log.1`, and the host writes a timestamped line once a minute with its memory use and the
|
|
||||||
window buffers and images it holds. Start it as yourself, never
|
|
||||||
as root: `chandra-session` refuses to run as root (`CHANDRA_ALLOW_ROOT=1` overrides). Installing
|
|
||||||
goes through `scripts/as-root`, which prefers run0 and falls back to sudo, doas or pkexec
|
|
||||||
(`CHANDRA_AS_ROOT` picks one). What it installs and its flags: [docs/install.md](docs/install.md). To run a graphical app as root inside Chandra, use
|
|
||||||
`chandra-root gparted`. Details, including polkit agents (`CHANDRA_POLKIT_AGENT`), are in
|
|
||||||
[docs/run0.md](docs/run0.md).
|
|
||||||
|
|
||||||
Face unlock, in the style of Howdy, comes with `just install`: then Settings > Session > Face unlock to
|
|
||||||
add a face and use it for the password dialog and sudo (a PAM module; the password always still
|
|
||||||
works). See [docs/face-unlock.md](docs/face-unlock.md), including what 2D face recognition can't
|
|
||||||
protect against.
|
|
||||||
|
|
||||||
## What works
|
|
||||||
|
|
||||||
**Windows**
|
|
||||||
|
|
||||||
* Wayland clients (xdg-shell, wl_shm buffers, clipboard), tiling or floating (the Tile | Float
|
|
||||||
panel toggle).
|
|
||||||
* Drag a window by its title bar, resize floating windows from corners and edges, double-click
|
|
||||||
the title bar to maximize or restore. Clicking a window raises it.
|
|
||||||
* App right-click menus (xdg_popup) stay open and on screen; apps that draw with Vulkan on wl_shm
|
|
||||||
(Zed, Delta) show every frame; buffers stay valid after their pool is destroyed.
|
|
||||||
* The mouse wheel scrolls windows.
|
|
||||||
* Window positions are remembered per app, and on login the session reopens the windows the last
|
|
||||||
one ended with (switch it off in Settings, or start with `--no-restore`).
|
|
||||||
|
|
||||||
**Shortcuts** (the defaults; rebind any of them in Settings > Shortcuts: click one and press the
|
|
||||||
new keys)
|
|
||||||
|
|
||||||
| Keys | Action |
|
|
||||||
|---|---|
|
|
||||||
| Super (tap) | Open the application launcher, with an overview of every window beside it (typing filters both) |
|
|
||||||
| Super+W | Overview alone: every window side by side; click one to bring it forward, middle-click to close it |
|
|
||||||
| Alt+Tab | Window switcher, with a live preview of each window |
|
|
||||||
| Super+D | Show the desktop (minimize every window); again to bring them back |
|
|
||||||
| Super+1 .. Super+9 | Open the dock's first nine apps (or bring their windows forward) |
|
|
||||||
| Super+Shift+S | Screenshot: select a region or click a window (F full screen, W active window) |
|
|
||||||
| Print / Super+Print / Super+Ctrl+Print | Screenshot of the full screen / active window / window under the pointer |
|
|
||||||
| Super+Shift+R | Record a region or window (Enter starts) |
|
|
||||||
| Super+Alt+R / Super+Ctrl+R | Record the whole screen / the window under the pointer |
|
|
||||||
| Super+. | Emoji picker: type to search, arrows and Enter to insert into the app you're typing in ([docs/emoji-picker.md](docs/emoji-picker.md)) |
|
|
||||||
|
|
||||||
Every screenshot opens in the screenshot editor first: annotate it (arrow, line, rectangle,
|
|
||||||
ellipse, pen, highlighter, text, numbered markers), hide parts of it (blur, pixelate, redact) or
|
|
||||||
crop it, then Enter saves and copies it, Ctrl+C only copies it and Esc discards it. See
|
|
||||||
[docs/screenshot-editor.md](docs/screenshot-editor.md).
|
|
||||||
|
|
||||||
Screenshots go to `~/Pictures/Screenshots/` and the clipboard; recordings (H.264 MP4, via
|
|
||||||
`ffmpeg`) go to `~/Videos/Screencasts/`. A red REC pill shows while recording; click it or press
|
|
||||||
a recording shortcut again to stop.
|
|
||||||
|
|
||||||
**Panel modules** (each one a separate `.wasm` component in `components/`)
|
|
||||||
|
|
||||||
* **Launcher** (application menu with search). Its menu ends in a session row: **Reload**
|
|
||||||
(reload every panel module from disk, keeping the panel and module settings; windows and apps
|
|
||||||
keep running), **Log out**, **Suspend**, **Restart** and **Power off**. Log out, Restart and
|
|
||||||
Power off ask for a second click.
|
|
||||||
* **Dock**, the panel's task manager: the pinned apps that aren't running as plain icons, then
|
|
||||||
after a divider every running app (pinned ones in pin order first), with their theme icons and
|
|
||||||
a dot per open window (lit when the app has focus). An app with windows is a pill with its window's title (or its name and window count) while there is room,
|
|
||||||
just its icon when the panel gets crowded. Click to start an app or bring its newest window
|
|
||||||
forward (restoring it if minimized); click a focused app again to step through its windows.
|
|
||||||
**Super+1** .. **Super+9** do the same for the first nine. Right-click an app for Open / New
|
|
||||||
window, Pin or Unpin, Move left / right, its window's Minimize and Maximize (or a list of its
|
|
||||||
windows), Quit, and Dock settings (icon size, magnification, running apps, window titles).
|
|
||||||
Pin apps from there or by right-clicking them in the Launcher's menu; an empty dock shows a "+"
|
|
||||||
that opens it. Pins live in state.edn. (It replaced the separate Task Manager: a saved panel
|
|
||||||
with the old `tasks` module gets the dock in its place.)
|
|
||||||
* **Network**: Wi-Fi and wired status, connect to Wi-Fi networks (with passwords), Wi-Fi and
|
|
||||||
airplane switches, through NetworkManager over D-Bus (read-only sysfs status without it).
|
|
||||||
* **Battery**: charge, charging state, time left, low-battery warning (only added on machines
|
|
||||||
with a battery).
|
|
||||||
* **Clocks**: the Digital Clock (12-hour with AM/PM by default, 24-hour in its Module settings)
|
|
||||||
and a binary clock.
|
|
||||||
* **Screenshot**, **Layout** (Tile | Float), **Inspector** (interface explorer).
|
|
||||||
* **Settings**: tabs for Session (reopen windows at login, remember window positions, dark
|
|
||||||
mode), Desktop (what fills the desktop, wallpapers), Panel (add, remove and replace modules), Power (screen off and
|
|
||||||
suspend when idle, battery, suspend / restart / power off), Shortcuts and Apps (the default web
|
|
||||||
browser, email, file manager, terminal, text editor, image viewer, video and music player, PDF
|
|
||||||
viewer and archive manager, chosen from the installed apps; written to
|
|
||||||
`~/.config/mimeapps.list`, so xdg-open and other apps follow it). Dark mode also sets the system
|
|
||||||
preference for apps: GTK, Qt and KDE settings, and in the login session Chandra serves
|
|
||||||
xdg-desktop-portal's Settings color-scheme itself, so Chrome, Firefox and Electron apps follow
|
|
||||||
the switch live (see [docs/dark-mode.md](docs/dark-mode.md)).
|
|
||||||
|
|
||||||
**Desktop modules**: a module can fill the desktop itself, behind the windows, instead of sitting
|
|
||||||
in the panel. Choose one in Settings > Desktop (*Wallpaper only*, *Clock card*, or any installed
|
|
||||||
desktop module); right-click the empty desktop for its settings. Three ship: **Icon Desktop** (app
|
|
||||||
icons you click to launch and drag to arrange, positions remembered), **Live Starfield** (an
|
|
||||||
animated starfield you steer with the pointer) and **Gamerig Library** (gamerig's game library
|
|
||||||
for a streaming rig, [docs/gamerig-library.md](docs/gamerig-library.md)). See
|
|
||||||
[docs/desktop-modules.md](docs/desktop-modules.md) to write one.
|
|
||||||
|
|
||||||
Right-click any module for **Module settings** (its own options, such as the clock's format,
|
|
||||||
saved in `state.edn`), **System settings**, Replace with and Remove widget. Right-clicking empty
|
|
||||||
panel space opens System settings on the Panel tab.
|
|
||||||
|
|
||||||
**Performance**: an idle desktop sleeps instead of redrawing (about 0.35 % CPU, down from about
|
|
||||||
9 %), only changed parts of the screen are redrawn, and screenshots, recording and `--gfx-out`
|
|
||||||
keep pixels on the host rather than copying them through the component.
|
|
||||||
|
|
||||||
## Configuration and state
|
|
||||||
|
|
||||||
* `~/.config/chandra/panel.edn`: the panel's modules, in order.
|
|
||||||
* `~/.config/chandra/state.edn`: layout mode, recent apps, window positions, the windows to
|
|
||||||
reopen, wallpaper, theme, power timeouts, the terminal terminal apps run in (`:terminal`, also
|
|
||||||
put first in `~/.config/xdg-terminals.list`), per-module settings (`:module-config`), what fills the desktop (`:desktop`), and the
|
|
||||||
Settings switches (`:restore-session false`, `:remember-windows false`, `:theme :light`).
|
|
||||||
* Themes: `config/themes/moon-night.json` (default) and `moon-day.json`.
|
|
||||||
|
|
||||||
## Known gaps
|
|
||||||
|
|
||||||
* 5 to 6 compositor tests fail on main in a cloud container and are not regressions
|
|
||||||
(`input_test`, plus `screenshot_test` when the screenshot module isn't built). Build the panel
|
|
||||||
modules first and run with `CHANDRA_TEST_CONFIG=1`, or more tests fail.
|
|
||||||
* The DRM/KMS login session and real-hardware paths (power actions, battery, NetworkManager on a
|
|
||||||
real machine) are not exercised by CI; they have been built and unit-tested, not run on
|
|
||||||
hardware in automation.
|
|
||||||
* Not done yet: lock screen, notifications daemon, system tray, virtual desktops, multi-monitor
|
|
||||||
configuration, idle-inhibit (a playing video doesn't keep the screen on), lid and power-button
|
|
||||||
actions (logind's defaults apply), VPN and enterprise Wi-Fi.
|
|
||||||
* Desktop Pulse (`compositor/wasm/testdata/pulse.component.wasm`) and the `moon-disc` panel
|
|
||||||
module have no source in the tree, so `scripts/build-modules.sh` skips moon-disc and a fresh
|
|
||||||
panel shows a "Module 'moon-disc' not found" notice (remove it in Settings, Panel tab).
|
|
||||||
|
|
||||||
## Layout
|
## Layout
|
||||||
|
|
||||||
```
|
```
|
||||||
wit/ WIT packages: chandra:desktop (components) and chandra:host (wit/host, what hosts serve)
|
wit/ the WIT package (host-agnostic API)
|
||||||
compositor/ the compositor in MoonBit: wayland/, shell/, scene/, raster/, kms/, network/, portal/, host/, ...
|
interface/ world/ generated shared MoonBit bindings (wit-bindgen; committed)
|
||||||
components/<n>/ one wasm component each: compositor, shell, panel modules (gen/ bindings + thin stub.mbt)
|
components/<n>/gen/ generated export bindings + hand-written thin stub.mbt
|
||||||
lib/ pure, unit-tested logic (panel layout, clock, launcher, battery, task pills, ...)
|
lib/ pure, unit-tested logic (ui helpers, panel layout, launcher state machine, ...)
|
||||||
wasmhost/ the host (MoonBit + wasmtime C API)
|
host/ JS host: src/desktop.mjs (core), loaders, renderers, tests, web/ (browser)
|
||||||
host/ the original M0 browser/Node host (jco), kept for the component demos
|
config/ desktop configs (grants!), themes, static app list
|
||||||
session/ chandra-session, chandra-root, chandra-ctl, chandra.desktop, chandra.portal, install.py
|
scripts/ setup-toolchain, gen-bindings, build, test, screenshot
|
||||||
config/ desktop configs, themes, app list
|
docs/ ARCHITECTURE, TOOLCHAIN, screenshots
|
||||||
scripts/ setup-toolchain, build-*, gen-bindings, as-root, remote-build, test
|
|
||||||
docs/ ARCHITECTURE, compositor-design, TOOLCHAIN, run0, dark-mode, reloading, screenshots
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
Copyright (c) 2016, The Inter Project Authors (https://github.com/rsms/inter)
|
|
||||||
Copyright (c) 2015-2025, Renzhi Li (aka. Belleve Invis, belleve@typeof.net)
|
|
||||||
Copyright (c) 2025, Jamie Gravendeel (https://jamie.garden)
|
|
||||||
Copyright (c) 2025, Florian Müllner
|
|
||||||
|
|
||||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
||||||
This license is copied below, and is also available with a FAQ at:
|
|
||||||
https://openfontlicense.org
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------
|
|
||||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
||||||
-----------------------------------------------------------
|
|
||||||
|
|
||||||
PREAMBLE
|
|
||||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
||||||
development of collaborative font projects, to support the font creation
|
|
||||||
efforts of academic and linguistic communities, and to provide a free and
|
|
||||||
open framework in which fonts may be shared and improved in partnership
|
|
||||||
with others.
|
|
||||||
|
|
||||||
The OFL allows the licensed fonts to be used, studied, modified and
|
|
||||||
redistributed freely as long as they are not sold by themselves. The
|
|
||||||
fonts, including any derivative works, can be bundled, embedded,
|
|
||||||
redistributed and/or sold with any software provided that any reserved
|
|
||||||
names are not used by derivative works. The fonts and derivatives,
|
|
||||||
however, cannot be released under any other type of license. The
|
|
||||||
requirement for fonts to remain under this license does not apply
|
|
||||||
to any document created using the fonts or their derivatives.
|
|
||||||
|
|
||||||
DEFINITIONS
|
|
||||||
"Font Software" refers to the set of files released by the Copyright
|
|
||||||
Holder(s) under this license and clearly marked as such. This may
|
|
||||||
include source files, build scripts and documentation.
|
|
||||||
|
|
||||||
"Reserved Font Name" refers to any names specified as such after the
|
|
||||||
copyright statement(s).
|
|
||||||
|
|
||||||
"Original Version" refers to the collection of Font Software components as
|
|
||||||
distributed by the Copyright Holder(s).
|
|
||||||
|
|
||||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
||||||
or substituting -- in part or in whole -- any of the components of the
|
|
||||||
Original Version, by changing formats or by porting the Font Software to a
|
|
||||||
new environment.
|
|
||||||
|
|
||||||
"Author" refers to any designer, engineer, programmer, technical
|
|
||||||
writer or other person who contributed to the Font Software.
|
|
||||||
|
|
||||||
PERMISSION & CONDITIONS
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
||||||
redistribute, and sell modified and unmodified copies of the Font
|
|
||||||
Software, subject to the following conditions:
|
|
||||||
|
|
||||||
1) Neither the Font Software nor any of its individual components,
|
|
||||||
in Original or Modified Versions, may be sold by itself.
|
|
||||||
|
|
||||||
2) Original or Modified Versions of the Font Software may be bundled,
|
|
||||||
redistributed and/or sold with any software, provided that each copy
|
|
||||||
contains the above copyright notice and this license. These can be
|
|
||||||
included either as stand-alone text files, human-readable headers or
|
|
||||||
in the appropriate machine-readable metadata fields within text or
|
|
||||||
binary files as long as those fields can be easily viewed by the user.
|
|
||||||
|
|
||||||
3) No Modified Version of the Font Software may use the Reserved Font
|
|
||||||
Name(s) unless explicit written permission is granted by the corresponding
|
|
||||||
Copyright Holder. This restriction only applies to the primary font name as
|
|
||||||
presented to the users.
|
|
||||||
|
|
||||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
||||||
Software shall not be used to promote, endorse or advertise any
|
|
||||||
Modified Version, except to acknowledge the contribution(s) of the
|
|
||||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
||||||
permission.
|
|
||||||
|
|
||||||
5) The Font Software, modified or unmodified, in part or in whole,
|
|
||||||
must be distributed entirely under this license, and must not be
|
|
||||||
distributed under any other license. The requirement for fonts to
|
|
||||||
remain under this license does not apply to any document created
|
|
||||||
using the Font Software.
|
|
||||||
|
|
||||||
TERMINATION
|
|
||||||
This license becomes null and void if any of the above conditions are
|
|
||||||
not met.
|
|
||||||
|
|
||||||
DISCLAIMER
|
|
||||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
||||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
||||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
||||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
||||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
||||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
Noto Color Emoji (assets/fonts/NotoColorEmoji.ttf, fetched by scripts/fetch-emoji-font.sh from
|
|
||||||
https://github.com/googlefonts/noto-emoji, v2.051): Copyright 2013-2025 Google LLC.
|
|
||||||
|
|
||||||
This Font Software is licensed under the SIL Open Font License,
|
|
||||||
Version 1.1.
|
|
||||||
|
|
||||||
This license is copied below, and is also available with a FAQ at:
|
|
||||||
http://scripts.sil.org/OFL
|
|
||||||
|
|
||||||
-----------------------------------------------------------
|
|
||||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
||||||
-----------------------------------------------------------
|
|
||||||
|
|
||||||
PREAMBLE
|
|
||||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
||||||
development of collaborative font projects, to support the font
|
|
||||||
creation efforts of academic and linguistic communities, and to
|
|
||||||
provide a free and open framework in which fonts may be shared and
|
|
||||||
improved in partnership with others.
|
|
||||||
|
|
||||||
The OFL allows the licensed fonts to be used, studied, modified and
|
|
||||||
redistributed freely as long as they are not sold by themselves. The
|
|
||||||
fonts, including any derivative works, can be bundled, embedded,
|
|
||||||
redistributed and/or sold with any software provided that any reserved
|
|
||||||
names are not used by derivative works. The fonts and derivatives,
|
|
||||||
however, cannot be released under any other type of license. The
|
|
||||||
requirement for fonts to remain under this license does not apply to
|
|
||||||
any document created using the fonts or their derivatives.
|
|
||||||
|
|
||||||
DEFINITIONS
|
|
||||||
"Font Software" refers to the set of files released by the Copyright
|
|
||||||
Holder(s) under this license and clearly marked as such. This may
|
|
||||||
include source files, build scripts and documentation.
|
|
||||||
|
|
||||||
"Reserved Font Name" refers to any names specified as such after the
|
|
||||||
copyright statement(s).
|
|
||||||
|
|
||||||
"Original Version" refers to the collection of Font Software
|
|
||||||
components as distributed by the Copyright Holder(s).
|
|
||||||
|
|
||||||
"Modified Version" refers to any derivative made by adding to,
|
|
||||||
deleting, or substituting -- in part or in whole -- any of the
|
|
||||||
components of the Original Version, by changing formats or by porting
|
|
||||||
the Font Software to a new environment.
|
|
||||||
|
|
||||||
"Author" refers to any designer, engineer, programmer, technical
|
|
||||||
writer or other person who contributed to the Font Software.
|
|
||||||
|
|
||||||
PERMISSION & CONDITIONS
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of the Font Software, to use, study, copy, merge, embed,
|
|
||||||
modify, redistribute, and sell modified and unmodified copies of the
|
|
||||||
Font Software, subject to the following conditions:
|
|
||||||
|
|
||||||
1) Neither the Font Software nor any of its individual components, in
|
|
||||||
Original or Modified Versions, may be sold by itself.
|
|
||||||
|
|
||||||
2) Original or Modified Versions of the Font Software may be bundled,
|
|
||||||
redistributed and/or sold with any software, provided that each copy
|
|
||||||
contains the above copyright notice and this license. These can be
|
|
||||||
included either as stand-alone text files, human-readable headers or
|
|
||||||
in the appropriate machine-readable metadata fields within text or
|
|
||||||
binary files as long as those fields can be easily viewed by the user.
|
|
||||||
|
|
||||||
3) No Modified Version of the Font Software may use the Reserved Font
|
|
||||||
Name(s) unless explicit written permission is granted by the
|
|
||||||
corresponding Copyright Holder. This restriction only applies to the
|
|
||||||
primary font name as presented to the users.
|
|
||||||
|
|
||||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
||||||
Software shall not be used to promote, endorse or advertise any
|
|
||||||
Modified Version, except to acknowledge the contribution(s) of the
|
|
||||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
||||||
permission.
|
|
||||||
|
|
||||||
5) The Font Software, modified or unmodified, in part or in whole,
|
|
||||||
must be distributed entirely under this license, and must not be
|
|
||||||
distributed under any other license. The requirement for fonts to
|
|
||||||
remain under this license does not apply to any document created using
|
|
||||||
the Font Software.
|
|
||||||
|
|
||||||
TERMINATION
|
|
||||||
This license becomes null and void if any of the above conditions are
|
|
||||||
not met.
|
|
||||||
|
|
||||||
DISCLAIMER
|
|
||||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
||||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
||||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
||||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
||||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
||||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
||||||
@@ -1,274 +0,0 @@
|
|||||||
// #region subtask
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct SubTask {
|
|
||||||
handle : Int
|
|
||||||
state : SubTaskState
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum SubTaskState {
|
|
||||||
Starting = 0
|
|
||||||
Started = 1
|
|
||||||
Returned = 2
|
|
||||||
Cancelled_before_started = 3
|
|
||||||
Cancelled_before_returned = 4
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn SubTaskState::from(int : Int) -> SubTaskState {
|
|
||||||
match int {
|
|
||||||
0 => Starting
|
|
||||||
1 => Started
|
|
||||||
2 => Returned
|
|
||||||
3 => Cancelled_before_started
|
|
||||||
4 => Cancelled_before_returned
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn SubTask::from(code : Int) -> SubTask {
|
|
||||||
{ handle: code >> 4, state: SubTaskState::from(code & 0xf) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// The baseline intrinsic cooperatively waits in the component model and
|
|
||||||
/// returns only after the subtask reaches a terminal state.
|
|
||||||
fn SubTask::cancel(self : SubTask) -> SubTaskState {
|
|
||||||
SubTaskState::from(subtask_cancel(self.handle))
|
|
||||||
}
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region events
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum EventCode {
|
|
||||||
None = 0
|
|
||||||
SubTask = 1
|
|
||||||
StreamRead = 2
|
|
||||||
StreamWrite = 3
|
|
||||||
FutureRead = 4
|
|
||||||
FutureWrite = 5
|
|
||||||
TaskCancelled = 6
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn EventCode::from(int : Int) -> EventCode {
|
|
||||||
match int {
|
|
||||||
0 => None
|
|
||||||
1 => SubTask
|
|
||||||
2 => StreamRead
|
|
||||||
3 => StreamWrite
|
|
||||||
4 => FutureRead
|
|
||||||
5 => FutureWrite
|
|
||||||
6 => TaskCancelled
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum Events {
|
|
||||||
None
|
|
||||||
Subtask(Int, SubTaskState)
|
|
||||||
StreamRead(Int, StreamResult)
|
|
||||||
StreamWrite(Int, StreamResult)
|
|
||||||
FutureRead(Int, FutureReadResult)
|
|
||||||
FutureWrite(Int, FutureWriteResult)
|
|
||||||
TaskCancelled
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn Events::new(code : EventCode, i : Int, j : Int) -> Events {
|
|
||||||
match code {
|
|
||||||
None => None
|
|
||||||
SubTask => Subtask(i, SubTaskState::from(j))
|
|
||||||
StreamRead => StreamRead(i, StreamResult::from(j))
|
|
||||||
StreamWrite => StreamWrite(i, StreamResult::from(j))
|
|
||||||
FutureRead => FutureRead(i, FutureReadResult::from(j))
|
|
||||||
FutureWrite => FutureWrite(i, FutureWriteResult::from(j))
|
|
||||||
TaskCancelled => TaskCancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region waitable set
|
|
||||||
|
|
||||||
///|
|
|
||||||
#borrow(array)
|
|
||||||
extern "wasm" fn int_array2ptr(array : FixedArray[Int]) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
|
||||||
|
|
||||||
///|
|
|
||||||
struct WaitableSet(Int) derive(Eq, Hash)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#deprecated
|
|
||||||
pub extend WaitableSet with Eq::{not_equal, equal}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#deprecated
|
|
||||||
pub extend WaitableSet with Hash::{hash, hash_combine}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn WaitableSet::new() -> WaitableSet {
|
|
||||||
WaitableSet(waitable_set_new())
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn WaitableSet::drop(self : Self) -> Unit {
|
|
||||||
waitable_set_drop(self.0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn WaitableSet::poll(self : Self) -> (Events, Int) {
|
|
||||||
let payload : FixedArray[Int] = FixedArray::make(2, 0)
|
|
||||||
let event = waitable_set_poll(self.0, int_array2ptr(payload))
|
|
||||||
(Events::new(EventCode::from(event), payload[0], payload[1]), payload[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region Future
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum FutureReadResult {
|
|
||||||
Completed = 0
|
|
||||||
Cancelled = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn FutureReadResult::from(int : Int) -> FutureReadResult {
|
|
||||||
match int {
|
|
||||||
0 => Completed
|
|
||||||
2 => Cancelled
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum FutureWriteResult {
|
|
||||||
Completed = 0
|
|
||||||
Dropped = 1
|
|
||||||
Cancelled = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn FutureWriteResult::from(int : Int) -> FutureWriteResult {
|
|
||||||
match int {
|
|
||||||
0 => Completed
|
|
||||||
1 => Dropped
|
|
||||||
2 => Cancelled
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region Stream
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct StreamResult {
|
|
||||||
progress : Int
|
|
||||||
copy_result : CopyResult
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn StreamResult::from(int : Int) -> StreamResult {
|
|
||||||
let progress = int >> 4
|
|
||||||
let copy_result = CopyResult::from(int & 0xf)
|
|
||||||
{ progress, copy_result }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum CopyResult {
|
|
||||||
Completed = 0
|
|
||||||
Dropped = 1
|
|
||||||
Cancelled = 2
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn CopyResult::from(int : Int) -> CopyResult {
|
|
||||||
match int {
|
|
||||||
0 => Completed
|
|
||||||
1 => Dropped
|
|
||||||
2 => Cancelled
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region callback code
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Code to let the runtime know what to do in the callback
|
|
||||||
priv enum CallbackCode {
|
|
||||||
Completed
|
|
||||||
Yield
|
|
||||||
Wait(WaitableSet)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn CallbackCode::encode(self : Self) -> Int {
|
|
||||||
match self {
|
|
||||||
Completed => 0
|
|
||||||
Yield => 1
|
|
||||||
Wait(id) => 2 | (id.0 << 4)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
|
|
||||||
// #region Component async primitives
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn subtask_cancel(id : Int) -> Int = "$root" "[subtask-cancel]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn subtask_drop(id : Int) = "$root" "[subtask-drop]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn tls_set(tls : Int) = "$root" "[context-set-0]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn tls_get() -> Int = "$root" "[context-get-0]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_cancel() = "[export]$root" "[task-cancel]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn waitable_set_new() -> Int = "$root" "[waitable-set-new]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn waitable_set_drop(set : Int) = "$root" "[waitable-set-drop]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// This poll is deliberately non-cancellable. Component-task cancellation is
|
|
||||||
/// delivered by the stackless callback instead of being consumed here.
|
|
||||||
fn waitable_set_poll(set : Int, payload : Int) -> Int = "$root" "[waitable-set-poll]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup_stream_new() -> Int64 = "$root" "[stream-new-unit]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup_stream_read(handle : Int, ptr : Int, len : Int) -> Int = "$root" "[async-lower][stream-read-unit]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup_stream_write(handle : Int, ptr : Int, len : Int) -> Int = "$root" "[async-lower][stream-write-unit]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup_stream_cancel_read(handle : Int) -> Int = "$root" "[stream-cancel-read-unit]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup_stream_drop_readable(handle : Int) = "$root" "[stream-drop-readable-unit]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup_stream_drop_writable(handle : Int) = "$root" "[stream-drop-writable-unit]"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn waitable_join(waitable : Int, set : Int) = "$root" "[waitable-join]"
|
|
||||||
|
|
||||||
// #endregion
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct CondVarWaiter {
|
|
||||||
// Once a signal is assigned, cancellation must not lose it before the
|
|
||||||
// waiter resumes. This matches moonbitlang/async's condition variable.
|
|
||||||
mut woken : Bool
|
|
||||||
mut coro : Coroutine?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// A condition variable for synchronization between tasks.
|
|
||||||
pub struct CondVar {
|
|
||||||
priv waiters : @deque.Deque[CondVarWaiter]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Create a new condition variable.
|
|
||||||
#alias(new, deprecated)
|
|
||||||
pub fn CondVar::CondVar() -> CondVar {
|
|
||||||
{ waiters: Deque([]) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Wait until the condition variable is signaled.
|
|
||||||
pub async fn CondVar::wait(self : CondVar) -> Unit noraise {
|
|
||||||
let waiter = { woken: false, coro: Some(current_coroutine()) }
|
|
||||||
self.waiters.push_back(waiter)
|
|
||||||
match suspend_check_cancel() {
|
|
||||||
Continue => ()
|
|
||||||
Cancelled if waiter.woken => ()
|
|
||||||
Cancelled => {
|
|
||||||
waiter.coro = None
|
|
||||||
raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Wake the first waiting task. Does nothing when no task is waiting.
|
|
||||||
pub fn CondVar::signal(self : CondVar) -> Unit {
|
|
||||||
while self.waiters.pop_front() is Some(waiter) {
|
|
||||||
if waiter.coro is Some(coro) {
|
|
||||||
waiter.woken = true
|
|
||||||
coro.wake()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Wake all waiting tasks. Does nothing when no task is waiting.
|
|
||||||
pub fn CondVar::broadcast(self : CondVar) -> Unit {
|
|
||||||
for waiter in self.waiters {
|
|
||||||
if waiter.coro is Some(coro) {
|
|
||||||
waiter.woken = true
|
|
||||||
coro.wake()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.waiters.clear()
|
|
||||||
}
|
|
||||||
@@ -1,218 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum SuspendResult {
|
|
||||||
Continue
|
|
||||||
Cancelled
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum State {
|
|
||||||
Done
|
|
||||||
Fail(Error)
|
|
||||||
Cancelled
|
|
||||||
Running
|
|
||||||
Suspend((SuspendResult) -> Unit)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
struct Coroutine {
|
|
||||||
coro_id : Int
|
|
||||||
lane : TaskLane
|
|
||||||
mut state : State
|
|
||||||
mut shielded : Bool
|
|
||||||
mut cancelled : Bool
|
|
||||||
mut ready : Bool
|
|
||||||
mut spawner : ((async () -> Unit) -> Unit)?
|
|
||||||
downstream : Set[Coroutine]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
impl Eq for Coroutine with fn equal(c1, c2) {
|
|
||||||
c1.coro_id == c2.coro_id
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
impl Hash for Coroutine with fn hash_combine(self, hasher) {
|
|
||||||
Hash::hash_combine(self.coro_id, hasher)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn Coroutine::wake(self : Coroutine) -> Unit {
|
|
||||||
if !self.ready {
|
|
||||||
self.ready = true
|
|
||||||
enqueue(self)
|
|
||||||
signal_component_task(self.lane)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn is_being_cancelled() -> Bool {
|
|
||||||
let coro = current_coroutine()
|
|
||||||
coro.cancelled && !coro.shielded
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub async fn check_cancellation() -> Unit noraise {
|
|
||||||
if is_being_cancelled() {
|
|
||||||
raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn Coroutine::cancel(self : Coroutine) -> Unit {
|
|
||||||
match self.state {
|
|
||||||
Done | Fail(_) | Cancelled => return
|
|
||||||
Running | Suspend(_) => ()
|
|
||||||
}
|
|
||||||
self.cancelled = true
|
|
||||||
if !self.shielded {
|
|
||||||
self.wake()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn suspend_check_cancel() -> SuspendResult noraise {
|
|
||||||
guard! scheduler.curr_coro is Some(coro)
|
|
||||||
if coro.cancelled && !coro.shielded {
|
|
||||||
return Cancelled
|
|
||||||
}
|
|
||||||
coro.lane.blocking += 1
|
|
||||||
defer {
|
|
||||||
coro.lane.blocking -= 1
|
|
||||||
}
|
|
||||||
async_suspend(fn(cont) {
|
|
||||||
guard! coro.state is Running
|
|
||||||
coro.state = Suspend(cont)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn suspend() -> Unit noraise {
|
|
||||||
match suspend_check_cancel() {
|
|
||||||
Continue => ()
|
|
||||||
Cancelled => raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn spawn_owned(
|
|
||||||
f : async () -> Unit,
|
|
||||||
lane : TaskLane,
|
|
||||||
inherited_spawner : ((async () -> Unit) -> Unit)?,
|
|
||||||
) -> Coroutine {
|
|
||||||
scheduler.coro_id += 1
|
|
||||||
let coro = {
|
|
||||||
state: Running,
|
|
||||||
ready: true,
|
|
||||||
shielded: false,
|
|
||||||
downstream: Set([]),
|
|
||||||
coro_id: scheduler.coro_id,
|
|
||||||
lane,
|
|
||||||
cancelled: false,
|
|
||||||
spawner: inherited_spawner,
|
|
||||||
}
|
|
||||||
fn run(_) {
|
|
||||||
run_async(() => {
|
|
||||||
// Even a task cancelled before its first turn enters its body, so entry
|
|
||||||
// defers run. Cancellation is delivered at its first suspension/check.
|
|
||||||
try capture_cancellation(f, handler=() => coro.state = Cancelled) catch {
|
|
||||||
err => coro.state = Fail(err)
|
|
||||||
} noraise {
|
|
||||||
_ if coro.state is Running => coro.state = Done
|
|
||||||
_ => ()
|
|
||||||
}
|
|
||||||
for coro in coro.downstream {
|
|
||||||
coro.wake()
|
|
||||||
}
|
|
||||||
coro.downstream.clear()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
coro.state = Suspend(run)
|
|
||||||
enqueue(coro)
|
|
||||||
coro
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn spawn(f : async () -> Unit) -> Coroutine {
|
|
||||||
match scheduler.curr_coro {
|
|
||||||
Some(parent) => spawn_owned(f, parent.lane, parent.spawner)
|
|
||||||
None => {
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
let task_state = component_task_state(waitable_set).unwrap()
|
|
||||||
spawn_owned(f, task_state.lane, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn Coroutine::unwrap(self : Coroutine) -> SuspendResult raise {
|
|
||||||
match self.state {
|
|
||||||
Done => Continue
|
|
||||||
Cancelled => Cancelled
|
|
||||||
Fail(err) => raise err
|
|
||||||
Running | Suspend(_) => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn Coroutine::wait(target : Coroutine) -> SuspendResult {
|
|
||||||
guard! scheduler.curr_coro is Some(coro)
|
|
||||||
guard! !physical_equal(coro, target)
|
|
||||||
match target.state {
|
|
||||||
Done => return Continue
|
|
||||||
Cancelled => return Cancelled
|
|
||||||
Fail(err) => raise err
|
|
||||||
Running | Suspend(_) => ()
|
|
||||||
}
|
|
||||||
target.downstream.add(coro)
|
|
||||||
defer target.downstream.remove(coro)
|
|
||||||
suspend()
|
|
||||||
target.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn Coroutine::check_error(coro : Coroutine) -> Unit raise {
|
|
||||||
match coro.state {
|
|
||||||
Fail(err) => raise err
|
|
||||||
Done | Cancelled | Running | Suspend(_) => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub async fn[X] protect_from_cancel(f : async () -> X) -> X nocancel {
|
|
||||||
guard! scheduler.curr_coro is Some(coro)
|
|
||||||
// The shield prevents delivery; capture_cancellation expresses that invariant
|
|
||||||
// in the effect type without changing normal error propagation.
|
|
||||||
capture_cancellation(handler=() => panic(), () => {
|
|
||||||
if coro.shielded {
|
|
||||||
f()
|
|
||||||
} else {
|
|
||||||
coro.shielded = true
|
|
||||||
defer {
|
|
||||||
coro.shielded = false
|
|
||||||
}
|
|
||||||
f()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Observe cancellation without clearing the task's sticky cancelled state.
|
|
||||||
/// Returns None on cancellation, Some on success, and propagates normal errors.
|
|
||||||
pub async fn[X] handle_cancellation(f : async () -> X) -> X? {
|
|
||||||
capture_cancellation(handler=() => None, () => Some(f()))
|
|
||||||
}
|
|
||||||
@@ -1,658 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// State owned by one P3 component task. `task.state` is the sole completion
|
|
||||||
/// source; `owned_coroutines` contains only runtime bridge coroutines.
|
|
||||||
priv struct ComponentTaskState {
|
|
||||||
lane : TaskLane
|
|
||||||
subscribers : Map[Int, Subscriber]
|
|
||||||
task : Coroutine
|
|
||||||
owned_coroutines : @set.Set[Coroutine]
|
|
||||||
mut resolution : TaskResolution
|
|
||||||
mut cancellation_pending : Bool
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct TaskWakeup {
|
|
||||||
reader : Int
|
|
||||||
writer : Int
|
|
||||||
mut reading : Bool
|
|
||||||
mut signaled : Bool
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct Subscriber {
|
|
||||||
mut event : Events?
|
|
||||||
coro : @set.Set[Coroutine]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum TaskResolution {
|
|
||||||
Pending
|
|
||||||
FlushPending
|
|
||||||
Resolved
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn current_waitableset() -> WaitableSet {
|
|
||||||
WaitableSet(tls_get())
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn component_task_state(waitable_set : WaitableSet) -> ComponentTaskState? {
|
|
||||||
component_tasks.get(waitable_set)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn finish_waitableset(
|
|
||||||
waitable_set : WaitableSet,
|
|
||||||
task_state : ComponentTaskState,
|
|
||||||
) -> Unit {
|
|
||||||
guard task_state.owned_coroutines.is_empty() else { panic() }
|
|
||||||
guard task_state.lane.blocking == 0 && task_state.lane.run_later.is_empty() else {
|
|
||||||
panic()
|
|
||||||
}
|
|
||||||
drop_task_wakeup(task_state)
|
|
||||||
guard task_state.subscribers.is_empty() else { panic() }
|
|
||||||
component_tasks.remove(waitable_set)
|
|
||||||
waitable_set.drop()
|
|
||||||
tls_set(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn acknowledge_cancellation(task_state : ComponentTaskState) -> Unit {
|
|
||||||
guard task_state.cancellation_pending else { return }
|
|
||||||
match task_state.task.state {
|
|
||||||
Done => () // The generated wrapper already called task.return.
|
|
||||||
Fail(_) | Cancelled =>
|
|
||||||
if task_state.resolution is Pending {
|
|
||||||
task_cancel()
|
|
||||||
task_state.resolution = Resolved
|
|
||||||
}
|
|
||||||
Running | Suspend(_) => return
|
|
||||||
}
|
|
||||||
task_state.cancellation_pending = false
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn notify_subscriber(
|
|
||||||
task_state : ComponentTaskState,
|
|
||||||
waitable_id : Int,
|
|
||||||
event : Events,
|
|
||||||
) -> Unit {
|
|
||||||
if task_state.lane.wakeup is Some(wakeup) && wakeup.reader == waitable_id {
|
|
||||||
guard! event is StreamRead(i, { progress: 1, copy_result: Completed }) &&
|
|
||||||
i == waitable_id
|
|
||||||
wakeup.reading = false
|
|
||||||
wakeup.signaled = false
|
|
||||||
task_state.subscribers.remove(waitable_id)
|
|
||||||
waitable_join(waitable_id, 0)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard! task_state.subscribers.get(waitable_id) is Some(subscriber)
|
|
||||||
subscriber.event = Some(event)
|
|
||||||
subscriber.coro.each(Coroutine::wake)
|
|
||||||
// Keep the delivered event discoverable until its waiter consumes it. A
|
|
||||||
// local cancellation can race with the wakeup, and cancelling an already
|
|
||||||
// terminal canonical operation would trap.
|
|
||||||
waitable_join(waitable_id, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn next_callback(
|
|
||||||
waitable_set : WaitableSet,
|
|
||||||
task_state : ComponentTaskState,
|
|
||||||
) -> Int {
|
|
||||||
if task_state.resolution is FlushPending {
|
|
||||||
task_state.resolution = Resolved
|
|
||||||
reschedule(task_state.lane)
|
|
||||||
}
|
|
||||||
acknowledge_cancellation(task_state)
|
|
||||||
if task_state.task.state is (Done | Fail(_) | Cancelled) &&
|
|
||||||
no_more_work(task_state.lane) {
|
|
||||||
let resolution = task_state.resolution
|
|
||||||
finish_waitableset(waitable_set, task_state)
|
|
||||||
guard! resolution is Resolved
|
|
||||||
return CallbackCode::Completed.encode()
|
|
||||||
}
|
|
||||||
if has_immediately_ready_task(task_state.lane) {
|
|
||||||
if !task_state.subscribers.is_empty() {
|
|
||||||
let (event, waitable_id) = waitable_set.poll()
|
|
||||||
match event {
|
|
||||||
None => ()
|
|
||||||
TaskCancelled => panic()
|
|
||||||
_ => notify_subscriber(task_state, waitable_id, event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tls_set(waitable_set.0)
|
|
||||||
return CallbackCode::Yield.encode()
|
|
||||||
}
|
|
||||||
tls_set(waitable_set.0)
|
|
||||||
arm_task_wakeup(waitable_set, task_state)
|
|
||||||
CallbackCode::Wait(waitable_set.0).encode()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn with_waitableset(f : async () -> Unit) -> Int {
|
|
||||||
let waitable_set = WaitableSet::new()
|
|
||||||
let lane = { blocking: 0, run_later: Deque([]), wakeup: None }
|
|
||||||
tls_set(waitable_set.0)
|
|
||||||
let task = spawn_owned(f, lane, None)
|
|
||||||
let task_state = {
|
|
||||||
lane,
|
|
||||||
subscribers: Map([]),
|
|
||||||
task,
|
|
||||||
owned_coroutines: Set([]),
|
|
||||||
resolution: Pending,
|
|
||||||
cancellation_pending: false,
|
|
||||||
}
|
|
||||||
component_tasks.set(waitable_set, task_state)
|
|
||||||
reschedule(lane)
|
|
||||||
next_callback(waitable_set, task_state)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Record that the generated export wrapper has resolved the component-model
|
|
||||||
/// task. One additional fair scheduling round lets the enclosing MoonBit task
|
|
||||||
/// group observe its body completing without draining runnable background work.
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn task_returned() -> Unit {
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
guard! component_task_state(waitable_set) is Some(task_state)
|
|
||||||
guard! task_state.resolution is Pending
|
|
||||||
task_state.resolution = FlushPending
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn cb(event : Int, waitable_id : Int, code : Int) -> Int {
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
guard component_task_state(waitable_set) is Some(task_state) else { panic() }
|
|
||||||
let events = Events::new(EventCode::from(event), waitable_id, code)
|
|
||||||
let preserve_task_wakeup = match events {
|
|
||||||
StreamRead(i, _) =>
|
|
||||||
match task_state.lane.wakeup {
|
|
||||||
Some(wakeup) => wakeup.reader == i
|
|
||||||
None => false
|
|
||||||
}
|
|
||||||
_ => false
|
|
||||||
}
|
|
||||||
cancel_task_wakeup_read(task_state, preserve=preserve_task_wakeup)
|
|
||||||
match events {
|
|
||||||
None => {
|
|
||||||
reschedule(task_state.lane)
|
|
||||||
next_callback(waitable_set, task_state)
|
|
||||||
}
|
|
||||||
TaskCancelled => {
|
|
||||||
task_state.cancellation_pending = true
|
|
||||||
task_state.task.cancel()
|
|
||||||
task_state.owned_coroutines.each(Coroutine::cancel)
|
|
||||||
reschedule(task_state.lane)
|
|
||||||
next_callback(waitable_set, task_state)
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
notify_subscriber(task_state, waitable_id, events)
|
|
||||||
reschedule(task_state.lane)
|
|
||||||
next_callback(waitable_set, task_state)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
let component_tasks : Map[WaitableSet, ComponentTaskState] = Map([])
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Spawn a coroutine owned by the current component-model async task.
|
|
||||||
///
|
|
||||||
/// This is intended for runtime bridge work such as lowering local futures and
|
|
||||||
/// streams to component-model handles. The coroutine does not participate in
|
|
||||||
/// `TaskGroup` structured concurrency, but it is cancelled when the current
|
|
||||||
/// component-model task is cancelled and keeps the waitable-set alive until it
|
|
||||||
/// terminates.
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn spawn_component_task_current(f : async () -> Unit) -> Unit {
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
guard! component_task_state(waitable_set) is Some(task_state)
|
|
||||||
let coro = spawn_owned(
|
|
||||||
async fn() -> Unit {
|
|
||||||
let coro = current_coroutine()
|
|
||||||
defer task_state.owned_coroutines.remove(coro)
|
|
||||||
f()
|
|
||||||
},
|
|
||||||
task_state.lane,
|
|
||||||
Some(spawn_component_task_current),
|
|
||||||
)
|
|
||||||
task_state.owned_coroutines.add(coro)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn has_component_task_scope() -> Bool {
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
component_task_state(waitable_set) is Some(_)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn current_component_task_token() -> Int {
|
|
||||||
current_waitableset().0
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn detach_waitable(task_state : ComponentTaskState, waitable_id : Int) -> Unit {
|
|
||||||
if task_state.subscribers.get(waitable_id) is Some(subscriber) {
|
|
||||||
subscriber.coro.clear()
|
|
||||||
task_state.subscribers.remove(waitable_id)
|
|
||||||
}
|
|
||||||
waitable_join(waitable_id, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn task_wakeup(lane : TaskLane) -> TaskWakeup {
|
|
||||||
match lane.wakeup {
|
|
||||||
Some(wakeup) => wakeup
|
|
||||||
None => {
|
|
||||||
let pair = task_wakeup_stream_new()
|
|
||||||
let wakeup = {
|
|
||||||
reader: pair.to_int(),
|
|
||||||
writer: (pair >> 32).to_int(),
|
|
||||||
reading: false,
|
|
||||||
signaled: false,
|
|
||||||
}
|
|
||||||
lane.wakeup = Some(wakeup)
|
|
||||||
wakeup
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn arm_task_wakeup(
|
|
||||||
waitable_set : WaitableSet,
|
|
||||||
task_state : ComponentTaskState,
|
|
||||||
) -> Unit {
|
|
||||||
let wakeup = task_wakeup(task_state.lane)
|
|
||||||
if wakeup.reading {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
let result = task_wakeup_stream_read(wakeup.reader, 0, 1)
|
|
||||||
guard! result == -1
|
|
||||||
let subscribers = task_state.subscribers
|
|
||||||
guard! subscribers.get(wakeup.reader) is None
|
|
||||||
subscribers.set(wakeup.reader, { event: None, coro: Set([]) })
|
|
||||||
waitable_join(wakeup.reader, waitable_set.0)
|
|
||||||
wakeup.reading = true
|
|
||||||
wakeup.signaled = false
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn signal_component_task(lane : TaskLane) -> Unit {
|
|
||||||
guard lane.wakeup is Some(wakeup) else { return }
|
|
||||||
if !wakeup.reading || wakeup.signaled {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
let result = StreamResult::from(task_wakeup_stream_write(wakeup.writer, 0, 1))
|
|
||||||
guard! result.progress == 1 && result.copy_result is Completed
|
|
||||||
wakeup.signaled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn cancel_task_wakeup_read(
|
|
||||||
task_state : ComponentTaskState,
|
|
||||||
preserve~ : Bool,
|
|
||||||
) -> Unit {
|
|
||||||
guard task_state.lane.wakeup is Some(wakeup) else { return }
|
|
||||||
if !wakeup.reading || preserve {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
waitable_join(wakeup.reader, 0)
|
|
||||||
task_state.subscribers.remove(wakeup.reader)
|
|
||||||
ignore(task_wakeup_stream_cancel_read(wakeup.reader))
|
|
||||||
wakeup.reading = false
|
|
||||||
wakeup.signaled = false
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn drop_task_wakeup(task_state : ComponentTaskState) -> Unit {
|
|
||||||
guard task_state.lane.wakeup is Some(wakeup) else { return }
|
|
||||||
if wakeup.reading {
|
|
||||||
waitable_join(wakeup.reader, 0)
|
|
||||||
task_state.subscribers.remove(wakeup.reader)
|
|
||||||
ignore(task_wakeup_stream_cancel_read(wakeup.reader))
|
|
||||||
}
|
|
||||||
task_wakeup_stream_drop_readable(wakeup.reader)
|
|
||||||
task_wakeup_stream_drop_writable(wakeup.writer)
|
|
||||||
task_state.lane.wakeup = None
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn cancel_waitable_event(
|
|
||||||
owner_task : Int,
|
|
||||||
waitable_id : Int,
|
|
||||||
cancel : () -> Events,
|
|
||||||
) -> Events {
|
|
||||||
let waitable_set = WaitableSet(owner_task)
|
|
||||||
guard! component_task_state(waitable_set) is Some(task_state)
|
|
||||||
guard! task_state.subscribers.get(waitable_id) is Some(subscriber)
|
|
||||||
|
|
||||||
// Baseline cancel-read is synchronous and traps while its endpoint belongs
|
|
||||||
// to a waitable set. Detach before invoking the generated intrinsic.
|
|
||||||
waitable_join(waitable_id, 0)
|
|
||||||
let event = match subscriber.event {
|
|
||||||
Some(event) => event
|
|
||||||
None => cancel()
|
|
||||||
}
|
|
||||||
subscriber.event = Some(event)
|
|
||||||
subscriber.coro.each(Coroutine::wake)
|
|
||||||
task_state.subscribers.remove(waitable_id)
|
|
||||||
event
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Complete an operation immediately or suspend until its terminal waitable
|
|
||||||
/// event arrives. This is the single owner of subscription registration and
|
|
||||||
/// detachment for future and stream copy operations.
|
|
||||||
async fn waitable_event(waitable_id : Int, immediate : Events?) -> Events {
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
let task_state = component_task_state(waitable_set).unwrap()
|
|
||||||
let subscribers = task_state.subscribers
|
|
||||||
match immediate {
|
|
||||||
Some(event) => {
|
|
||||||
if subscribers.get(waitable_id) is Some(subscriber) {
|
|
||||||
subscriber.event = Some(event)
|
|
||||||
subscriber.coro.each(Coroutine::wake)
|
|
||||||
subscriber.coro.clear()
|
|
||||||
waitable_join(waitable_id, 0)
|
|
||||||
}
|
|
||||||
subscribers.remove(waitable_id)
|
|
||||||
event
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let subscriber = if subscribers.get(waitable_id) is Some(subscriber) {
|
|
||||||
subscriber
|
|
||||||
} else {
|
|
||||||
let subscriber = { event: None, coro: Set([]) }
|
|
||||||
subscribers.set(waitable_id, subscriber)
|
|
||||||
subscriber
|
|
||||||
}
|
|
||||||
guard! subscriber.event is None
|
|
||||||
waitable_join(waitable_id, waitable_set.0)
|
|
||||||
let coro = current_coroutine()
|
|
||||||
subscriber.coro.add(coro)
|
|
||||||
defer subscriber.coro.remove(coro)
|
|
||||||
let event = match suspend_check_cancel() {
|
|
||||||
Continue => subscriber.event.unwrap()
|
|
||||||
Cancelled =>
|
|
||||||
match subscriber.event {
|
|
||||||
// Completion owns the canonical buffer once its event is
|
|
||||||
// delivered, even if local cancellation wakes the same coroutine.
|
|
||||||
Some(event) => event
|
|
||||||
None => raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
subscribers.remove(waitable_id)
|
|
||||||
event
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub async fn suspend_for_subtask(
|
|
||||||
val : Int,
|
|
||||||
settle_lowered_arguments : (Bool) -> Unit,
|
|
||||||
drop_returned_result : () -> Unit,
|
|
||||||
) -> Unit {
|
|
||||||
let task = SubTask::from(val)
|
|
||||||
let mut cleaned = false
|
|
||||||
|
|
||||||
// Once the subtask leaves Starting, canonical arguments are either
|
|
||||||
// transferred or rejected. Generated code owns the corresponding action.
|
|
||||||
fn settle_arguments(state : SubTaskState) -> Unit {
|
|
||||||
if !cleaned && !(state is Starting) {
|
|
||||||
cleaned = true
|
|
||||||
settle_lowered_arguments(state is Cancelled_before_started)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Immediate completion without a handle.
|
|
||||||
if task.handle == 0 {
|
|
||||||
settle_arguments(task.state)
|
|
||||||
match task.state {
|
|
||||||
Returned => return
|
|
||||||
Cancelled_before_started => raise SubTaskCancelled(before_started=true)
|
|
||||||
Cancelled_before_returned => raise SubTaskCancelled(before_started=false)
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
defer subtask_drop(task.handle)
|
|
||||||
|
|
||||||
// Initial state, return if finished
|
|
||||||
settle_arguments(task.state)
|
|
||||||
match task.state {
|
|
||||||
Returned => return
|
|
||||||
Cancelled_before_started => raise SubTaskCancelled(before_started=true)
|
|
||||||
Cancelled_before_returned => raise SubTaskCancelled(before_started=false)
|
|
||||||
_ => ()
|
|
||||||
}
|
|
||||||
|
|
||||||
// A subtask can report intermediate states, so register the same subscriber
|
|
||||||
// again after each callback until a terminal state arrives.
|
|
||||||
let waitable_set = current_waitableset()
|
|
||||||
let task_state = component_task_state(waitable_set).unwrap()
|
|
||||||
let set = @set.Set([])
|
|
||||||
let subscriber = { event: None, coro: set }
|
|
||||||
let coro = current_coroutine()
|
|
||||||
set.add(coro)
|
|
||||||
defer {
|
|
||||||
set.remove(coro)
|
|
||||||
detach_waitable(task_state, task.handle)
|
|
||||||
}
|
|
||||||
for ;; {
|
|
||||||
let subscribers = task_state.subscribers
|
|
||||||
guard! subscribers.get(task.handle) is None
|
|
||||||
subscribers.set(task.handle, subscriber)
|
|
||||||
waitable_join(task.handle, waitable_set.0)
|
|
||||||
if suspend_check_cancel() is Cancelled {
|
|
||||||
protect_from_cancel(() => {
|
|
||||||
detach_waitable(task_state, task.handle)
|
|
||||||
// Consume a delivered terminal event before requesting cancellation:
|
|
||||||
// cancelling an already terminal canonical operation traps.
|
|
||||||
if subscriber.event is Some(Subtask(i, state)) {
|
|
||||||
guard! i == task.handle
|
|
||||||
settle_arguments(state)
|
|
||||||
match state {
|
|
||||||
Returned => {
|
|
||||||
drop_returned_result()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Cancelled_before_started | Cancelled_before_returned => return
|
|
||||||
Starting | Started => subscriber.event = None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let state = task.cancel()
|
|
||||||
settle_arguments(state)
|
|
||||||
match state {
|
|
||||||
Returned => drop_returned_result()
|
|
||||||
Cancelled_before_started | Cancelled_before_returned => ()
|
|
||||||
Starting | Started => panic()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
|
|
||||||
task_state.subscribers.remove(task.handle)
|
|
||||||
|
|
||||||
// Subsequent state, return if finished
|
|
||||||
if subscriber.event is Some(Subtask(i, state)) {
|
|
||||||
guard! i == task.handle
|
|
||||||
settle_arguments(state)
|
|
||||||
match state {
|
|
||||||
Returned => return
|
|
||||||
Cancelled_before_started => raise SubTaskCancelled(before_started=true)
|
|
||||||
Cancelled_before_returned =>
|
|
||||||
raise SubTaskCancelled(before_started=false)
|
|
||||||
_ => subscriber.event = None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub async fn suspend_for_future_read(idx : Int, val : Int) -> Unit {
|
|
||||||
let event = if val == -1 {
|
|
||||||
waitable_event(idx, None)
|
|
||||||
} else {
|
|
||||||
let result = FutureReadResult::from(val)
|
|
||||||
waitable_event(idx, Some(FutureRead(idx, result)))
|
|
||||||
}
|
|
||||||
guard! event is FutureRead(i, result) && i == idx
|
|
||||||
match result {
|
|
||||||
Completed => return
|
|
||||||
Cancelled => raise FutureReadError::Cancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn cancel_future_read(
|
|
||||||
owner_task : Int,
|
|
||||||
idx : Int,
|
|
||||||
cancel : () -> Int,
|
|
||||||
) -> Bool {
|
|
||||||
let event = cancel_waitable_event(owner_task, idx, () => {
|
|
||||||
FutureRead(idx, FutureReadResult::from(cancel()))
|
|
||||||
})
|
|
||||||
guard! event is FutureRead(i, result) && i == idx
|
|
||||||
match result {
|
|
||||||
Completed => true
|
|
||||||
Cancelled => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn cancel_stream_read(
|
|
||||||
owner_task : Int,
|
|
||||||
idx : Int,
|
|
||||||
cancel : () -> Int,
|
|
||||||
) -> Int {
|
|
||||||
let event = cancel_waitable_event(owner_task, idx, () => {
|
|
||||||
StreamRead(idx, StreamResult::from(cancel()))
|
|
||||||
})
|
|
||||||
guard! event is StreamRead(i, result) && i == idx
|
|
||||||
result.progress
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn cancel_stream_write(idx : Int, cancel : () -> Int) -> Int {
|
|
||||||
let event = cancel_waitable_event(current_component_task_token(), idx, () => {
|
|
||||||
StreamWrite(idx, StreamResult::from(cancel()))
|
|
||||||
})
|
|
||||||
guard! event is StreamWrite(i, result) && i == idx
|
|
||||||
result.progress
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub async fn suspend_for_future_write_terminal(idx : Int, val : Int) -> Bool? {
|
|
||||||
let event = if val == -1 {
|
|
||||||
waitable_event(idx, None)
|
|
||||||
} else {
|
|
||||||
let result = FutureWriteResult::from(val)
|
|
||||||
waitable_event(idx, Some(FutureWrite(idx, result)))
|
|
||||||
}
|
|
||||||
guard! event is FutureWrite(i, result) && i == idx
|
|
||||||
match result {
|
|
||||||
Completed => Some(true)
|
|
||||||
Dropped => Some(false)
|
|
||||||
Cancelled => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub async fn suspend_for_stream_read(idx : Int, val : Int) -> (Int, Bool) {
|
|
||||||
let event = if val == -1 {
|
|
||||||
waitable_event(idx, None)
|
|
||||||
} else {
|
|
||||||
let result = StreamResult::from(val)
|
|
||||||
waitable_event(idx, Some(StreamRead(idx, result)))
|
|
||||||
}
|
|
||||||
guard! event is StreamRead(i, { progress, copy_result }) && i == idx
|
|
||||||
match copy_result {
|
|
||||||
Completed => return (progress, false)
|
|
||||||
Dropped => return (progress, true)
|
|
||||||
Cancelled =>
|
|
||||||
if progress > 0 {
|
|
||||||
return (progress, false)
|
|
||||||
} else {
|
|
||||||
raise StreamReadCancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub async fn suspend_for_stream_write(idx : Int, val : Int) -> (Int, Bool) {
|
|
||||||
let event = if val == -1 {
|
|
||||||
waitable_event(idx, None)
|
|
||||||
} else {
|
|
||||||
let result = StreamResult::from(val)
|
|
||||||
waitable_event(idx, Some(StreamWrite(idx, result)))
|
|
||||||
}
|
|
||||||
guard! event is StreamWrite(i, { progress, copy_result }) && i == idx
|
|
||||||
match copy_result {
|
|
||||||
Completed => return (progress, false)
|
|
||||||
Dropped => return (progress, true)
|
|
||||||
Cancelled => return (progress, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub suberror OpCancelled {
|
|
||||||
SubTaskCancelled(before_started~ : Bool)
|
|
||||||
StreamReadCancelled
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub(all) suberror FutureReadError {
|
|
||||||
Cancelled
|
|
||||||
Dropped
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub(all) suberror EndpointBusy {
|
|
||||||
Read
|
|
||||||
Write
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// A mutex for synchronization between tasks.
|
|
||||||
#valtype
|
|
||||||
pub struct Mutex {
|
|
||||||
priv semaphore : Semaphore
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Create a mutex in the released state.
|
|
||||||
pub fn Mutex::Mutex() -> Mutex {
|
|
||||||
{ semaphore: Semaphore(1) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Acquire the mutex in FIFO order.
|
|
||||||
pub async fn Mutex::acquire(self : Mutex) -> Unit noraise {
|
|
||||||
self.semaphore.acquire()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Try to acquire the mutex without waiting.
|
|
||||||
pub fn Mutex::try_acquire(self : Mutex) -> Bool {
|
|
||||||
self.semaphore.try_acquire()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Release the mutex. The mutex must already be acquired.
|
|
||||||
pub fn Mutex::release(self : Mutex) -> Unit {
|
|
||||||
self.semaphore.release()
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn[T] async_suspend(cb : ((T) -> Unit) -> Unit) -> T noraise + nocancel = "%async.suspend"
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn run_async(f : async () -> Unit noraise + nocancel) = "%async.run"
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Deliver cancellation from the runtime, or resume it after boundary cleanup.
|
|
||||||
#internal(wit_bindgen, "runtime and generated binding code only")
|
|
||||||
#doc(hidden)
|
|
||||||
pub async fn[X] raise_cancellation_signal() -> X noraise = "%async.raise_cancellation_signal"
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn[X] capture_cancellation(
|
|
||||||
f : async () -> X,
|
|
||||||
handler~ : async () -> X nocancel,
|
|
||||||
) -> X nocancel = "%async.capture_cancellation"
|
|
||||||
@@ -1,211 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum PromiseState[X] {
|
|
||||||
Pending
|
|
||||||
Completed(X)
|
|
||||||
Failed(Error)
|
|
||||||
Closed
|
|
||||||
ReaderDropped
|
|
||||||
Consumed
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct PromiseCell[X] {
|
|
||||||
mut state : PromiseState[X]
|
|
||||||
ready : Semaphore
|
|
||||||
cleanup : ((X) -> Unit)?
|
|
||||||
mut reading : Bool
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// The local producer side of a one-shot `Future[X]`.
|
|
||||||
///
|
|
||||||
/// This is a MoonBit coordination value, not a component-model future writable
|
|
||||||
/// endpoint. Conversion to a component future remains generated at an FFI site.
|
|
||||||
pub struct Promise[X] {
|
|
||||||
priv cell : Ref[PromiseCell[X]]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub(all) suberror PromiseClosed derive(Debug)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#deprecated
|
|
||||||
pub extend PromiseClosed with Debug::{to_repr}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] new_future_promise(cleanup : ((X) -> Unit)?) -> (Future[X], Promise[X]) {
|
|
||||||
let cell : Ref[PromiseCell[X]] = {
|
|
||||||
val: {
|
|
||||||
state: Pending,
|
|
||||||
ready: Semaphore(1, initial_value=0),
|
|
||||||
cleanup,
|
|
||||||
reading: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
({ inner: Promised(cell) }, { cell, })
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Create a local one-shot Future/Promise pair.
|
|
||||||
pub fn[X] Future::new() -> (Future[X], Promise[X]) {
|
|
||||||
new_future_promise(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Create a local one-shot Future/Promise pair with value-discard cleanup.
|
|
||||||
///
|
|
||||||
/// The cleanup callback runs when the promise completed successfully but the
|
|
||||||
/// future is dropped before consuming the value.
|
|
||||||
pub fn[X] Future::new_with_cleanup(
|
|
||||||
cleanup : (X) -> Unit,
|
|
||||||
) -> (Future[X], Promise[X]) {
|
|
||||||
new_future_promise(Some(cleanup))
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Complete the paired future with `value`.
|
|
||||||
///
|
|
||||||
/// `true` means the future accepted ownership. `false` means its reader was
|
|
||||||
/// already dropped and the caller retains ownership of `value`.
|
|
||||||
pub fn[X] Promise::complete(self : Promise[X], value : X) -> Bool {
|
|
||||||
match self.cell.val.state {
|
|
||||||
Pending => {
|
|
||||||
self.cell.val.state = Completed(value)
|
|
||||||
self.cell.val.ready.release()
|
|
||||||
true
|
|
||||||
}
|
|
||||||
ReaderDropped => false
|
|
||||||
Completed(_) | Failed(_) | Closed | Consumed =>
|
|
||||||
abort("promise already settled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Complete the paired future with an error raised by `Future::get`.
|
|
||||||
pub fn[X] Promise::fail(self : Promise[X], error : Error) -> Bool {
|
|
||||||
match self.cell.val.state {
|
|
||||||
Pending => {
|
|
||||||
self.cell.val.state = Failed(error)
|
|
||||||
self.cell.val.ready.release()
|
|
||||||
true
|
|
||||||
}
|
|
||||||
ReaderDropped => false
|
|
||||||
Completed(_) | Failed(_) | Closed | Consumed =>
|
|
||||||
abort("promise already settled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Close the paired future without producing a value.
|
|
||||||
///
|
|
||||||
/// A local waiter observes `PromiseClosed`. An outgoing component future still
|
|
||||||
/// cannot represent this terminal state and follows the bridge's no-value
|
|
||||||
/// failure policy if it has already crossed an FFI boundary.
|
|
||||||
pub fn[X] Promise::close(self : Promise[X]) -> Bool {
|
|
||||||
match self.cell.val.state {
|
|
||||||
Pending => {
|
|
||||||
self.cell.val.state = Closed
|
|
||||||
self.cell.val.ready.release()
|
|
||||||
true
|
|
||||||
}
|
|
||||||
ReaderDropped => false
|
|
||||||
Completed(_) | Failed(_) | Closed | Consumed =>
|
|
||||||
abort("promise already settled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] consume_promised_future(cell : Ref[PromiseCell[X]]) -> X raise {
|
|
||||||
match cell.val.state {
|
|
||||||
Completed(value) => {
|
|
||||||
cell.val.state = Consumed
|
|
||||||
value
|
|
||||||
}
|
|
||||||
Failed(error) => {
|
|
||||||
cell.val.state = Consumed
|
|
||||||
raise error
|
|
||||||
}
|
|
||||||
Closed => {
|
|
||||||
cell.val.state = Consumed
|
|
||||||
raise PromiseClosed
|
|
||||||
}
|
|
||||||
Pending => panic()
|
|
||||||
ReaderDropped => raise FutureReadError::Dropped
|
|
||||||
Consumed => abort("future already consumed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn[X] promised_future_get(cell : Ref[PromiseCell[X]]) -> X {
|
|
||||||
if cell.val.reading {
|
|
||||||
abort("future read already in progress")
|
|
||||||
}
|
|
||||||
match cell.val.state {
|
|
||||||
Pending => {
|
|
||||||
cell.val.reading = true
|
|
||||||
{
|
|
||||||
errdefer {
|
|
||||||
cell.val.reading = false
|
|
||||||
if cell.val.state is Pending {
|
|
||||||
cell.val.state = ReaderDropped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cell.val.ready.acquire()
|
|
||||||
}
|
|
||||||
cell.val.reading = false
|
|
||||||
consume_promised_future(cell)
|
|
||||||
}
|
|
||||||
Completed(_) | Failed(_) | Closed => consume_promised_future(cell)
|
|
||||||
ReaderDropped => consume_promised_future(cell)
|
|
||||||
Consumed => abort("future already consumed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] promised_future_drop(
|
|
||||||
cell : Ref[PromiseCell[X]],
|
|
||||||
fallback_cleanup : ((X) -> Unit)?,
|
|
||||||
) -> Unit {
|
|
||||||
if cell.val.reading {
|
|
||||||
match cell.val.state {
|
|
||||||
Pending => {
|
|
||||||
cell.val.state = ReaderDropped
|
|
||||||
cell.val.ready.release()
|
|
||||||
}
|
|
||||||
// Once settlement assigned a result, the waiting reader owns it.
|
|
||||||
Completed(_) | Failed(_) | Closed => ()
|
|
||||||
ReaderDropped | Consumed => ()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
match cell.val.state {
|
|
||||||
Pending => cell.val.state = ReaderDropped
|
|
||||||
Completed(value) => {
|
|
||||||
cell.val.state = Consumed
|
|
||||||
match cell.val.cleanup {
|
|
||||||
Some(cleanup) => cleanup(value)
|
|
||||||
None =>
|
|
||||||
match fallback_cleanup {
|
|
||||||
Some(cleanup) => cleanup(value)
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Failed(_) | Closed => cell.val.state = Consumed
|
|
||||||
ReaderDropped | Consumed => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct Scheduler {
|
|
||||||
mut coro_id : Int
|
|
||||||
mut curr_coro : Coroutine?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// The per-component-task part of the scheduler. Coroutines retain this lane,
|
|
||||||
/// not `ComponentTaskState`, so a retired task cannot be kept alive by an RC
|
|
||||||
/// cycle through its registry entry or subscribers.
|
|
||||||
priv struct TaskLane {
|
|
||||||
mut blocking : Int
|
|
||||||
run_later : @deque.Deque[Coroutine]
|
|
||||||
mut wakeup : TaskWakeup?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
let scheduler : Scheduler = { coro_id: 0, curr_coro: None }
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn current_coroutine() -> Coroutine {
|
|
||||||
scheduler.curr_coro.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn enqueue(coro : Coroutine) -> Unit {
|
|
||||||
coro.lane.run_later.push_back(coro)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn has_immediately_ready_task(lane : TaskLane) -> Bool {
|
|
||||||
!lane.run_later.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn no_more_work(lane : TaskLane) -> Bool {
|
|
||||||
lane.blocking == 0 && lane.run_later.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Run one fair scheduling round for a component task. Coroutines woken or
|
|
||||||
/// spawned during this round are left for the next round so the component
|
|
||||||
/// event loop gets a chance to poll completed waitables.
|
|
||||||
fn reschedule(lane : TaskLane) -> Unit {
|
|
||||||
let count = lane.run_later.length()
|
|
||||||
for _ in 0..<count {
|
|
||||||
guard lane.run_later.pop_front() is Some(coro) else { break }
|
|
||||||
coro.ready = false
|
|
||||||
guard coro.state is Suspend(cont) else { }
|
|
||||||
coro.state = Running
|
|
||||||
let last_coro = scheduler.curr_coro
|
|
||||||
scheduler.curr_coro = Some(coro)
|
|
||||||
if coro.cancelled && !coro.shielded {
|
|
||||||
cont(Cancelled)
|
|
||||||
} else {
|
|
||||||
cont(Continue)
|
|
||||||
}
|
|
||||||
scheduler.curr_coro = last_coro
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct SemaphoreWaiter {
|
|
||||||
// Once a permit is assigned, cancellation must not lose it before the
|
|
||||||
// waiter resumes. This matches moonbitlang/async's semaphore semantics.
|
|
||||||
mut acquired : Bool
|
|
||||||
mut coro : Coroutine?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub struct Semaphore {
|
|
||||||
size : Int
|
|
||||||
priv mut value : Int
|
|
||||||
priv waiters : @deque.Deque[SemaphoreWaiter]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Create a semaphore with at most `size` permits.
|
|
||||||
///
|
|
||||||
/// The initial number of available permits is `initial_value`, or `size` when
|
|
||||||
/// omitted.
|
|
||||||
#alias(new, deprecated)
|
|
||||||
pub fn Semaphore::Semaphore(
|
|
||||||
size : Int,
|
|
||||||
initial_value? : Int = size,
|
|
||||||
) -> Semaphore {
|
|
||||||
if size <= 0 {
|
|
||||||
abort("size of semaphore must be positive")
|
|
||||||
}
|
|
||||||
guard! initial_value >= 0 && initial_value <= size
|
|
||||||
{ value: initial_value, size, waiters: Deque([]) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Release one permit. Waiting coroutines acquire permits in FIFO order.
|
|
||||||
///
|
|
||||||
/// Each call must correspond one-to-one with a successful `acquire`. Calling
|
|
||||||
/// `release` without owning a permit is invalid.
|
|
||||||
pub fn Semaphore::release(self : Semaphore) -> Unit {
|
|
||||||
if self.value >= self.size {
|
|
||||||
abort("semaphore: too many release")
|
|
||||||
}
|
|
||||||
while self.waiters.pop_front() is Some(waiter) {
|
|
||||||
if waiter.coro is Some(coro) {
|
|
||||||
waiter.acquired = true
|
|
||||||
coro.wake()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} nobreak {
|
|
||||||
self.value += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Acquire one permit, suspending in FIFO order when none is available.
|
|
||||||
pub async fn Semaphore::acquire(self : Semaphore) -> Unit noraise {
|
|
||||||
if self.value > 0 {
|
|
||||||
self.value -= 1
|
|
||||||
} else {
|
|
||||||
let waiter = { coro: Some(current_coroutine()), acquired: false }
|
|
||||||
self.waiters.push_back(waiter)
|
|
||||||
match suspend_check_cancel() {
|
|
||||||
Continue => ()
|
|
||||||
Cancelled if waiter.acquired => ()
|
|
||||||
Cancelled => {
|
|
||||||
waiter.coro = None
|
|
||||||
raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Try to acquire one permit without suspending.
|
|
||||||
pub fn Semaphore::try_acquire(self : Semaphore) -> Bool {
|
|
||||||
if self.value > 0 {
|
|
||||||
self.value -= 1
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// `Task[X]` represents a running task with result type `X`,
|
|
||||||
/// it can be used to wait and retrieve the result value of the task.
|
|
||||||
pub struct Task[X] {
|
|
||||||
priv value : Ref[X?]
|
|
||||||
priv coro : Coroutine
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// An observed task terminated by cancellation. This is an ordinary error in
|
|
||||||
/// the waiter, distinct from cancellation of the waiter itself.
|
|
||||||
pub suberror TaskCancelled derive(Debug)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#deprecated
|
|
||||||
pub extend TaskCancelled with Debug::{to_repr}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Wait for a task and retrieve its result value.
|
|
||||||
/// If the task fails, `wait` will also fail with the same error.
|
|
||||||
///
|
|
||||||
/// If the target task was cancelled, `wait` raises `TaskCancelled`.
|
|
||||||
/// Cancellation of the current task propagates as a cancellation signal.
|
|
||||||
pub async fn[X] Task::wait(self : Task[X]) -> X {
|
|
||||||
match self.coro.wait() {
|
|
||||||
Continue => self.value.val.unwrap()
|
|
||||||
Cancelled => raise TaskCancelled::TaskCancelled
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Try to obtain the result of the task.
|
|
||||||
/// If the task already terminated, its result value will be returned.
|
|
||||||
/// If the task already failed, `try_wait` will fail immediately.
|
|
||||||
/// If the task is still running, `try_wait` returns `None`.
|
|
||||||
/// `try_wait` is a synchoronous function: it never blocks.
|
|
||||||
pub fn[X] Task::try_wait(self : Task[X]) -> X? raise {
|
|
||||||
self.coro.check_error()
|
|
||||||
self.value.val
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Cancel a running task. Cancelling a terminated task has no effect.
|
|
||||||
/// Subsequent attempt to wait for a cancelled task will receive error.
|
|
||||||
pub fn[X] Task::cancel(self : Task[X]) -> Unit {
|
|
||||||
self.coro.cancel()
|
|
||||||
}
|
|
||||||
@@ -1,269 +0,0 @@
|
|||||||
// Copyright 2025 International Digital Economy Academy
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum TaskGroupState {
|
|
||||||
Done
|
|
||||||
Fail(Error)
|
|
||||||
Cancelled
|
|
||||||
Running
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct GroupDeferBlock(async () -> Unit nocancel)
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// A `TaskGroup` can be used to spawn children tasks that run in parallel.
|
|
||||||
/// Task groups implements *structured concurrency*:
|
|
||||||
/// a task group will only return after all its children task terminates.
|
|
||||||
///
|
|
||||||
/// Task groups also handles *error propagation*:
|
|
||||||
/// by default, if any child task raises error,
|
|
||||||
/// the whole task group will also raise that error,
|
|
||||||
/// and all other remaining child tasks will be cancelled.
|
|
||||||
///
|
|
||||||
/// The type parameter `X` in `TaskGroup[X]` is the result type of the group,
|
|
||||||
/// see `with_task_group` for more detail.
|
|
||||||
pub struct TaskGroup[X] {
|
|
||||||
priv children : Set[Coroutine]
|
|
||||||
priv parent : Coroutine
|
|
||||||
priv mut waiting : Int
|
|
||||||
priv mut state : TaskGroupState
|
|
||||||
priv mut result : X?
|
|
||||||
priv group_defer : Array[GroupDeferBlock]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] TaskGroup::spawn_coroutine(
|
|
||||||
self : TaskGroup[X],
|
|
||||||
f : async () -> Unit,
|
|
||||||
no_wait~ : Bool,
|
|
||||||
allow_failure~ : Bool,
|
|
||||||
) -> Coroutine {
|
|
||||||
guard self.state is Running || !self.children.is_empty() else {
|
|
||||||
abort("trying to spawn from a terminated task group")
|
|
||||||
}
|
|
||||||
if !no_wait {
|
|
||||||
self.waiting += 1
|
|
||||||
}
|
|
||||||
async fn worker() {
|
|
||||||
let coro = current_coroutine()
|
|
||||||
defer {
|
|
||||||
self.children.remove(coro)
|
|
||||||
if !no_wait {
|
|
||||||
self.waiting -= 1
|
|
||||||
if self.waiting == 0 && self.state is Running {
|
|
||||||
for child in self.children {
|
|
||||||
child.cancel()
|
|
||||||
}
|
|
||||||
self.state = Done
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if self.children.is_empty() {
|
|
||||||
self.parent.wake()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
f() catch {
|
|
||||||
err if allow_failure => raise err
|
|
||||||
err => {
|
|
||||||
if self.state is Running {
|
|
||||||
for child in self.children {
|
|
||||||
child.cancel()
|
|
||||||
}
|
|
||||||
self.state = Fail(err)
|
|
||||||
} else {
|
|
||||||
self.state = Fail(err)
|
|
||||||
}
|
|
||||||
raise err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let coro = spawn(worker)
|
|
||||||
self.children.add(coro)
|
|
||||||
if !(self.state is Running) {
|
|
||||||
coro.cancel()
|
|
||||||
}
|
|
||||||
coro
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Spawn a child task in a task group, and run it asynchronously in the background.
|
|
||||||
///
|
|
||||||
/// Unless `no_wait` (`false` by default) is `true`,
|
|
||||||
/// the task group will wait for this task to complete before normal exit.
|
|
||||||
/// No matter what the value of `no_wait` is,
|
|
||||||
/// `with_task_group` will only return after all child tasks terminate.
|
|
||||||
/// The task will be cancelled automatically if it is still running
|
|
||||||
/// when the task group wishes to terminate.
|
|
||||||
///
|
|
||||||
/// Unless `allow_failure` (`false` by default) is `true`,
|
|
||||||
/// the whole task group will also fail if the spawned task fails,
|
|
||||||
/// other tasks in the group will be cancelled in this case.
|
|
||||||
///
|
|
||||||
/// Cancelled tasks are not considered failing. A normal error raised during
|
|
||||||
/// cancellation cleanup is still considered a failure.
|
|
||||||
///
|
|
||||||
/// A task spawned while the group is cancelling still starts, but begins in a
|
|
||||||
/// cancelled state so its entry cleanup can run. Spawning after the group has
|
|
||||||
/// terminated or from a group defer is invalid and aborts.
|
|
||||||
///
|
|
||||||
/// It is undefined whether the child task will start running immediately
|
|
||||||
/// before `spawn_bg` returns.
|
|
||||||
pub fn[X] TaskGroup::spawn_bg(
|
|
||||||
self : TaskGroup[X],
|
|
||||||
f : async () -> Unit,
|
|
||||||
no_wait? : Bool = false,
|
|
||||||
allow_failure? : Bool = false,
|
|
||||||
) -> Unit {
|
|
||||||
ignore(self.spawn_coroutine(f, no_wait~, allow_failure~))
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Spawn a child task in a task group, compute a result asynchronously.
|
|
||||||
/// A task handle will be returned, the result value of the task can be waited
|
|
||||||
/// and retrieved using `.wait()`, or cancelled using `.cancel()`.
|
|
||||||
///
|
|
||||||
/// Unless `no_wait` (`false` by default) is `true`,
|
|
||||||
/// the task group will wait for this task to complete before normal exit.
|
|
||||||
/// No matter what the value of `no_wait` is,
|
|
||||||
/// `with_task_group` will only return after all child tasks terminate.
|
|
||||||
/// The task will be cancelled automatically if it is still running
|
|
||||||
/// when the task group wishes to terminate.
|
|
||||||
///
|
|
||||||
/// Unless `allow_failure` (`false` by default) is `true`,
|
|
||||||
/// the whole task group will also fail if the spawned task fails,
|
|
||||||
/// other tasks in the group will be cancelled in this case.
|
|
||||||
///
|
|
||||||
/// Cancelled tasks are not considered failing. A normal error raised during
|
|
||||||
/// cancellation cleanup is still considered a failure.
|
|
||||||
///
|
|
||||||
/// A task spawned while the group is cancelling still starts, but begins in a
|
|
||||||
/// cancelled state so its entry cleanup can run. Spawning after the group has
|
|
||||||
/// terminated or from a group defer is invalid and aborts.
|
|
||||||
///
|
|
||||||
/// It is undefined whether the child task will start running immediately
|
|
||||||
/// before `spawn` returns.
|
|
||||||
pub fn[G, X] TaskGroup::spawn(
|
|
||||||
self : TaskGroup[G],
|
|
||||||
f : async () -> X,
|
|
||||||
no_wait? : Bool = false,
|
|
||||||
allow_failure? : Bool = false,
|
|
||||||
) -> Task[X] {
|
|
||||||
let value = @ref.new(Option::None)
|
|
||||||
let coro = self.spawn_coroutine(
|
|
||||||
() => value.val = Some(f()),
|
|
||||||
no_wait~,
|
|
||||||
allow_failure~,
|
|
||||||
)
|
|
||||||
{ value, coro }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Attach a defer block, represented as a cleanup function, to a task group.
|
|
||||||
/// The cleanup function runs after every child task has terminated. Spawning a
|
|
||||||
/// new child from a group defer is invalid and aborts.
|
|
||||||
/// Group scoped defer blocks are executed in FILO order, just like normal `defer`.
|
|
||||||
/// `with_task_group` will only exit after all group defer blocks terminate.
|
|
||||||
///
|
|
||||||
/// Group defer blocks must be `nocancel`. Use `protect_from_cancel` when
|
|
||||||
/// running cancellable operations in a group defer.
|
|
||||||
/// It is highly recommended to add a hard timeout to async defer block in this case,
|
|
||||||
/// to avoid infinite hanging due to blocked operation.
|
|
||||||
pub fn[X] TaskGroup::add_defer(
|
|
||||||
self : TaskGroup[X],
|
|
||||||
block : async () -> Unit nocancel,
|
|
||||||
) -> Unit {
|
|
||||||
guard self.state is Running else {
|
|
||||||
abort("trying to attach defer to a terminated task group")
|
|
||||||
}
|
|
||||||
self.group_defer.push(GroupDeferBlock(block))
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// `with_task_group(f)` creates a new task group and run `f` with the new group.
|
|
||||||
/// `f` itself will be run in a child task of the new group.
|
|
||||||
/// `with_task_group` exits after all the whole group terminates,
|
|
||||||
/// which means all child tasks in the group have terminated, including `f`.
|
|
||||||
///
|
|
||||||
/// If all children task terminate successfully,
|
|
||||||
/// `with_task_group` will return the result of `f`.
|
|
||||||
pub async fn[X] with_task_group(f : async (TaskGroup[X]) -> X) -> X {
|
|
||||||
let tg = {
|
|
||||||
children: Set([]),
|
|
||||||
parent: current_coroutine(),
|
|
||||||
waiting: 0,
|
|
||||||
state: Running,
|
|
||||||
result: None,
|
|
||||||
group_defer: [],
|
|
||||||
}
|
|
||||||
let curr = current_coroutine()
|
|
||||||
let prev = curr.spawner
|
|
||||||
curr.spawner = Some(fn(child) { tg.spawn_bg(child) })
|
|
||||||
defer {
|
|
||||||
curr.spawner = prev
|
|
||||||
}
|
|
||||||
tg.spawn_bg(() => {
|
|
||||||
let value = f(tg)
|
|
||||||
if tg.result is None {
|
|
||||||
tg.result = Some(value)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
match suspend_check_cancel() {
|
|
||||||
Continue => ()
|
|
||||||
Cancelled => {
|
|
||||||
if tg.state is Running {
|
|
||||||
tg.state = Cancelled
|
|
||||||
}
|
|
||||||
if !tg.children.is_empty() {
|
|
||||||
for child in tg.children {
|
|
||||||
child.cancel()
|
|
||||||
}
|
|
||||||
protect_from_cancel(() => suspend())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
guard! tg.children.is_empty()
|
|
||||||
while tg.group_defer.pop() is Some(defer_block) {
|
|
||||||
defer_block() catch {
|
|
||||||
err => if tg.state is Done { tg.state = Fail(err) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match tg.state {
|
|
||||||
Done => tg.result.unwrap()
|
|
||||||
Fail(err) => raise err
|
|
||||||
Cancelled => raise_cancellation_signal()
|
|
||||||
Running => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Force a task group to terminate immediately with the given result value.
|
|
||||||
/// All child tasks in the group, including potentially the current one,
|
|
||||||
/// will be cancelled.
|
|
||||||
pub async fn[X] TaskGroup::return_immediately(
|
|
||||||
self : TaskGroup[X],
|
|
||||||
value : X,
|
|
||||||
) -> Unit noraise {
|
|
||||||
if self.result is None {
|
|
||||||
self.result = Some(value)
|
|
||||||
}
|
|
||||||
if self.state is Running {
|
|
||||||
self.state = Done
|
|
||||||
for child in self.children {
|
|
||||||
child.cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
check_cancellation()
|
|
||||||
}
|
|
||||||
@@ -1,964 +0,0 @@
|
|||||||
///|
|
|
||||||
pub struct Sink[X] {
|
|
||||||
priv write : async (ArrayView[X]) -> Int
|
|
||||||
priv close : async () -> Unit
|
|
||||||
priv is_open : () -> Bool
|
|
||||||
priv has_cleanup : () -> Bool
|
|
||||||
priv cleanup : ((X) -> Unit)?
|
|
||||||
priv write_window_size : Int
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
let default_sink_write_window_size : Int = 64
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// The write callback must either raise before consuming any value or return
|
|
||||||
/// the number consumed. It must not raise after partial consumption because
|
|
||||||
/// `ArrayView` cannot carry an ownership offset through an exception.
|
|
||||||
/// The close callback must be idempotent.
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub fn[X] Sink::from_callbacks(
|
|
||||||
write : async (ArrayView[X]) -> Int,
|
|
||||||
close : async () -> Unit,
|
|
||||||
is_open : () -> Bool,
|
|
||||||
cleanup : ((X) -> Unit)?,
|
|
||||||
write_window_size : Int,
|
|
||||||
) -> Sink[X] {
|
|
||||||
guard! write_window_size > 0
|
|
||||||
{
|
|
||||||
write,
|
|
||||||
close,
|
|
||||||
is_open,
|
|
||||||
has_cleanup: () => cleanup is Some(_),
|
|
||||||
cleanup,
|
|
||||||
write_window_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Consumes up to one bounded window and returns the number of values consumed.
|
|
||||||
///
|
|
||||||
/// Consumption includes values accepted by the peer and values recursively
|
|
||||||
/// cleaned after the peer closes. Check `is_open()` to distinguish those cases.
|
|
||||||
pub async fn[X] Sink::write(self : Sink[X], data : ArrayView[X]) -> Int {
|
|
||||||
if data.length() == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
let length = if data.length() < self.write_window_size {
|
|
||||||
data.length()
|
|
||||||
} else {
|
|
||||||
self.write_window_size
|
|
||||||
}
|
|
||||||
// The callback may suspend, so it must not retain the caller's borrowed view.
|
|
||||||
let buffer = FixedArray::from_array(data[:length])
|
|
||||||
let written = (self.write)(buffer[:])
|
|
||||||
guard! written >= 0 && written <= length
|
|
||||||
if !(self.is_open)() && written < length && (self.has_cleanup)() {
|
|
||||||
self.cleanup_unaccepted(buffer[:], written)
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
written
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Writes one bounded window from an immutable byte view.
|
|
||||||
///
|
|
||||||
/// This is the byte-stream counterpart of `write(ArrayView[T])`. MoonBit keeps
|
|
||||||
/// `BytesView` and `ArrayView[Byte]` as distinct types, so byte strings need a
|
|
||||||
/// separate entry point until the language provides a shared read-only view.
|
|
||||||
pub async fn Sink::write_bytes(self : Sink[Byte], data : BytesView) -> Int {
|
|
||||||
if data.length() == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
let length = if data.length() < self.write_window_size {
|
|
||||||
data.length()
|
|
||||||
} else {
|
|
||||||
self.write_window_size
|
|
||||||
}
|
|
||||||
let buffer = data[:length].to_fixedarray()
|
|
||||||
let written = (self.write)(buffer[:])
|
|
||||||
guard! written >= 0 && written <= length
|
|
||||||
if !(self.is_open)() && written < length && (self.has_cleanup)() {
|
|
||||||
self.cleanup_unaccepted(buffer[:], written)
|
|
||||||
return length
|
|
||||||
}
|
|
||||||
written
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Returns whether the sink can still accept values from its peer.
|
|
||||||
pub fn[X] Sink::is_open(self : Sink[X]) -> Bool {
|
|
||||||
(self.is_open)()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] Sink::cleanup_unaccepted(
|
|
||||||
self : Sink[X],
|
|
||||||
data : ArrayView[X],
|
|
||||||
offset : Int,
|
|
||||||
) -> Unit {
|
|
||||||
match self.cleanup {
|
|
||||||
Some(cleanup) =>
|
|
||||||
for i in offset..<data.length() {
|
|
||||||
cleanup(data[i])
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn Sink::cleanup_unaccepted_bytes(
|
|
||||||
self : Sink[Byte],
|
|
||||||
data : BytesView,
|
|
||||||
offset : Int,
|
|
||||||
) -> Unit {
|
|
||||||
match self.cleanup {
|
|
||||||
Some(cleanup) =>
|
|
||||||
for i in offset..<data.length() {
|
|
||||||
cleanup(data[i])
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Writes the entire slice to the sink unless the sink closes first.
|
|
||||||
///
|
|
||||||
/// Returns `true` if all items were written while the sink remained open and
|
|
||||||
/// `false` if the endpoint closed during the operation. When the sink has a
|
|
||||||
/// cleanup callback, every item was either accepted or cleaned and must not be
|
|
||||||
/// retried. Producers of explicitly managed resources must use such a sink.
|
|
||||||
pub async fn[X] Sink::write_all(self : Sink[X], data : ArrayView[X]) -> Bool {
|
|
||||||
let mut offset = 0
|
|
||||||
for ;; {
|
|
||||||
if offset >= data.length() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
let written = {
|
|
||||||
errdefer self.cleanup_unaccepted(data, offset)
|
|
||||||
self.write(data[offset:])
|
|
||||||
}
|
|
||||||
offset = offset + written
|
|
||||||
if !self.is_open() {
|
|
||||||
self.cleanup_unaccepted(data, offset)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
guard! written > 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Writes the entire byte view unless the sink closes first.
|
|
||||||
pub async fn Sink::write_all_bytes(self : Sink[Byte], data : BytesView) -> Bool {
|
|
||||||
let mut offset = 0
|
|
||||||
for ;; {
|
|
||||||
if offset >= data.length() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
let written = {
|
|
||||||
errdefer self.cleanup_unaccepted_bytes(data, offset)
|
|
||||||
self.write_bytes(data[offset:])
|
|
||||||
}
|
|
||||||
offset = offset + written
|
|
||||||
if !self.is_open() {
|
|
||||||
self.cleanup_unaccepted_bytes(data, offset)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
guard! written > 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Closes the writable end of the stream.
|
|
||||||
///
|
|
||||||
/// Closing is idempotent. A peer waiting for values observes the end of the
|
|
||||||
/// stream after any values already accepted by the sink have been read.
|
|
||||||
pub async fn[X] Sink::close(self : Sink[X]) -> Unit {
|
|
||||||
(self.close)()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct LocalFutureState[X] {
|
|
||||||
mut value : X?
|
|
||||||
mut closed : Bool
|
|
||||||
cleanup : ((X) -> Unit)?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] local_future_close(
|
|
||||||
state : Ref[LocalFutureState[X]],
|
|
||||||
fallback_cleanup : ((X) -> Unit)?,
|
|
||||||
) -> Unit {
|
|
||||||
if state.val.closed {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
match state.val.value {
|
|
||||||
Some(value) => {
|
|
||||||
state.val.value = None
|
|
||||||
match state.val.cleanup {
|
|
||||||
Some(cleanup) => cleanup(value)
|
|
||||||
None =>
|
|
||||||
match fallback_cleanup {
|
|
||||||
Some(cleanup) => cleanup(value)
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
state.val.closed = true
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] local_future_get(state : Ref[LocalFutureState[X]]) -> X raise {
|
|
||||||
match state.val.value {
|
|
||||||
Some(value) => {
|
|
||||||
state.val.value = None
|
|
||||||
state.val.closed = true
|
|
||||||
value
|
|
||||||
}
|
|
||||||
None => raise FutureReadError::Dropped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct StreamPipeReader[X] {
|
|
||||||
max : Int
|
|
||||||
mut value : FixedArray[X]?
|
|
||||||
mut ended : Bool
|
|
||||||
mut coro : Coroutine?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct StreamPipeWriter[X] {
|
|
||||||
mut value : FixedArray[X]?
|
|
||||||
mut accepted : Int
|
|
||||||
mut coro : Coroutine?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct StreamPipe[X] {
|
|
||||||
capacity : Int
|
|
||||||
chunks : @deque.Deque[FixedArray[X]]
|
|
||||||
mut buffered : Int
|
|
||||||
mut writer_closed : Bool
|
|
||||||
mut reader_dropped : Bool
|
|
||||||
mut cleanup : ((X) -> Unit)?
|
|
||||||
mut head : FixedArray[X]?
|
|
||||||
mut head_pos : Int
|
|
||||||
readers : @deque.Deque[StreamPipeReader[X]]
|
|
||||||
writers : @deque.Deque[StreamPipeWriter[X]]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] wake_stream_pipe_reader(
|
|
||||||
reader : StreamPipeReader[X],
|
|
||||||
value : FixedArray[X]?,
|
|
||||||
ended : Bool,
|
|
||||||
) -> Unit {
|
|
||||||
match reader.coro {
|
|
||||||
Some(coro) => {
|
|
||||||
reader.value = value
|
|
||||||
reader.ended = ended
|
|
||||||
reader.coro = None
|
|
||||||
coro.wake()
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] wake_stream_pipe_writer(
|
|
||||||
writer : StreamPipeWriter[X],
|
|
||||||
accepted : Int,
|
|
||||||
) -> Unit {
|
|
||||||
match writer.coro {
|
|
||||||
Some(coro) => {
|
|
||||||
writer.accepted = accepted
|
|
||||||
writer.value = None
|
|
||||||
writer.coro = None
|
|
||||||
coro.wake()
|
|
||||||
}
|
|
||||||
None => writer.value = None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] take_stream_pipe_reader(
|
|
||||||
pipe : Ref[StreamPipe[X]],
|
|
||||||
) -> StreamPipeReader[X]? {
|
|
||||||
while pipe.val.readers.pop_front() is Some(reader) {
|
|
||||||
if reader.coro is Some(_) {
|
|
||||||
return Some(reader)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] take_stream_pipe_writer(
|
|
||||||
pipe : Ref[StreamPipe[X]],
|
|
||||||
) -> StreamPipeWriter[X]? {
|
|
||||||
while pipe.val.writers.pop_front() is Some(writer) {
|
|
||||||
if writer.coro is Some(_) && writer.value is Some(_) {
|
|
||||||
return Some(writer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] fill_stream_pipe_from_writers(pipe : Ref[StreamPipe[X]]) -> Unit {
|
|
||||||
if pipe.val.capacity <= 0 || pipe.val.writer_closed || pipe.val.reader_dropped {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for ;; {
|
|
||||||
let available = pipe.val.capacity - pipe.val.buffered
|
|
||||||
if available <= 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard take_stream_pipe_writer(pipe) is Some(writer) else { return }
|
|
||||||
guard writer.value is Some(data) else { continue }
|
|
||||||
let take = if available < data.length() { available } else { data.length() }
|
|
||||||
let chunk = FixedArray::from_array(data[:take])
|
|
||||||
pipe.val.chunks.push_back(chunk)
|
|
||||||
pipe.val.buffered = pipe.val.buffered + take
|
|
||||||
wake_stream_pipe_writer(writer, take)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] stream_pipe_drop_reader(pipe : Ref[StreamPipe[X]]) -> Unit {
|
|
||||||
stream_pipe_drop_reader_with_cleanup(pipe, pipe.val.cleanup)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] stream_pipe_drop_reader_with_cleanup(
|
|
||||||
pipe : Ref[StreamPipe[X]],
|
|
||||||
cleanup : ((X) -> Unit)?,
|
|
||||||
) -> Unit {
|
|
||||||
if pipe.val.reader_dropped {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => {
|
|
||||||
match pipe.val.head {
|
|
||||||
Some(head) =>
|
|
||||||
for i in pipe.val.head_pos..<head.length() {
|
|
||||||
cleanup(head[i])
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
while pipe.val.chunks.pop_front() is Some(chunk) {
|
|
||||||
for value in chunk {
|
|
||||||
cleanup(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
pipe.val.head = None
|
|
||||||
pipe.val.head_pos = 0
|
|
||||||
pipe.val.chunks.clear()
|
|
||||||
pipe.val.buffered = 0
|
|
||||||
pipe.val.reader_dropped = true
|
|
||||||
while pipe.val.readers.pop_front() is Some(reader) {
|
|
||||||
wake_stream_pipe_reader(reader, None, true)
|
|
||||||
}
|
|
||||||
while pipe.val.writers.pop_front() is Some(writer) {
|
|
||||||
let consumed = match (cleanup, writer.value) {
|
|
||||||
(Some(cleanup), Some(data)) => {
|
|
||||||
for value in data {
|
|
||||||
cleanup(value)
|
|
||||||
}
|
|
||||||
data.length()
|
|
||||||
}
|
|
||||||
_ => 0
|
|
||||||
}
|
|
||||||
wake_stream_pipe_writer(writer, consumed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] stream_pipe_reject_reader(
|
|
||||||
pipe : Ref[StreamPipe[X]],
|
|
||||||
fallback : (X) -> Unit,
|
|
||||||
) -> Unit {
|
|
||||||
if pipe.val.cleanup is None {
|
|
||||||
pipe.val.cleanup = Some(fallback)
|
|
||||||
}
|
|
||||||
stream_pipe_drop_reader(pipe)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] stream_pipe_close_writer(pipe : Ref[StreamPipe[X]]) -> Unit {
|
|
||||||
if pipe.val.writer_closed {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pipe.val.writer_closed = true
|
|
||||||
while pipe.val.writers.pop_front() is Some(writer) {
|
|
||||||
wake_stream_pipe_writer(writer, 0)
|
|
||||||
}
|
|
||||||
if pipe.val.buffered == 0 {
|
|
||||||
while pipe.val.readers.pop_front() is Some(reader) {
|
|
||||||
wake_stream_pipe_reader(reader, None, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn[X] stream_pipe_write(
|
|
||||||
pipe : Ref[StreamPipe[X]],
|
|
||||||
data : ArrayView[X],
|
|
||||||
) -> Int {
|
|
||||||
if data.length() == 0 || pipe.val.writer_closed || pipe.val.reader_dropped {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
match take_stream_pipe_reader(pipe) {
|
|
||||||
Some(reader) => {
|
|
||||||
let take = if reader.max < data.length() {
|
|
||||||
reader.max
|
|
||||||
} else {
|
|
||||||
data.length()
|
|
||||||
}
|
|
||||||
let chunk = FixedArray::from_array(data[:take])
|
|
||||||
wake_stream_pipe_reader(reader, Some(chunk), false)
|
|
||||||
return take
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
if pipe.val.capacity > 0 && pipe.val.buffered < pipe.val.capacity {
|
|
||||||
let available = pipe.val.capacity - pipe.val.buffered
|
|
||||||
let take = if available < data.length() { available } else { data.length() }
|
|
||||||
let chunk = FixedArray::from_array(data[:take])
|
|
||||||
pipe.val.chunks.push_back(chunk)
|
|
||||||
pipe.val.buffered = pipe.val.buffered + take
|
|
||||||
return take
|
|
||||||
}
|
|
||||||
let writer = StreamPipeWriter::{
|
|
||||||
value: Some(FixedArray::from_array(data)),
|
|
||||||
accepted: 0,
|
|
||||||
coro: Some(current_coroutine()),
|
|
||||||
}
|
|
||||||
pipe.val.writers.push_back(writer)
|
|
||||||
match suspend_check_cancel() {
|
|
||||||
Continue => ()
|
|
||||||
Cancelled if writer.coro is None => return writer.accepted
|
|
||||||
Cancelled => {
|
|
||||||
writer.coro = None
|
|
||||||
writer.value = None
|
|
||||||
raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.value = None
|
|
||||||
writer.accepted
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
async fn[X] stream_pipe_read(
|
|
||||||
pipe : Ref[StreamPipe[X]],
|
|
||||||
count : Int,
|
|
||||||
) -> FixedArray[X]? {
|
|
||||||
if count <= 0 {
|
|
||||||
return Some([])
|
|
||||||
}
|
|
||||||
for ;; {
|
|
||||||
if pipe.val.reader_dropped {
|
|
||||||
return None
|
|
||||||
}
|
|
||||||
match pipe.val.head {
|
|
||||||
Some(head) => {
|
|
||||||
let available = head.length() - pipe.val.head_pos
|
|
||||||
let take = if count < available { count } else { available }
|
|
||||||
let result = FixedArray::from_array(
|
|
||||||
head[pipe.val.head_pos:pipe.val.head_pos + take],
|
|
||||||
)
|
|
||||||
pipe.val.head_pos = pipe.val.head_pos + take
|
|
||||||
pipe.val.buffered = pipe.val.buffered - take
|
|
||||||
if pipe.val.head_pos >= head.length() {
|
|
||||||
pipe.val.head = None
|
|
||||||
pipe.val.head_pos = 0
|
|
||||||
}
|
|
||||||
fill_stream_pipe_from_writers(pipe)
|
|
||||||
return Some(result)
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
if pipe.val.chunks.pop_front() is Some(chunk) {
|
|
||||||
pipe.val.head = Some(chunk)
|
|
||||||
pipe.val.head_pos = 0
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
match take_stream_pipe_writer(pipe) {
|
|
||||||
Some(writer) => {
|
|
||||||
guard writer.value is Some(data) else { continue }
|
|
||||||
let take = if count < data.length() { count } else { data.length() }
|
|
||||||
let result = FixedArray::from_array(data[:take])
|
|
||||||
wake_stream_pipe_writer(writer, take)
|
|
||||||
return Some(result)
|
|
||||||
}
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
if pipe.val.writer_closed {
|
|
||||||
return None
|
|
||||||
}
|
|
||||||
let reader = StreamPipeReader::{
|
|
||||||
max: count,
|
|
||||||
value: None,
|
|
||||||
ended: false,
|
|
||||||
coro: Some(current_coroutine()),
|
|
||||||
}
|
|
||||||
pipe.val.readers.push_back(reader)
|
|
||||||
if suspend_check_cancel() is Cancelled {
|
|
||||||
reader.coro = None
|
|
||||||
match reader.value {
|
|
||||||
Some(value) => return Some(value)
|
|
||||||
None if reader.ended => return None
|
|
||||||
None => raise_cancellation_signal()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match reader.value {
|
|
||||||
Some(value) => return Some(value)
|
|
||||||
None if reader.ended => return None
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct FutureSource[X] {
|
|
||||||
get : async () -> X
|
|
||||||
close : async () -> Unit
|
|
||||||
close_sync : () -> Unit
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct FutureProducerState[X] {
|
|
||||||
mut producer : (async () -> X)?
|
|
||||||
mut on_unstarted_drop : (() -> Unit)?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum FutureState[X] {
|
|
||||||
Source(FutureSource[X])
|
|
||||||
Local(Ref[LocalFutureState[X]])
|
|
||||||
Pending(Ref[FutureProducerState[X]])
|
|
||||||
Promised(Ref[PromiseCell[X]])
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub struct Future[X] {
|
|
||||||
priv inner : FutureState[X]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn[X] Future::ready_with_cleanup(
|
|
||||||
value : X,
|
|
||||||
cleanup : (X) -> Unit,
|
|
||||||
) -> Future[X] {
|
|
||||||
let state : Ref[LocalFutureState[X]] = {
|
|
||||||
val: { value: Some(value), closed: false, cleanup: Some(cleanup) },
|
|
||||||
}
|
|
||||||
{ inner: Local(state) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn[X] Future::ready(value : X) -> Future[X] {
|
|
||||||
let state : Ref[LocalFutureState[X]] = {
|
|
||||||
val: { value: Some(value), closed: false, cleanup: None },
|
|
||||||
}
|
|
||||||
{ inner: Local(state) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Creates a lazy future from an async producer.
|
|
||||||
///
|
|
||||||
/// `on_unstarted_drop`, when present, releases resources captured by a producer
|
|
||||||
/// that is discarded before its first `get`. Once the producer starts, it owns
|
|
||||||
/// its runtime cleanup and this callback is never invoked.
|
|
||||||
pub fn[X] Future::from(
|
|
||||||
producer : async () -> X,
|
|
||||||
on_unstarted_drop? : () -> Unit,
|
|
||||||
) -> Future[X] {
|
|
||||||
{ inner: Pending({ val: { producer: Some(producer), on_unstarted_drop } }) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub fn[X] Future::from_source(
|
|
||||||
get : async () -> X,
|
|
||||||
close : async () -> Unit,
|
|
||||||
close_sync : () -> Unit,
|
|
||||||
) -> Future[X] {
|
|
||||||
{ inner: Source({ get, close, close_sync }) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Waits for and consumes the future's single value.
|
|
||||||
///
|
|
||||||
/// A `Future` is a one-shot value. Calling `get` again, or calling it after the
|
|
||||||
/// readable end has been dropped, fails instead of replaying the value.
|
|
||||||
pub async fn[X] Future::get(self : Future[X]) -> X {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.get)()
|
|
||||||
Local(state) => local_future_get(state)
|
|
||||||
Pending(state) =>
|
|
||||||
match state.val.producer {
|
|
||||||
Some(producer) => {
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
producer()
|
|
||||||
}
|
|
||||||
None => raise FutureReadError::Dropped
|
|
||||||
}
|
|
||||||
Promised(cell) => promised_future_get(cell)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Releases the readable end without consuming its value.
|
|
||||||
///
|
|
||||||
/// If the value is already locally available, its registered cleanup callback
|
|
||||||
/// runs. An unstarted lazy producer is discarded and runs
|
|
||||||
/// `on_unstarted_drop`, when provided.
|
|
||||||
pub async fn[X] Future::drop(self : Future[X]) -> Unit noraise {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.close)() catch { _ => () }
|
|
||||||
Local(state) => local_future_close(state, None)
|
|
||||||
Pending(state) => {
|
|
||||||
let cleanup = state.val.on_unstarted_drop
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => cleanup()
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Promised(cell) => promised_future_drop(cell, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub fn[X] Future::drop_sync(self : Future[X]) -> Unit {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.close_sync)()
|
|
||||||
Local(state) => local_future_close(state, None)
|
|
||||||
Pending(state) => {
|
|
||||||
let cleanup = state.val.on_unstarted_drop
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => cleanup()
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Promised(cell) => promised_future_drop(cell, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Rejects a future that was prepared for a component-model transfer but was
|
|
||||||
/// never transferred. A locally available value uses its registered cleanup,
|
|
||||||
/// falling back to the generated canonical-ABI cleanup. An unstarted producer
|
|
||||||
/// is discarded without running it.
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub async fn[X] Future::reject(
|
|
||||||
self : Future[X],
|
|
||||||
cleanup : (X) -> Unit,
|
|
||||||
) -> Unit noraise {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.close)() catch { _ => () }
|
|
||||||
Local(state) => local_future_close(state, Some(cleanup))
|
|
||||||
Pending(state) => {
|
|
||||||
let cleanup = state.val.on_unstarted_drop
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => cleanup()
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Promised(cell) => promised_future_drop(cell, Some(cleanup))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct StreamProducerState[X] {
|
|
||||||
mut producer : (async (Sink[X]) -> Unit)?
|
|
||||||
mut cleanup : ((X) -> Unit)?
|
|
||||||
mut on_unstarted_drop : (() -> Unit)?
|
|
||||||
mut pipe : Ref[StreamPipe[X]]?
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv struct StreamSource[X] {
|
|
||||||
read : async (Int) -> FixedArray[X]?
|
|
||||||
close : async () -> Unit
|
|
||||||
close_sync : () -> Unit
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
priv enum StreamState[X] {
|
|
||||||
Source(StreamSource[X])
|
|
||||||
Local(Ref[StreamPipe[X]])
|
|
||||||
Outgoing(Ref[StreamProducerState[X]])
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub struct Stream[X] {
|
|
||||||
priv inner : StreamState[X]
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] new_stream_pipe(
|
|
||||||
capacity : Int,
|
|
||||||
cleanup : ((X) -> Unit)?,
|
|
||||||
) -> Ref[StreamPipe[X]] {
|
|
||||||
if capacity < 0 {
|
|
||||||
abort("stream capacity must not be negative")
|
|
||||||
}
|
|
||||||
{
|
|
||||||
val: {
|
|
||||||
capacity,
|
|
||||||
chunks: Deque([]),
|
|
||||||
buffered: 0,
|
|
||||||
writer_closed: false,
|
|
||||||
reader_dropped: false,
|
|
||||||
cleanup,
|
|
||||||
head: None,
|
|
||||||
head_pos: 0,
|
|
||||||
readers: Deque([]),
|
|
||||||
writers: Deque([]),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] stream_pipe_sink(pipe : Ref[StreamPipe[X]]) -> Sink[X] {
|
|
||||||
{
|
|
||||||
write: (data : ArrayView[X]) => stream_pipe_write(pipe, data),
|
|
||||||
close: () => stream_pipe_close_writer(pipe),
|
|
||||||
is_open: () => !pipe.val.writer_closed && !pipe.val.reader_dropped,
|
|
||||||
has_cleanup: () => pipe.val.cleanup is Some(_),
|
|
||||||
cleanup: Some(fn(value) {
|
|
||||||
match pipe.val.cleanup {
|
|
||||||
Some(cleanup) => cleanup(value)
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
write_window_size: default_sink_write_window_size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] new_local_stream(
|
|
||||||
capacity : Int,
|
|
||||||
cleanup : ((X) -> Unit)?,
|
|
||||||
) -> (Stream[X], Sink[X]) {
|
|
||||||
let pipe = new_stream_pipe(capacity, cleanup)
|
|
||||||
let stream = Stream::{ inner: Local(pipe) }
|
|
||||||
let sink = stream_pipe_sink(pipe)
|
|
||||||
(stream, sink)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn spawn_stream_producer(task : async () -> Unit) -> Unit {
|
|
||||||
let current = current_coroutine()
|
|
||||||
match current.spawner {
|
|
||||||
Some(spawner) => spawner(task)
|
|
||||||
None => ignore(spawn(task))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn[X] materialize_stream_producer(
|
|
||||||
state : Ref[StreamProducerState[X]],
|
|
||||||
) -> Ref[StreamPipe[X]] {
|
|
||||||
match state.val.pipe {
|
|
||||||
Some(pipe) => pipe
|
|
||||||
None => {
|
|
||||||
let pipe = new_stream_pipe(0, state.val.cleanup)
|
|
||||||
state.val.cleanup = None
|
|
||||||
state.val.pipe = Some(pipe)
|
|
||||||
match state.val.producer {
|
|
||||||
Some(producer) => {
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
let sink = stream_pipe_sink(pipe)
|
|
||||||
spawn_stream_producer(async fn() {
|
|
||||||
defer stream_pipe_close_writer(pipe)
|
|
||||||
producer(sink) catch {
|
|
||||||
_ => ()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
None => stream_pipe_close_writer(pipe)
|
|
||||||
}
|
|
||||||
pipe
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn[X] Stream::new(capacity? : Int = 0) -> (Stream[X], Sink[X]) {
|
|
||||||
new_local_stream(capacity, None)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Creates a local stream whose unread values are cleaned up when its readable
|
|
||||||
/// end is dropped.
|
|
||||||
pub fn[X] Stream::new_with_cleanup(
|
|
||||||
cleanup : (X) -> Unit,
|
|
||||||
capacity? : Int = 0,
|
|
||||||
) -> (Stream[X], Sink[X]) {
|
|
||||||
new_local_stream(capacity, Some(cleanup))
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Creates a lazy stream from an async producer.
|
|
||||||
///
|
|
||||||
/// `cleanup`, when present, releases buffered or pending values if the readable
|
|
||||||
/// end is dropped after the producer starts. Producers of explicitly managed
|
|
||||||
/// resources should provide it.
|
|
||||||
///
|
|
||||||
/// `on_unstarted_drop`, when present, releases resources captured by a producer
|
|
||||||
/// that is discarded before its first read or before a prepared component
|
|
||||||
/// transfer commits. Once the producer starts, it owns its runtime cleanup and
|
|
||||||
/// this callback is never invoked.
|
|
||||||
pub fn[X] Stream::produce(
|
|
||||||
producer : async (Sink[X]) -> Unit,
|
|
||||||
cleanup? : (X) -> Unit,
|
|
||||||
on_unstarted_drop? : () -> Unit,
|
|
||||||
) -> Stream[X] {
|
|
||||||
{
|
|
||||||
inner: Outgoing({
|
|
||||||
val: { producer: Some(producer), cleanup, on_unstarted_drop, pipe: None },
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub fn[X] Stream::from_source(
|
|
||||||
read : async (Int) -> FixedArray[X]?,
|
|
||||||
close : async () -> Unit,
|
|
||||||
close_sync : () -> Unit,
|
|
||||||
) -> Stream[X] {
|
|
||||||
{ inner: Source({ read, close, close_sync }) }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Reads at most `count` values from the stream.
|
|
||||||
///
|
|
||||||
/// `Some(values)` contains the next owned chunk and may be empty when the peer
|
|
||||||
/// reports no progress. `None` means the writable end has closed and no
|
|
||||||
/// buffered values remain. A non-positive count returns an empty chunk without
|
|
||||||
/// waiting.
|
|
||||||
pub async fn[X] Stream::read(self : Stream[X], count : Int) -> FixedArray[X]? {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.read)(count)
|
|
||||||
Local(pipe) => stream_pipe_read(pipe, count)
|
|
||||||
Outgoing(state) =>
|
|
||||||
stream_pipe_read(materialize_stream_producer(state), count)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Releases the readable end and discards unread values.
|
|
||||||
///
|
|
||||||
/// Registered cleanup callbacks run for locally buffered values. An unstarted
|
|
||||||
/// lazy producer is discarded and runs `on_unstarted_drop`, when provided.
|
|
||||||
pub async fn[X] Stream::drop(self : Stream[X]) -> Unit noraise {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.close)() catch { _ => () }
|
|
||||||
Local(pipe) => stream_pipe_drop_reader(pipe)
|
|
||||||
Outgoing(state) =>
|
|
||||||
match state.val.pipe {
|
|
||||||
Some(pipe) => stream_pipe_drop_reader(pipe)
|
|
||||||
None => {
|
|
||||||
let cleanup = state.val.on_unstarted_drop
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.cleanup = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => cleanup()
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub fn[X] Stream::drop_sync(self : Stream[X]) -> Unit {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.close_sync)()
|
|
||||||
Local(pipe) => stream_pipe_drop_reader(pipe)
|
|
||||||
Outgoing(state) =>
|
|
||||||
match state.val.pipe {
|
|
||||||
Some(pipe) => stream_pipe_drop_reader(pipe)
|
|
||||||
None => {
|
|
||||||
let cleanup = state.val.on_unstarted_drop
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.cleanup = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => cleanup()
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub fn[X] Stream::take_producer(self : Stream[X]) -> (async (Sink[X]) -> Unit)? {
|
|
||||||
match self.inner {
|
|
||||||
Outgoing(state) =>
|
|
||||||
match state.val.pipe {
|
|
||||||
Some(_) => None
|
|
||||||
None => {
|
|
||||||
let producer = state.val.producer
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.cleanup = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
producer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Source(_) | Local(_) => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
/// Rejects a stream that was prepared for a component-model transfer but was
|
|
||||||
/// never transferred. Component sources are closed immediately and buffered
|
|
||||||
/// local values are cleaned synchronously. An unstarted producer is discarded
|
|
||||||
/// without running it; its optional `on_unstarted_drop` callback owns cleanup of
|
|
||||||
/// captured resources.
|
|
||||||
#internal(wit_bindgen, "generated binding code only")
|
|
||||||
pub async fn[X] Stream::reject(
|
|
||||||
self : Stream[X],
|
|
||||||
cleanup : (X) -> Unit,
|
|
||||||
) -> Unit noraise {
|
|
||||||
match self.inner {
|
|
||||||
Source(source) => (source.close)() catch { _ => () }
|
|
||||||
Local(pipe) => stream_pipe_reject_reader(pipe, cleanup)
|
|
||||||
Outgoing(state) =>
|
|
||||||
match state.val.pipe {
|
|
||||||
Some(pipe) => stream_pipe_reject_reader(pipe, cleanup)
|
|
||||||
None => {
|
|
||||||
let cleanup = state.val.on_unstarted_drop
|
|
||||||
state.val.producer = None
|
|
||||||
state.val.cleanup = None
|
|
||||||
state.val.on_unstarted_drop = None
|
|
||||||
match cleanup {
|
|
||||||
Some(cleanup) => cleanup()
|
|
||||||
None => ()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import {
|
|
||||||
"moonbitlang/core/deque" @deque,
|
|
||||||
"moonbitlang/core/ref" @ref,
|
|
||||||
"moonbitlang/core/set" @set,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
supported_targets = "wasm"
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
|
||||||
package "chandra/desktop/async-core"
|
|
||||||
|
|
||||||
import {
|
|
||||||
"moonbitlang/core/debug",
|
|
||||||
}
|
|
||||||
|
|
||||||
// Values
|
|
||||||
pub async fn check_cancellation() -> Unit noraise
|
|
||||||
|
|
||||||
pub async fn[X] handle_cancellation(async () -> X) -> X?
|
|
||||||
|
|
||||||
pub fn is_being_cancelled() -> Bool
|
|
||||||
|
|
||||||
pub async fn[X] protect_from_cancel(async () -> X) -> X nocancel
|
|
||||||
|
|
||||||
pub async fn[X] with_task_group(async (TaskGroup[X]) -> X) -> X
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
pub(all) suberror EndpointBusy {
|
|
||||||
Read
|
|
||||||
Write
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(all) suberror FutureReadError {
|
|
||||||
Cancelled
|
|
||||||
Dropped
|
|
||||||
}
|
|
||||||
|
|
||||||
pub suberror OpCancelled {
|
|
||||||
SubTaskCancelled(before_started~ : Bool)
|
|
||||||
StreamReadCancelled
|
|
||||||
}
|
|
||||||
|
|
||||||
#derive(@debug.Debug)
|
|
||||||
pub(all) suberror PromiseClosed
|
|
||||||
#deprecated
|
|
||||||
pub fn PromiseClosed::to_repr(Self) -> @debug.Repr
|
|
||||||
|
|
||||||
#derive(@debug.Debug)
|
|
||||||
pub suberror TaskCancelled
|
|
||||||
#deprecated
|
|
||||||
pub fn TaskCancelled::to_repr(Self) -> @debug.Repr
|
|
||||||
|
|
||||||
// Types and methods
|
|
||||||
pub struct CondVar {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
#alias(new, deprecated)
|
|
||||||
pub fn CondVar::CondVar() -> Self
|
|
||||||
pub fn CondVar::broadcast(Self) -> Unit
|
|
||||||
pub fn CondVar::signal(Self) -> Unit
|
|
||||||
pub async fn CondVar::wait(Self) -> Unit noraise
|
|
||||||
|
|
||||||
type Coroutine
|
|
||||||
|
|
||||||
pub struct Future[X] {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub async fn[X] Future::drop(Self[X]) -> Unit noraise
|
|
||||||
pub fn[X] Future::drop_sync(Self[X]) -> Unit
|
|
||||||
pub fn[X] Future::from(async () -> X, on_unstarted_drop? : () -> Unit) -> Self[X]
|
|
||||||
pub fn[X] Future::from_source(async () -> X, async () -> Unit, () -> Unit) -> Self[X]
|
|
||||||
pub async fn[X] Future::get(Self[X]) -> X
|
|
||||||
pub fn[X] Future::new() -> (Self[X], Promise[X])
|
|
||||||
pub fn[X] Future::new_with_cleanup((X) -> Unit) -> (Self[X], Promise[X])
|
|
||||||
pub fn[X] Future::ready(X) -> Self[X]
|
|
||||||
pub fn[X] Future::ready_with_cleanup(X, (X) -> Unit) -> Self[X]
|
|
||||||
pub async fn[X] Future::reject(Self[X], (X) -> Unit) -> Unit noraise
|
|
||||||
|
|
||||||
pub struct Mutex {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub fn Mutex::Mutex() -> Self
|
|
||||||
pub async fn Mutex::acquire(Self) -> Unit noraise
|
|
||||||
pub fn Mutex::release(Self) -> Unit
|
|
||||||
pub fn Mutex::try_acquire(Self) -> Bool
|
|
||||||
|
|
||||||
pub struct Promise[X] {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub fn[X] Promise::close(Self[X]) -> Bool
|
|
||||||
pub fn[X] Promise::complete(Self[X], X) -> Bool
|
|
||||||
pub fn[X] Promise::fail(Self[X], Error) -> Bool
|
|
||||||
|
|
||||||
pub struct Semaphore {
|
|
||||||
size : Int32
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
#alias(new, deprecated)
|
|
||||||
pub fn Semaphore::Semaphore(Int32, initial_value? : Int32) -> Self
|
|
||||||
pub async fn Semaphore::acquire(Self) -> Unit noraise
|
|
||||||
pub fn Semaphore::release(Self) -> Unit
|
|
||||||
pub fn Semaphore::try_acquire(Self) -> Bool
|
|
||||||
|
|
||||||
pub struct Sink[X] {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub async fn[X] Sink::close(Self[X]) -> Unit
|
|
||||||
pub fn[X] Sink::from_callbacks(async (ArrayView[X]) -> Int32, async () -> Unit, () -> Bool, ((X) -> Unit)?, Int32) -> Self[X]
|
|
||||||
pub fn[X] Sink::is_open(Self[X]) -> Bool
|
|
||||||
pub async fn[X] Sink::write(Self[X], ArrayView[X]) -> Int32
|
|
||||||
pub async fn[X] Sink::write_all(Self[X], ArrayView[X]) -> Bool
|
|
||||||
pub async fn Sink::write_all_bytes(Self[Byte], BytesView) -> Bool
|
|
||||||
pub async fn Sink::write_bytes(Self[Byte], BytesView) -> Int32
|
|
||||||
|
|
||||||
pub struct Stream[X] {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub async fn[X] Stream::drop(Self[X]) -> Unit noraise
|
|
||||||
pub fn[X] Stream::drop_sync(Self[X]) -> Unit
|
|
||||||
pub fn[X] Stream::from_source(async (Int32) -> FixedArray[X]?, async () -> Unit, () -> Unit) -> Self[X]
|
|
||||||
pub fn[X] Stream::new(capacity? : Int32) -> (Self[X], Sink[X])
|
|
||||||
pub fn[X] Stream::new_with_cleanup((X) -> Unit, capacity? : Int32) -> (Self[X], Sink[X])
|
|
||||||
pub fn[X] Stream::produce(async (Sink[X]) -> Unit, cleanup? : (X) -> Unit, on_unstarted_drop? : () -> Unit) -> Self[X]
|
|
||||||
pub async fn[X] Stream::read(Self[X], Int32) -> FixedArray[X]?
|
|
||||||
pub async fn[X] Stream::reject(Self[X], (X) -> Unit) -> Unit noraise
|
|
||||||
pub fn[X] Stream::take_producer(Self[X]) -> (async (Sink[X]) -> Unit)?
|
|
||||||
|
|
||||||
pub struct Task[X] {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub fn[X] Task::cancel(Self[X]) -> Unit
|
|
||||||
pub fn[X] Task::try_wait(Self[X]) -> X? raise
|
|
||||||
pub async fn[X] Task::wait(Self[X]) -> X
|
|
||||||
|
|
||||||
pub struct TaskGroup[X] {
|
|
||||||
// private fields
|
|
||||||
}
|
|
||||||
pub fn[X] TaskGroup::add_defer(Self[X], async () -> Unit nocancel) -> Unit
|
|
||||||
pub async fn[X] TaskGroup::return_immediately(Self[X], X) -> Unit noraise
|
|
||||||
pub fn[G, X] TaskGroup::spawn(Self[G], async () -> X, no_wait? : Bool, allow_failure? : Bool) -> Task[X]
|
|
||||||
pub fn[X] TaskGroup::spawn_bg(Self[X], async () -> Unit, no_wait? : Bool, allow_failure? : Bool) -> Unit
|
|
||||||
|
|
||||||
#derive(Eq, Hash)
|
|
||||||
type WaitableSet
|
|
||||||
#deprecated
|
|
||||||
pub fn WaitableSet::equal(Self, Self) -> Bool
|
|
||||||
#deprecated
|
|
||||||
pub fn WaitableSet::hash(Self) -> Int32
|
|
||||||
#deprecated
|
|
||||||
pub fn WaitableSet::hash_combine(Self, Hasher) -> Unit
|
|
||||||
#deprecated
|
|
||||||
pub fn WaitableSet::not_equal(Self, Self) -> Bool
|
|
||||||
|
|
||||||
// Type aliases
|
|
||||||
|
|
||||||
// Traits
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
[Plymouth Theme]
|
|
||||||
Name=Chandra
|
|
||||||
Description=The Chandra moon over a boot progress bar
|
|
||||||
ModuleName=script
|
|
||||||
|
|
||||||
[script]
|
|
||||||
ImageDir=/usr/share/plymouth/themes/chandra
|
|
||||||
ScriptFile=/usr/share/plymouth/themes/chandra/chandra.script
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
# Chandra boot splash: the moon, and a progress bar under it while booting.
|
|
||||||
#
|
|
||||||
# Plymouth estimates the progress from how long the last boot took (/var/lib/plymouth/boot-duration;
|
|
||||||
# 60 s until it has seen one boot). The bar eases towards that estimate and never moves backwards.
|
|
||||||
|
|
||||||
Window.SetBackgroundTopColor(0.086, 0.129, 0.243); # #16213e
|
|
||||||
Window.SetBackgroundBottomColor(0.043, 0.063, 0.125); # #0b1020
|
|
||||||
|
|
||||||
center_x = Window.GetX() + Window.GetWidth() / 2;
|
|
||||||
center_y = Window.GetY() + Window.GetHeight() * 0.44;
|
|
||||||
|
|
||||||
# The art is sized for a 1080 px wide panel; scale it on anything else.
|
|
||||||
art_scale = Window.GetWidth() / 1080;
|
|
||||||
if (Window.GetHeight() < Window.GetWidth()) art_scale = Window.GetHeight() / 1080;
|
|
||||||
|
|
||||||
fun load(name) {
|
|
||||||
local.image = Image(name);
|
|
||||||
if (art_scale == 1) return image;
|
|
||||||
return image.Scale(Math.Int(image.GetWidth() * art_scale), Math.Int(image.GetHeight() * art_scale));
|
|
||||||
}
|
|
||||||
|
|
||||||
moon = load("moon.png");
|
|
||||||
moon_sprite = Sprite(moon);
|
|
||||||
moon_sprite.SetPosition(Math.Int(center_x - moon.GetWidth() / 2), Math.Int(center_y - moon.GetHeight() / 2), 1);
|
|
||||||
|
|
||||||
track = load("track.png");
|
|
||||||
fill = load("fill.png");
|
|
||||||
bar_x = Math.Int(center_x - track.GetWidth() / 2);
|
|
||||||
bar_y = Math.Int(center_y + moon.GetHeight() / 2 + 120 * art_scale);
|
|
||||||
|
|
||||||
mode = Plymouth.GetMode();
|
|
||||||
has_bar = mode == "boot";
|
|
||||||
|
|
||||||
if (has_bar) {
|
|
||||||
track_sprite = Sprite(track);
|
|
||||||
track_sprite.SetPosition(bar_x, bar_y, 1);
|
|
||||||
fill_sprite = Sprite();
|
|
||||||
fill_sprite.SetPosition(bar_x, bar_y, 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
target = 0;
|
|
||||||
shown = 0;
|
|
||||||
drawn = 0;
|
|
||||||
|
|
||||||
fun draw_bar() {
|
|
||||||
if (!has_bar) return;
|
|
||||||
local.width = Math.Int(fill.GetWidth() * shown);
|
|
||||||
# Redraw only when the bar grows by a pixel; narrower than its rounded cap it isn't drawn.
|
|
||||||
if (width <= drawn || width < fill.GetHeight()) return;
|
|
||||||
global.drawn = width;
|
|
||||||
fill_sprite.SetImage(fill.Crop(0, 0, width, fill.GetHeight()));
|
|
||||||
}
|
|
||||||
|
|
||||||
fun progress_callback(duration, progress) {
|
|
||||||
if (progress > 1) progress = 1;
|
|
||||||
if (progress > target) global.target = progress;
|
|
||||||
}
|
|
||||||
Plymouth.SetBootProgressFunction(progress_callback);
|
|
||||||
|
|
||||||
phase = 0;
|
|
||||||
ticks = 0;
|
|
||||||
fun refresh_callback() {
|
|
||||||
if (shown < target) {
|
|
||||||
global.shown = shown + (target - shown) * 0.08;
|
|
||||||
draw_bar();
|
|
||||||
}
|
|
||||||
# A slow breath, 12 times a second: the panel is drawn by the CPU.
|
|
||||||
global.ticks = ticks + 1;
|
|
||||||
if (ticks >= 4) {
|
|
||||||
global.ticks = 0;
|
|
||||||
global.phase = phase + 0.10;
|
|
||||||
moon_sprite.SetOpacity(0.86 + 0.14 * Math.Cos(phase));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Plymouth.SetRefreshFunction(refresh_callback);
|
|
||||||
|
|
||||||
# The session is about to take the screen: finish the bar.
|
|
||||||
fun quit_callback() {
|
|
||||||
global.target = 1;
|
|
||||||
global.shown = 1;
|
|
||||||
draw_bar();
|
|
||||||
moon_sprite.SetOpacity(1);
|
|
||||||
}
|
|
||||||
Plymouth.SetQuitFunction(quit_callback);
|
|
||||||
|
|
||||||
# fsck, emergency mode and the like.
|
|
||||||
message_sprite = Sprite();
|
|
||||||
fun message_callback(text) {
|
|
||||||
local.image = Image.Text(text, 0.78, 0.84, 1);
|
|
||||||
message_sprite.SetImage(image);
|
|
||||||
message_sprite.SetPosition(Math.Int(center_x - image.GetWidth() / 2), bar_y + Math.Int(60 * art_scale), 3);
|
|
||||||
}
|
|
||||||
Plymouth.SetMessageFunction(message_callback);
|
|
||||||
|
Before Width: | Height: | Size: 515 B |
|
Before Width: | Height: | Size: 47 KiB |
|
Before Width: | Height: | Size: 342 B |
@@ -1,4 +0,0 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" width="540" height="12">
|
|
||||||
<defs><linearGradient id="g"><stop offset="0" stop-color="#7c9cff"/><stop offset="1" stop-color="#eef4ff"/></linearGradient></defs>
|
|
||||||
<rect width="540" height="12" rx="6" fill="url(#g)"/>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 262 B |
@@ -1,17 +0,0 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="480" height="480">
|
|
||||||
<!-- The favicon's moon without its tile; boot/plymouth/render.sh turns it into chandra/moon.png. -->
|
|
||||||
<defs>
|
|
||||||
<linearGradient id="moon" x1="0.15" y1="0.1" x2="0.85" y2="0.9">
|
|
||||||
<stop offset="0%" stop-color="#eef4ff"/>
|
|
||||||
<stop offset="48%" stop-color="#a5bdff"/>
|
|
||||||
<stop offset="100%" stop-color="#7c9cff"/>
|
|
||||||
</linearGradient>
|
|
||||||
<radialGradient id="glow">
|
|
||||||
<stop offset="45%" stop-color="#7c9cff" stop-opacity="0.22"/>
|
|
||||||
<stop offset="100%" stop-color="#7c9cff" stop-opacity="0"/>
|
|
||||||
</radialGradient>
|
|
||||||
</defs>
|
|
||||||
<circle cx="32" cy="32" r="32" fill="url(#glow)"/>
|
|
||||||
<path transform="translate(-1 0)" fill="url(#moon)"
|
|
||||||
d="M 30.66 13.15 A 19 19 0 1 0 50.44 39.53 A 16.5 16.5 0 0 1 30.66 13.15 Z"/>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 827 B |
@@ -1,5 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# Render the theme's PNGs from the SVGs beside this script (needs rsvg-convert).
|
|
||||||
set -e
|
|
||||||
cd "$(dirname "$0")"
|
|
||||||
for f in moon track fill; do rsvg-convert "$f.svg" -o "chandra/$f.png"; done
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" width="540" height="12"><rect width="540" height="12" rx="6" fill="#7c9cff" fill-opacity="0.18"/></svg>
|
|
||||||
|
Before Width: | Height: | Size: 144 B |
@@ -1,219 +0,0 @@
|
|||||||
#include <math.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
#ifdef _OPENMP
|
|
||||||
#include <omp.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Unpack 10-bit CSI-2 packed 4x4 Quad-Bayer SRGGB into 4x4 supercell BGR16 (0..4092). */
|
|
||||||
void quad_csi2p_to_bgr16_4x4(
|
|
||||||
const uint8_t *raw,
|
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
int stride,
|
|
||||||
uint16_t *out_bgr16
|
|
||||||
) {
|
|
||||||
if (!raw || !out_bgr16 || width < 4 || height < 4 || stride <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const int out_w = width / 4;
|
|
||||||
const int out_h = height / 4;
|
|
||||||
|
|
||||||
#ifdef _OPENMP
|
|
||||||
#pragma omp parallel for schedule(static) num_threads(4) if (out_h >= 64)
|
|
||||||
#endif
|
|
||||||
for (int cy = 0; cy < out_h; cy++) {
|
|
||||||
const uint8_t *r0 = raw + (size_t)(cy * 4 + 0) * (size_t)stride;
|
|
||||||
const uint8_t *r1 = raw + (size_t)(cy * 4 + 1) * (size_t)stride;
|
|
||||||
const uint8_t *r2 = raw + (size_t)(cy * 4 + 2) * (size_t)stride;
|
|
||||||
const uint8_t *r3 = raw + (size_t)(cy * 4 + 3) * (size_t)stride;
|
|
||||||
uint16_t *dst = out_bgr16 + (size_t)cy * (size_t)out_w * 3;
|
|
||||||
|
|
||||||
for (int cx = 0; cx < out_w; cx++) {
|
|
||||||
const size_t off = (size_t)cx * 5;
|
|
||||||
const uint8_t lo0 = r0[off + 4];
|
|
||||||
const uint8_t lo1 = r1[off + 4];
|
|
||||||
const uint8_t lo2 = r2[off + 4];
|
|
||||||
const uint8_t lo3 = r3[off + 4];
|
|
||||||
|
|
||||||
/* Rows 4y, 4y+1: (p0, p1) = R, (p2, p3) = Gr */
|
|
||||||
uint32_t r_sum = ((uint32_t)r0[off + 0] + r0[off + 1] + r1[off + 0] + r1[off + 1]) << 2;
|
|
||||||
r_sum += (uint32_t)(lo0 & 3U) + ((lo0 >> 2) & 3U) + (lo1 & 3U) + ((lo1 >> 2) & 3U);
|
|
||||||
|
|
||||||
uint32_t gr_sum = ((uint32_t)r0[off + 2] + r0[off + 3] + r1[off + 2] + r1[off + 3]) << 2;
|
|
||||||
gr_sum += (uint32_t)((lo0 >> 4) & 3U) + ((lo0 >> 6) & 3U) + ((lo1 >> 4) & 3U) + ((lo1 >> 6) & 3U);
|
|
||||||
|
|
||||||
/* Rows 4y+2, 4y+3: (p0, p1) = Gb, (p2, p3) = B */
|
|
||||||
uint32_t gb_sum = ((uint32_t)r2[off + 0] + r2[off + 1] + r3[off + 0] + r3[off + 1]) << 2;
|
|
||||||
gb_sum += (uint32_t)(lo2 & 3U) + ((lo2 >> 2) & 3U) + (lo3 & 3U) + ((lo3 >> 2) & 3U);
|
|
||||||
|
|
||||||
uint32_t b_sum = ((uint32_t)r2[off + 2] + r2[off + 3] + r3[off + 2] + r3[off + 3]) << 2;
|
|
||||||
b_sum += (uint32_t)((lo2 >> 4) & 3U) + ((lo2 >> 6) & 3U) + ((lo3 >> 4) & 3U) + ((lo3 >> 6) & 3U);
|
|
||||||
|
|
||||||
uint32_t g_avg = (gr_sum + gb_sum + 1U) >> 1;
|
|
||||||
|
|
||||||
dst[0] = (uint16_t)b_sum;
|
|
||||||
dst[1] = (uint16_t)g_avg;
|
|
||||||
dst[2] = (uint16_t)r_sum;
|
|
||||||
dst += 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Unpack 10-bit CSI-2 packed 4x4 Quad-Bayer SRGGB via 2x2 binning into 16-bit Bayer (0..4092). */
|
|
||||||
void quad_csi2p_to_bayer16_2x2(
|
|
||||||
const uint8_t *raw,
|
|
||||||
int width,
|
|
||||||
int height,
|
|
||||||
int stride,
|
|
||||||
uint16_t *out_bayer16
|
|
||||||
) {
|
|
||||||
if (!raw || !out_bayer16 || width < 4 || height < 2 || stride <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const int nblk = width / 4;
|
|
||||||
const int out_w = nblk * 2;
|
|
||||||
const int out_h = height / 2;
|
|
||||||
|
|
||||||
#ifdef _OPENMP
|
|
||||||
#pragma omp parallel for schedule(static) num_threads(4) if (out_h >= 64)
|
|
||||||
#endif
|
|
||||||
for (int by = 0; by < out_h; by++) {
|
|
||||||
const uint8_t *r0 = raw + (size_t)(by * 2 + 0) * (size_t)stride;
|
|
||||||
const uint8_t *r1 = raw + (size_t)(by * 2 + 1) * (size_t)stride;
|
|
||||||
uint16_t *dst = out_bayer16 + (size_t)by * (size_t)out_w;
|
|
||||||
|
|
||||||
for (int bx = 0; bx < nblk; bx++) {
|
|
||||||
const size_t off = (size_t)bx * 5;
|
|
||||||
const uint8_t lo0 = r0[off + 4];
|
|
||||||
const uint8_t lo1 = r1[off + 4];
|
|
||||||
|
|
||||||
uint32_t c0 = ((uint32_t)r0[off + 0] + r0[off + 1] + r1[off + 0] + r1[off + 1]) << 2;
|
|
||||||
c0 += (uint32_t)(lo0 & 3U) + ((lo0 >> 2) & 3U) + (lo1 & 3U) + ((lo1 >> 2) & 3U);
|
|
||||||
|
|
||||||
uint32_t c1 = ((uint32_t)r0[off + 2] + r0[off + 3] + r1[off + 2] + r1[off + 3]) << 2;
|
|
||||||
c1 += (uint32_t)((lo0 >> 4) & 3U) + ((lo0 >> 6) & 3U) + ((lo1 >> 4) & 3U) + ((lo1 >> 6) & 3U);
|
|
||||||
|
|
||||||
dst[0] = (uint16_t)c0;
|
|
||||||
dst[1] = (uint16_t)c1;
|
|
||||||
dst += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fused black-level subtraction, AWB + exposure scaling, 3x3 linear RGB CCM, and LUT tone-map. */
|
|
||||||
void isp_ccm_tone_bgr16_to_bgr8(
|
|
||||||
const uint16_t *in_bgr16,
|
|
||||||
uint8_t *out_bgr8,
|
|
||||||
int pixels,
|
|
||||||
float black,
|
|
||||||
float scale_norm,
|
|
||||||
float gain_b,
|
|
||||||
float gain_g,
|
|
||||||
float gain_r,
|
|
||||||
const float ccm_rgb[9],
|
|
||||||
const uint8_t lut[4096],
|
|
||||||
float lut_max
|
|
||||||
) {
|
|
||||||
if (!in_bgr16 || !out_bgr8 || !ccm_rgb || !lut || pixels <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const float m00 = ccm_rgb[0], m01 = ccm_rgb[1], m02 = ccm_rgb[2];
|
|
||||||
const float m10 = ccm_rgb[3], m11 = ccm_rgb[4], m12 = ccm_rgb[5];
|
|
||||||
const float m20 = ccm_rgb[6], m21 = ccm_rgb[7], m22 = ccm_rgb[8];
|
|
||||||
const float sb = scale_norm * gain_b;
|
|
||||||
const float sg = scale_norm * gain_g;
|
|
||||||
const float sr = scale_norm * gain_r;
|
|
||||||
const float inv_lut = 4095.0f / (lut_max > 1e-6f ? lut_max : 1.0f);
|
|
||||||
|
|
||||||
#ifdef _OPENMP
|
|
||||||
#pragma omp parallel for schedule(static) num_threads(4) if (pixels >= 16384)
|
|
||||||
#endif
|
|
||||||
for (int i = 0; i < pixels; i++) {
|
|
||||||
const size_t idx = (size_t)i * 3;
|
|
||||||
float b = (float)in_bgr16[idx + 0] - black;
|
|
||||||
float g = (float)in_bgr16[idx + 1] - black;
|
|
||||||
float r = (float)in_bgr16[idx + 2] - black;
|
|
||||||
if (b < 0.0f) b = 0.0f;
|
|
||||||
if (g < 0.0f) g = 0.0f;
|
|
||||||
if (r < 0.0f) r = 0.0f;
|
|
||||||
|
|
||||||
b *= sb;
|
|
||||||
g *= sg;
|
|
||||||
r *= sr;
|
|
||||||
|
|
||||||
float rc = m00 * r + m01 * g + m02 * b;
|
|
||||||
float gc = m10 * r + m11 * g + m12 * b;
|
|
||||||
float bc = m20 * r + m21 * g + m22 * b;
|
|
||||||
|
|
||||||
int ir = (int)(rc * inv_lut + 0.5f);
|
|
||||||
int ig = (int)(gc * inv_lut + 0.5f);
|
|
||||||
int ib = (int)(bc * inv_lut + 0.5f);
|
|
||||||
if (ir < 0) ir = 0; else if (ir > 4095) ir = 4095;
|
|
||||||
if (ig < 0) ig = 0; else if (ig > 4095) ig = 4095;
|
|
||||||
if (ib < 0) ib = 0; else if (ib > 4095) ib = 4095;
|
|
||||||
|
|
||||||
out_bgr8[idx + 0] = lut[ib];
|
|
||||||
out_bgr8[idx + 1] = lut[ig];
|
|
||||||
out_bgr8[idx + 2] = lut[ir];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Motion-guarded multi-frame temporal merge in linear 16-bit BGR space. */
|
|
||||||
void burst_merge_bgr16(
|
|
||||||
const uint16_t *base,
|
|
||||||
const uint16_t *const *others,
|
|
||||||
int num_others,
|
|
||||||
uint16_t *out,
|
|
||||||
int pixels,
|
|
||||||
float noise_thresh
|
|
||||||
) {
|
|
||||||
if (!base || !out || pixels <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const float inv_nt = 1.0f / (noise_thresh > 1.0f ? noise_thresh : 1.0f);
|
|
||||||
|
|
||||||
#ifdef _OPENMP
|
|
||||||
#pragma omp parallel for schedule(static) num_threads(4) if (pixels >= 16384)
|
|
||||||
#endif
|
|
||||||
for (int i = 0; i < pixels; i++) {
|
|
||||||
const size_t idx = (size_t)i * 3;
|
|
||||||
const float b0 = (float)base[idx + 0];
|
|
||||||
const float g0 = (float)base[idx + 1];
|
|
||||||
const float r0 = (float)base[idx + 2];
|
|
||||||
const float l0 = 0.114f * b0 + 0.587f * g0 + 0.299f * r0;
|
|
||||||
|
|
||||||
float acc_b = b0;
|
|
||||||
float acc_g = g0;
|
|
||||||
float acc_r = r0;
|
|
||||||
float w_sum = 1.0f;
|
|
||||||
|
|
||||||
for (int k = 0; k < num_others; k++) {
|
|
||||||
const uint16_t *oth = others[k];
|
|
||||||
if (!oth) continue;
|
|
||||||
const float bk = (float)oth[idx + 0];
|
|
||||||
const float gk = (float)oth[idx + 1];
|
|
||||||
const float rk = (float)oth[idx + 2];
|
|
||||||
const float lk = 0.114f * bk + 0.587f * gk + 0.299f * rk;
|
|
||||||
const float d = fabsf(lk - l0) * inv_nt;
|
|
||||||
const float d2 = d * d;
|
|
||||||
const float w = 1.0f / (1.0f + d2 * d2);
|
|
||||||
acc_b += w * bk;
|
|
||||||
acc_g += w * gk;
|
|
||||||
acc_r += w * rk;
|
|
||||||
w_sum += w;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float inv_w = 1.0f / w_sum;
|
|
||||||
int ob = (int)(acc_b * inv_w + 0.5f);
|
|
||||||
int og = (int)(acc_g * inv_w + 0.5f);
|
|
||||||
int or_v = (int)(acc_r * inv_w + 0.5f);
|
|
||||||
if (ob < 0) ob = 0; else if (ob > 65535) ob = 65535;
|
|
||||||
if (og < 0) og = 0; else if (og > 65535) og = 65535;
|
|
||||||
if (or_v < 0) or_v = 0; else if (or_v > 65535) or_v = 65535;
|
|
||||||
out[idx + 0] = (uint16_t)ob;
|
|
||||||
out[idx + 1] = (uint16_t)og;
|
|
||||||
out[idx + 2] = (uint16_t)or_v;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
[Desktop Entry]
|
|
||||||
Type=Application
|
|
||||||
Name=Camera
|
|
||||||
GenericName=Camera
|
|
||||||
Comment=Take photos and record videos
|
|
||||||
Exec=/usr/local/bin/chandra-camera
|
|
||||||
Icon=chandra-camera
|
|
||||||
Terminal=false
|
|
||||||
Categories=AudioVideo;Video;Photography;Utility;
|
|
||||||
StartupNotify=true
|
|
||||||
StartupWMClass=chandra-camera
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128" width="128" height="128">
|
|
||||||
<defs>
|
|
||||||
<linearGradient id="bg" x1="0" y1="0" x2="1" y2="1">
|
|
||||||
<stop offset="0%" stop-color="#1b223a"/>
|
|
||||||
<stop offset="100%" stop-color="#0f1322"/>
|
|
||||||
</linearGradient>
|
|
||||||
<linearGradient id="lensRing" x1="0" y1="0" x2="1" y2="1">
|
|
||||||
<stop offset="0%" stop-color="#8ba6ff"/>
|
|
||||||
<stop offset="100%" stop-color="#4e6ee8"/>
|
|
||||||
</linearGradient>
|
|
||||||
<radialGradient id="glass" cx="40%" cy="35%" r="60%">
|
|
||||||
<stop offset="0%" stop-color="#2c3e75"/>
|
|
||||||
<stop offset="55%" stop-color="#11172b"/>
|
|
||||||
<stop offset="100%" stop-color="#080b14"/>
|
|
||||||
</radialGradient>
|
|
||||||
</defs>
|
|
||||||
<rect x="8" y="8" width="112" height="112" rx="28" fill="url(#bg)" stroke="#3b4973" stroke-width="2"/>
|
|
||||||
<path d="M36 42 L44 33 C46 31 48 30 51 30 L77 30 C80 30 82 31 84 33 L92 42 L98 42 C104 42 108 46 108 52 L108 86 C108 92 104 96 98 96 L30 96 C24 96 20 92 20 86 L20 52 C20 46 24 42 30 42 Z"
|
|
||||||
fill="#242c47" stroke="#53669c" stroke-width="2"/>
|
|
||||||
<circle cx="64" cy="68" r="22" fill="#0d111d" stroke="url(#lensRing)" stroke-width="4"/>
|
|
||||||
<circle cx="64" cy="68" r="15" fill="url(#glass)"/>
|
|
||||||
<circle cx="58" cy="61" r="4.5" fill="#9fb7ff" fill-opacity="0.72"/>
|
|
||||||
<circle cx="71" cy="74" r="2.2" fill="#6e8cff" fill-opacity="0.55"/>
|
|
||||||
<circle cx="94" cy="52" r="3.5" fill="#6e8cff"/>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 1.4 KiB |
@@ -1,24 +0,0 @@
|
|||||||
# SPDX-License-Identifier: CC0-1.0
|
|
||||||
%YAML 1.1
|
|
||||||
---
|
|
||||||
version: 1
|
|
||||||
algorithms:
|
|
||||||
- BlackLevel:
|
|
||||||
- Awb:
|
|
||||||
- Ccm:
|
|
||||||
ccms:
|
|
||||||
- ct: 3000
|
|
||||||
ccm: [ 1.38, -0.22, -0.16,
|
|
||||||
-0.28, 1.42, -0.14,
|
|
||||||
-0.03, -0.56, 1.59 ]
|
|
||||||
- ct: 4500
|
|
||||||
ccm: [ 1.40, -0.24, -0.16,
|
|
||||||
-0.24, 1.44, -0.20,
|
|
||||||
-0.03, -0.46, 1.49 ]
|
|
||||||
- ct: 6500
|
|
||||||
ccm: [ 1.44, -0.28, -0.16,
|
|
||||||
-0.21, 1.43, -0.22,
|
|
||||||
-0.05, -0.42, 1.47 ]
|
|
||||||
- Adjust:
|
|
||||||
- Agc:
|
|
||||||
...
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
# SPDX-License-Identifier: CC0-1.0
|
|
||||||
%YAML 1.1
|
|
||||||
---
|
|
||||||
version: 1
|
|
||||||
algorithms:
|
|
||||||
- BlackLevel:
|
|
||||||
- Awb:
|
|
||||||
- Ccm:
|
|
||||||
ccms:
|
|
||||||
- ct: 2890
|
|
||||||
ccm: [ 1.36754, -0.18448, -0.18306,
|
|
||||||
-0.32356, 1.44826, -0.12471,
|
|
||||||
-0.00412, -0.69936, 1.70348 ]
|
|
||||||
- ct: 3550
|
|
||||||
ccm: [ 1.42836, -0.27235, -0.15601,
|
|
||||||
-0.28751, 1.41075, -0.12325,
|
|
||||||
-0.01812, -0.54849, 1.56661 ]
|
|
||||||
- ct: 4500
|
|
||||||
ccm: [ 1.36328, -0.19569, -0.16759,
|
|
||||||
-0.25254, 1.52248, -0.26994,
|
|
||||||
-0.01575, -0.53155, 1.54729 ]
|
|
||||||
- ct: 5700
|
|
||||||
ccm: [ 1.49207, -0.37245, -0.11963,
|
|
||||||
-0.21493, 1.40005, -0.18512,
|
|
||||||
-0.03781, -0.38779, 1.42561 ]
|
|
||||||
- ct: 7900
|
|
||||||
ccm: [ 1.34849, -0.05425, -0.29424,
|
|
||||||
-0.22182, 1.77684, -0.55502,
|
|
||||||
-0.07403, -0.55336, 1.62739 ]
|
|
||||||
- Adjust:
|
|
||||||
- Agc:
|
|
||||||
...
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
# SPDX-License-Identifier: CC0-1.0
|
|
||||||
%YAML 1.1
|
|
||||||
---
|
|
||||||
version: 1
|
|
||||||
algorithms:
|
|
||||||
- BlackLevel:
|
|
||||||
- Awb:
|
|
||||||
- Adjust:
|
|
||||||
- Agc:
|
|
||||||
...
|
|
||||||
@@ -1,415 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""Host-side unit tests for chandra-camera frame/camera/AE/AF/VFR-mux helpers (no hardware required)."""
|
|
||||||
|
|
||||||
import ctypes
|
|
||||||
import importlib.machinery
|
|
||||||
import importlib.util
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import shutil
|
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
try:
|
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
HAS_CV2_NUMPY = True
|
|
||||||
except ImportError:
|
|
||||||
cv2 = None
|
|
||||||
np = None
|
|
||||||
HAS_CV2_NUMPY = False
|
|
||||||
|
|
||||||
CAM_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "chandra-camera")
|
|
||||||
_loader = importlib.machinery.SourceFileLoader("chandra_camera_mod", CAM_PATH)
|
|
||||||
_spec = importlib.util.spec_from_loader("chandra_camera_mod", _loader)
|
|
||||||
cam_mod = importlib.util.module_from_spec(_spec)
|
|
||||||
sys.modules["chandra_camera_mod"] = cam_mod
|
|
||||||
_loader.exec_module(cam_mod)
|
|
||||||
|
|
||||||
|
|
||||||
class _FakeSubdevIO:
|
|
||||||
"""In-memory mock for _V4L2SubdevIO supporting query/get/set/close."""
|
|
||||||
|
|
||||||
def __init__(self, ctrls):
|
|
||||||
# ctrls: cid -> (mn, mx, step, df, val)
|
|
||||||
self.ctrls = {k: list(v) for k, v in ctrls.items()}
|
|
||||||
self.history = []
|
|
||||||
self.closed = False
|
|
||||||
|
|
||||||
def query(self, cid):
|
|
||||||
if cid not in self.ctrls:
|
|
||||||
raise OSError("unsupported ctrl 0x%x" % cid)
|
|
||||||
mn, mx, step, df, _ = self.ctrls[cid]
|
|
||||||
return mn, mx, step, df
|
|
||||||
|
|
||||||
def get(self, cid):
|
|
||||||
if cid not in self.ctrls:
|
|
||||||
raise OSError("unsupported ctrl 0x%x" % cid)
|
|
||||||
return self.ctrls[cid][4]
|
|
||||||
|
|
||||||
def set(self, cid, val):
|
|
||||||
if cid not in self.ctrls:
|
|
||||||
raise OSError("unsupported ctrl 0x%x" % cid)
|
|
||||||
mn, mx, _step, _df, _ = self.ctrls[cid]
|
|
||||||
v = max(mn, min(int(val), mx))
|
|
||||||
self.ctrls[cid][4] = v
|
|
||||||
self.history.append((cid, v))
|
|
||||||
return v
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.closed = True
|
|
||||||
|
|
||||||
|
|
||||||
def _make_quad_csi2p_image(w=16, h=16, top_val=800, bot_val=800, bg_val=96):
|
|
||||||
"""Build a 10-bit CSI-2 packed 4x4 Quad-Bayer SRGGB frame preserving all 10 bits."""
|
|
||||||
stride = (w // 4) * 5
|
|
||||||
arr = np.zeros((h, stride), dtype=np.uint8)
|
|
||||||
|
|
||||||
def pack4(p0, p1, p2, p3):
|
|
||||||
b0 = (p0 >> 2) & 0xFF
|
|
||||||
b1 = (p1 >> 2) & 0xFF
|
|
||||||
b2 = (p2 >> 2) & 0xFF
|
|
||||||
b3 = (p3 >> 2) & 0xFF
|
|
||||||
b4 = (p0 & 3) | ((p1 & 3) << 2) | ((p2 & 3) << 4) | ((p3 & 3) << 6)
|
|
||||||
return b0, b1, b2, b3, b4
|
|
||||||
|
|
||||||
for y in range(h):
|
|
||||||
top = y < (h // 2)
|
|
||||||
for x_blk in range(w // 4):
|
|
||||||
if (y % 4) < 2:
|
|
||||||
c0 = top_val if top else bg_val # R
|
|
||||||
c1 = bg_val # Gr
|
|
||||||
else:
|
|
||||||
c0 = bg_val # Gb
|
|
||||||
c1 = bg_val if top else bot_val # B
|
|
||||||
arr[y, x_blk * 5:x_blk * 5 + 5] = pack4(c0, c0, c1, c1)
|
|
||||||
return arr.tobytes(), stride
|
|
||||||
|
|
||||||
|
|
||||||
class TestCameraHelpers(unittest.TestCase):
|
|
||||||
def test_sort_and_label_cameras(self):
|
|
||||||
raw = [
|
|
||||||
("/base/front", "libcamera", "imx371", 0, 90, 4656 * 3496),
|
|
||||||
("/base/back2", "libcamera", "imx376", 1, 270, 2592 * 1944),
|
|
||||||
("/base/back1", "libcamera", "imx519", 1, 270, 4656 * 3496),
|
|
||||||
]
|
|
||||||
cams = cam_mod.sort_and_label_cameras(raw)
|
|
||||||
self.assertEqual([c.model for c in cams], ["imx519", "imx371", "imx376"])
|
|
||||||
self.assertEqual([c.label for c in cams], ["Back · imx519", "Front · imx371", "Back 2 · imx376"])
|
|
||||||
self.assertEqual(cam_mod.select_initial_camera(cams, ""), 0)
|
|
||||||
self.assertEqual(cam_mod.select_initial_camera(cams, "front"), 1)
|
|
||||||
self.assertEqual(cam_mod.select_initial_camera(cams, "back"), 0)
|
|
||||||
self.assertEqual(cam_mod.select_initial_camera(cams, "imx376"), 2)
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_convert_abgr8888_with_stride_and_rotations(self):
|
|
||||||
h, w, stride = 3, 2, 12
|
|
||||||
buf = bytearray(h * stride)
|
|
||||||
buf[0:4] = bytes([10, 20, 200, 255])
|
|
||||||
buf[4:8] = bytes([40, 50, 60, 255])
|
|
||||||
|
|
||||||
bgr0 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=0, cv2_mod=cv2, np_mod=np)
|
|
||||||
self.assertEqual(bgr0.shape, (3, 2, 3))
|
|
||||||
self.assertEqual(tuple(bgr0[0, 0]), (200, 20, 10))
|
|
||||||
self.assertEqual(tuple(bgr0[0, 1]), (60, 50, 40))
|
|
||||||
|
|
||||||
bgr90 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=90, cv2_mod=cv2, np_mod=np)
|
|
||||||
self.assertEqual(bgr90.shape, (2, 3, 3))
|
|
||||||
self.assertEqual(tuple(bgr90[1, 0]), (200, 20, 10))
|
|
||||||
|
|
||||||
bgr270 = cam_mod.convert_frame(buf, w, h, stride, "ABGR8888", rot=270, cv2_mod=cv2, np_mod=np)
|
|
||||||
self.assertEqual(bgr270.shape, (2, 3, 3))
|
|
||||||
self.assertEqual(tuple(bgr270[0, 2]), (200, 20, 10))
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_convert_argb_rgb_and_cairo_bgra(self):
|
|
||||||
buf = bytes([210, 110, 30, 255, 10, 20, 30, 255])
|
|
||||||
bgr = cam_mod.convert_frame(buf, 2, 1, 8, "ARGB8888", rot=0, cv2_mod=cv2, np_mod=np)
|
|
||||||
self.assertEqual(tuple(bgr[0, 0]), (210, 110, 30))
|
|
||||||
|
|
||||||
rgb_buf = bytes([11, 22, 33, 44, 55, 66])
|
|
||||||
bgr2 = cam_mod.convert_frame(rgb_buf, 2, 1, 6, "RGB888", rot=180, cv2_mod=cv2, np_mod=np)
|
|
||||||
self.assertEqual(tuple(bgr2[0, 0]), (66, 55, 44))
|
|
||||||
self.assertEqual(tuple(bgr2[0, 1]), (33, 22, 11))
|
|
||||||
|
|
||||||
argb32 = cam_mod.bgr_to_cairo_argb32(bgr, np_mod=np)
|
|
||||||
self.assertEqual(argb32.shape, (1, 2, 4))
|
|
||||||
self.assertEqual(tuple(argb32[0, 0]), (210, 110, 30, 255))
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_ensure_even_frame(self):
|
|
||||||
odd = np.zeros((101, 203, 3), dtype=np.uint8)
|
|
||||||
even = cam_mod.ensure_even_frame(odd)
|
|
||||||
self.assertEqual(even.shape, (100, 202, 3))
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_debayer_quad_csi2p(self):
|
|
||||||
raw, stride = _make_quad_csi2p_image(w=8, h=8, top_val=800, bot_val=800, bg_val=96)
|
|
||||||
out_live = cam_mod.debayer_quad_csi2p(
|
|
||||||
raw, 8, 8, stride, rot=90, target_size=None, high_quality=False, cv2_mod=cv2, np_mod=np
|
|
||||||
)
|
|
||||||
out_hq = cam_mod.debayer_quad_csi2p(
|
|
||||||
raw, 8, 8, stride, rot=0, target_size=(6, 4), high_quality=True, cv2_mod=cv2, np_mod=np
|
|
||||||
)
|
|
||||||
self.assertEqual(out_live.shape, (4, 4, 3))
|
|
||||||
self.assertEqual(out_hq.shape, (4, 6, 3))
|
|
||||||
self.assertGreater(float(out_hq[0, :, 2].mean()), float(out_hq[0, :, 0].mean()) + 30.0)
|
|
||||||
self.assertGreater(float(out_hq[3, :, 0].mean()), float(out_hq[3, :, 2].mean()) + 30.0)
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_c_isp_vs_numpy_fallback_and_10bit_lsbs(self):
|
|
||||||
lib = cam_mod.load_isp_lib()
|
|
||||||
self.assertIsNotNone(lib, "expected compiled build/libchandra-camera-isp.so")
|
|
||||||
|
|
||||||
raw_a, stride = _make_quad_csi2p_image(w=16, h=16, top_val=803, bot_val=803, bg_val=99)
|
|
||||||
raw_b, _ = _make_quad_csi2p_image(w=16, h=16, top_val=800, bot_val=800, bg_val=96)
|
|
||||||
|
|
||||||
# Verify 10-bit LSBs change raw_a vs raw_b in C 4x4 unpacking.
|
|
||||||
arr_a = np.frombuffer(raw_a, dtype=np.uint8).reshape((16, stride))
|
|
||||||
arr_b = np.frombuffer(raw_b, dtype=np.uint8).reshape((16, stride))
|
|
||||||
out_a = np.empty((4, 4, 3), dtype=np.uint16)
|
|
||||||
out_b = np.empty((4, 4, 3), dtype=np.uint16)
|
|
||||||
lib.quad_csi2p_to_bgr16_4x4(arr_a.ctypes.data, 16, 16, stride, out_a.ctypes.data)
|
|
||||||
lib.quad_csi2p_to_bgr16_4x4(arr_b.ctypes.data, 16, 16, stride, out_b.ctypes.data)
|
|
||||||
self.assertEqual(int(out_a[0, 0, 2]) - int(out_b[0, 0, 2]), 12) # 4 pixels * 3 LSB = 12
|
|
||||||
|
|
||||||
# Compare full C ISP vs NumPy fallback on both 4x4 live and 2x2 HQ paths.
|
|
||||||
c_live = cam_mod.debayer_quad_csi2p(
|
|
||||||
raw_a, 16, 16, stride, rot=90, target_size=(4, 4), high_quality=False, cv2_mod=cv2, np_mod=np
|
|
||||||
)
|
|
||||||
c_hq = cam_mod.debayer_quad_csi2p(
|
|
||||||
raw_a, 16, 16, stride, rot=0, target_size=None, high_quality=True, cv2_mod=cv2, np_mod=np
|
|
||||||
)
|
|
||||||
saved_lib, saved_tried = cam_mod._ISP_LIB, cam_mod._ISP_LIB_TRIED
|
|
||||||
try:
|
|
||||||
cam_mod._ISP_LIB = None
|
|
||||||
cam_mod._ISP_LIB_TRIED = True
|
|
||||||
np_live = cam_mod.debayer_quad_csi2p(
|
|
||||||
raw_a, 16, 16, stride, rot=90, target_size=(4, 4), high_quality=False, cv2_mod=cv2, np_mod=np
|
|
||||||
)
|
|
||||||
np_hq = cam_mod.debayer_quad_csi2p(
|
|
||||||
raw_a, 16, 16, stride, rot=0, target_size=None, high_quality=True, cv2_mod=cv2, np_mod=np
|
|
||||||
)
|
|
||||||
finally:
|
|
||||||
cam_mod._ISP_LIB = saved_lib
|
|
||||||
cam_mod._ISP_LIB_TRIED = saved_tried
|
|
||||||
|
|
||||||
self.assertEqual(c_live.shape, (4, 4, 3))
|
|
||||||
self.assertEqual(c_hq.shape, (8, 8, 3))
|
|
||||||
self.assertLessEqual(int(np.abs(c_live.astype(np.int32) - np_live.astype(np.int32)).max()), 1)
|
|
||||||
self.assertLessEqual(int(np.abs(c_hq.astype(np.int32) - np_hq.astype(np.int32)).max()), 1)
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_burst_merge_bgr16(self):
|
|
||||||
lib = cam_mod.load_isp_lib()
|
|
||||||
self.assertIsNotNone(lib)
|
|
||||||
rng = np.random.default_rng(42)
|
|
||||||
h, w = 32, 32
|
|
||||||
base_clean = np.full((h, w, 3), 1000, dtype=np.int32)
|
|
||||||
frames = []
|
|
||||||
for _ in range(4):
|
|
||||||
noise = rng.normal(0.0, 25.0, size=(h, w, 3))
|
|
||||||
f = np.clip(base_clean + noise, 0, 4092).astype(np.uint16)
|
|
||||||
frames.append(np.ascontiguousarray(f))
|
|
||||||
# Inject strong motion artifact in frame 2 on top-left 8x8 block.
|
|
||||||
frames[2][:8, :8, :] = 3500
|
|
||||||
|
|
||||||
base = frames[0]
|
|
||||||
others = frames[1:]
|
|
||||||
ptrs = (ctypes.c_void_p * len(others))(*[x.ctypes.data for x in others])
|
|
||||||
out = np.empty_like(base)
|
|
||||||
lib.burst_merge_bgr16(base.ctypes.data, ptrs, len(others), out.ctypes.data, int(h * w), 120.0)
|
|
||||||
|
|
||||||
static_std_before = float(base[12:, 12:, :].std())
|
|
||||||
static_std_after = float(out[12:, 12:, :].std())
|
|
||||||
self.assertLess(static_std_after, static_std_before * 0.7)
|
|
||||||
# Moving patch from frame 2 must not ghost into merged static base.
|
|
||||||
self.assertLess(float(out[:8, :8, :].mean()), 1200.0)
|
|
||||||
|
|
||||||
def test_quad_sensor_ae_gain_priority_and_video_mode(self):
|
|
||||||
io = _FakeSubdevIO({
|
|
||||||
cam_mod.V4L2_CID_VBLANK: (56, 62029, 1, 56, 56),
|
|
||||||
cam_mod.V4L2_CID_EXPOSURE: (4, 3542, 1, 1000, 1000),
|
|
||||||
cam_mod.V4L2_CID_ANALOGUE_GAIN: (0, 480, 1, 0, 0),
|
|
||||||
cam_mod.V4L2_CID_DIGITAL_GAIN: (0, 4096, 1, 1024, 1024),
|
|
||||||
})
|
|
||||||
ae = cam_mod.QuadSensorAE("/dev/null", io=io)
|
|
||||||
# Mid-high EV (e.g. 10x base exposure): should stay at vb_min and raise analogue gain up to 16x first.
|
|
||||||
vb, exp, ag, dg = ae.plan(3542.0 * 10.0)
|
|
||||||
self.assertEqual(vb, 56)
|
|
||||||
self.assertEqual(exp, 3542)
|
|
||||||
self.assertGreater(ag, 400)
|
|
||||||
self.assertEqual(dg, 1024)
|
|
||||||
|
|
||||||
# Very low light: photo mode extends vblank up to vb_cap_photo, video mode tightens to vb_cap_video.
|
|
||||||
vb_photo, _, _, _ = ae.plan(ae.e_max)
|
|
||||||
self.assertEqual(vb_photo, ae.vb_cap_photo)
|
|
||||||
ae.cur_vblank = vb_photo
|
|
||||||
ae.ev_total = ae.e_max
|
|
||||||
ae.set_video_mode(True)
|
|
||||||
self.assertLessEqual(ae.cur_vblank, ae.vb_cap_video)
|
|
||||||
|
|
||||||
ae.close()
|
|
||||||
self.assertTrue(io.closed)
|
|
||||||
self.assertEqual(io.get(cam_mod.V4L2_CID_VBLANK), 56)
|
|
||||||
self.assertEqual(io.get(cam_mod.V4L2_CID_EXPOSURE), 1000)
|
|
||||||
self.assertEqual(io.get(cam_mod.V4L2_CID_ANALOGUE_GAIN), 0)
|
|
||||||
self.assertEqual(io.get(cam_mod.V4L2_CID_DIGITAL_GAIN), 1024)
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "requires cv2 and numpy")
|
|
||||||
def test_contrast_af_sweep_finds_sharp_peak(self):
|
|
||||||
io = _FakeSubdevIO({
|
|
||||||
cam_mod.V4L2_CID_FOCUS_ABSOLUTE: (0, 1023, 1, 0, 120),
|
|
||||||
})
|
|
||||||
af = cam_mod.ContrastAF("/dev/null", io=io)
|
|
||||||
|
|
||||||
# Build crisp high-contrast checkerboard target.
|
|
||||||
sharp_img = np.zeros((160, 160, 3), dtype=np.uint8)
|
|
||||||
for y in range(0, 160, 8):
|
|
||||||
for x in range(0, 160, 8):
|
|
||||||
if ((x // 8) + (y // 8)) % 2 == 0:
|
|
||||||
sharp_img[y:y + 8, x:x + 8] = 220
|
|
||||||
|
|
||||||
af.trigger(0.5, 0.5)
|
|
||||||
steps = 0
|
|
||||||
while af.state != "locked" and steps < 100:
|
|
||||||
dist = abs(af.cur_pos - 600)
|
|
||||||
sigma = 0.2 + dist / 90.0
|
|
||||||
if sigma > 0.6:
|
|
||||||
frame = cv2.GaussianBlur(sharp_img, (0, 0), sigma)
|
|
||||||
else:
|
|
||||||
frame = sharp_img.copy()
|
|
||||||
af.step(frame, cv2_mod=cv2, np_mod=np)
|
|
||||||
steps += 1
|
|
||||||
|
|
||||||
self.assertEqual(af.state, "locked")
|
|
||||||
self.assertLess(abs(af.best_pos - 600), 60)
|
|
||||||
af.close()
|
|
||||||
self.assertTrue(io.closed)
|
|
||||||
self.assertEqual(io.get(cam_mod.V4L2_CID_FOCUS_ABSOLUTE), 120)
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY and shutil.which("ffmpeg") and shutil.which("ffprobe"), "requires cv2, numpy, ffmpeg, ffprobe")
|
|
||||||
def test_matroska_i420_muxer_vfr_and_cluster_rollover(self):
|
|
||||||
w, h = 64, 48
|
|
||||||
muxer = cam_mod.MatroskaI420Muxer(w, h)
|
|
||||||
timestamps = [0, 33, 67, 100, 145, 200, 31000, 31040]
|
|
||||||
bgr = np.full((h, w, 3), 120, dtype=np.uint8)
|
|
||||||
i420 = cv2.cvtColor(bgr, cv2.COLOR_BGR2YUV_I420).tobytes()
|
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as td:
|
|
||||||
out_mp4 = os.path.join(td, "vfr.mp4")
|
|
||||||
cmd = [
|
|
||||||
"ffmpeg", "-y", "-loglevel", "error",
|
|
||||||
"-f", "matroska", "-i", "-",
|
|
||||||
"-fps_mode", "passthrough",
|
|
||||||
"-enc_time_base", "demux",
|
|
||||||
"-video_track_timescale", "1000",
|
|
||||||
"-c:v", "libx264", "-preset", "superfast", "-crf", "22", "-bf", "0",
|
|
||||||
"-pix_fmt", "yuv420p",
|
|
||||||
"-movflags", "+faststart", out_mp4,
|
|
||||||
]
|
|
||||||
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
||||||
for ts in timestamps:
|
|
||||||
proc.stdin.write(muxer.encode_frame(i420, ts))
|
|
||||||
proc.stdin.close()
|
|
||||||
rc = proc.wait(timeout=15)
|
|
||||||
err = proc.stderr.read().decode("utf-8", "replace")
|
|
||||||
proc.stderr.close()
|
|
||||||
self.assertEqual(rc, 0, msg="ffmpeg failed: " + err)
|
|
||||||
|
|
||||||
info = json.loads(subprocess.check_output([
|
|
||||||
"ffprobe", "-v", "error", "-show_streams", "-show_frames", "-of", "json", out_mp4
|
|
||||||
], text=True))
|
|
||||||
stream = info["streams"][0]
|
|
||||||
frames = info["frames"]
|
|
||||||
self.assertEqual(stream["codec_name"], "h264")
|
|
||||||
self.assertEqual(stream["pix_fmt"], "yuv420p")
|
|
||||||
self.assertEqual(len(frames), len(timestamps))
|
|
||||||
pts_ms = [round(float(f["pts_time"]) * 1000.0) for f in frames]
|
|
||||||
self.assertAlmostEqual(pts_ms[-1], 31040.0, delta=5.0)
|
|
||||||
self.assertAlmostEqual(pts_ms[4], 145.0, delta=5.0)
|
|
||||||
|
|
||||||
|
|
||||||
class PreviewCostTests(unittest.TestCase):
|
|
||||||
"""The viewfinder decodes small, paced frames; whoever asks gets full ones."""
|
|
||||||
|
|
||||||
def engine(self):
|
|
||||||
eng = cam_mod.CameraEngine([])
|
|
||||||
eng._frames_since_open = cam_mod.PREVIEW_SETTLE_FRAMES
|
|
||||||
return eng
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "needs OpenCV")
|
|
||||||
def test_half_frame_is_the_full_one_scaled(self):
|
|
||||||
rng = np.random.default_rng(3)
|
|
||||||
small = rng.integers(0, 256, (6, 8, 4), dtype=np.uint8)
|
|
||||||
big = small.repeat(2, axis=0).repeat(2, axis=1)
|
|
||||||
stride = 16 * 4 + 12
|
|
||||||
rows = np.zeros((12, stride), np.uint8)
|
|
||||||
rows[:, :64] = big.reshape(12, 64)
|
|
||||||
for rot in (0, 90, 270):
|
|
||||||
for fmt in ("ABGR8888", "ARGB8888"):
|
|
||||||
kw = dict(rot=rot, cv2_mod=cv2, np_mod=np)
|
|
||||||
full = cam_mod.convert_frame(rows.tobytes(), 16, 12, stride, fmt, **kw)
|
|
||||||
half = cam_mod.convert_frame(rows.tobytes(), 16, 12, stride, fmt, half=True, **kw)
|
|
||||||
self.assertTrue(np.array_equal(half, full[::2, ::2]))
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "needs OpenCV")
|
|
||||||
def test_preview_is_paced(self):
|
|
||||||
eng = self.engine()
|
|
||||||
due = sum(eng._preview_frame_due(100.0 + i / 34.0) for i in range(34 * 4))
|
|
||||||
self.assertTrue(92 <= due <= 98, due)
|
|
||||||
eng._frames_since_open = 0
|
|
||||||
self.assertTrue(eng._preview_frame_due(100.0) and eng._preview_frame_due(100.0))
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY, "needs OpenCV")
|
|
||||||
def test_full_frames_are_for_whoever_asks(self):
|
|
||||||
eng = self.engine()
|
|
||||||
self.assertFalse(eng._needs_full_frame())
|
|
||||||
self.assertIsNone(eng.grab_full_frame(timeout=0.01))
|
|
||||||
self.assertTrue(eng._needs_full_frame())
|
|
||||||
eng._full_wanted = False
|
|
||||||
|
|
||||||
class AF:
|
|
||||||
state = "coarse"
|
|
||||||
|
|
||||||
eng._af = AF()
|
|
||||||
self.assertTrue(eng._needs_full_frame())
|
|
||||||
|
|
||||||
|
|
||||||
class QRTests(unittest.TestCase):
|
|
||||||
def test_reporter_reports_once_until_out_of_view(self):
|
|
||||||
r = cam_mod.QRReporter()
|
|
||||||
self.assertFalse(r.update("", 0.0))
|
|
||||||
self.assertTrue(r.update("a", 1.0))
|
|
||||||
self.assertFalse(r.update("a", 2.0))
|
|
||||||
self.assertFalse(r.update("a", 4.5))
|
|
||||||
self.assertTrue(r.update("b", 4.6))
|
|
||||||
self.assertTrue(r.update("b", 9.0))
|
|
||||||
|
|
||||||
def test_line_is_one_ascii_line(self):
|
|
||||||
line = cam_mod.qr_line("a b\nframe 1 1 4\u202e%")
|
|
||||||
self.assertNotIn("\n", line)
|
|
||||||
self.assertTrue(line.isascii())
|
|
||||||
self.assertEqual(line.split(" "), ["qr", "a%20b%0Aframe%201%201%204%E2%80%AE%25"])
|
|
||||||
|
|
||||||
@unittest.skipUnless(HAS_CV2_NUMPY and hasattr(cv2, "QRCodeEncoder"), "needs OpenCV")
|
|
||||||
def test_scanner_reads_codes_in_a_viewfinder_frame(self):
|
|
||||||
text = "https://example.org/chandra"
|
|
||||||
code = cv2.QRCodeEncoder.create().encode(text)
|
|
||||||
code = cv2.copyMakeBorder(code, 4, 4, 4, 4, cv2.BORDER_CONSTANT, value=255)
|
|
||||||
for px in (500, 200): # the small one needs the finer view of the middle
|
|
||||||
frame = np.full((1600, 1200, 3), 110, np.uint8)
|
|
||||||
q = cv2.cvtColor(cv2.resize(code, (px, px), interpolation=cv2.INTER_NEAREST), cv2.COLOR_GRAY2BGR)
|
|
||||||
y, x = (1600 - px) // 2, (1200 - px) // 2
|
|
||||||
frame[y:y + px, x:x + px] = q
|
|
||||||
sc = cam_mod.QRScanner(cv2)
|
|
||||||
self.assertIn(text, [sc.scan(frame), sc.scan(frame)])
|
|
||||||
sc = cam_mod.QRScanner(cv2)
|
|
||||||
blank = np.full((1600, 1200, 3), 110, np.uint8)
|
|
||||||
self.assertEqual([sc.scan(blank), sc.scan(blank)], ["", ""])
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn mbt_ffi_cabi_realloc(
|
|
||||||
src_offset : Int,
|
|
||||||
src_size : Int,
|
|
||||||
_dst_alignment : Int,
|
|
||||||
dst_size : Int,
|
|
||||||
) -> Int {
|
|
||||||
// malloc
|
|
||||||
if src_offset == 0 && src_size == 0 {
|
|
||||||
return mbt_ffi_malloc(dst_size)
|
|
||||||
}
|
|
||||||
// free
|
|
||||||
if dst_size == 0 {
|
|
||||||
mbt_ffi_free(src_offset)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
// realloc
|
|
||||||
let dst = mbt_ffi_malloc(dst_size)
|
|
||||||
mbt_ffi_copy(dst, src_offset, if src_size < dst_size { src_size } else { dst_size })
|
|
||||||
mbt_ffi_free(src_offset)
|
|
||||||
dst
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_copy(dest : Int, src : Int, len : Int) =
|
|
||||||
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
|
||||||
@plasmoid.wasmExportRender(p0, p1)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportRenderPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportPopupPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportPopup() -> Int {
|
|
||||||
@plasmoid.wasmExportPopup()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItems() -> Int {
|
|
||||||
@module-config.wasmExportConfigItems()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
@module-config.wasmExportSetConfig(p0, p1, p2, p3)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportInfo() -> Int {
|
|
||||||
@plasmoid.wasmExportInfo()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportInfoPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
|
||||||
@module-config.wasmExportConfigItemsPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
Optional: a module's own settings. A module that exports this gets a "Module settings" entry in
|
|
||||||
its right-click menu, listing the items below; the shell draws the menu (the same for every
|
|
||||||
module), remembers each choice per module in state.edn (`:module-config`), and hands the saved
|
|
||||||
values back through `set-config` right after the module is loaded. A module without it simply
|
|
||||||
has no settings: the export is looked up by name, so older modules keep loading.
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItems() -> Int {
|
|
||||||
|
|
||||||
let (result) : (Array[ConfigItem]) = config_items();
|
|
||||||
let return_area = mbt_ffi_malloc(8)
|
|
||||||
|
|
||||||
let address3 = mbt_ffi_malloc((result).length() * 32);
|
|
||||||
for index4 = 0; index4 < (result).length(); index4 = index4 + 1 {
|
|
||||||
let iter_elem : ConfigItem = (result)[(index4)]
|
|
||||||
let iter_base = address3 + (index4 * 32);
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((iter_elem).key)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, (iter_elem).key.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ptr)
|
|
||||||
|
|
||||||
let ptr0 = mbt_ffi_str2ptr((iter_elem).label)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (iter_elem).label.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ptr0)
|
|
||||||
|
|
||||||
let address = mbt_ffi_malloc(((iter_elem).choices).length() * 8);
|
|
||||||
for index = 0; index < ((iter_elem).choices).length(); index = index + 1 {
|
|
||||||
let iter_elem : String = ((iter_elem).choices)[(index)]
|
|
||||||
let iter_base = address + (index * 8);
|
|
||||||
|
|
||||||
let ptr1 = mbt_ffi_str2ptr(iter_elem)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, iter_elem.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ptr1)
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((iter_elem).choices).length())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, address)
|
|
||||||
|
|
||||||
let ptr2 = mbt_ffi_str2ptr((iter_elem).value)
|
|
||||||
mbt_ffi_store32((iter_base) + 28, (iter_elem).value.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ptr2)
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 4, (result).length())
|
|
||||||
mbt_ffi_store32((return_area) + 0, address3)
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
|
||||||
|
|
||||||
for index0 = 0; index0 < (mbt_ffi_load32((p0) + 4)); index0 = index0 + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((p0) + 0)) + (index0 * 32)
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 0))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 8))
|
|
||||||
|
|
||||||
for index = 0; index < (mbt_ffi_load32((iter_base) + 20)); index = index + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((iter_base) + 16)) + (index * 8)
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 16))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 24))
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
|
|
||||||
let result = mbt_ffi_ptr2str(p0, p1)
|
|
||||||
|
|
||||||
let result0 = mbt_ffi_ptr2str(p2, p3)
|
|
||||||
|
|
||||||
let (result1) : (Bool) = set_config(result, result0);
|
|
||||||
let ret = (if result1 { 1 } else { 0 })
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_load32(offset : Int) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_free(position : Int) =
|
|
||||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/lib/battery" @logic,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
|
||||||
package "chandra/desktop/components/battery/gen/interface/chandra/desktop/module-config"
|
|
||||||
|
|
||||||
// Values
|
|
||||||
pub fn config_items() -> Array[ConfigItem]
|
|
||||||
|
|
||||||
pub fn set_config(String, String) -> Bool
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
|
|
||||||
// Types and methods
|
|
||||||
pub(all) struct ConfigItem {
|
|
||||||
key : String
|
|
||||||
label : String
|
|
||||||
choices : Array[String]
|
|
||||||
value : String
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type aliases
|
|
||||||
|
|
||||||
// Traits
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
///| The battery module's right-click settings (percentage, time remaining, what shows without a
|
|
||||||
/// battery); the logic is in lib/battery.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn config_items() -> Array[ConfigItem] {
|
|
||||||
@logic.config.items().map(e => { key: e.key, label: e.label, choices: e.choices, value: e.value })
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn set_config(key : String, value : String) -> Bool {
|
|
||||||
@logic.config.set(key, value)
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
///|
|
|
||||||
pub(all) struct ConfigItem {
|
|
||||||
key : String; label : String; choices : Array[String]; value : String
|
|
||||||
}
|
|
||||||
///|
|
|
||||||
declare pub fn config_items() -> Array[ConfigItem]
|
|
||||||
///|
|
|
||||||
/// Set `key` to `value` (unknown keys and values are ignored). True when the widget should redraw.
|
|
||||||
declare pub fn set_config(key : String, value : String) -> Bool
|
|
||||||
|
|
||||||
@@ -1,431 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportInfo() -> Int {
|
|
||||||
|
|
||||||
let (result) : (PlasmoidInfo) = info();
|
|
||||||
let return_area = mbt_ffi_malloc(32)
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((result).id)
|
|
||||||
mbt_ffi_store32((return_area) + 4, (result).id.length())
|
|
||||||
mbt_ffi_store32((return_area) + 0, ptr)
|
|
||||||
|
|
||||||
let ptr0 = mbt_ffi_str2ptr((result).name)
|
|
||||||
mbt_ffi_store32((return_area) + 12, (result).name.length())
|
|
||||||
mbt_ffi_store32((return_area) + 8, ptr0)
|
|
||||||
mbt_ffi_store32((return_area) + 16, (((result).preferred).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((return_area) + 20, (((result).preferred).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((return_area) + 24, ((result).min_width).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((return_area) + 28, (if (result).stretch { 1 } else { 0 }))
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 0))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 8))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
|
||||||
|
|
||||||
let (result) : (Array[@types.DrawOp]) = render(@types.Size::{w : (p0).reinterpret_as_uint(), h : (p1).reinterpret_as_uint()});
|
|
||||||
let return_area = mbt_ffi_malloc(8)
|
|
||||||
|
|
||||||
let address = mbt_ffi_malloc((result).length() * 32);
|
|
||||||
for index = 0; index < (result).length(); index = index + 1 {
|
|
||||||
let iter_elem : @types.DrawOp = (result)[(index)]
|
|
||||||
let iter_base = address + (index * 32);
|
|
||||||
|
|
||||||
match iter_elem {
|
|
||||||
FillRect(payload) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (0))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload).radius).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
StrokeRect(payload0) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (1))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload0).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload0).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload0).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload0).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload0).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload0).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload0).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload0).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload0).width).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 28, ((payload0).radius).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Line(payload1) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (2))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload1).start).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload1).start).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload1).end).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 16, ((payload1).end).y)
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload1).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload1).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload1).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload1).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload1).width).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Circle(payload2) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (3))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload2).center).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload2).center).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload2).radius).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 16, (((payload2).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 17, (((payload2).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 18, (((payload2).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 19, (((payload2).color).a).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (if (payload2).filled { 1 } else { 0 }))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Text(payload3) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (4))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload3).origin).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload3).origin).y)
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((payload3).text)
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (payload3).text.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ptr)
|
|
||||||
mbt_ffi_store32((iter_base) + 20, (((payload3).style).size).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 24, ((((payload3).style).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 25, ((((payload3).style).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 26, ((((payload3).style).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 27, ((((payload3).style).color).a).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 28, ((payload3).style).align.ordinal())
|
|
||||||
mbt_ffi_store8((iter_base) + 29, (if ((payload3).style).bold { 1 } else { 0 }))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
PushClip(payload4) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (5))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, (payload4).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, (payload4).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload4).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, ((payload4).h).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
PopClip => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload6) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 4, (result).length())
|
|
||||||
mbt_ffi_store32((return_area) + 0, address)
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|
||||||
|
|
||||||
for index = 0; index < (mbt_ffi_load32((p0) + 4)); index = index + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((p0) + 0)) + (index * 32)
|
|
||||||
|
|
||||||
match (mbt_ffi_load8_u((iter_base) + 0)) {
|
|
||||||
0 => ()
|
|
||||||
1 => ()
|
|
||||||
2 => ()
|
|
||||||
3 => ()
|
|
||||||
4 => {
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 12))
|
|
||||||
|
|
||||||
}
|
|
||||||
5 => ()
|
|
||||||
6 => ()
|
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportPopup() -> Int {
|
|
||||||
|
|
||||||
let (result) : (PopupSurface?) = popup();
|
|
||||||
let return_area = mbt_ffi_malloc(20)
|
|
||||||
|
|
||||||
match (result) {
|
|
||||||
None => {
|
|
||||||
mbt_ffi_store8((return_area) + 0, (0))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Some(payload0) => {
|
|
||||||
mbt_ffi_store8((return_area) + 0, (1))
|
|
||||||
mbt_ffi_store32((return_area) + 4, (((payload0).size).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((return_area) + 8, (((payload0).size).h).reinterpret_as_int())
|
|
||||||
|
|
||||||
let address = mbt_ffi_malloc(((payload0).ops).length() * 32);
|
|
||||||
for index = 0; index < ((payload0).ops).length(); index = index + 1 {
|
|
||||||
let iter_elem : @types.DrawOp = ((payload0).ops)[(index)]
|
|
||||||
let iter_base = address + (index * 32);
|
|
||||||
|
|
||||||
match iter_elem {
|
|
||||||
FillRect(payload1) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (0))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload1).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload1).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload1).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload1).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload1).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload1).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload1).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload1).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload1).radius).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
StrokeRect(payload2) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (1))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload2).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload2).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload2).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload2).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload2).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload2).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload2).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload2).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload2).width).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 28, ((payload2).radius).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Line(payload3) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (2))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload3).start).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload3).start).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload3).end).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 16, ((payload3).end).y)
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload3).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload3).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload3).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload3).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload3).width).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Circle(payload4) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (3))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload4).center).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload4).center).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload4).radius).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 16, (((payload4).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 17, (((payload4).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 18, (((payload4).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 19, (((payload4).color).a).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (if (payload4).filled { 1 } else { 0 }))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Text(payload5) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (4))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload5).origin).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload5).origin).y)
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((payload5).text)
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (payload5).text.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ptr)
|
|
||||||
mbt_ffi_store32((iter_base) + 20, (((payload5).style).size).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 24, ((((payload5).style).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 25, ((((payload5).style).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 26, ((((payload5).style).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 27, ((((payload5).style).color).a).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 28, ((payload5).style).align.ordinal())
|
|
||||||
mbt_ffi_store8((iter_base) + 29, (if ((payload5).style).bold { 1 } else { 0 }))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
PushClip(payload6) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (5))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, (payload6).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, (payload6).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload6).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, ((payload6).h).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
PopClip => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload8) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 16, ((payload0).ops).length())
|
|
||||||
mbt_ffi_store32((return_area) + 12, address)
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|
||||||
|
|
||||||
match (mbt_ffi_load8_u((p0) + 0)) {
|
|
||||||
0 => ()
|
|
||||||
1 => {
|
|
||||||
|
|
||||||
for index = 0; index < (mbt_ffi_load32((p0) + 16)); index = index + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((p0) + 12)) + (index * 32)
|
|
||||||
|
|
||||||
match (mbt_ffi_load8_u((iter_base) + 0)) {
|
|
||||||
0 => ()
|
|
||||||
1 => ()
|
|
||||||
2 => ()
|
|
||||||
3 => ()
|
|
||||||
4 => {
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 12))
|
|
||||||
|
|
||||||
}
|
|
||||||
5 => ()
|
|
||||||
6 => ()
|
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 12))
|
|
||||||
|
|
||||||
}
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
|
|
||||||
let lifted = match (p1) {
|
|
||||||
0 => {
|
|
||||||
|
|
||||||
@types.InputEvent::PointerMove(@types.Point::{x : p2, y : p3})
|
|
||||||
}
|
|
||||||
1 => {
|
|
||||||
|
|
||||||
@types.InputEvent::PointerDown(@types.Point::{x : p2, y : p3})
|
|
||||||
}
|
|
||||||
2 => {
|
|
||||||
|
|
||||||
@types.InputEvent::PointerUp(@types.Point::{x : p2, y : p3})
|
|
||||||
}
|
|
||||||
3 => {
|
|
||||||
|
|
||||||
@types.InputEvent::PointerLeave
|
|
||||||
}
|
|
||||||
4 => {
|
|
||||||
|
|
||||||
@types.InputEvent::FocusLost
|
|
||||||
}
|
|
||||||
5 => {
|
|
||||||
|
|
||||||
let result = mbt_ffi_ptr2str(p2, p3)
|
|
||||||
|
|
||||||
@types.InputEvent::KeyDown(result)
|
|
||||||
}
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
|
|
||||||
let (result0) : (Bool) = on_event(Surface::from(p0), lifted);
|
|
||||||
let ret = (if result0 { 1 } else { 0 })
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
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) =
|
|
||||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_store32(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_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)
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/interface/chandra/desktop/types" @types,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/battery" @battery,
|
|
||||||
"chandra/desktop/lib/battery" @logic,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
///| Implementation of `chandra:desktop/plasmoid` for the battery module: charge as an icon and
|
|
||||||
/// percentage, charging state and time remaining, turning amber when low and red when critical.
|
|
||||||
/// A click opens the details; a critical battery opens them by itself once per discharge spell.
|
|
||||||
/// All drawing is in lib/battery; this file only adapts the `battery` capability.
|
|
||||||
|
|
||||||
///|
|
|
||||||
let open_ : Ref[Bool] = { val: false }
|
|
||||||
|
|
||||||
///| The open popup is the low-battery warning (not a click).
|
|
||||||
let warning : Ref[Bool] = { val: false }
|
|
||||||
|
|
||||||
///|
|
|
||||||
let low : @logic.LowWarning = @logic.LowWarning::new()
|
|
||||||
|
|
||||||
///| The host's reading, sampled about once a second.
|
|
||||||
fn reading() -> @logic.Reading {
|
|
||||||
let s = @battery.read()
|
|
||||||
{ percent: s.percent, state: s.state.ordinal(), secs_left: s.secs_left.reinterpret_as_int() }
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn info() -> PlasmoidInfo {
|
|
||||||
let (id, name, w, h, min_w) = @logic.info()
|
|
||||||
{
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() },
|
|
||||||
min_width: min_w.reinterpret_as_uint(),
|
|
||||||
stretch: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
|
|
||||||
let r = reading()
|
|
||||||
if low.update(r) {
|
|
||||||
open_.val = true
|
|
||||||
warning.val = true
|
|
||||||
}
|
|
||||||
@logic.render(size, @theme.get_tokens(), r, @logic.config, open=open_.val && !warning.val)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn popup() -> PopupSurface? {
|
|
||||||
if !open_.val {
|
|
||||||
return None
|
|
||||||
}
|
|
||||||
let (w, h, ops) = @logic.render_popup(@theme.get_tokens(), reading(), warning=warning.val)
|
|
||||||
Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops })
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
fn close() -> Unit {
|
|
||||||
open_.val = false
|
|
||||||
warning.val = false
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool {
|
|
||||||
match (target, event) {
|
|
||||||
(WIDGET, PointerDown(_)) => {
|
|
||||||
if open_.val {
|
|
||||||
close()
|
|
||||||
} else {
|
|
||||||
open_.val = true
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
(POPUP, PointerDown(_)) => {
|
|
||||||
close()
|
|
||||||
true
|
|
||||||
}
|
|
||||||
(_, FocusLost) | (_, KeyDown("Escape")) =>
|
|
||||||
if open_.val {
|
|
||||||
close()
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
_ => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/components/battery/gen/interface/chandra/desktop/module-config" @module-config,
|
|
||||||
"chandra/desktop/components/battery/gen/interface/chandra/desktop/plasmoid" @plasmoid,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
|
|
||||||
pkgtype(kind: "foreign_library")
|
|
||||||
|
|
||||||
options(
|
|
||||||
link: {
|
|
||||||
"wasm": {
|
|
||||||
"export-memory-name": "memory",
|
|
||||||
"heap-start-address": 16,
|
|
||||||
"exports": [
|
|
||||||
"mbt_ffi_cabi_realloc:cabi_realloc",
|
|
||||||
"wasmExportConfigItems:chandra:desktop/module-config@0.1.0#config-items",
|
|
||||||
"wasmExportConfigItemsPostReturn:cabi_post_chandra:desktop/module-config@0.1.0#config-items",
|
|
||||||
"wasmExportInfo:chandra:desktop/plasmoid@0.1.0#info",
|
|
||||||
"wasmExportInfoPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#info",
|
|
||||||
"wasmExportOnEvent:chandra:desktop/plasmoid@0.1.0#on-event",
|
|
||||||
"wasmExportPopup:chandra:desktop/plasmoid@0.1.0#popup",
|
|
||||||
"wasmExportPopupPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#popup",
|
|
||||||
"wasmExportRender:chandra:desktop/plasmoid@0.1.0#render",
|
|
||||||
"wasmExportRenderPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#render",
|
|
||||||
"wasmExportSetConfig:chandra:desktop/module-config@0.1.0#set-config"
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
|
||||||
package "chandra/desktop/components/battery/gen"
|
|
||||||
|
|
||||||
// Values
|
|
||||||
pub fn mbt_ffi_cabi_realloc(Int32, Int32, Int32, Int32) -> Int32
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
|
|
||||||
// Types and methods
|
|
||||||
|
|
||||||
// Type aliases
|
|
||||||
|
|
||||||
// Traits
|
|
||||||
@@ -44,24 +44,18 @@ pub fn wasmExportInfo() -> Int {
|
|||||||
@plasmoid.wasmExportInfo()
|
@plasmoid.wasmExportInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportInfoPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItems() -> Int {
|
|
||||||
@module-config.wasmExportConfigItems()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
||||||
@plasmoid.wasmExportRender(p0, p1)
|
@plasmoid.wasmExportRender(p0, p1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportInfoPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
||||||
@@ -80,21 +74,9 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
@plasmoid.wasmExportPopupPostReturn(p0)
|
@plasmoid.wasmExportPopupPostReturn(p0)
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
@module-config.wasmExportSetConfig(p0, p1, p2, p3)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
||||||
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
|
||||||
@module-config.wasmExportConfigItemsPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
Optional: a module's own settings. A module that exports this gets a "Module settings" entry in
|
|
||||||
its right-click menu, listing the items below; the shell draws the menu (the same for every
|
|
||||||
module), remembers each choice per module in state.edn (`:module-config`), and hands the saved
|
|
||||||
values back through `set-config` right after the module is loaded. A module without it simply
|
|
||||||
has no settings: the export is looked up by name, so older modules keep loading.
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItems() -> Int {
|
|
||||||
|
|
||||||
let (result) : (Array[ConfigItem]) = config_items();
|
|
||||||
let return_area = mbt_ffi_malloc(8)
|
|
||||||
|
|
||||||
let address3 = mbt_ffi_malloc((result).length() * 32);
|
|
||||||
for index4 = 0; index4 < (result).length(); index4 = index4 + 1 {
|
|
||||||
let iter_elem : ConfigItem = (result)[(index4)]
|
|
||||||
let iter_base = address3 + (index4 * 32);
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((iter_elem).key)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, (iter_elem).key.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ptr)
|
|
||||||
|
|
||||||
let ptr0 = mbt_ffi_str2ptr((iter_elem).label)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (iter_elem).label.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ptr0)
|
|
||||||
|
|
||||||
let address = mbt_ffi_malloc(((iter_elem).choices).length() * 8);
|
|
||||||
for index = 0; index < ((iter_elem).choices).length(); index = index + 1 {
|
|
||||||
let iter_elem : String = ((iter_elem).choices)[(index)]
|
|
||||||
let iter_base = address + (index * 8);
|
|
||||||
|
|
||||||
let ptr1 = mbt_ffi_str2ptr(iter_elem)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, iter_elem.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ptr1)
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((iter_elem).choices).length())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, address)
|
|
||||||
|
|
||||||
let ptr2 = mbt_ffi_str2ptr((iter_elem).value)
|
|
||||||
mbt_ffi_store32((iter_base) + 28, (iter_elem).value.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ptr2)
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 4, (result).length())
|
|
||||||
mbt_ffi_store32((return_area) + 0, address3)
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
|
||||||
|
|
||||||
for index0 = 0; index0 < (mbt_ffi_load32((p0) + 4)); index0 = index0 + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((p0) + 0)) + (index0 * 32)
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 0))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 8))
|
|
||||||
|
|
||||||
for index = 0; index < (mbt_ffi_load32((iter_base) + 20)); index = index + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((iter_base) + 16)) + (index * 8)
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 16))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 24))
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
|
|
||||||
let result = mbt_ffi_ptr2str(p0, p1)
|
|
||||||
|
|
||||||
let result0 = mbt_ffi_ptr2str(p2, p3)
|
|
||||||
|
|
||||||
let (result1) : (Bool) = set_config(result, result0);
|
|
||||||
let ret = (if result1 { 1 } else { 0 })
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_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_store32(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/lib/clock" @logic,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
|
||||||
package "chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/module-config"
|
|
||||||
|
|
||||||
// Values
|
|
||||||
pub fn config_items() -> Array[ConfigItem]
|
|
||||||
|
|
||||||
pub fn set_config(String, String) -> Bool
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
|
|
||||||
// Types and methods
|
|
||||||
pub(all) struct ConfigItem {
|
|
||||||
key : String
|
|
||||||
label : String
|
|
||||||
choices : Array[String]
|
|
||||||
value : String
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type aliases
|
|
||||||
|
|
||||||
// Traits
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
///| The binary clock's right-click settings (seconds columns); the logic is in lib/clock.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn config_items() -> Array[ConfigItem] {
|
|
||||||
@logic.binary_config.binary_items().map(e => {
|
|
||||||
key: e.key,
|
|
||||||
label: e.label,
|
|
||||||
choices: e.choices,
|
|
||||||
value: e.value,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn set_config(key : String, value : String) -> Bool {
|
|
||||||
@logic.binary_config.set(key, value)
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
///|
|
|
||||||
pub(all) struct ConfigItem {
|
|
||||||
key : String; label : String; choices : Array[String]; value : String
|
|
||||||
}
|
|
||||||
///|
|
|
||||||
declare pub fn config_items() -> Array[ConfigItem]
|
|
||||||
///|
|
|
||||||
/// Set `key` to `value` (unknown keys and values are ignored). True when the widget should redraw.
|
|
||||||
declare pub fn set_config(key : String, value : String) -> Bool
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/interface/chandra/desktop/types" @types,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/clock" @clock,
|
|
||||||
"chandra/desktop/lib/clock" @logic,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
///| Implementation of `chandra:desktop/plasmoid` for the binary clock.
|
|
||||||
/// All logic lives in lib/clock; this file only adapts host capabilities.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn info() -> PlasmoidInfo {
|
|
||||||
let (id, name, w, h, min_w) = @logic.binary_info()
|
|
||||||
{
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() },
|
|
||||||
min_width: min_w.reinterpret_as_uint(),
|
|
||||||
stretch: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
|
|
||||||
@logic.render_binary(size, @theme.get_tokens(), @clock.now(), seconds=@logic.binary_config.seconds)
|
|
||||||
}
|
|
||||||
|
|
||||||
///| The calendar flyout (left click opens it; the shell slides it out of the panel).
|
|
||||||
pub fn popup() -> PopupSurface? {
|
|
||||||
let cal = @logic.binary_config.cal
|
|
||||||
if !cal.open {
|
|
||||||
return None
|
|
||||||
}
|
|
||||||
let (w, h) = cal.size()
|
|
||||||
let ops = cal.render(@theme.get_tokens(), @clock.now())
|
|
||||||
Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops })
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool {
|
|
||||||
@logic.binary_config.cal.handle(target is POPUP, event, @clock.now())
|
|
||||||
}
|
|
||||||
@@ -130,16 +130,6 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
|||||||
PopClip => {
|
PopClip => {
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
mbt_ffi_store8((iter_base) + 0, (6))
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload6) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,7 +160,6 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,16 +278,6 @@ pub fn wasmExportPopup() -> Int {
|
|||||||
PopClip => {
|
PopClip => {
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
mbt_ffi_store8((iter_base) + 0, (6))
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload8) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -337,7 +316,6 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,26 +368,18 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
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_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)
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
|
||||||
#|(func (param i32) (result i32) 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_free(position : Int) =
|
||||||
|
#|(func (param i32) local.get 0 call $moonbit.decref)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
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)
|
||||||
@@ -418,14 +388,22 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
|||||||
#| local.get 1)
|
#| local.get 1)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
#owned(str)
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||||
|
#|(func (param i32) (result i32) local.get 0)
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_free(position : Int) =
|
|
||||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
||||||
|
|
||||||
|
///|
|
||||||
|
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 =
|
||||||
|
#|(func (param i32) (param i32) (result i32)
|
||||||
|
#| local.get 0
|
||||||
|
#| local.get 1 call $moonbit.init_array16
|
||||||
|
#| local.get 0)
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import {
|
||||||
|
"moonplasma/desktop/interface/moonplasma/desktop/types" @types,
|
||||||
|
"moonplasma/desktop/interface/moonplasma/desktop/theme" @theme,
|
||||||
|
"moonplasma/desktop/interface/moonplasma/desktop/clock" @clock,
|
||||||
|
"moonplasma/desktop/lib/clock" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/plasmoid"
|
package "moonplasma/desktop/components/clock-binary/gen/interface/moonplasma/desktop/plasmoid"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
"chandra/desktop/interface/chandra/desktop/types",
|
"moonplasma/desktop/interface/moonplasma/desktop/types",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
///| Implementation of `moonplasma:desktop/plasmoid` for the binary clock.
|
||||||
|
/// All logic lives in lib/clock; this file only adapts host capabilities.
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn info() -> PlasmoidInfo {
|
||||||
|
let (id, name, w, h, min_w) = @logic.binary_info()
|
||||||
|
{
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() },
|
||||||
|
min_width: min_w.reinterpret_as_uint(),
|
||||||
|
stretch: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
|
||||||
|
@logic.render_binary(size, @theme.get_tokens(), @clock.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn popup() -> PopupSurface? {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn on_event(_target : Surface, _event : @types.InputEvent) -> Bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
"chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/module-config" @module-config,
|
"moonplasma/desktop/components/clock-binary/gen/interface/moonplasma/desktop/plasmoid" @plasmoid,
|
||||||
"chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/plasmoid" @plasmoid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
warnings = "-44"
|
warnings = "-44"
|
||||||
@@ -14,16 +13,13 @@ options(
|
|||||||
"heap-start-address": 16,
|
"heap-start-address": 16,
|
||||||
"exports": [
|
"exports": [
|
||||||
"mbt_ffi_cabi_realloc:cabi_realloc",
|
"mbt_ffi_cabi_realloc:cabi_realloc",
|
||||||
"wasmExportConfigItems:chandra:desktop/module-config@0.1.0#config-items",
|
"wasmExportInfo:moonplasma:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportConfigItemsPostReturn:cabi_post_chandra:desktop/module-config@0.1.0#config-items",
|
"wasmExportInfoPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportInfo:chandra:desktop/plasmoid@0.1.0#info",
|
"wasmExportOnEvent:moonplasma:desktop/plasmoid@0.1.0#on-event",
|
||||||
"wasmExportInfoPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#info",
|
"wasmExportPopup:moonplasma:desktop/plasmoid@0.1.0#popup",
|
||||||
"wasmExportOnEvent:chandra:desktop/plasmoid@0.1.0#on-event",
|
"wasmExportPopupPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#popup",
|
||||||
"wasmExportPopup:chandra:desktop/plasmoid@0.1.0#popup",
|
"wasmExportRender:moonplasma:desktop/plasmoid@0.1.0#render",
|
||||||
"wasmExportPopupPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#popup",
|
"wasmExportRenderPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#render"
|
||||||
"wasmExportRender:chandra:desktop/plasmoid@0.1.0#render",
|
|
||||||
"wasmExportRenderPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#render",
|
|
||||||
"wasmExportSetConfig:chandra:desktop/module-config@0.1.0#set-config"
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "chandra/desktop/components/clock-binary/gen"
|
package "moonplasma/desktop/components/clock-binary/gen"
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
pub fn mbt_ffi_cabi_realloc(Int32, Int32, Int32, Int32) -> Int32
|
pub fn mbt_ffi_cabi_realloc(Int32, Int32, Int32, Int32) -> Int32
|
||||||
|
|||||||
@@ -38,63 +38,45 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
|||||||
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)
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
@module-config.wasmExportSetConfig(p0, p1, p2, p3)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportInfo() -> Int {
|
pub fn wasmExportInfo() -> Int {
|
||||||
@plasmoid.wasmExportInfo()
|
@plasmoid.wasmExportInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItems() -> Int {
|
|
||||||
@module-config.wasmExportConfigItems()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
|
||||||
@plasmoid.wasmExportRender(p0, p1)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportPopup() -> Int {
|
|
||||||
@plasmoid.wasmExportPopup()
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
|
||||||
@module-config.wasmExportConfigItemsPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportInfoPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
||||||
@plasmoid.wasmExportRenderPostReturn(p0)
|
@plasmoid.wasmExportRenderPostReturn(p0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportInfoPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
||||||
@plasmoid.wasmExportPopupPostReturn(p0)
|
@plasmoid.wasmExportPopupPostReturn(p0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportPopup() -> Int {
|
||||||
|
@plasmoid.wasmExportPopup()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportRender(p0, p1)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
Optional: a module's own settings. A module that exports this gets a "Module settings" entry in
|
|
||||||
its right-click menu, listing the items below; the shell draws the menu (the same for every
|
|
||||||
module), remembers each choice per module in state.edn (`:module-config`), and hands the saved
|
|
||||||
values back through `set-config` right after the module is loaded. A module without it simply
|
|
||||||
has no settings: the export is looked up by name, so older modules keep loading.
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItems() -> Int {
|
|
||||||
|
|
||||||
let (result) : (Array[ConfigItem]) = config_items();
|
|
||||||
let return_area = mbt_ffi_malloc(8)
|
|
||||||
|
|
||||||
let address3 = mbt_ffi_malloc((result).length() * 32);
|
|
||||||
for index4 = 0; index4 < (result).length(); index4 = index4 + 1 {
|
|
||||||
let iter_elem : ConfigItem = (result)[(index4)]
|
|
||||||
let iter_base = address3 + (index4 * 32);
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((iter_elem).key)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, (iter_elem).key.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ptr)
|
|
||||||
|
|
||||||
let ptr0 = mbt_ffi_str2ptr((iter_elem).label)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (iter_elem).label.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ptr0)
|
|
||||||
|
|
||||||
let address = mbt_ffi_malloc(((iter_elem).choices).length() * 8);
|
|
||||||
for index = 0; index < ((iter_elem).choices).length(); index = index + 1 {
|
|
||||||
let iter_elem : String = ((iter_elem).choices)[(index)]
|
|
||||||
let iter_base = address + (index * 8);
|
|
||||||
|
|
||||||
let ptr1 = mbt_ffi_str2ptr(iter_elem)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, iter_elem.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ptr1)
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((iter_elem).choices).length())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, address)
|
|
||||||
|
|
||||||
let ptr2 = mbt_ffi_str2ptr((iter_elem).value)
|
|
||||||
mbt_ffi_store32((iter_base) + 28, (iter_elem).value.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ptr2)
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 4, (result).length())
|
|
||||||
mbt_ffi_store32((return_area) + 0, address3)
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
|
||||||
|
|
||||||
for index0 = 0; index0 < (mbt_ffi_load32((p0) + 4)); index0 = index0 + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((p0) + 0)) + (index0 * 32)
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 0))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 8))
|
|
||||||
|
|
||||||
for index = 0; index < (mbt_ffi_load32((iter_base) + 20)); index = index + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((iter_base) + 16)) + (index * 8)
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 16))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 24))
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 0))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|
||||||
|
|
||||||
let result = mbt_ffi_ptr2str(p0, p1)
|
|
||||||
|
|
||||||
let result0 = mbt_ffi_ptr2str(p2, p3)
|
|
||||||
|
|
||||||
let (result1) : (Bool) = set_config(result, result0);
|
|
||||||
let ret = (if result1 { 1 } else { 0 })
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_store32(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
|
||||||
#|(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)
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/lib/clock" @logic,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
|
||||||
package "chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/module-config"
|
|
||||||
|
|
||||||
// Values
|
|
||||||
pub fn config_items() -> Array[ConfigItem]
|
|
||||||
|
|
||||||
pub fn set_config(String, String) -> Bool
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
|
|
||||||
// Types and methods
|
|
||||||
pub(all) struct ConfigItem {
|
|
||||||
key : String
|
|
||||||
label : String
|
|
||||||
choices : Array[String]
|
|
||||||
value : String
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type aliases
|
|
||||||
|
|
||||||
// Traits
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
///| The digital clock's right-click settings (time format, AM/PM, date line); the logic is in lib/clock.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn config_items() -> Array[ConfigItem] {
|
|
||||||
@logic.digital_config.digital_items().map(e => {
|
|
||||||
key: e.key,
|
|
||||||
label: e.label,
|
|
||||||
choices: e.choices,
|
|
||||||
value: e.value,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn set_config(key : String, value : String) -> Bool {
|
|
||||||
@logic.digital_config.set(key, value)
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
///|
|
|
||||||
pub(all) struct ConfigItem {
|
|
||||||
key : String; label : String; choices : Array[String]; value : String
|
|
||||||
}
|
|
||||||
///|
|
|
||||||
declare pub fn config_items() -> Array[ConfigItem]
|
|
||||||
///|
|
|
||||||
/// Set `key` to `value` (unknown keys and values are ignored). True when the widget should redraw.
|
|
||||||
declare pub fn set_config(key : String, value : String) -> Bool
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/interface/chandra/desktop/types" @types,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/clock" @clock,
|
|
||||||
"chandra/desktop/lib/clock" @logic,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
///| Implementation of `chandra:desktop/plasmoid` for the digital clock.
|
|
||||||
/// All logic lives in lib/clock; this file only adapts host capabilities.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn info() -> PlasmoidInfo {
|
|
||||||
let (id, name, w, h, min_w) = @logic.digital_info()
|
|
||||||
{
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
preferred: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() },
|
|
||||||
min_width: min_w.reinterpret_as_uint(),
|
|
||||||
stretch: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
|
|
||||||
let c = @logic.digital_config
|
|
||||||
@logic.render_digital(size, @theme.get_tokens(), @clock.now(), hour12=c.hour12, show_date=c.show_date, ampm=c.ampm)
|
|
||||||
}
|
|
||||||
|
|
||||||
///| The calendar flyout (left click opens it; the shell slides it out of the panel).
|
|
||||||
pub fn popup() -> PopupSurface? {
|
|
||||||
let c = @logic.digital_config
|
|
||||||
if !c.cal.open {
|
|
||||||
return None
|
|
||||||
}
|
|
||||||
let (w, h) = c.cal.size()
|
|
||||||
let ops = c.cal.render(@theme.get_tokens(), @clock.now(), hour12=c.hour12, ampm=c.ampm)
|
|
||||||
Some({ size: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() }, ops })
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool {
|
|
||||||
@logic.digital_config.cal.handle(target is POPUP, event, @clock.now())
|
|
||||||
}
|
|
||||||
@@ -130,16 +130,6 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
|||||||
PopClip => {
|
PopClip => {
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
mbt_ffi_store8((iter_base) + 0, (6))
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload6) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload6).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload6).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload6).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload6).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload6).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,7 +160,6 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,16 +278,6 @@ pub fn wasmExportPopup() -> Int {
|
|||||||
PopClip => {
|
PopClip => {
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
mbt_ffi_store8((iter_base) + 0, (6))
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload8) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -337,7 +316,6 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,6 +368,10 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : 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_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)
|
||||||
@@ -397,6 +379,27 @@ 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_free(position : Int) =
|
||||||
|
#|(func (param i32) local.get 0 call $moonbit.decref)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
|
||||||
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
|
||||||
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
||||||
|
|
||||||
|
///|
|
||||||
|
#owned(str)
|
||||||
|
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||||
|
#|(func (param i32) (result i32) local.get 0)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
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)
|
||||||
@@ -404,28 +407,3 @@ 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) =
|
|
||||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_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_store8(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import {
|
||||||
|
"moonplasma/desktop/interface/moonplasma/desktop/types" @types,
|
||||||
|
"moonplasma/desktop/interface/moonplasma/desktop/theme" @theme,
|
||||||
|
"moonplasma/desktop/interface/moonplasma/desktop/clock" @clock,
|
||||||
|
"moonplasma/desktop/lib/clock" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/plasmoid"
|
package "moonplasma/desktop/components/clock-digital/gen/interface/moonplasma/desktop/plasmoid"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
"chandra/desktop/interface/chandra/desktop/types",
|
"moonplasma/desktop/interface/moonplasma/desktop/types",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
///| Implementation of `moonplasma:desktop/plasmoid` for the digital clock.
|
||||||
|
/// All logic lives in lib/clock; this file only adapts host capabilities.
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn info() -> PlasmoidInfo {
|
||||||
|
let (id, name, w, h, min_w) = @logic.digital_info()
|
||||||
|
{
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
preferred: { w: w.reinterpret_as_uint(), h: h.reinterpret_as_uint() },
|
||||||
|
min_width: min_w.reinterpret_as_uint(),
|
||||||
|
stretch: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
|
||||||
|
@logic.render_digital(size, @theme.get_tokens(), @clock.now())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn popup() -> PopupSurface? {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn on_event(_target : Surface, _event : @types.InputEvent) -> Bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
"chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/module-config" @module-config,
|
"moonplasma/desktop/components/clock-digital/gen/interface/moonplasma/desktop/plasmoid" @plasmoid,
|
||||||
"chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/plasmoid" @plasmoid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
warnings = "-44"
|
warnings = "-44"
|
||||||
@@ -14,16 +13,13 @@ options(
|
|||||||
"heap-start-address": 16,
|
"heap-start-address": 16,
|
||||||
"exports": [
|
"exports": [
|
||||||
"mbt_ffi_cabi_realloc:cabi_realloc",
|
"mbt_ffi_cabi_realloc:cabi_realloc",
|
||||||
"wasmExportConfigItems:chandra:desktop/module-config@0.1.0#config-items",
|
"wasmExportInfo:moonplasma:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportConfigItemsPostReturn:cabi_post_chandra:desktop/module-config@0.1.0#config-items",
|
"wasmExportInfoPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportInfo:chandra:desktop/plasmoid@0.1.0#info",
|
"wasmExportOnEvent:moonplasma:desktop/plasmoid@0.1.0#on-event",
|
||||||
"wasmExportInfoPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#info",
|
"wasmExportPopup:moonplasma:desktop/plasmoid@0.1.0#popup",
|
||||||
"wasmExportOnEvent:chandra:desktop/plasmoid@0.1.0#on-event",
|
"wasmExportPopupPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#popup",
|
||||||
"wasmExportPopup:chandra:desktop/plasmoid@0.1.0#popup",
|
"wasmExportRender:moonplasma:desktop/plasmoid@0.1.0#render",
|
||||||
"wasmExportPopupPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#popup",
|
"wasmExportRenderPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#render"
|
||||||
"wasmExportRender:chandra:desktop/plasmoid@0.1.0#render",
|
|
||||||
"wasmExportRenderPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#render",
|
|
||||||
"wasmExportSetConfig:chandra:desktop/module-config@0.1.0#set-config"
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "chandra/desktop/components/clock-digital/gen"
|
package "moonplasma/desktop/components/clock-digital/gen"
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
pub fn mbt_ffi_cabi_realloc(Int32, Int32, Int32, Int32) -> Int32
|
pub fn mbt_ffi_cabi_realloc(Int32, Int32, Int32, Int32) -> Int32
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn mbt_ffi_cabi_realloc(
|
|
||||||
src_offset : Int,
|
|
||||||
src_size : Int,
|
|
||||||
_dst_alignment : Int,
|
|
||||||
dst_size : Int,
|
|
||||||
) -> Int {
|
|
||||||
// malloc
|
|
||||||
if src_offset == 0 && src_size == 0 {
|
|
||||||
return mbt_ffi_malloc(dst_size)
|
|
||||||
}
|
|
||||||
// free
|
|
||||||
if dst_size == 0 {
|
|
||||||
mbt_ffi_free(src_offset)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
// realloc
|
|
||||||
let dst = mbt_ffi_malloc(dst_size)
|
|
||||||
mbt_ffi_copy(dst, src_offset, if src_size < dst_size { src_size } else { dst_size })
|
|
||||||
mbt_ffi_free(src_offset)
|
|
||||||
dst
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_copy(dest : Int, src : Int, len : Int) =
|
|
||||||
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRun(p0 : Int, p1 : Int) -> Int {
|
|
||||||
@compositor.wasmExportRun(p0, p1)
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/components/compositor/gen/world/compositor" @compositor,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
|
|
||||||
pkgtype(kind: "foreign_library")
|
|
||||||
|
|
||||||
options(
|
|
||||||
link: {
|
|
||||||
"wasm": {
|
|
||||||
"export-memory-name": "memory",
|
|
||||||
"heap-start-address": 16,
|
|
||||||
"exports": [
|
|
||||||
"mbt_ffi_cabi_realloc:cabi_realloc",
|
|
||||||
"wasmExportRun:run"
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRun(p0 : Int, p1 : Int) -> Int {
|
|
||||||
|
|
||||||
let array : Array[String] = [];
|
|
||||||
for index = 0; index < (p1); index = index + 1 {
|
|
||||||
let iter_base = (p0) + (index * 8)
|
|
||||||
|
|
||||||
let result = mbt_ffi_ptr2str(mbt_ffi_load32((iter_base) + 0), mbt_ffi_load32((iter_base) + 4))
|
|
||||||
|
|
||||||
array.push(result)
|
|
||||||
}
|
|
||||||
mbt_ffi_free(p0)
|
|
||||||
|
|
||||||
let (result0) : (Int) = run(array);
|
|
||||||
let ret = result0
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_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)
|
|
||||||
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/compositor/app" @app,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
///| The compositor component's entry point: everything lives in compositor/app.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn run(args : Array[String]) -> Int {
|
|
||||||
@app.run(args)
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
///|
|
|
||||||
/// Run the compositor until it exits. `args` are the command-line arguments (without argv[0]).
|
|
||||||
declare pub fn run(args : Array[String]) -> Int
|
|
||||||
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn mbt_ffi_cabi_realloc(
|
|
||||||
src_offset : Int,
|
|
||||||
src_size : Int,
|
|
||||||
_dst_alignment : Int,
|
|
||||||
dst_size : Int,
|
|
||||||
) -> Int {
|
|
||||||
// malloc
|
|
||||||
if src_offset == 0 && src_size == 0 {
|
|
||||||
return mbt_ffi_malloc(dst_size)
|
|
||||||
}
|
|
||||||
// free
|
|
||||||
if dst_size == 0 {
|
|
||||||
mbt_ffi_free(src_offset)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
// realloc
|
|
||||||
let dst = mbt_ffi_malloc(dst_size)
|
|
||||||
mbt_ffi_copy(dst, src_offset, if src_size < dst_size { src_size } else { dst_size })
|
|
||||||
mbt_ffi_free(src_offset)
|
|
||||||
dst
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_copy(dest : Int, src : Int, len : Int) =
|
|
||||||
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRender(p0 : Int) -> Int {
|
|
||||||
@decoration.wasmExportRender(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|
||||||
@decoration.wasmExportRenderPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,226 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRender(p0 : Int) -> Int {
|
|
||||||
|
|
||||||
let result = mbt_ffi_ptr2str(mbt_ffi_load32((p0) + 8), mbt_ffi_load32((p0) + 12))
|
|
||||||
|
|
||||||
let result0 = mbt_ffi_ptr2str(mbt_ffi_load32((p0) + 16), mbt_ffi_load32((p0) + 20))
|
|
||||||
|
|
||||||
let lifted : @types.Size? = match mbt_ffi_load8_u((p0) + 48) {
|
|
||||||
0 => Option::None
|
|
||||||
1 => {
|
|
||||||
|
|
||||||
Option::Some(@types.Size::{w : (mbt_ffi_load32((p0) + 52)).reinterpret_as_uint(), h : (mbt_ffi_load32((p0) + 56)).reinterpret_as_uint()})
|
|
||||||
}
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
|
|
||||||
let (result1) : (DecorationFrame) = render(@policy.WindowState::{id : (mbt_ffi_load64((p0) + 0)).reinterpret_as_uint64(), app_id : result, title : result0, geometry : @types.Rect::{x : mbt_ffi_load32((p0) + 24), y : mbt_ffi_load32((p0) + 28), w : (mbt_ffi_load32((p0) + 32)).reinterpret_as_uint(), h : (mbt_ffi_load32((p0) + 36)).reinterpret_as_uint()}, min_size : @types.Size::{w : (mbt_ffi_load32((p0) + 40)).reinterpret_as_uint(), h : (mbt_ffi_load32((p0) + 44)).reinterpret_as_uint()}, max_size : lifted, focused : (mbt_ffi_load8_u((p0) + 60) != 0), maximized : (mbt_ffi_load8_u((p0) + 61) != 0), fullscreen : (mbt_ffi_load8_u((p0) + 62) != 0), workspace : (mbt_ffi_load32((p0) + 64)).reinterpret_as_uint(), caps : @policy.WindowCaps(mbt_ffi_load8_u((p0) + 68).to_byte())}, @types.Size::{w : (mbt_ffi_load32((p0) + 72)).reinterpret_as_uint(), h : (mbt_ffi_load32((p0) + 76)).reinterpret_as_uint()});
|
|
||||||
mbt_ffi_free(p0)
|
|
||||||
let return_area = mbt_ffi_malloc(32)
|
|
||||||
mbt_ffi_store32((return_area) + 0, (((result1).insets).0).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((return_area) + 4, (((result1).insets).1).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((return_area) + 8, (((result1).insets).2).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((return_area) + 12, (((result1).insets).3).reinterpret_as_int())
|
|
||||||
|
|
||||||
let address = mbt_ffi_malloc(((result1).ops).length() * 32);
|
|
||||||
for index = 0; index < ((result1).ops).length(); index = index + 1 {
|
|
||||||
let iter_elem : @types.DrawOp = ((result1).ops)[(index)]
|
|
||||||
let iter_base = address + (index * 32);
|
|
||||||
|
|
||||||
match iter_elem {
|
|
||||||
FillRect(payload) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (0))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload).radius).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
StrokeRect(payload2) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (1))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload2).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload2).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload2).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload2).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload2).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload2).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload2).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload2).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload2).width).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 28, ((payload2).radius).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Line(payload3) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (2))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload3).start).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload3).start).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload3).end).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 16, ((payload3).end).y)
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (((payload3).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 21, (((payload3).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 22, (((payload3).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 23, (((payload3).color).a).to_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 24, ((payload3).width).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Circle(payload4) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (3))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload4).center).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload4).center).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload4).radius).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 16, (((payload4).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 17, (((payload4).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 18, (((payload4).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 19, (((payload4).color).a).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 20, (if (payload4).filled { 1 } else { 0 }))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Text(payload5) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (4))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload5).origin).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload5).origin).y)
|
|
||||||
|
|
||||||
let ptr = mbt_ffi_str2ptr((payload5).text)
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (payload5).text.length())
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ptr)
|
|
||||||
mbt_ffi_store32((iter_base) + 20, (((payload5).style).size).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 24, ((((payload5).style).color).r).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 25, ((((payload5).style).color).g).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 26, ((((payload5).style).color).b).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 27, ((((payload5).style).color).a).to_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 28, ((payload5).style).align.ordinal())
|
|
||||||
mbt_ffi_store8((iter_base) + 29, (if ((payload5).style).bold { 1 } else { 0 }))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
PushClip(payload6) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (5))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, (payload6).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, (payload6).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, ((payload6).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, ((payload6).h).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
PopClip => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (6))
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
Image(payload8) => {
|
|
||||||
mbt_ffi_store8((iter_base) + 0, (7))
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((payload8).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, ((payload8).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((payload8).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 16, (((payload8).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 20, ((payload8).image).reinterpret_as_int())
|
|
||||||
|
|
||||||
()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 20, ((result1).ops).length())
|
|
||||||
mbt_ffi_store32((return_area) + 16, address)
|
|
||||||
|
|
||||||
let address9 = mbt_ffi_malloc(((result1).hit_regions).length() * 20);
|
|
||||||
for index10 = 0; index10 < ((result1).hit_regions).length(); index10 = index10 + 1 {
|
|
||||||
let iter_elem : HitRegion = ((result1).hit_regions)[(index10)]
|
|
||||||
let iter_base = address9 + (index10 * 20);
|
|
||||||
mbt_ffi_store32((iter_base) + 0, ((iter_elem).rect).x)
|
|
||||||
mbt_ffi_store32((iter_base) + 4, ((iter_elem).rect).y)
|
|
||||||
mbt_ffi_store32((iter_base) + 8, (((iter_elem).rect).w).reinterpret_as_int())
|
|
||||||
mbt_ffi_store32((iter_base) + 12, (((iter_elem).rect).h).reinterpret_as_int())
|
|
||||||
mbt_ffi_store8((iter_base) + 16, (iter_elem).role.ordinal())
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_store32((return_area) + 28, ((result1).hit_regions).length())
|
|
||||||
mbt_ffi_store32((return_area) + 24, address9)
|
|
||||||
let ret = return_area
|
|
||||||
return ret
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|
||||||
|
|
||||||
for index = 0; index < (mbt_ffi_load32((p0) + 20)); index = index + 1 {
|
|
||||||
let iter_base = (mbt_ffi_load32((p0) + 16)) + (index * 32)
|
|
||||||
|
|
||||||
match (mbt_ffi_load8_u((iter_base) + 0)) {
|
|
||||||
0 => ()
|
|
||||||
1 => ()
|
|
||||||
2 => ()
|
|
||||||
3 => ()
|
|
||||||
4 => {
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((iter_base) + 12))
|
|
||||||
|
|
||||||
}
|
|
||||||
5 => ()
|
|
||||||
6 => ()
|
|
||||||
7 => ()
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 16))
|
|
||||||
mbt_ffi_free(mbt_ffi_load32((p0) + 24))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_load64(offset : Int) -> Int64 =
|
|
||||||
#|(func (param i32) (result i64) local.get 0 i64.load)
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_store8(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
|
||||||
|
|
||||||
///|
|
|
||||||
extern "wasm" fn mbt_ffi_free(position : Int) =
|
|
||||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
|
||||||
|
|
||||||
///|
|
|
||||||
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_store32(offset : Int, value : Int) =
|
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
import {
|
|
||||||
"chandra/desktop/interface/chandra/desktop/types" @types,
|
|
||||||
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
|
||||||
"chandra/desktop/interface/chandra/wm/policy" @policy,
|
|
||||||
"chandra/desktop/lib/ui" @ui,
|
|
||||||
}
|
|
||||||
|
|
||||||
warnings = "-44"
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
|
||||||
package "chandra/desktop/components/decoration-breeze/gen/interface/chandra/wm/decoration"
|
|
||||||
|
|
||||||
import {
|
|
||||||
"chandra/desktop/interface/chandra/desktop/types",
|
|
||||||
"chandra/desktop/interface/chandra/wm/policy",
|
|
||||||
}
|
|
||||||
|
|
||||||
// Values
|
|
||||||
pub fn render(@policy.WindowState, @types.Size) -> DecorationFrame
|
|
||||||
|
|
||||||
// Errors
|
|
||||||
|
|
||||||
// Types and methods
|
|
||||||
pub(all) struct DecorationFrame {
|
|
||||||
insets : (UInt32, UInt32, UInt32, UInt32)
|
|
||||||
ops : Array[@types.DrawOp]
|
|
||||||
hit_regions : Array[HitRegion]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(all) struct HitRegion {
|
|
||||||
rect : @types.Rect
|
|
||||||
role : HitRole
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(all) enum HitRole {
|
|
||||||
NONE
|
|
||||||
TITLEBAR
|
|
||||||
CLOSE
|
|
||||||
MAXIMIZE
|
|
||||||
MINIMIZE
|
|
||||||
RESIZE_N
|
|
||||||
RESIZE_S
|
|
||||||
RESIZE_E
|
|
||||||
RESIZE_W
|
|
||||||
RESIZE_NW
|
|
||||||
RESIZE_NE
|
|
||||||
RESIZE_SW
|
|
||||||
RESIZE_SE
|
|
||||||
}
|
|
||||||
pub fn HitRole::from(Int32) -> Self
|
|
||||||
pub fn HitRole::ordinal(Self) -> Int32
|
|
||||||
|
|
||||||
// Type aliases
|
|
||||||
|
|
||||||
// Traits
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
///| `chandra:wm/decoration` Breeze-inspired server-side decoration component.
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn render(
|
|
||||||
win : @policy.WindowState,
|
|
||||||
outer_size : @types.Size,
|
|
||||||
) -> DecorationFrame {
|
|
||||||
if win.fullscreen {
|
|
||||||
return { insets: (0U, 0U, 0U, 0U), ops: [], hit_regions: [] }
|
|
||||||
}
|
|
||||||
let t = @theme.get_tokens()
|
|
||||||
let p = t.palette
|
|
||||||
let w = outer_size.w.reinterpret_as_int()
|
|
||||||
let h = outer_size.h.reinterpret_as_int()
|
|
||||||
let title_h = 32
|
|
||||||
let border = 2
|
|
||||||
let radius = t.radius.reinterpret_as_int()
|
|
||||||
|
|
||||||
let title_bg = if win.focused { p.surface } else { @ui.with_alpha(p.surface, 210) }
|
|
||||||
let border_col = if win.focused { p.accent } else { p.border }
|
|
||||||
let title_fg = if win.focused { p.foreground } else { p.foreground_muted }
|
|
||||||
|
|
||||||
let ops : Array[@types.DrawOp] = [
|
|
||||||
// Titlebar + outer frame background
|
|
||||||
@ui.fill_rect(@ui.rect(0, 0, w, title_h), title_bg, radius=radius),
|
|
||||||
@ui.stroke_rect(@ui.rect(0, 0, w, h), border_col, width=border, radius=radius),
|
|
||||||
// Subtle separator under titlebar
|
|
||||||
@ui.line(border, title_h - 1, w - border, title_h - 1, p.border, width=1),
|
|
||||||
]
|
|
||||||
|
|
||||||
let label = if win.title.length() > 0 { win.title } else { win.app_id }
|
|
||||||
ops.push(
|
|
||||||
@ui.text(
|
|
||||||
14,
|
|
||||||
title_h / 2,
|
|
||||||
label,
|
|
||||||
t.font_size.reinterpret_as_int(),
|
|
||||||
title_fg,
|
|
||||||
align=LEFT,
|
|
||||||
bold=win.focused,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Window control buttons on the right: [ _ ] [ [] ] [ X ]
|
|
||||||
let btn_size = 18
|
|
||||||
let btn_y = (title_h - btn_size) / 2
|
|
||||||
let close_x = w - 10 - btn_size
|
|
||||||
let max_x = close_x - 8 - btn_size
|
|
||||||
let min_x = max_x - 8 - btn_size
|
|
||||||
|
|
||||||
let close_bg = if win.focused { @ui.color(218, 68, 83, 255) } else { p.surface_hover }
|
|
||||||
let btn_bg = p.surface_hover
|
|
||||||
|
|
||||||
ops.push(@ui.fill_rect(@ui.rect(min_x, btn_y, btn_size, btn_size), btn_bg, radius=9))
|
|
||||||
ops.push(@ui.line(min_x + 5, btn_y + btn_size - 6, min_x + btn_size - 5, btn_y + btn_size - 6, title_fg, width=2))
|
|
||||||
|
|
||||||
ops.push(@ui.fill_rect(@ui.rect(max_x, btn_y, btn_size, btn_size), btn_bg, radius=9))
|
|
||||||
ops.push(@ui.stroke_rect(@ui.rect(max_x + 5, btn_y + 5, btn_size - 10, btn_size - 10), title_fg, width=1))
|
|
||||||
|
|
||||||
ops.push(@ui.fill_rect(@ui.rect(close_x, btn_y, btn_size, btn_size), close_bg, radius=9))
|
|
||||||
ops.push(@ui.line(close_x + 5, btn_y + 5, close_x + btn_size - 5, btn_y + btn_size - 5, p.accent_foreground, width=2))
|
|
||||||
ops.push(@ui.line(close_x + btn_size - 5, btn_y + 5, close_x + 5, btn_y + btn_size - 5, p.accent_foreground, width=2))
|
|
||||||
|
|
||||||
let hit_regions : Array[HitRegion] = [
|
|
||||||
{ rect: @ui.rect(close_x, btn_y, btn_size, btn_size), role: CLOSE },
|
|
||||||
{ rect: @ui.rect(max_x, btn_y, btn_size, btn_size), role: MAXIMIZE },
|
|
||||||
{ rect: @ui.rect(min_x, btn_y, btn_size, btn_size), role: MINIMIZE },
|
|
||||||
{ rect: @ui.rect(border, border, w - 2 * border, title_h - border), role: TITLEBAR },
|
|
||||||
{ rect: @ui.rect(0, 0, 8, 8), role: RESIZE_NW },
|
|
||||||
{ rect: @ui.rect(w - 8, 0, 8, 8), role: RESIZE_NE },
|
|
||||||
{ rect: @ui.rect(0, h - 8, 8, 8), role: RESIZE_SW },
|
|
||||||
{ rect: @ui.rect(w - 8, h - 8, 8, 8), role: RESIZE_SE },
|
|
||||||
{ rect: @ui.rect(8, 0, w - 16, 4), role: RESIZE_N },
|
|
||||||
{ rect: @ui.rect(8, h - 4, w - 16, 4), role: RESIZE_S },
|
|
||||||
{ rect: @ui.rect(0, 8, 4, h - 16), role: RESIZE_W },
|
|
||||||
{ rect: @ui.rect(w - 4, 8, 4, h - 16), role: RESIZE_E },
|
|
||||||
]
|
|
||||||
|
|
||||||
{
|
|
||||||
insets: (title_h.reinterpret_as_uint(), border.reinterpret_as_uint(), border.reinterpret_as_uint(), border.reinterpret_as_uint()),
|
|
||||||
ops,
|
|
||||||
hit_regions,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
|
||||||
///|
|
|
||||||
pub(all) enum HitRole {
|
|
||||||
NONE; TITLEBAR; CLOSE; MAXIMIZE; MINIMIZE; RESIZE_N; RESIZE_S; RESIZE_E; RESIZE_W; RESIZE_NW; RESIZE_NE; RESIZE_SW; RESIZE_SE
|
|
||||||
} derive()
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn HitRole::ordinal(self : HitRole) -> Int {
|
|
||||||
match self {
|
|
||||||
NONE => 0
|
|
||||||
TITLEBAR => 1
|
|
||||||
CLOSE => 2
|
|
||||||
MAXIMIZE => 3
|
|
||||||
MINIMIZE => 4
|
|
||||||
RESIZE_N => 5
|
|
||||||
RESIZE_S => 6
|
|
||||||
RESIZE_E => 7
|
|
||||||
RESIZE_W => 8
|
|
||||||
RESIZE_NW => 9
|
|
||||||
RESIZE_NE => 10
|
|
||||||
RESIZE_SW => 11
|
|
||||||
RESIZE_SE => 12
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
pub fn HitRole::from(self : Int) -> HitRole {
|
|
||||||
match self {
|
|
||||||
0 => NONE
|
|
||||||
1 => TITLEBAR
|
|
||||||
2 => CLOSE
|
|
||||||
3 => MAXIMIZE
|
|
||||||
4 => MINIMIZE
|
|
||||||
5 => RESIZE_N
|
|
||||||
6 => RESIZE_S
|
|
||||||
7 => RESIZE_E
|
|
||||||
8 => RESIZE_W
|
|
||||||
9 => RESIZE_NW
|
|
||||||
10 => RESIZE_NE
|
|
||||||
11 => RESIZE_SW
|
|
||||||
12 => RESIZE_SE
|
|
||||||
_ => panic()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
///|
|
|
||||||
pub(all) struct HitRegion {
|
|
||||||
rect : @types.Rect; role : HitRole
|
|
||||||
}
|
|
||||||
///|
|
|
||||||
pub(all) struct DecorationFrame {
|
|
||||||
insets : (UInt, UInt, UInt, UInt); ops : Array[@types.DrawOp]; hit_regions : Array[HitRegion]
|
|
||||||
}
|
|
||||||
///|
|
|
||||||
declare pub fn render(win : @policy.WindowState, outer_size : @types.Size) -> DecorationFrame
|
|
||||||
|
|
||||||