1
//! Render-only draft natives.
2
//!
3
//! These host fns take no database action; each mutates the
4
//! [`TransactionDraft`](crate::draft::TransactionDraft) held in the session's
5
//! Store data, which the render entry point reads back via `store.into_data()`.
6
//!
7
//! They are *functionally* render-only: the linker binds them on BOTH the
8
//! normal eval channel ([`super::link`]) and the render surface
9
//! ([`super::link_render`]) — the import must resolve wherever a compiled module
10
//! references it — but the draft accumulator is armed only under render
11
//! (`SessionData::for_render`). On the normal channel `with_draft` finds no
12
//! draft and traps, so a non-render program can compile/link a draft native yet
13
//! never accumulate anything. That trap is a safety net, not the boundary; the
14
//! real gate is the render compiler-spec allowlist (a normal template/eval that
15
//! shouldn't see these simply doesn't have them in scope). They are harmless on
16
//! the normal channel either way: no DB, no secret, no mutation.
17
//!
18
//! `draft-split` takes the entity refs returned by `get-account` /
19
//! `get-commodity` plus a rational amount; it reads each entity's `id` field
20
//! (slot 0, per `ENTITY_LAYOUTS`) host-side so the draft records stable uuids.
21

            
22
use nomiscript::EntityKind;
23
use scripting::runtime::{read_entity_string_field, read_ratio_arg, read_string_arg};
24
use wasmtime::{ArrayRef, Linker, Rooted, StructRef};
25

            
26
use crate::draft::{DraftSplit, DraftTag};
27
use crate::natives::args::args_error;
28
use crate::natives::native::register_native;
29
use crate::session::SessionData;
30

            
31
pub const REGISTERED_COMMANDS: &[&str] = &[
32
    "set-draft-note",
33
    "set-draft-date",
34
    "draft-split",
35
    "draft-tag",
36
    "draft-split-tag",
37
];
38

            
39
type StringArg = Option<Rooted<ArrayRef>>;
40
type StructArg = Option<Rooted<StructRef>>;
41

            
42
236810
pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
43
236810
    register_native(
44
236810
        linker,
45
236810
        "template_set_draft_note",
46
5297
        |caller, (note_arg,): (StringArg,)| {
47
5297
            Box::pin(async move {
48
5297
                let note = read_string_arg(caller, note_arg)?
49
                    .ok_or_else(|| args_error("set-draft-note: missing note"))?;
50
5297
                caller.data().with_draft(|d| d.set_note(note))?;
51
5297
                Ok(1)
52
5297
            })
53
5297
        },
54
    )?;
55
236810
    register_native(
56
236810
        linker,
57
236810
        "template_set_draft_date",
58
4814
        |caller, (date_arg,): (StringArg,)| {
59
4814
            Box::pin(async move {
60
4814
                let date = read_string_arg(caller, date_arg)?
61
                    .ok_or_else(|| args_error("set-draft-date: missing date"))?;
62
4814
                caller.data().with_draft(|d| d.set_date(date))?;
63
4814
                Ok(1)
64
4814
            })
65
4814
        },
66
    )?;
67
236810
    register_native(
68
236810
        linker,
69
236810
        "template_draft_split",
70
11718
        |caller, (account_arg, commodity_arg, amount_arg): (StructArg, StructArg, StructArg)| {
71
11718
            Box::pin(async move {
72
                let account_id =
73
11718
                    read_entity_string_field(caller, account_arg, EntityKind::Account, "id")?
74
                        .ok_or_else(|| args_error("draft-split: missing account"))?;
75
                let commodity_id =
76
11718
                    read_entity_string_field(caller, commodity_arg, EntityKind::Commodity, "id")?
77
                        .ok_or_else(|| args_error("draft-split: missing commodity"))?;
78
11718
                let (value_num, value_denom) = read_ratio_arg(caller, amount_arg)?
79
                    .ok_or_else(|| args_error("draft-split: missing amount"))?;
80
                // Return the split's index as the handle a template passes to
81
                // `draft-split-tag` to tag this specific split. Reject the absurd
82
                // >i32::MAX case BEFORE inserting so a failed call never leaves a
83
                // partially-mutated draft (and a stale handle can never silently
84
                // address the wrong split).
85
11718
                let mut index: Option<usize> = None;
86
11718
                caller.data().with_draft(|d| {
87
11718
                    if i32::try_from(d.split_count()).is_ok() {
88
11718
                        index = Some(d.add_split(DraftSplit {
89
11718
                            account_id,
90
11718
                            commodity_id,
91
11718
                            value_num,
92
11718
                            value_denom,
93
11718
                            tags: Vec::new(),
94
11718
                        }));
95
                    }
96
11718
                })?;
97
11718
                let index = index.ok_or_else(|| args_error("draft-split: too many splits"))?;
98
11718
                i32::try_from(index).map_err(|_| args_error("draft-split: too many splits"))
99
11718
            })
100
11718
        },
101
    )?;
102
236810
    register_native(
103
236810
        linker,
104
236810
        "template_draft_tag",
105
4974
        |caller, (name_arg, value_arg): (StringArg, StringArg)| {
106
4974
            Box::pin(async move {
107
4974
                let name = read_string_arg(caller, name_arg)?
108
                    .ok_or_else(|| args_error("draft-tag: missing name"))?;
109
4974
                let value = read_string_arg(caller, value_arg)?
110
                    .ok_or_else(|| args_error("draft-tag: missing value"))?;
111
4974
                caller
112
4974
                    .data()
113
4974
                    .with_draft(|d| d.add_tag(DraftTag { name, value }))?;
114
4974
                Ok(1)
115
4974
            })
116
4974
        },
117
    )?;
118
236810
    register_native(
119
236810
        linker,
120
236810
        "template_draft_split_tag",
121
2567
        |caller, (index_arg, name_arg, value_arg): (i32, StringArg, StringArg)| {
122
2567
            Box::pin(async move {
123
2567
                let index = usize::try_from(index_arg)
124
                    .map_err(|_| args_error("draft-split-tag: split index must be non-negative"))?;
125
2567
                let name = read_string_arg(caller, name_arg)?
126
                    .ok_or_else(|| args_error("draft-split-tag: missing name"))?;
127
2567
                let value = read_string_arg(caller, value_arg)?
128
                    .ok_or_else(|| args_error("draft-split-tag: missing value"))?;
129
2567
                let mut ok = false;
130
2567
                caller
131
2567
                    .data()
132
2567
                    .with_draft(|d| ok = d.add_split_tag(index, DraftTag { name, value }))?;
133
2567
                if !ok {
134
160
                    return Err(args_error(format!(
135
160
                        "draft-split-tag: no draft split at index {index} \
136
160
                         (pass the value returned by draft-split)"
137
160
                    )));
138
2407
                }
139
2407
                Ok(1)
140
2567
            })
141
2567
        },
142
    )?;
143
236810
    Ok(())
144
236810
}