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

            
12
use scripting::runtime::{alloc_pair_chain, alloc_string_ref};
13
use wasmtime::{AnyRef, ArrayRef, Caller, Linker, Rooted, StructRef};
14

            
15
use 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)`.
21
pub(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.
24
pub(crate) type StringArgTriple = (StringArg, StringArg, StringArg);
25

            
26
/// Allocates a `pair:string` chain from host strings.
27
1920
pub(crate) async fn alloc_string_pair_chain(
28
1920
    caller: &mut Caller<'_, SessionData>,
29
1920
    lines: Vec<String>,
30
1920
) -> wasmtime::Result<Option<Rooted<StructRef>>> {
31
1920
    let mut anyrefs: Vec<Option<Rooted<AnyRef>>> = Vec::with_capacity(lines.len());
32
1920
    for line in lines {
33
1600
        let s = alloc_string_ref(caller, line.as_bytes())?;
34
1600
        anyrefs.push(Some(s.to_anyref()));
35
    }
36
1920
    alloc_pair_chain(caller, anyrefs).await
37
1920
}
38

            
39
pub mod account;
40
mod args;
41
pub mod catch_each;
42
pub mod chart;
43
pub mod commodity;
44
pub mod config;
45
pub mod date;
46
pub mod env_io;
47
pub mod forms;
48
mod generated_specs;
49
pub mod meta;
50
pub(crate) mod native;
51
pub mod raise;
52
mod render;
53
pub mod report;
54
pub mod script_store;
55
pub mod split;
56
pub mod ssh_key;
57
pub mod tag;
58
pub mod template;
59
pub mod template_store;
60
pub mod transaction;
61

            
62
pub use generated_specs::all_compiler_specs;
63
pub 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.
69
230199
pub fn link(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
70
230199
    meta::register(linker)?;
71
230199
    env_io::register(linker)?;
72
230199
    raise::register(linker)?;
73
230199
    catch_each::register(linker)?;
74
230199
    chart::register(linker)?;
75
230199
    account::register(linker)?;
76
230199
    commodity::register(linker)?;
77
230199
    config::register(linker)?;
78
230199
    date::register(linker)?;
79
230199
    forms::register(linker)?;
80
230199
    report::register(linker)?;
81
230199
    split::register(linker)?;
82
230199
    ssh_key::register(linker)?;
83
230199
    tag::register(linker)?;
84
230199
    script_store::register(linker)?;
85
230199
    template_store::register(linker)?;
86
230199
    template::register(linker)?;
87
230199
    transaction::register(linker)?;
88
230199
    Ok(())
89
230199
}
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]
97
5
pub fn all_registered_commands() -> Vec<&'static str> {
98
5
    [
99
5
        account::REGISTERED_COMMANDS,
100
5
        chart::REGISTERED_COMMANDS,
101
5
        commodity::REGISTERED_COMMANDS,
102
5
        config::REGISTERED_COMMANDS,
103
5
        date::REGISTERED_COMMANDS,
104
5
        forms::REGISTERED_COMMANDS,
105
5
        report::REGISTERED_COMMANDS,
106
5
        split::REGISTERED_COMMANDS,
107
5
        script_store::REGISTERED_COMMANDS,
108
5
        ssh_key::REGISTERED_COMMANDS,
109
5
        tag::REGISTERED_COMMANDS,
110
5
        template_store::REGISTERED_COMMANDS,
111
5
        transaction::REGISTERED_COMMANDS,
112
5
    ]
113
5
    .iter()
114
65
    .flat_map(|d| d.iter().copied())
115
    .collect()
116
5
}
117

            
118
#[cfg(test)]
119
#[path = "tests_registry.rs"]
120
mod tests;