Skip to main content

rpc/natives/
mod.rs

1//! Canonical host-fn registry for the 32 server commands.
2//!
3//! Each domain owns a submodule that mirrors the `server::command::*` shape and
4//! holds one file per server command. Commands register their host fn through a
5//! per-domain `register` entry point; the top-level `register_all` aggregates
6//! every domain so a wasmtime [`Linker`] gains the full
7//! canonical surface in one call.
8//!
9//! Subsequent commits fill each domain. This commit lays down the directory
10//! shape so per-command commits stay focused on one server command at a time.
11
12use scripting::runtime::{alloc_pair_chain, alloc_string_ref};
13use wasmtime::{AnyRef, ArrayRef, Caller, Linker, Rooted, StructRef};
14
15use crate::session::SessionData;
16
17/// One stringly-typed wasm host-fn argument as it lands on the
18/// `linker.func_wrap_async` closure: nullable because the arg can be
19/// absent, `Rooted<ArrayRef>` because the wasm side encodes strings as
20/// `(ref null $i8_array)`.
21pub(crate) type StringArg = Option<Rooted<ArrayRef>>;
22/// Three-arg tuple shape used by `account_set_account_tag` and other
23/// host fns that take three string args.
24pub(crate) type StringArgTriple = (StringArg, StringArg, StringArg);
25
26/// Allocates a `pair:string` chain from host strings.
27pub(crate) async fn alloc_string_pair_chain(
28    caller: &mut Caller<'_, SessionData>,
29    lines: Vec<String>,
30) -> wasmtime::Result<Option<Rooted<StructRef>>> {
31    let mut anyrefs: Vec<Option<Rooted<AnyRef>>> = Vec::with_capacity(lines.len());
32    for line in lines {
33        let s = alloc_string_ref(caller, line.as_bytes())?;
34        anyrefs.push(Some(s.to_anyref()));
35    }
36    alloc_pair_chain(caller, anyrefs).await
37}
38
39pub mod account;
40mod args;
41pub mod catch_each;
42pub mod chart;
43pub mod commodity;
44pub mod config;
45pub mod date;
46pub mod env_io;
47pub mod forms;
48mod generated_specs;
49pub mod meta;
50pub(crate) mod native;
51pub mod raise;
52mod render;
53pub mod report;
54pub mod script_store;
55pub mod split;
56pub mod ssh_key;
57pub mod tag;
58pub mod template;
59pub mod template_store;
60pub mod transaction;
61
62pub use generated_specs::all_compiler_specs;
63pub use render::{RENDER_NATIVE_ALLOWLIST, link_render, render_compiler_specs};
64
65/// Aggregator: registers every domain's host fns on `linker` in one call.
66/// Bound to [`SessionData`] now that `meta` natives consult `ScriptCtx`
67/// (user_id) — generic-T register fns from the empty per-domain modules
68/// still satisfy this concrete type because they don't touch the Store data.
69pub fn link(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
70    meta::register(linker)?;
71    env_io::register(linker)?;
72    raise::register(linker)?;
73    catch_each::register(linker)?;
74    chart::register(linker)?;
75    account::register(linker)?;
76    commodity::register(linker)?;
77    config::register(linker)?;
78    date::register(linker)?;
79    forms::register(linker)?;
80    report::register(linker)?;
81    split::register(linker)?;
82    ssh_key::register(linker)?;
83    tag::register(linker)?;
84    script_store::register(linker)?;
85    template_store::register(linker)?;
86    template::register(linker)?;
87    transaction::register(linker)?;
88    Ok(())
89}
90
91// `all_compiler_specs` lives in `generated_specs` (tangled from
92// `doc/scripting/native_reference.org` by `rpc/build.rs`) — see the
93// `pub use` import above. Adding or modifying a native means editing
94// the org table; cargo build regenerates the Rust.
95
96#[must_use]
97pub fn all_registered_commands() -> Vec<&'static str> {
98    [
99        account::REGISTERED_COMMANDS,
100        chart::REGISTERED_COMMANDS,
101        commodity::REGISTERED_COMMANDS,
102        config::REGISTERED_COMMANDS,
103        date::REGISTERED_COMMANDS,
104        forms::REGISTERED_COMMANDS,
105        report::REGISTERED_COMMANDS,
106        split::REGISTERED_COMMANDS,
107        script_store::REGISTERED_COMMANDS,
108        ssh_key::REGISTERED_COMMANDS,
109        tag::REGISTERED_COMMANDS,
110        template_store::REGISTERED_COMMANDS,
111        transaction::REGISTERED_COMMANDS,
112    ]
113    .iter()
114    .flat_map(|d| d.iter().copied())
115    .collect()
116}
117
118#[cfg(test)]
119#[path = "tests_registry.rs"]
120mod tests;