1use scripting::runtime::{alloc_commodity_ref, alloc_ratio_ref, alloc_string_ref, read_string_arg};
6use server::command::account::{ListAccounts, ListAccountsForManage};
7use wasmtime::{ArrayRef, Linker, Rooted};
8
9use crate::natives::native::register_native;
10use crate::session::SessionData;
11
12mod balance;
13pub(crate) mod create;
14mod list;
15mod merge;
16#[cfg(test)]
17mod tests;
18#[cfg(test)]
19mod tests_balance;
20#[cfg(test)]
21mod tests_lookup;
22
23pub const REGISTERED_COMMANDS: &[&str] = &[
24 "create-account",
25 "merge-accounts",
26 "list-accounts",
27 "list-accounts-for-manage",
28 "get-account-for-manage",
29 "set-account-tag",
30 "clear-account-tag",
31 "get-account",
32 "get-account-commodities",
33 "get-balance",
34 "get-balances",
35];
36
37pub fn register(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
38 register_readonly(linker)?;
39 register_unlisted(linker)?;
40 register_mutators(linker)?;
41 Ok(())
42}
43
44pub fn register_readonly(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
45 register_native(linker, "account_list_accounts", |caller, ()| {
46 Box::pin(async move {
47 let user_id = caller.data().ctx().user_id;
48 let result = ListAccounts::new().user_id(user_id).run().await;
49 let entries = list::list_account_entries("list-accounts", result)?;
50 list::alloc_account_chain(caller, entries).await
51 })
52 })?;
53 register_native(
54 linker,
55 "account_get_account",
56 |caller, (key_arg,): (Option<Rooted<ArrayRef>>,)| {
57 Box::pin(async move {
58 let user_id = caller.data().ctx().user_id;
59 let key = read_string_arg(caller, key_arg)?;
60 list::run_get_account(caller, user_id, key).await
61 })
62 },
63 )?;
64 register_native(
65 linker,
66 "account_get_balance",
67 |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
68 Box::pin(async move {
69 let user_id = caller.data().ctx().user_id;
70 let id = read_string_arg(caller, id_arg)?;
71 let (numer, denom) = balance::run_get_balance_single(user_id, id).await?;
72 Ok(Some(alloc_ratio_ref(caller, numer, denom)?))
73 })
74 },
75 )?;
76 register_native(
77 linker,
78 "account_get_account_commodities",
79 |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
80 Box::pin(async move {
81 let user_id = caller.data().ctx().user_id;
82 let id = read_string_arg(caller, id_arg)?;
83 list::run_get_account_commodities(caller, user_id, id).await
84 })
85 },
86 )?;
87 register_native(linker, "account_list_accounts_for_manage", |caller, ()| {
88 Box::pin(async move {
89 let user_id = caller.data().ctx().user_id;
90 let result = ListAccountsForManage::new().user_id(user_id).run().await;
91 let entries = list::list_account_entries("list-accounts-for-manage", result)?;
92 list::alloc_account_chain(caller, entries).await
93 })
94 })?;
95 register_native(
96 linker,
97 "account_get_account_for_manage",
98 |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
99 Box::pin(async move {
100 let user_id = caller.data().ctx().user_id;
101 let id = read_string_arg(caller, id_arg)?;
102 list::run_get_account_for_manage(caller, user_id, id).await
103 })
104 },
105 )?;
106 register_native(linker, "account_account_count", |caller, ()| {
107 Box::pin(async move {
108 let user_id = caller.data().ctx().user_id;
109 balance::count_accounts(user_id).await
110 })
111 })?;
112 register_native(
113 linker,
114 "account_account_balance",
115 |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
116 Box::pin(async move {
117 let user_id = caller.data().ctx().user_id;
118 let id = read_string_arg(caller, id_arg)?;
119 let (numer, denom, commodity_id) = balance::resolve_balance(user_id, id).await?;
120 let ref_ = alloc_commodity_ref(caller, numer, denom, commodity_id).await?;
121 Ok(Some(ref_))
122 })
123 },
124 )?;
125 Ok(())
126}
127
128pub fn register_unlisted(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
130 register_native(
131 linker,
132 "account_get_balances",
133 |caller, (id_arg,): (Option<Rooted<ArrayRef>>,)| {
134 Box::pin(async move {
135 let user_id = caller.data().ctx().user_id;
136 let id = read_string_arg(caller, id_arg)?;
137 let lines = balance::run_get_balances(user_id, id).await?;
138 crate::natives::alloc_string_pair_chain(caller, lines).await
139 })
140 },
141 )?;
142 Ok(())
143}
144
145pub fn register_mutators(linker: &mut Linker<SessionData>) -> wasmtime::Result<()> {
146 register_native(
147 linker,
148 "account_set_account_tag",
149 |caller, (id_arg, name_arg, value_arg): super::StringArgTriple| {
150 Box::pin(async move {
151 let user_id = caller.data().ctx().user_id;
152 let id = read_string_arg(caller, id_arg)?;
153 let name = read_string_arg(caller, name_arg)?;
154 let value = read_string_arg(caller, value_arg)?;
155 let (done, notice) = create::run_set_account_tag(user_id, id, name, value).await?;
156 notice
157 .into_iter()
158 .for_each(|n| caller.data().push_notice(n));
159 Ok(done)
160 })
161 },
162 )?;
163 register_native(
164 linker,
165 "account_clear_account_tag",
166 |caller, (id_arg, name_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)| {
167 Box::pin(async move {
168 let user_id = caller.data().ctx().user_id;
169 let id = read_string_arg(caller, id_arg)?;
170 let name = read_string_arg(caller, name_arg)?;
171 create::run_clear_account_tag(user_id, id, name).await
172 })
173 },
174 )?;
175 register_native(
176 linker,
177 "account_create_account",
178 |caller, (name_arg, parent_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)| {
179 Box::pin(async move {
180 let user_id = caller.data().ctx().user_id;
181 let name = read_string_arg(caller, name_arg)?;
182 let parent = read_string_arg(caller, parent_arg)?;
183 let (id, notice) = create::run_create_account(user_id, name, parent).await?;
184 notice
185 .into_iter()
186 .for_each(|n| caller.data().push_notice(n));
187 Ok(Some(alloc_string_ref(caller, id.as_bytes())?))
188 })
189 },
190 )?;
191 register_native(
192 linker,
193 "account_merge_accounts",
194 |caller, (into_arg, from_arg): (Option<Rooted<ArrayRef>>, Option<Rooted<ArrayRef>>)| {
195 Box::pin(async move {
196 let user_id = caller.data().ctx().user_id;
197 let into = read_string_arg(caller, into_arg)?;
198 let from = read_string_arg(caller, from_arg)?;
199 let id = merge::run_merge_accounts(user_id, into, from).await?;
200 Ok(Some(alloc_string_ref(caller, id.as_bytes())?))
201 })
202 },
203 )?;
204 Ok(())
205}
206
207pub(super) fn quote_string(s: &str) -> String {
208 let mut q = String::with_capacity(s.len() + 2);
209 q.push('"');
210 for ch in s.chars() {
211 match ch {
212 '"' => q.push_str("\\\""),
213 '\\' => q.push_str("\\\\"),
214 other => q.push(other),
215 }
216 }
217 q.push('"');
218 q
219}