Compare commits
174
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcd7751f1a | ||
|
|
7912a0b32b | ||
|
|
44688453d7 | ||
|
|
27252a97ab | ||
|
|
738928352e | ||
|
|
2752b81e36 | ||
|
|
70b24ffe47 | ||
|
|
50149e6b99 | ||
|
|
77fcc4db1c | ||
|
|
76a23f7816 | ||
|
|
80af76fb15 | ||
|
|
2c33c4a847 | ||
|
|
516180196f | ||
|
|
ff6f733810 | ||
|
|
db442879f5 | ||
|
|
41c6ec1135 | ||
|
|
024f18ce61 | ||
|
|
14acc5d42e | ||
|
|
2c2ce3f913 | ||
|
|
f2b2618104 | ||
|
|
2960005139 | ||
|
|
02c831576a | ||
|
|
1154437050 | ||
|
|
f556df2f4b | ||
|
|
fa932f9ac0 | ||
|
|
b4f7149c72 | ||
|
|
f0c39757d7 | ||
|
|
63ce79fa7c | ||
|
|
9f7489cfea | ||
|
|
0a9787ae36 | ||
|
|
81cb8f9a1c | ||
|
|
d521d0d840 | ||
|
|
4920f505e5 | ||
|
|
e184e29600 | ||
|
|
ae48cc5bf4 | ||
|
|
71f0b858d6 | ||
|
|
1aabccba5d | ||
|
|
01bd8281d5 | ||
|
|
5403777451 | ||
|
|
3e35c4dfa5 | ||
|
|
e6be3825d0 | ||
|
|
90fdda2455 | ||
|
|
b097116ded | ||
|
|
fa634956cc | ||
|
|
ac2d0fa205 | ||
|
|
932fca627e | ||
|
|
0f71779819 | ||
|
|
30f5e88639 | ||
|
|
acf2daa399 | ||
|
|
0feea23a06 | ||
|
|
2a67bc83d4 | ||
|
|
6253371cb3 | ||
|
|
42aa770452 | ||
|
|
114f0b4e04 | ||
|
|
da56f59cf7 | ||
|
|
4437a3a576 | ||
|
|
c45e0ef431 | ||
|
|
0ce0de0356 | ||
|
|
326dd06f30 | ||
|
|
bb7c9b947f | ||
|
|
442f9477b4 | ||
|
|
99c6c66726 | ||
|
|
68164ff75e | ||
|
|
0fc2598d27 | ||
|
|
a215dd6bf7 | ||
|
|
10659e7655 | ||
|
|
34885bfdda | ||
|
|
8ecdef17d4 | ||
|
|
c1936299cb | ||
|
|
005e11e7bf | ||
|
|
c7a6c8376e | ||
|
|
9c0a794c2d | ||
|
|
f844ac66ff | ||
|
|
f33f84c35b | ||
|
|
2774516abf | ||
|
|
b4b9ba0e10 | ||
|
|
caa12058fd | ||
|
|
3b15248124 | ||
|
|
1f4b686087 | ||
|
|
88915842c2 | ||
|
|
05fe70a2f6 | ||
|
|
9431254de0 | ||
|
|
f52a5ff40e | ||
|
|
2f4ce827c3 | ||
|
|
2636373a4f | ||
|
|
ba82d3c59b | ||
|
|
2d9f4e34c5 | ||
|
|
c32a31ffd7 | ||
|
|
51b914f6cf | ||
|
|
8f88d93d9f | ||
|
|
85f6ff42f4 | ||
|
|
b33373d801 | ||
|
|
3454619162 | ||
|
|
4fc4a963b4 | ||
|
|
a7909014b8 | ||
|
|
f2e870c251 | ||
|
|
0e6a1257e1 | ||
|
|
ee019ecbaa | ||
|
|
a0b878af23 | ||
|
|
c169489d44 | ||
|
|
d4d099a0e4 | ||
|
|
9a25d70f77 | ||
|
|
46780c399b | ||
|
|
f7f90e1c41 | ||
|
|
ab01688778 | ||
|
|
c78afbd29e | ||
|
|
af774d76fe | ||
|
|
5dfe34027b | ||
|
|
7248e0ca6e | ||
|
|
0f4c84b966 | ||
|
|
11d7e4d276 | ||
|
|
d0d375c071 | ||
|
|
63e63c65e0 | ||
|
|
c6e31b4a8f | ||
|
|
66bf293085 | ||
|
|
70ad87f2d2 | ||
|
|
095bbc5906 | ||
|
|
235f17cd70 | ||
|
|
4b99b0063e | ||
|
|
69a163bc08 | ||
|
|
61796b9f69 | ||
|
|
b50e79acaa | ||
|
|
0ba975da67 | ||
|
|
c03d4c6c17 | ||
|
|
95b7275b3a | ||
|
|
e769566d8f | ||
|
|
5c3bebc7fd | ||
|
|
0b9af11232 | ||
|
|
4ff933ce54 | ||
|
|
6aba9b6a4f | ||
|
|
92d2b29889 | ||
|
|
1bbdf58372 | ||
|
|
a0872c9b40 | ||
|
|
69a1a0738c | ||
|
|
a9417fbccf | ||
|
|
8a3ea16b1c | ||
|
|
306a7c121e | ||
|
|
f05677178a | ||
|
|
95a21a0b84 | ||
|
|
82cac8a915 | ||
|
|
df1f37ee78 | ||
|
|
6fc921b431 | ||
|
|
79321790a1 | ||
|
|
bf07af2c43 | ||
|
|
bc1e3dfdbd | ||
|
|
3b2e19ace5 | ||
|
|
200b02d0f1 | ||
|
|
bca69d8c90 | ||
|
|
8724805a72 | ||
|
|
783618b89f | ||
|
|
0145981c6a | ||
|
|
080377fd79 | ||
|
|
50442a7e30 | ||
|
|
b2557aca52 | ||
|
|
5b32cf9808 | ||
|
|
7a37039673 | ||
|
|
49e91b4c01 | ||
|
|
57d5376211 | ||
|
|
7154312919 | ||
|
|
ee698c9846 | ||
|
|
9e7687bea8 | ||
|
|
9b31a7c476 | ||
|
|
abdc5de8e3 | ||
|
|
59ea224d38 | ||
|
|
a63f65806d | ||
|
|
a1b85474f8 | ||
|
|
a265eebf02 | ||
|
|
7caadb3f7f | ||
|
|
ccb809bdc3 | ||
|
|
769ab829c8 | ||
|
|
3cb8417699 | ||
|
|
ae4f3fa93c | ||
|
|
17a8a46cfd | ||
|
|
880ecd4102 |
No files matched your search
+16
-1
@@ -6,6 +6,13 @@ 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
|
||||||
@@ -20,8 +27,16 @@ 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 wasm-tools, wit-bindgen, wasmtime
|
- name: Install native compositor build dependencies
|
||||||
|
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,3 +5,12 @@ 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,88 +1,239 @@
|
|||||||
# moon-plasma
|
# Chandra
|
||||||
|
|
||||||
A modular, KDE-Plasma-inspired desktop environment where **every part is a
|
A modular, KDE-Plasma-inspired Wayland desktop where **every part is a WebAssembly component**
|
||||||
WebAssembly component** (Component Model, WIT interfaces) written in
|
(Component Model, WIT interfaces) written in [MoonBit](https://www.moonbitlang.com), on top of a
|
||||||
[MoonBit](https://www.moonbitlang.com), on top of a deliberately tiny trusted host.
|
deliberately small host. The compositor itself (Wayland server, window manager, scene, shell) is a
|
||||||
|
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.
|
||||||
|
|
||||||
*Headless-Chromium capture of the M0 browser host: the shell, the launcher and the clock
|
Repository: <https://git.site.latha.org/experiments/chandra>
|
||||||
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,
|
Plasma's strength is its decomposition (shell, widgets, window manager, runner, settings modules,
|
||||||
settings modules, notifications, tray). moon-plasma keeps that decomposition and
|
notifications, tray). Chandra keeps that decomposition and makes each seam a **typed WIT
|
||||||
makes each seam a **typed WIT interface** with **deny-by-default capabilities**:
|
interface** with **deny-by-default capabilities**:
|
||||||
|
|
||||||
* the **host** only owns engine/lifecycle, devices, the capability broker and the
|
* the **host** only owns the wasm engine, devices (sockets, files, DRM/KMS, libinput), the
|
||||||
render-list rasteriser;
|
capability broker and the rasteriser;
|
||||||
* **everything else is a component**: swap the clock for another implementation,
|
* **everything else is a component**: swap the clock for another implementation, swap the shell,
|
||||||
swap the whole shell, change the theme, without rebuilding the rest;
|
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 + the desktop config don't grant.
|
* components can't touch anything their imports and the desktop config don't grant.
|
||||||
|
|
||||||
Read [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) (Plasma→component mapping, host
|
Design: [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md) (Plasma to component mapping, permissions,
|
||||||
decision, rendering, input, permissions, versioning, roadmap M0–M8) and
|
versioning) and [docs/compositor-design.md](docs/compositor-design.md) (the compositor and its
|
||||||
[docs/TOOLCHAIN.md](docs/TOOLCHAIN.md) (what works today, exact versions).
|
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/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).
|
||||||
|
|
||||||
## Status: M0 (proof of concept)
|
## How it runs
|
||||||
|
|
||||||
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
|
||||||
|
```
|
||||||
|
|
||||||
* WIT package `moonplasma:desktop@0.1.0` (`wit/`): worlds `widget`, `clock-widget`,
|
The **MoonBit host** ([wasmhost/](wasmhost/README.md)) is the only host. It links
|
||||||
`launcher-widget`, `panel`, `shell`.
|
`libwasmtime` from the upstream C API release instead of compiling wasmtime, so a cold build takes
|
||||||
* Four MoonBit components, each an independently built `.wasm` component of 24–33 KB:
|
about 4 seconds. (An earlier host in Rust on the wasmtime crate, `host-wasm/`, was removed on
|
||||||
`clock-digital`, `clock-binary` (drop-in replacement), `launcher` (kickoff-like with
|
2026-11-26.) A **native build** of the same
|
||||||
keyboard search), `shell` (desktop background + bottom panel layout + theme tokens).
|
compositor code (no wasm, `just run-native`) is used by the test suite.
|
||||||
* 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.
|
|
||||||
|
|
||||||
Stubbed / not done (see ARCHITECTURE §10): no window manager/compositor, search runner,
|
The compositor runs **nested**, as a window on your current Wayland session, as a **login
|
||||||
notifications, settings, tray, session, shortcuts, clipboard, files; `launch` only logs;
|
session** on DRM/KMS with libinput (`--drm`, through libseat/logind), or **on an X server**
|
||||||
host does input hit-testing; fixed-advance text metrics, no real fonts/icons; native
|
(`--x11`: a window under an X window manager, or the whole screen without one), for NVIDIA's
|
||||||
wasmtime host is M3; no CPU/fuel limits under jco.
|
proprietary driver on Xorg ([docs/x11.md](docs/x11.md)).
|
||||||
|
|
||||||
## Quickstart
|
## Quickstart
|
||||||
|
|
||||||
Requirements: Linux x86_64, Node ≥ 20, [MoonBit](https://www.moonbitlang.com/download).
|
Requirements: Linux x86_64, [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"
|
||||||
scripts/setup-toolchain.sh # wasm-tools, wit-bindgen, wasmtime -> ./.tools (no root)
|
moon update # fetch mooncakes dependencies
|
||||||
(cd host && npm ci) # jco
|
scripts/setup-toolchain.sh # wasm-tools, wit-bindgen, wasmtime + its C API -> ./.tools (no root)
|
||||||
scripts/build.sh # MoonBit -> components -> build/components, build/jco
|
|
||||||
scripts/test.sh # everything CI runs
|
|
||||||
|
|
||||||
node host/src/cli.mjs --time 2026-11-24T14:07:00 --click-launcher # terminal render
|
just run # build compositor.wasm, wasmhost and the modules; open in a window
|
||||||
node host/serve.mjs 8080 # browser host: http://127.0.0.1:8080/
|
just client # start foot inside it (or: just client konsole)
|
||||||
node host/serve.mjs & # then try ?config=desktop.binary-clock.json or ?theme=moon-day
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Swap the clock **without rebuilding anything else**:
|
| Recipe | What it does |
|
||||||
|
|---|---|
|
||||||
|
| `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 |
|
||||||
|
|
||||||
```bash
|
`build/wasmhost build/compositor.wasm --socket PATH` takes `--window` (nested), `--drm` (login
|
||||||
node host/src/cli.mjs --config config/desktop.binary-clock.json
|
session), `--x11` (on Xorg; `--fullscreen` under a window manager, `--login-session` to act as the
|
||||||
scripts/build.sh clock-binary # builds just that component
|
desktop session), `--theme PATH`, `--no-restore`, `--profile`, `--size WxH` and `--gfx-out frame.png`
|
||||||
# in the browser console: await moonPlasma.swap('clock', 'clock-binary')
|
(render to a PNG, headless; add `--serve` to keep refreshing it). `WASMHOST_PROFILE=1` prints the
|
||||||
```
|
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)).
|
||||||
|
|
||||||
Screenshots: see the end of [docs/TOOLCHAIN.md](docs/TOOLCHAIN.md).
|
`CHANDRA_DEBUG_CONFIGURE=1` (any host) logs each window's configures, acks and committed buffer
|
||||||
|
sizes, for an app that draws at the wrong size.
|
||||||
|
|
||||||
|
The installed session logs to `~/.local/state/chandra/session.log`. 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/ the WIT package (host-agnostic API)
|
wit/ WIT packages: chandra:desktop (components) and chandra:host (wit/host, what hosts serve)
|
||||||
interface/ world/ generated shared MoonBit bindings (wit-bindgen; committed)
|
compositor/ the compositor in MoonBit: wayland/, shell/, scene/, raster/, kms/, network/, portal/, host/, ...
|
||||||
components/<n>/gen/ generated export bindings + hand-written thin stub.mbt
|
components/<n>/ one wasm component each: compositor, shell, panel modules (gen/ bindings + thin stub.mbt)
|
||||||
lib/ pure, unit-tested logic (ui helpers, panel layout, launcher state machine, ...)
|
lib/ pure, unit-tested logic (panel layout, clock, launcher, battery, task pills, ...)
|
||||||
host/ JS host: src/desktop.mjs (core), loaders, renderers, tests, web/ (browser)
|
wasmhost/ the host (MoonBit + wasmtime C API)
|
||||||
config/ desktop configs (grants!), themes, static app list
|
host/ the original M0 browser/Node host (jco), kept for the component demos
|
||||||
scripts/ setup-toolchain, gen-bindings, build, test, screenshot
|
session/ chandra-session, chandra-root, chandra-ctl, chandra.desktop, chandra.portal, install.py
|
||||||
docs/ ARCHITECTURE, TOOLCHAIN, screenshots
|
config/ desktop configs, themes, app list
|
||||||
|
scripts/ setup-toolchain, build-*, gen-bindings, as-root, remote-build, test
|
||||||
|
docs/ ARCHITECTURE, compositor-design, TOOLCHAIN, run0, dark-mode, reloading, screenshots
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,96 @@
|
|||||||
|
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.
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
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.
|
||||||
@@ -0,0 +1,274 @@
|
|||||||
|
// #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
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
// 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()
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
// 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()))
|
||||||
|
}
|
||||||
@@ -0,0 +1,658 @@
|
|||||||
|
// 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
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
// 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()
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
// 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"
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
// 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 => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
// 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()
|
||||||
|
}
|
||||||
@@ -0,0 +1,269 @@
|
|||||||
|
// 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()
|
||||||
|
}
|
||||||
@@ -0,0 +1,964 @@
|
|||||||
|
///|
|
||||||
|
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 => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import {
|
||||||
|
"moonbitlang/core/deque" @deque,
|
||||||
|
"moonbitlang/core/ref" @ref,
|
||||||
|
"moonbitlang/core/set" @set,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
|
supported_targets = "wasm"
|
||||||
@@ -0,0 +1,148 @@
|
|||||||
|
// 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
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
// 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 wasmExportPopup() -> Int {
|
||||||
|
@plasmoid.wasmExportPopup()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#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 wasmExportInfo() -> Int {
|
||||||
|
@plasmoid.wasmExportInfo()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportRenderPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportInfoPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
||||||
|
@module-config.wasmExportConfigItemsPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportRender(p0, p1)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportPopupPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
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.
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
// 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_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)
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/lib/battery" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// 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
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
///| 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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
File renamed without changes.
+40
-18
@@ -130,6 +130,16 @@ 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())
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
|
7 => ()
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,6 +289,16 @@ 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())
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
|
7 => ()
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,16 +391,28 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
|
extern "wasm" fn mbt_ffi_free(position : Int) =
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
|
#|(func (param i32) local.get 0 call $moonbit.decref)
|
||||||
|
|
||||||
|
///|
|
||||||
|
#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) =
|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
|
||||||
#|(func (param i32) local.get 0 call $moonbit.decref)
|
#|(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_malloc(size : Int) -> Int =
|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
||||||
@@ -388,22 +422,10 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
|||||||
#| local.get 1)
|
#| local.get 1)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#owned(str)
|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
|
||||||
#|(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 =
|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
#|(func (param i32) (result i32) local.get 0 i32.load)
|
||||||
|
|
||||||
///|
|
|
||||||
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 {
|
||||||
|
"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"
|
||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "moonplasma/desktop/components/launcher/gen/interface/moonplasma/desktop/plasmoid"
|
package "chandra/desktop/components/battery/gen/interface/chandra/desktop/plasmoid"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
"moonplasma/desktop/interface/moonplasma/desktop/types",
|
"chandra/desktop/interface/chandra/desktop/types",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
///| 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
|
||||||
|
}
|
||||||
|
}
|
||||||
File renamed without changes.
@@ -0,0 +1,30 @@
|
|||||||
|
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"
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// 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
|
||||||
@@ -50,24 +50,30 @@ 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)
|
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportRenderPostReturn(p0)
|
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportPopup() -> Int {
|
pub fn wasmExportPopup() -> Int {
|
||||||
@plasmoid.wasmExportPopup()
|
@plasmoid.wasmExportPopup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportConfigItems() -> Int {
|
||||||
|
@module-config.wasmExportConfigItems()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#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 wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
||||||
@@ -76,7 +82,19 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
||||||
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
@plasmoid.wasmExportInfoPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
||||||
|
@module-config.wasmExportConfigItemsPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportRenderPostReturn(p0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
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.
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
// 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_store32(offset : Int, value : Int) =
|
||||||
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_free(position : Int) =
|
||||||
|
#|(func (param i32) local.get 0 call $moonbit.decref)
|
||||||
|
|
||||||
|
///|
|
||||||
|
#owned(str)
|
||||||
|
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||||
|
#|(func (param i32) (result i32) local.get 0)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
||||||
|
#|(func (param i32) (result i32) local.get 0 i32.load)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
||||||
|
#|(func (param i32) (result i32) (local i32)
|
||||||
|
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
|
||||||
|
#| local.tee 1 i32.const 0 call $moonbit.init_array8
|
||||||
|
#| local.get 1)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
|
||||||
|
#|(func (param i32) (param i32) (result i32)
|
||||||
|
#| local.get 0
|
||||||
|
#| local.get 1 call $moonbit.init_array16
|
||||||
|
#| local.get 0)
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/lib/clock" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
// 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
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
///| 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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
File renamed without changes.
+43
-21
@@ -130,6 +130,16 @@ 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())
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
|
7 => ()
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,6 +289,16 @@ 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())
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
|
7 => ()
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,10 +390,25 @@ pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
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 =
|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
#|(func (param i32) (result i32) local.get 0 i32.load)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
|
||||||
|
#|(func (param i32) (param i32) (result i32)
|
||||||
|
#| local.get 0
|
||||||
|
#| local.get 1 call $moonbit.init_array16
|
||||||
|
#| local.get 0)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
|
||||||
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
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)
|
||||||
@@ -379,31 +416,16 @@ 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)
|
#owned(str)
|
||||||
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||||
#|(func (param i32) (result i32) local.get 0)
|
#|(func (param i32) (result i32) local.get 0)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
|
||||||
#|(func (param i32) (param i32) (result i32)
|
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
|
||||||
#| 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)
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
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"
|
||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "moonplasma/desktop/components/clock-binary/gen/interface/moonplasma/desktop/plasmoid"
|
package "chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/plasmoid"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
"moonplasma/desktop/interface/moonplasma/desktop/types",
|
"chandra/desktop/interface/chandra/desktop/types",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
///| 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())
|
||||||
|
}
|
||||||
File renamed without changes.
@@ -1,8 +0,0 @@
|
|||||||
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,29 +0,0 @@
|
|||||||
///| 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,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
"moonplasma/desktop/components/clock-binary/gen/interface/moonplasma/desktop/plasmoid" @plasmoid,
|
"chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/module-config" @module-config,
|
||||||
|
"chandra/desktop/components/clock-binary/gen/interface/chandra/desktop/plasmoid" @plasmoid,
|
||||||
}
|
}
|
||||||
|
|
||||||
warnings = "-44"
|
warnings = "-44"
|
||||||
@@ -13,13 +14,16 @@ options(
|
|||||||
"heap-start-address": 16,
|
"heap-start-address": 16,
|
||||||
"exports": [
|
"exports": [
|
||||||
"mbt_ffi_cabi_realloc:cabi_realloc",
|
"mbt_ffi_cabi_realloc:cabi_realloc",
|
||||||
"wasmExportInfo:moonplasma:desktop/plasmoid@0.1.0#info",
|
"wasmExportConfigItems:chandra:desktop/module-config@0.1.0#config-items",
|
||||||
"wasmExportInfoPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#info",
|
"wasmExportConfigItemsPostReturn:cabi_post_chandra:desktop/module-config@0.1.0#config-items",
|
||||||
"wasmExportOnEvent:moonplasma:desktop/plasmoid@0.1.0#on-event",
|
"wasmExportInfo:chandra:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportPopup:moonplasma:desktop/plasmoid@0.1.0#popup",
|
"wasmExportInfoPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportPopupPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#popup",
|
"wasmExportOnEvent:chandra:desktop/plasmoid@0.1.0#on-event",
|
||||||
"wasmExportRender:moonplasma:desktop/plasmoid@0.1.0#render",
|
"wasmExportPopup:chandra:desktop/plasmoid@0.1.0#popup",
|
||||||
"wasmExportRenderPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#render"
|
"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,5 +1,5 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "moonplasma/desktop/components/clock-binary/gen"
|
package "chandra/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
|
||||||
|
|||||||
@@ -44,6 +44,18 @@ pub fn wasmExportInfo() -> Int {
|
|||||||
@plasmoid.wasmExportInfo()
|
@plasmoid.wasmExportInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportConfigItems() -> Int {
|
||||||
|
@module-config.wasmExportConfigItems()
|
||||||
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
||||||
@@ -52,14 +64,8 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
pub fn wasmExportSetConfig(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
||||||
@plasmoid.wasmExportInfoPostReturn(p0)
|
@module-config.wasmExportSetConfig(p0, p1, p2, p3)
|
||||||
}
|
|
||||||
|
|
||||||
///|
|
|
||||||
#doc(hidden)
|
|
||||||
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|
||||||
@plasmoid.wasmExportPopupPostReturn(p0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
@@ -68,6 +74,12 @@ pub fn wasmExportPopup() -> Int {
|
|||||||
@plasmoid.wasmExportPopup()
|
@plasmoid.wasmExportPopup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportInfoPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
||||||
@@ -76,7 +88,13 @@ pub fn wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
|||||||
|
|
||||||
///|
|
///|
|
||||||
#doc(hidden)
|
#doc(hidden)
|
||||||
pub fn wasmExportOnEvent(p0 : Int, p1 : Int, p2 : Int, p3 : Int) -> Int {
|
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
||||||
@plasmoid.wasmExportOnEvent(p0, p1, p2, p3)
|
@plasmoid.wasmExportPopupPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
||||||
|
@module-config.wasmExportConfigItemsPostReturn(p0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
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.
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
// 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_store32(offset : Int, value : Int) =
|
||||||
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
||||||
|
#|(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_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_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)
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/lib/clock" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
// 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
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
///| 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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
File renamed without changes.
+38
-16
@@ -130,6 +130,16 @@ 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())
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,6 +170,7 @@ pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
|
7 => ()
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,6 +289,16 @@ 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())
|
||||||
|
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,6 +337,7 @@ pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
|||||||
}
|
}
|
||||||
5 => ()
|
5 => ()
|
||||||
6 => ()
|
6 => ()
|
||||||
|
7 => ()
|
||||||
_ => panic()
|
_ => panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,18 +390,6 @@ 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)
|
|
||||||
|
|
||||||
///|
|
|
||||||
#owned(str)
|
|
||||||
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) =
|
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)
|
||||||
@@ -389,8 +399,8 @@ extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
|
|||||||
#|(func (param i32) (result i32) local.get 0 i32.load)
|
#|(func (param i32) (result i32) local.get 0 i32.load)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
|
||||||
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
|
||||||
@@ -404,6 +414,18 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
|
|||||||
#| local.get 1)
|
#| local.get 1)
|
||||||
|
|
||||||
///|
|
///|
|
||||||
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
|
||||||
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
|
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
|
||||||
|
|
||||||
|
///|
|
||||||
|
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)
|
||||||
|
|
||||||
|
///|
|
||||||
|
#owned(str)
|
||||||
|
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
|
||||||
|
#|(func (param i32) (result i32) local.get 0)
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
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"
|
||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "moonplasma/desktop/components/clock-digital/gen/interface/moonplasma/desktop/plasmoid"
|
package "chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/plasmoid"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
"moonplasma/desktop/interface/moonplasma/desktop/types",
|
"chandra/desktop/interface/chandra/desktop/types",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
///| 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())
|
||||||
|
}
|
||||||
File renamed without changes.
@@ -1,8 +0,0 @@
|
|||||||
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,29 +0,0 @@
|
|||||||
///| 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,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
"moonplasma/desktop/components/clock-digital/gen/interface/moonplasma/desktop/plasmoid" @plasmoid,
|
"chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/module-config" @module-config,
|
||||||
|
"chandra/desktop/components/clock-digital/gen/interface/chandra/desktop/plasmoid" @plasmoid,
|
||||||
}
|
}
|
||||||
|
|
||||||
warnings = "-44"
|
warnings = "-44"
|
||||||
@@ -13,13 +14,16 @@ options(
|
|||||||
"heap-start-address": 16,
|
"heap-start-address": 16,
|
||||||
"exports": [
|
"exports": [
|
||||||
"mbt_ffi_cabi_realloc:cabi_realloc",
|
"mbt_ffi_cabi_realloc:cabi_realloc",
|
||||||
"wasmExportInfo:moonplasma:desktop/plasmoid@0.1.0#info",
|
"wasmExportConfigItems:chandra:desktop/module-config@0.1.0#config-items",
|
||||||
"wasmExportInfoPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#info",
|
"wasmExportConfigItemsPostReturn:cabi_post_chandra:desktop/module-config@0.1.0#config-items",
|
||||||
"wasmExportOnEvent:moonplasma:desktop/plasmoid@0.1.0#on-event",
|
"wasmExportInfo:chandra:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportPopup:moonplasma:desktop/plasmoid@0.1.0#popup",
|
"wasmExportInfoPostReturn:cabi_post_chandra:desktop/plasmoid@0.1.0#info",
|
||||||
"wasmExportPopupPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#popup",
|
"wasmExportOnEvent:chandra:desktop/plasmoid@0.1.0#on-event",
|
||||||
"wasmExportRender:moonplasma:desktop/plasmoid@0.1.0#render",
|
"wasmExportPopup:chandra:desktop/plasmoid@0.1.0#popup",
|
||||||
"wasmExportRenderPostReturn:cabi_post_moonplasma:desktop/plasmoid@0.1.0#render"
|
"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,5 +1,5 @@
|
|||||||
// Generated using `moon info`, DON'T EDIT IT
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
package "moonplasma/desktop/components/clock-digital/gen"
|
package "chandra/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
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
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"
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
// 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)
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/compositor/app" @app,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
///| The compositor component's entry point: everything lives in compositor/app.
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn run(args : Array[String]) -> Int {
|
||||||
|
@app.run(args)
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,226 @@
|
|||||||
|
// 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)
|
||||||
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
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"
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
// 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
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
///| `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,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/components/decoration-breeze/gen/interface/chandra/wm/decoration" @decoration,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
|
|
||||||
|
pkgtype(kind: "foreign_library")
|
||||||
|
|
||||||
|
options(
|
||||||
|
link: {
|
||||||
|
"wasm": {
|
||||||
|
"export-memory-name": "memory",
|
||||||
|
"heap-start-address": 16,
|
||||||
|
"exports": [
|
||||||
|
"mbt_ffi_cabi_realloc:cabi_realloc",
|
||||||
|
"wasmExportRender:chandra:wm/decoration@0.2.0#render",
|
||||||
|
"wasmExportRenderPostReturn:cabi_post_chandra:wm/decoration@0.2.0#render"
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
|
package "chandra/desktop/components/decoration-breeze/gen"
|
||||||
|
|
||||||
|
// Values
|
||||||
|
pub fn mbt_ffi_cabi_realloc(Int32, Int32, Int32, Int32) -> Int32
|
||||||
|
|
||||||
|
// Errors
|
||||||
|
|
||||||
|
// Types and methods
|
||||||
|
|
||||||
|
// Type aliases
|
||||||
|
|
||||||
|
// Traits
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
// 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 wasmExportConfigItems() -> Int {
|
||||||
|
@module-config.wasmExportConfigItems()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportConfigItemsPostReturn(p0 : Int) -> Unit {
|
||||||
|
@module-config.wasmExportConfigItemsPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportPopupPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportPopupPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportInfoPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportInfoPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportInfo() -> Int {
|
||||||
|
@plasmoid.wasmExportInfo()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportPopup() -> Int {
|
||||||
|
@plasmoid.wasmExportPopup()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#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 wasmExportRender(p0 : Int, p1 : Int) -> Int {
|
||||||
|
@plasmoid.wasmExportRender(p0, p1)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#doc(hidden)
|
||||||
|
pub fn wasmExportRenderPostReturn(p0 : Int) -> Unit {
|
||||||
|
@plasmoid.wasmExportRenderPostReturn(p0)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
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.
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#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_malloc(size : Int) -> Int =
|
||||||
|
#|(func (param i32) (result i32) (local i32)
|
||||||
|
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
|
||||||
|
#| local.tee 1 i32.const 0 call $moonbit.init_array8
|
||||||
|
#| local.get 1)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_free(position : Int) =
|
||||||
|
#|(func (param i32) local.get 0 call $moonbit.decref)
|
||||||
|
|
||||||
|
///|
|
||||||
|
extern "wasm" fn mbt_ffi_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_load32(offset : Int) -> Int =
|
||||||
|
#|(func (param i32) (result i32) local.get 0 i32.load)
|
||||||
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/lib/dock" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
|
package "chandra/desktop/components/dock/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
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
///| The dock's right-click settings (icon size, magnification, running apps) and its pinned apps
|
||||||
|
/// (a hidden setting the shell's per-app menu changes); the logic is in lib/dock.
|
||||||
|
|
||||||
|
///|
|
||||||
|
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)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
The widget (plasmoid) contract.
|
||||||
@@ -0,0 +1,431 @@
|
|||||||
|
// 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_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_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)
|
||||||
|
|
||||||
|
///|
|
||||||
|
#owned(str)
|
||||||
|
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_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,11 @@
|
|||||||
|
import {
|
||||||
|
"chandra/desktop/interface/chandra/desktop/types" @types,
|
||||||
|
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
||||||
|
"chandra/desktop/interface/chandra/desktop/app-catalog" @app_catalog,
|
||||||
|
"chandra/desktop/interface/chandra/desktop/app-launch" @app_launch,
|
||||||
|
"chandra/desktop/interface/chandra/desktop/windows" @windows,
|
||||||
|
"chandra/desktop/interface/chandra/desktop/icons" @icons,
|
||||||
|
"chandra/desktop/lib/dock" @logic,
|
||||||
|
}
|
||||||
|
|
||||||
|
warnings = "-44"
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
|
package "chandra/desktop/components/dock/gen/interface/chandra/desktop/plasmoid"
|
||||||
|
|
||||||
|
import {
|
||||||
|
"chandra/desktop/interface/chandra/desktop/types",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values
|
||||||
|
pub fn info() -> PlasmoidInfo
|
||||||
|
|
||||||
|
pub fn on_event(Surface, @types.InputEvent) -> Bool
|
||||||
|
|
||||||
|
pub fn popup() -> PopupSurface?
|
||||||
|
|
||||||
|
pub fn render(@types.Size) -> Array[@types.DrawOp]
|
||||||
|
|
||||||
|
// Errors
|
||||||
|
|
||||||
|
// Types and methods
|
||||||
|
pub(all) struct PlasmoidInfo {
|
||||||
|
id : String
|
||||||
|
name : String
|
||||||
|
preferred : @types.Size
|
||||||
|
min_width : UInt32
|
||||||
|
stretch : Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(all) struct PopupSurface {
|
||||||
|
size : @types.Size
|
||||||
|
ops : Array[@types.DrawOp]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(all) enum Surface {
|
||||||
|
WIDGET
|
||||||
|
POPUP
|
||||||
|
}
|
||||||
|
pub fn Surface::from(Int32) -> Self
|
||||||
|
pub fn Surface::ordinal(Self) -> Int32
|
||||||
|
|
||||||
|
// Type aliases
|
||||||
|
|
||||||
|
// Traits
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
///| Implementation of `chandra:desktop/plasmoid` for the dock, the panel's task manager: pinned
|
||||||
|
/// apps, then running ones, with their theme icons, a dot per open window and (room allowing)
|
||||||
|
/// their window titles. A click starts the app or brings its windows
|
||||||
|
/// forward. Layout, hit-testing and drawing live in lib/dock (the shell uses the same code for the
|
||||||
|
/// hover label and the per-app right-click menu); this adapts host capabilities.
|
||||||
|
|
||||||
|
///| Item under the pointer, or -1.
|
||||||
|
let hover : Ref[Int] = Ref::new(-1)
|
||||||
|
|
||||||
|
///| Size of the widget at its last render (events carry no size).
|
||||||
|
let width : Ref[Int] = Ref::new(200)
|
||||||
|
|
||||||
|
///|
|
||||||
|
let height : Ref[Int] = Ref::new(34)
|
||||||
|
|
||||||
|
///| The windows as the dock sees them: `all` has the app ids, `list-windows` (by the low 32 bits
|
||||||
|
/// of the id) which are minimized.
|
||||||
|
fn dock_windows() -> Array[@logic.DockWindow] {
|
||||||
|
let mini : Map[UInt, Bool] = Map([])
|
||||||
|
for w in @windows.list_windows() {
|
||||||
|
if w.minimized != 0U {
|
||||||
|
mini.set(w.id, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@windows.all().map(d => {
|
||||||
|
id: d.id,
|
||||||
|
app_id: d.app_id,
|
||||||
|
focused: d.focused,
|
||||||
|
title: d.title,
|
||||||
|
minimized: mini.contains((d.id & 0xFFFFFFFFUL).to_uint()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The window with keyboard focus, if any.
|
||||||
|
fn focused_window() -> UInt64? {
|
||||||
|
for d in @windows.all() {
|
||||||
|
if d.focused {
|
||||||
|
return Some(d.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn current_items() -> Array[@logic.DockItem] {
|
||||||
|
@logic.items(@logic.config, @app_catalog.list_apps(), dock_windows())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn info() -> PlasmoidInfo {
|
||||||
|
{
|
||||||
|
id: "org.chandra.dock",
|
||||||
|
name: "Dock",
|
||||||
|
// The shell works out the real width from the items as they change.
|
||||||
|
preferred: { w: @logic.preferred_width(current_items(), @logic.config, 34).reinterpret_as_uint(), h: 34U },
|
||||||
|
min_width: 60,
|
||||||
|
// The panel's task list: it takes the free room, and what follows packs right.
|
||||||
|
stretch: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn render(size : @types.Size) -> Array[@types.DrawOp] {
|
||||||
|
width.val = size.w.reinterpret_as_int()
|
||||||
|
height.val = size.h.reinterpret_as_int()
|
||||||
|
@logic.render(size, @theme.get_tokens(), @logic.config, current_items(), hover.val, @icons.app_icon)
|
||||||
|
}
|
||||||
|
|
||||||
|
///| The dock has no popup: the shell draws its hover label and right-click menu.
|
||||||
|
pub fn popup() -> PopupSurface? {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn on_event(_target : Surface, event : @types.InputEvent) -> Bool {
|
||||||
|
let (h, redraw, action) = @logic.handle(
|
||||||
|
current_items(),
|
||||||
|
@logic.config,
|
||||||
|
hover.val,
|
||||||
|
width.val,
|
||||||
|
height.val,
|
||||||
|
focused_window(),
|
||||||
|
event,
|
||||||
|
)
|
||||||
|
hover.val = h
|
||||||
|
match action {
|
||||||
|
Launch(id) => ignore(@app_launch.launch(id))
|
||||||
|
Focus(win) => @windows.focus(win)
|
||||||
|
Nothing => ()
|
||||||
|
}
|
||||||
|
redraw
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// Generated by `wit-bindgen` 0.63.0. DO NOT EDIT!
|
||||||
|
///|
|
||||||
|
pub(all) struct PlasmoidInfo {
|
||||||
|
id : String; name : String; preferred : @types.Size; min_width : UInt; stretch : Bool
|
||||||
|
}
|
||||||
|
///|
|
||||||
|
pub(all) struct PopupSurface {
|
||||||
|
size : @types.Size; ops : Array[@types.DrawOp]
|
||||||
|
}
|
||||||
|
///|
|
||||||
|
pub(all) enum Surface {
|
||||||
|
WIDGET; POPUP
|
||||||
|
} derive()
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Surface::ordinal(self : Surface) -> Int {
|
||||||
|
match self {
|
||||||
|
WIDGET => 0
|
||||||
|
POPUP => 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Surface::from(self : Int) -> Surface {
|
||||||
|
match self {
|
||||||
|
0 => WIDGET
|
||||||
|
1 => POPUP
|
||||||
|
_ => panic()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
///|
|
||||||
|
declare pub fn info() -> PlasmoidInfo
|
||||||
|
///|
|
||||||
|
/// Draw the widget into a `size` box (local coordinates).
|
||||||
|
declare pub fn render(size : @types.Size) -> Array[@types.DrawOp]
|
||||||
|
///|
|
||||||
|
/// Present when the widget wants a popup surface (e.g. a launcher menu).
|
||||||
|
declare pub fn popup() -> PopupSurface?
|
||||||
|
///|
|
||||||
|
/// Returns true when the widget's visuals changed and a redraw is needed.
|
||||||
|
declare pub fn on_event(target : Surface, event : @types.InputEvent) -> Bool
|
||||||
|
|
||||||
Loaded 100 of 875 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user