1#[allow(
24 unused_imports,
25 clippy::wildcard_imports,
26 reason = "Standard workspace module prelude"
27)]
28use crate::utilities::*;
29
30use crate::converters::dctext::DcList;
31use crate::converters::wasm::{
32 dcmstring_to_wasm, dcmstring_to_wasm_with_resolver,
33};
34use crate::{DcChar, DcMstring, DcProjectionPolicy, DcString};
35use ctb_formats_dcdata::syntax::{SyntaxRuleResolver, default_syntax_resolver};
36pub use ctb_formats_dcdata::wat::{
37 wat_to_dc_shorthand_string, wat_to_dc_shorthands,
38};
39
40pub fn wat_to_dcstring(wat_src: &str) -> Result<DcString> {
46 let shorthands = wat_to_dc_shorthands(wat_src)?;
47 let mut dc_str = DcString::new();
48 for sh in shorthands {
49 dc_str.push(DcChar::from_u128(sh.to_global_id()?));
50 }
51 Ok(dc_str)
52}
53
54pub fn wat_to_dclist(wat_src: &str) -> Result<DcList> {
59 let shorthands = wat_to_dc_shorthands(wat_src)?;
60 shorthands
61 .into_iter()
62 .map(|s| s.to_global_id().map_err(anyhow::Error::from))
63 .collect()
64}
65
66pub fn wat_to_dcmstring(wat_src: &str) -> Result<DcMstring> {
72 let resolver = default_syntax_resolver();
73 wat_to_dcmstring_with_resolver(wat_src, resolver.as_ref())
74}
75
76pub fn wat_to_dcmstring_with_resolver(
81 wat_src: &str,
82 resolver: &dyn SyntaxRuleResolver,
83) -> Result<DcMstring> {
84 let dc_str = wat_to_dcstring(wat_src)?;
85 DcMstring::from_dc_str_with_policy_and_resolver(
86 &dc_str,
87 &DcProjectionPolicy::strict(),
88 resolver,
89 )
90}
91
92pub fn wat_to_wasm(wat_src: &str) -> Result<Vec<u8>> {
98 let mstr = wat_to_dcmstring(wat_src)?;
99 dcmstring_to_wasm(&mstr)
100}
101
102pub fn wat_to_wasm_with_resolver(
108 wat_src: &str,
109 resolver: &dyn SyntaxRuleResolver,
110) -> Result<Vec<u8>> {
111 let mstr = wat_to_dcmstring_with_resolver(wat_src, resolver)?;
112 dcmstring_to_wasm_with_resolver(&mstr, resolver)
113}
114
115#[cfg(test)]
116#[allow(
117 clippy::panic,
118 clippy::expect_used,
119 clippy::unwrap_used,
120 clippy::unwrap_in_result,
121 clippy::panic_in_result_fn,
122 clippy::indexing_slicing,
123 clippy::arithmetic_side_effects,
124 reason = "Standard repository test boilerplate"
125)]
126mod tests {
127 use super::*;
128 use crate::converters::wasm::{DcWasmSession, WasmStepOutcome};
129
130 #[crate::ctb_test]
131 fn test_wat_to_dcmstring_and_tinywasm_execution() -> Result<()> {
132 let wat = r#"
133 (module
134 (func (export "main") (result i32)
135 ;; Copy input mailbox (0..8) to output mailbox (8..16)
136 (i32.store (i32.const 8) (i32.load (i32.const 0)))
137 (i32.store (i32.const 12) (i32.load (i32.const 4)))
138 (i32.const 0)))
139 "#;
140 let mstr = wat_to_dcmstring(wat)?;
141 let mut input = DcMstring::new();
142 input.push_str("Hello from WAT!");
143 let mut session = DcWasmSession::from_dc_wasm(&mstr, &input, None)?;
144 let WasmStepOutcome::Complete(out) = session.step()? else {
145 bail!("Expected Complete step outcome");
146 };
147 assert_eq!(out.to_dc_string()?, input.to_dc_string()?);
148 Ok(())
149 }
150
151 #[crate::ctb_test]
152 fn test_wat_mutual_return_call_constant_stack_and_quota_resumption()
153 -> Result<()> {
154 let wat = r#"
158 (module
159 (func $ping (param $n i32) (param $acc i32) (result i32)
160 (if (result i32) (i32.le_s (local.get $n) (i32.const 0))
161 (then (local.get $acc))
162 (else
163 (block (result i32)
164 ;; Push a dead intermediate value onto the stack before preparing
165 ;; tail-call arguments inside a nested block, then `return_call`.
166 (i32.const 999999)
167 (return_call $pong
168 (i32.sub (local.get $n) (i32.const 1))
169 (i32.add (local.get $acc) (i32.const 1)))))))
170 (func $pong (param $n i32) (param $acc i32) (result i32)
171 (if (result i32) (i32.le_s (local.get $n) (i32.const 0))
172 (then (local.get $acc))
173 (else
174 (return_call $ping
175 (i32.sub (local.get $n) (i32.const 1))
176 (i32.add (local.get $acc) (i32.const 2))))))
177 (func (export "main") (result i32)
178 ;; Store sum in scratch linear memory at 64, echo input mailbox, return 0
179 (i32.store (i32.const 64) (call $ping (i32.const 5000) (i32.const 0)))
180 (i32.store (i32.const 8) (i32.load (i32.const 0)))
181 (i32.store (i32.const 12) (i32.load (i32.const 4)))
182 (i32.const 0)))
183 "#;
184 let mstr = wat_to_dcmstring(wat)?;
185 let mut input = DcMstring::new();
186 input.push_str("ok");
187
188 let resolver = ctb_formats_dcdata::syntax::default_syntax_resolver();
189 let limits = crate::converters::wasm::DcWasmResourceLimits::default()
190 .with_max_instructions_per_step(5_000);
191 let mut session = DcWasmSession::from_dc_wasm_with_limits(
192 &mstr,
193 &input,
194 None,
195 resolver.as_ref(),
196 limits,
197 )?;
198 let err = session
199 .step()
200 .expect_err("5,000 mutual tail calls must suspend at initial quota");
201 assert!(
202 err.downcast_ref::<crate::converters::wasm::WasmResourceInterruption>()
203 .is_some()
204 );
205
206 session.increase_compute_quota(100_000);
207 let WasmStepOutcome::Complete(out) = session.step()? else {
208 bail!("Expected Complete step outcome after quota resumption");
209 };
210 assert_eq!(out.to_dc_string()?, input.to_dc_string()?);
211 assert_eq!(session.read_memory(64, 4)?, 7_500_i32.to_le_bytes());
213 Ok(())
214 }
215
216 #[crate::ctb_test]
217 fn test_tinywasm_return_call_indirect_constant_stack() -> Result<()> {
218 let wasm_bytes: Vec<u8> = vec![
221 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0b, 0x02,
224 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f,
225 0x60, 0x00, 0x01, 0x7f,
226 0x03, 0x04, 0x03, 0x00, 0x00, 0x01,
228 0x04, 0x05, 0x01, 0x70, 0x01, 0x02, 0x02,
230 0x05, 0x03, 0x01, 0x00, 0x01,
232 0x07, 0x11, 0x02,
234 0x06, b'm', b'e', b'm', b'o', b'r', b'y', 0x02, 0x00,
235 0x04, b'm', b'a', b'i', b'n', 0x00, 0x02,
236 0x09, 0x08, 0x01, 0x00, 0x41, 0x00, 0x0b, 0x02, 0x00, 0x01,
238 0x0a, 0x4e, 0x03,
240 0x1e, 0x00,
242 0x20, 0x00, 0x41, 0x00, 0x4c, 0x04, 0x7f, 0x20, 0x01, 0x05, 0x41, 0x2a, 0x20, 0x00, 0x41, 0x01, 0x6b, 0x20, 0x01, 0x41, 0x03, 0x6a, 0x41, 0x01, 0x13, 0x00, 0x00, 0x0b, 0x0b,
252 0x1c, 0x00,
254 0x20, 0x00, 0x41, 0x00, 0x4c,
255 0x04, 0x7f,
256 0x20, 0x01,
257 0x05,
258 0x20, 0x00, 0x41, 0x01, 0x6b, 0x20, 0x01, 0x41, 0x01, 0x6a, 0x41, 0x00, 0x13, 0x00, 0x00, 0x0b, 0x0b,
263 0x10, 0x00,
265 0x41, 0x20, 0x41, 0xa0, 0x1f, 0x41, 0x00, 0x10, 0x00, 0x36, 0x02, 0x00, 0x41, 0x00, 0x0b,
272 ];
273 let mut session = DcWasmSession::from_wasm_bytes(
274 &wasm_bytes,
275 &DcMstring::new(),
276 None,
277 )?;
278 let outcome = session.step()?;
279 assert!(matches!(outcome, WasmStepOutcome::Complete(_)));
280 assert_eq!(session.read_memory(32, 4)?, 8_000_i32.to_le_bytes());
282 Ok(())
283 }
284}