Skip to main content

vm/
lib.rs

1//使用 cranelift 作为后端 直接 jit 解释脚本
2mod binary;
3mod memory;
4mod native;
5pub use native::{ANY, STD, ZustCallback};
6
7mod fns;
8use anyhow::{Result, anyhow};
9pub use fns::{FnInfo, FnVariant};
10mod context;
11pub use context::BuildContext;
12
13mod rt;
14use cranelift::prelude::types;
15use dynamic::{Dynamic, Type};
16pub use rt::JITRunTime;
17mod db_module;
18mod gpu_layout;
19mod gpu_module;
20mod http_module;
21mod llm_module;
22mod oss_module;
23mod root_module;
24pub use gpu_layout::{GpuFieldLayout, GpuStructLayout};
25
26use std::sync::{OnceLock, RwLock, Weak};
27static PTR_TYPE: OnceLock<types::Type> = OnceLock::new();
28pub fn ptr_type() -> types::Type {
29    PTR_TYPE.get().cloned().unwrap()
30}
31
32pub fn get_type(ty: &Type) -> Result<types::Type> {
33    if ty.is_f64() {
34        Ok(types::F64)
35    } else if ty.is_f32() {
36        Ok(types::F32)
37    } else if ty.is_int() | ty.is_uint() {
38        match ty.width() {
39            1 => Ok(types::I8),
40            2 => Ok(types::I16),
41            4 => Ok(types::I32),
42            8 => Ok(types::I64),
43            _ => Err(anyhow!("非法类型 {:?}", ty)),
44        }
45    } else if let Type::Bool = ty {
46        Ok(types::I8)
47    } else {
48        Ok(ptr_type())
49    }
50}
51
52use compiler::Symbol;
53use cranelift::prelude::*;
54use cranelift_module::Module;
55
56pub fn init_jit(mut jit: JITRunTime) -> Result<JITRunTime> {
57    jit.add_all()?;
58    Ok(jit)
59}
60
61use std::sync::Arc;
62unsafe impl Send for JITRunTime {}
63unsafe impl Sync for JITRunTime {}
64
65pub(crate) fn with_vm_context<T>(context: *const Weak<RwLock<JITRunTime>>, f: impl FnOnce(&Vm) -> Result<T>) -> Result<T> {
66    if context.is_null() {
67        return Err(anyhow!("VM context is null"));
68    }
69    let jit = unsafe { &*context }.upgrade().ok_or_else(|| anyhow!("VM context has expired"))?;
70    let vm = Vm { jit };
71    f(&vm)
72}
73
74fn add_method_field(jit: &mut JITRunTime, def: &str, method: &str, id: u32) -> Result<()> {
75    let def_id = jit.get_id(def)?;
76    if let Some((_, define)) = jit.compiler.symbols.get_symbol_mut(def_id) {
77        if let Symbol::Struct(Type::Struct { params, fields }, _) = define {
78            fields.push((method.into(), Type::Symbol { id, params: params.clone() }));
79        }
80    }
81    Ok(())
82}
83
84fn add_native_module_fns(jit: &mut JITRunTime, module: &str, fns: &[(&str, &[Type], Type, *const u8)]) -> Result<()> {
85    jit.add_module(module);
86    for (name, arg_tys, ret_ty, fn_ptr) in fns {
87        let full_name = format!("{}::{}", module, name);
88        jit.add_native_ptr(&full_name, name, arg_tys, ret_ty.clone(), *fn_ptr)?;
89    }
90    jit.pop_module();
91    Ok(())
92}
93
94impl JITRunTime {
95    fn add_memory_runtime(&mut self) -> Result<()> {
96        self.native_symbols.write().unwrap().insert("__vm_scope_enter".to_string(), memory::scope_enter as *const () as usize);
97        self.native_symbols.write().unwrap().insert("__vm_scope_exit_void".to_string(), memory::scope_exit_void as *const () as usize);
98        self.native_symbols.write().unwrap().insert("__vm_scope_exit_dynamic".to_string(), memory::scope_exit_dynamic as *const () as usize);
99        self.native_symbols.write().unwrap().insert("__vm_scope_exit_bytes".to_string(), memory::scope_exit_bytes as *const () as usize);
100        self.native_symbols.write().unwrap().insert("__vm_struct_alloc".to_string(), native::struct_alloc as *const () as usize);
101        self.native_symbols.write().unwrap().insert("__vm_repeat_fill".to_string(), native::repeat_fill as *const () as usize);
102        self.native_symbols.write().unwrap().insert("__vm_strcat".to_string(), native::strcat as *const () as usize);
103        self.native_symbols.write().unwrap().insert("__vm_strcat_i64".to_string(), native::strcat_i64 as *const () as usize);
104        self.native_symbols.write().unwrap().insert("__vm_strcat_assign".to_string(), native::strcat_assign as *const () as usize);
105        self.native_symbols.write().unwrap().insert("__vm_callback_new".to_string(), native::callback_new as *const () as usize);
106        self.native_symbols.write().unwrap().insert("__vm_spawn_ptr".to_string(), native::spawn_ptr as *const () as usize);
107        self.native_symbols.write().unwrap().insert("__vm_struct_from_ptr".to_string(), native::struct_from_ptr as *const () as usize);
108        self.native_symbols.write().unwrap().insert("__vm_array_from_ptr".to_string(), native::array_from_ptr as *const () as usize);
109        self.native_symbols.write().unwrap().insert("__vm_array_to_ptr".to_string(), native::array_to_ptr as *const () as usize);
110        self.native_symbols.write().unwrap().insert("__vm_arith_fault".to_string(), memory::arith_fault as *const () as usize);
111
112        let void_sig = self.get_sig(&[], Type::Void)?;
113        self.scope_enter_fn = Some(self.module.declare_function("__vm_scope_enter", cranelift_module::Linkage::Import, &void_sig)?);
114        self.scope_exit_void_fn = Some(self.module.declare_function("__vm_scope_exit_void", cranelift_module::Linkage::Import, &void_sig)?);
115
116        let dynamic_sig = self.get_sig(&[Type::Any], Type::Any)?;
117        self.scope_exit_dynamic_fn = Some(self.module.declare_function("__vm_scope_exit_dynamic", cranelift_module::Linkage::Import, &dynamic_sig)?);
118
119        let bytes_sig = self.get_sig(&[Type::Any, Type::I64], Type::Any)?;
120        self.scope_exit_bytes_fn = Some(self.module.declare_function("__vm_scope_exit_bytes", cranelift_module::Linkage::Import, &bytes_sig)?);
121
122        let struct_alloc_sig = self.get_sig(&[Type::I64], Type::Any)?;
123        self.struct_alloc_fn = Some(self.module.declare_function("__vm_struct_alloc", cranelift_module::Linkage::Import, &struct_alloc_sig)?);
124
125        let repeat_fill_sig = self.get_sig(&[Type::Any, Type::I64, Type::I64, Type::I64], Type::Void)?;
126        self.repeat_fill_fn = Some(self.module.declare_function("__vm_repeat_fill", cranelift_module::Linkage::Import, &repeat_fill_sig)?);
127
128        let strcat_sig = self.get_sig(&[Type::Str, Type::Str], Type::Str)?;
129        self.strcat_fn = Some(self.module.declare_function("__vm_strcat", cranelift_module::Linkage::Import, &strcat_sig)?);
130
131        let strcat_i64_sig = self.get_sig(&[Type::Str, Type::I64], Type::Str)?;
132        self.strcat_i64_fn = Some(self.module.declare_function("__vm_strcat_i64", cranelift_module::Linkage::Import, &strcat_i64_sig)?);
133
134        let strcat_assign_sig = self.get_sig(&[Type::Any, Type::Any], Type::Any)?;
135        self.strcat_assign_fn = Some(self.module.declare_function("__vm_strcat_assign", cranelift_module::Linkage::Import, &strcat_assign_sig)?);
136
137        let callback_new_sig = self.get_sig(&[Type::I64, Type::I64, Type::I64, Type::Any], Type::Any)?;
138        self.callback_new_fn = Some(self.module.declare_function("__vm_callback_new", cranelift_module::Linkage::Import, &callback_new_sig)?);
139
140        let spawn_ptr_sig = self.get_sig(&[Type::I64, Type::I64, Type::Any], Type::Bool)?;
141        self.spawn_ptr_fn = Some(self.module.declare_function("__vm_spawn_ptr", cranelift_module::Linkage::Import, &spawn_ptr_sig)?);
142
143        let struct_from_ptr_sig = self.get_sig(&[Type::I64, Type::I64], Type::Any)?;
144        self.struct_from_ptr_fn = Some(self.module.declare_function("__vm_struct_from_ptr", cranelift_module::Linkage::Import, &struct_from_ptr_sig)?);
145        self.array_from_ptr_fn = Some(self.module.declare_function("__vm_array_from_ptr", cranelift_module::Linkage::Import, &struct_from_ptr_sig)?);
146        let array_to_ptr_sig = self.get_sig(&[Type::Any, Type::Any, Type::I64], Type::Void)?;
147        self.array_to_ptr_fn = Some(self.module.declare_function("__vm_array_to_ptr", cranelift_module::Linkage::Import, &array_to_ptr_sig)?);
148
149        self.arith_fault_fn = Some(self.module.declare_function("__vm_arith_fault", cranelift_module::Linkage::Import, &void_sig)?);
150        Ok(())
151    }
152
153    pub fn add_module(&mut self, name: &str) {
154        self.compiler.symbols.add_module(name.into());
155    }
156
157    pub fn pop_module(&mut self) {
158        self.compiler.symbols.pop_module();
159    }
160
161    pub fn add_native_const(&mut self, name: &str, value: impl Into<Dynamic>, ty: Type) -> u32 {
162        self.compiler.add_symbol(name, Symbol::Const { value: value.into(), ty, is_pub: true })
163    }
164
165    pub fn add_type(&mut self, name: &str, ty: Type, is_pub: bool) -> u32 {
166        self.compiler.add_symbol(name, Symbol::Struct(ty, is_pub))
167    }
168
169    pub fn add_empty_type(&mut self, name: &str) -> Result<u32> {
170        match self.get_id(name) {
171            Ok(id) => Ok(id),
172            Err(_) => Ok(self.add_type(name, Type::Struct { params: Vec::new(), fields: Vec::new() }, true)),
173        }
174    }
175
176    pub fn add_native_module_ptr(&mut self, module: &str, name: &str, arg_tys: &[Type], ret_ty: Type, fn_ptr: *const u8) -> Result<u32> {
177        self.add_module(module);
178        let full_name = format!("{}::{}", module, name);
179        let result = self.add_native_ptr(&full_name, name, arg_tys, ret_ty, fn_ptr);
180        self.pop_module();
181        result
182    }
183
184    pub(crate) fn add_native_module_context_ptr(&mut self, module: &str, name: &str, arg_tys: &[Type], ret_ty: Type, fn_ptr: *const u8) -> Result<u32> {
185        self.add_module(module);
186        let full_name = format!("{}::{}", module, name);
187        let result = self.add_context_native_ptr(&full_name, name, arg_tys, ret_ty, fn_ptr);
188        self.pop_module();
189        result
190    }
191
192    pub fn add_native_method_ptr(&mut self, def: &str, method: &str, arg_tys: &[Type], ret_ty: Type, fn_ptr: *const u8) -> Result<u32> {
193        self.add_empty_type(def)?;
194        let full_name = format!("{}::{}", def, method);
195        let id = self.add_native_ptr(&full_name, &full_name, arg_tys, ret_ty, fn_ptr)?;
196        add_method_field(self, def, method, id)?;
197        Ok(id)
198    }
199
200    pub fn add_std(&mut self) -> Result<()> {
201        if self.compiler.symbols.get_id("std::print").is_ok() {
202            return Ok(());
203        }
204        self.add_module("std");
205        for (name, arg_tys, ret_ty, fn_ptr) in STD {
206            self.add_native_ptr(name, name, arg_tys, ret_ty, fn_ptr)?;
207        }
208        self.add_context_native_ptr("import", "import", &[Type::Any, Type::Any], Type::Bool, native::import_with_vm as *const u8)?;
209        self.add_context_native_ptr("spawn", "spawn", &[Type::Any, Type::Any], Type::Bool, native::spawn_with_vm as *const u8)?;
210        Ok(())
211    }
212
213    pub fn add_any(&mut self) -> Result<()> {
214        if self.compiler.symbols.get_id("Any").is_ok() && self.compiler.symbols.get_id("Any::is_map").is_ok() {
215            return Ok(());
216        }
217        for (name, arg_tys, ret_ty, fn_ptr) in ANY {
218            let (_, method) = name.split_once("::").ok_or_else(|| anyhow!("非法 Any 方法名 {}", name))?;
219            self.add_native_method_ptr("Any", method, arg_tys, ret_ty, fn_ptr)?;
220        }
221        Ok(())
222    }
223
224    pub fn add_vec(&mut self) -> Result<()> {
225        self.add_empty_type("Vec")?;
226        let vec_def = Type::Symbol { id: self.get_id("Vec")?, params: Vec::new() };
227        self.add_inline("Vec::swap", vec![vec_def.clone(), Type::I64, Type::I64], Type::Void, |ctx: Option<&mut BuildContext>, args: Vec<Value>| {
228            if let Some(ctx) = ctx {
229                let width = ctx.builder.ins().iconst(types::I64, 4);
230                let offset_val = ctx.builder.ins().imul(args[1], width); // i * 4 i32大小四字节
231                let final_addr = ctx.builder.ins().iadd(args[0], offset_val); // base + (i*4)
232                let dest = ctx.builder.ins().imul(args[2], width);
233                let dest_addr = ctx.builder.ins().iadd(args[0], dest); // base + (i*4)
234                let dest_val = ctx.builder.ins().load(types::I32, MemFlags::trusted(), dest_addr, 0);
235                let v = ctx.builder.ins().load(types::I32, MemFlags::trusted(), final_addr, 0);
236                ctx.builder.ins().store(MemFlags::trusted(), v, dest_addr, 0);
237                ctx.builder.ins().store(MemFlags::trusted(), dest_val, final_addr, 0);
238            }
239            Err(anyhow!("无返回值"))
240        })?;
241
242        self.add_inline("Vec::get_idx", vec![vec_def.clone(), Type::I64], Type::I32, |ctx: Option<&mut BuildContext>, args: Vec<Value>| {
243            if let Some(ctx) = ctx {
244                let width = ctx.builder.ins().iconst(types::I64, 4);
245                let offset_val = ctx.builder.ins().imul(args[1], width); // i * 4 i32大小四字节
246                let final_addr = ctx.builder.ins().iadd(args[0], offset_val);
247                Ok((Some(ctx.builder.ins().load(types::I32, MemFlags::trusted(), final_addr, 0)), Type::I32))
248            } else {
249                Ok((None, Type::I32))
250            }
251        })?;
252        Ok(())
253    }
254
255    pub fn add_llm(&mut self) -> Result<()> {
256        add_native_module_fns(self, "llm", &llm_module::LLM_NATIVE)
257    }
258
259    pub fn add_root(&mut self) -> Result<()> {
260        add_native_module_fns(self, "root", &root_module::ROOT_NATIVE)?;
261        self.add_native_module_context_ptr("root", "add_fn", &[Type::Any, Type::Any], Type::Bool, root_module::root_add_fn_with_vm as *const u8)?;
262        Ok(())
263    }
264
265    pub fn add_http(&mut self) -> Result<()> {
266        add_native_module_fns(self, "http", &http_module::HTTP_NATIVE)?;
267        http_module::add_root_handlers()
268    }
269
270    pub fn add_oss(&mut self) -> Result<()> {
271        add_native_module_fns(self, "oss", &oss_module::OSS_NATIVE)
272    }
273
274    pub fn add_db(&mut self) -> Result<()> {
275        add_native_module_fns(self, "db", &db_module::DB_NATIVE)
276    }
277
278    pub fn add_gpu(&mut self) -> Result<()> {
279        add_native_module_fns(self, "gpu", &gpu_module::GPU_NATIVE)
280    }
281
282    pub fn add_all(&mut self) -> Result<()> {
283        self.add_std()?;
284        self.add_any()?;
285        self.add_vec()?;
286        self.add_llm()?;
287        self.add_root()?;
288        self.add_http()?;
289        self.add_oss()?;
290        self.add_db()?;
291        self.add_gpu()?;
292        Ok(())
293    }
294}
295
296#[derive(Clone)]
297pub struct Vm {
298    pub jit: Arc<RwLock<JITRunTime>>,
299}
300
301impl Vm {
302    pub fn new() -> Self {
303        dynamic::set_dynamic_return_handler(memory::take_dynamic_return);
304        let jit = Arc::new(RwLock::new(JITRunTime::new(|_| {})));
305        {
306            let mut guard = jit.write().unwrap();
307            guard.set_owner(Arc::downgrade(&jit));
308            guard.add_memory_runtime().expect("register VM memory runtime");
309            guard.add_std().expect("register VM std runtime");
310            guard.add_any().expect("register VM Any runtime");
311        }
312        Self { jit }
313    }
314
315    pub fn with_all() -> Result<Self> {
316        let vm = Self::new();
317        vm.jit.write().unwrap().add_all()?;
318        Ok(vm)
319    }
320
321    pub fn import(&self, name: &str, path: &str) -> Result<()> {
322        if root::contains(path) {
323            let code = root::get(path).unwrap();
324            if code.is_str() {
325                self.jit.write().unwrap().import_code(name, code.as_str().as_bytes().to_vec())
326            } else {
327                self.jit.write().unwrap().import_code(name, code.get_dynamic("code").ok_or(anyhow!("{:?} 没有 code 成员", code))?.as_str().as_bytes().to_vec())
328            }
329        } else {
330            self.jit.write().unwrap().compiler.import_file(name, path)?;
331            Ok(())
332        }
333    }
334}
335
336impl Default for Vm {
337    fn default() -> Self {
338        Self::new()
339    }
340}
341
342#[cfg(test)]
343mod tests {
344    use super::{GpuStructLayout, Vm, ZustCallback};
345    use dynamic::{CustomProperty, Dynamic, ToJson, Type};
346    use std::collections::BTreeMap;
347    use std::sync::{Mutex, RwLock};
348
349    /// Test-only wrapper for a compiled function pointer + return type.
350    struct TestFn {
351        ptr: *const u8,
352        ret: Type,
353    }
354
355    impl TestFn {
356        fn ptr(&self) -> *const u8 {
357            self.ptr
358        }
359        fn ret_ty(&self) -> &Type {
360            &self.ret
361        }
362    }
363
364    /// Test-only convenience wrapping `vm.jit.write().unwrap()` calls.
365    trait VmTestExt {
366        fn import_code(&self, name: &str, code: Vec<u8>) -> anyhow::Result<()>;
367        fn get_fn(&self, name: &str, arg_tys: &[Type]) -> anyhow::Result<TestFn>;
368        fn get_fn_with_params(&self, name: &str, arg_tys: &[Type], generic_args: &[Type]) -> anyhow::Result<TestFn>;
369        fn get_fn_ptr(&self, name: &str, arg_tys: &[Type]) -> anyhow::Result<(*const u8, Type)>;
370        fn infer(&self, name: &str, arg_tys: &[Type]) -> anyhow::Result<Type>;
371        fn add_native_module_ptr(&self, module: &str, name: &str, arg_tys: &[Type], ret_ty: Type, ptr: *const u8) -> anyhow::Result<u32>;
372        fn add_native_method_ptr(&self, def: &str, method: &str, arg_tys: &[Type], ret_ty: Type, ptr: *const u8) -> anyhow::Result<u32>;
373        fn add_empty_type(&self, name: &str) -> anyhow::Result<u32>;
374        fn add_std(&self) -> anyhow::Result<()>;
375        fn add_any(&self) -> anyhow::Result<()>;
376        fn get_symbol(&self, name: &str, params: Vec<Type>) -> anyhow::Result<Type>;
377        fn gpu_struct_layout(&self, name: &str, params: &[Type]) -> anyhow::Result<GpuStructLayout>;
378        fn load(&self, code: Vec<u8>, arg_name: smol_str::SmolStr) -> anyhow::Result<(i64, Type)>;
379    }
380
381    impl VmTestExt for Vm {
382        fn import_code(&self, name: &str, code: Vec<u8>) -> anyhow::Result<()> {
383            self.jit.write().unwrap().import_code(name, code)
384        }
385        fn get_fn(&self, name: &str, arg_tys: &[Type]) -> anyhow::Result<TestFn> {
386            let (ptr, ret) = self.jit.write().unwrap().get_fn_ptr(name, arg_tys)?;
387            Ok(TestFn { ptr, ret })
388        }
389        fn get_fn_with_params(&self, name: &str, arg_tys: &[Type], generic_args: &[Type]) -> anyhow::Result<TestFn> {
390            let (ptr, ret) = self.jit.write().unwrap().get_fn_ptr_with_params(name, arg_tys, generic_args)?;
391            Ok(TestFn { ptr, ret })
392        }
393        fn get_fn_ptr(&self, name: &str, arg_tys: &[Type]) -> anyhow::Result<(*const u8, Type)> {
394            self.jit.write().unwrap().get_fn_ptr(name, arg_tys)
395        }
396        fn infer(&self, name: &str, arg_tys: &[Type]) -> anyhow::Result<Type> {
397            self.jit.write().unwrap().get_type(name, arg_tys)
398        }
399        fn add_native_module_ptr(&self, module: &str, name: &str, arg_tys: &[Type], ret_ty: Type, ptr: *const u8) -> anyhow::Result<u32> {
400            self.jit.write().unwrap().add_native_module_ptr(module, name, arg_tys, ret_ty, ptr)
401        }
402        fn add_native_method_ptr(&self, def: &str, method: &str, arg_tys: &[Type], ret_ty: Type, ptr: *const u8) -> anyhow::Result<u32> {
403            self.jit.write().unwrap().add_native_method_ptr(def, method, arg_tys, ret_ty, ptr)
404        }
405        fn add_empty_type(&self, name: &str) -> anyhow::Result<u32> {
406            self.jit.write().unwrap().add_empty_type(name)
407        }
408        fn add_std(&self) -> anyhow::Result<()> {
409            self.jit.write().unwrap().add_std()
410        }
411        fn add_any(&self) -> anyhow::Result<()> {
412            self.jit.write().unwrap().add_any()
413        }
414        fn get_symbol(&self, name: &str, params: Vec<Type>) -> anyhow::Result<Type> {
415            Ok(Type::Symbol { id: self.jit.write().unwrap().get_id(name)?, params })
416        }
417        fn gpu_struct_layout(&self, name: &str, params: &[Type]) -> anyhow::Result<GpuStructLayout> {
418            let jit = self.jit.write().unwrap();
419            GpuStructLayout::from_symbol_table(&jit.compiler.symbols, name, params)
420        }
421        fn load(&self, code: Vec<u8>, arg_name: smol_str::SmolStr) -> anyhow::Result<(i64, Type)> {
422            self.jit.write().unwrap().load(code, arg_name)
423        }
424    }
425
426    extern "C" fn math_double(value: i64) -> i64 {
427        value * 2
428    }
429
430    #[test]
431    fn build_context_set_var_fills_sparse_none_slots() -> anyhow::Result<()> {
432        use crate::context::{BuildContext, LocalVar};
433        use cranelift::codegen::ir::{Function, Signature, UserFuncName};
434        use cranelift::codegen::isa::CallConv;
435        use cranelift::prelude::{FunctionBuilder, FunctionBuilderContext};
436
437        let mut function = Function::with_name_signature(UserFuncName::user(0, 0), Signature::new(CallConv::Fast));
438        let mut function_ctx = FunctionBuilderContext::new();
439        let builder = FunctionBuilder::new(&mut function, &mut function_ctx);
440        let mut ctx = BuildContext::new(builder, &[], Type::Void)?;
441
442        ctx.set_var(33, LocalVar::None)?;
443
444        assert!(matches!(ctx.get_var(32)?, LocalVar::None));
445        assert!(matches!(ctx.get_var(33)?, LocalVar::None));
446        assert!(ctx.get_var(34).is_err());
447        Ok(())
448    }
449
450    #[test]
451    fn vm_can_add_native_after_jit_creation() -> anyhow::Result<()> {
452        let vm = Vm::new();
453        vm.add_native_module_ptr("math", "double", &[Type::I64], Type::I64, math_double as *const u8)?;
454        vm.import_code(
455            "vm_dynamic_native",
456            br#"
457            pub fn run(value: i64) {
458                math::double(value)
459            }
460            "#
461            .to_vec(),
462        )?;
463
464        let compiled = vm.get_fn("vm_dynamic_native::run", &[Type::I64])?;
465        assert_eq!(compiled.ret_ty(), &Type::I64);
466        let run: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
467        assert_eq!(run(21), 42);
468        Ok(())
469    }
470
471    #[test]
472    fn vm_new_registers_std_and_any() -> anyhow::Result<()> {
473        let vm = Vm::new();
474        vm.add_std()?;
475        vm.add_any()?;
476        assert_eq!(vm.infer("std::print", &[Type::Any])?, Type::Void);
477        assert_eq!(vm.infer("std::sqrt", &[Type::F64])?, Type::F64);
478
479        vm.import_code(
480            "vm_new_default_any",
481            br#"
482            pub fn has_items(content) {
483                if content.is_map() {
484                    if content.contains("items") {
485                        return content.items.len() > 0;
486                    }
487                }
488                false
489            }
490            "#
491            .to_vec(),
492        )?;
493
494        assert_eq!(vm.infer("vm_new_default_any::has_items", &[Type::Any])?, Type::Bool);
495        let compiled = vm.get_fn("vm_new_default_any::has_items", &[Type::Any])?;
496        assert_eq!(compiled.ret_ty(), &Type::Bool);
497        Ok(())
498    }
499
500    #[test]
501    fn std_sqrt_is_available_as_top_level_function() -> anyhow::Result<()> {
502        let vm = Vm::with_all()?;
503        vm.import_code(
504            "vm_std_sqrt",
505            br#"
506            pub fn run() {
507                sqrt(9.0f64)
508            }
509            "#
510            .to_vec(),
511        )?;
512
513        let compiled = vm.get_fn("vm_std_sqrt::run", &[])?;
514        assert_eq!(compiled.ret_ty(), &Type::F64);
515        let run: extern "C" fn() -> f64 = unsafe { std::mem::transmute(compiled.ptr()) };
516        assert_eq!(run(), 3.0);
517        Ok(())
518    }
519
520    #[test]
521    fn tuple_assignment_uses_simultaneous_scalar_temps() -> anyhow::Result<()> {
522        let vm = Vm::with_all()?;
523        vm.import_code(
524            "vm_tuple_assignment",
525            br#"
526            pub fn swap() {
527                let a = 1i64;
528                let b = 2i64;
529                (a, b) = (b, a);
530                a * 10i64 + b
531            }
532
533            pub fn fib(n: i64) {
534                let a = 0i64;
535                let b = 1i64;
536                for _ in 0..n {
537                    (a, b) = (b, (a + b) % 1000000007i64);
538                }
539                a
540            }
541            "#
542            .to_vec(),
543        )?;
544
545        let swap = vm.get_fn("vm_tuple_assignment::swap", &[])?;
546        let swap: extern "C" fn() -> i64 = unsafe { std::mem::transmute(swap.ptr()) };
547        assert_eq!(swap(), 21);
548
549        let fib = vm.get_fn("vm_tuple_assignment::fib", &[Type::I64])?;
550        let fib: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(fib.ptr()) };
551        assert_eq!(fib(10), 55);
552        Ok(())
553    }
554
555    #[test]
556    fn nested_struct_arg_return_struct_field_is_static_field_access() -> anyhow::Result<()> {
557        let vm = Vm::with_all()?;
558        vm.import_code(
559            "vm_nested_struct_return_field",
560            br#"
561            pub struct Inner {
562                value: i64,
563            }
564
565            pub struct RoleMini {
566                inner: Inner,
567                hp: i64,
568            }
569
570            pub struct TeamMini {
571                role: RoleMini,
572            }
573
574            pub struct BigSummary {
575                winner: i64,
576                loser: i64,
577            }
578
579            pub fn make_big_with_team(team: TeamMini) {
580                let score = team.role.inner.value;
581                BigSummary{winner: score, loser: 0}
582            }
583
584            pub fn read_team_winner_direct() {
585                let team = TeamMini{role: RoleMini{inner: Inner{value: 9}, hp: 1}};
586                make_big_with_team(team).winner
587            }
588
589            pub fn read_team_winner_bound() {
590                let team = TeamMini{role: RoleMini{inner: Inner{value: 9}, hp: 1}};
591                let summary = make_big_with_team(team);
592                summary.winner
593            }
594            "#
595            .to_vec(),
596        )?;
597
598        let compiled = vm.get_fn("vm_nested_struct_return_field::read_team_winner_direct", &[])?;
599        assert_eq!(compiled.ret_ty(), &Type::I64);
600        let direct: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
601        assert_eq!(direct(), 9);
602
603        let compiled = vm.get_fn("vm_nested_struct_return_field::read_team_winner_bound", &[])?;
604        assert_eq!(compiled.ret_ty(), &Type::I64);
605        let bound: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
606        assert_eq!(bound(), 9);
607        Ok(())
608    }
609
610    #[test]
611    fn any_push_does_not_consume_reused_value() -> anyhow::Result<()> {
612        let vm = Vm::with_all()?;
613        vm.import_code(
614            "vm_any_push_reused_value",
615            br#"
616            pub fn run() {
617                let role_id = "acct_role_2";
618                let updated = [];
619                updated.push(role_id);
620                {
621                    ok: true,
622                    user_id: role_id,
623                    first: updated.get_idx(0)
624                }
625            }
626            "#
627            .to_vec(),
628        )?;
629
630        let compiled = vm.get_fn("vm_any_push_reused_value::run", &[])?;
631        assert_eq!(compiled.ret_ty(), &Type::Any);
632        let run: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
633        let result = unsafe { &*run() };
634        assert_eq!(result.get_dynamic("ok").and_then(|value| value.as_bool()), Some(true));
635        assert_eq!(result.get_dynamic("user_id").map(|value| value.as_str().to_string()), Some("acct_role_2".to_string()));
636        assert_eq!(result.get_dynamic("first").map(|value| value.as_str().to_string()), Some("acct_role_2".to_string()));
637        Ok(())
638    }
639
640    #[test]
641    fn negate_narrow_integers() -> anyhow::Result<()> {
642        let vm = Vm::with_all()?;
643        vm.import_code(
644            "vm_neg_narrow",
645            br#"
646            pub fn neg_i8(a: i8) { -a }
647            pub fn neg_i16(a: i16) { -a }
648            "#
649            .to_vec(),
650        )?;
651
652        let neg_i8 = vm.get_fn("vm_neg_narrow::neg_i8", &[Type::I8])?;
653        assert_eq!(neg_i8.ret_ty(), &Type::I8);
654        let neg_i8: extern "C" fn(i8) -> i8 = unsafe { std::mem::transmute(neg_i8.ptr()) };
655        assert_eq!(neg_i8(5), -5);
656        assert_eq!(neg_i8(-7), 7);
657
658        let neg_i16 = vm.get_fn("vm_neg_narrow::neg_i16", &[Type::I16])?;
659        assert_eq!(neg_i16.ret_ty(), &Type::I16);
660        let neg_i16: extern "C" fn(i16) -> i16 = unsafe { std::mem::transmute(neg_i16.ptr()) };
661        assert_eq!(neg_i16(5), -5);
662        assert_eq!(neg_i16(-300), 300);
663        Ok(())
664    }
665
666    #[test]
667    fn integer_divide_by_zero_does_not_crash() -> anyhow::Result<()> {
668        let vm = Vm::with_all()?;
669        vm.import_code(
670            "vm_div_by_zero",
671            br#"
672            pub fn divz(a: i64, b: i64) { a / b }
673            pub fn modz(a: i64, b: i64) { a % b }
674            pub fn overflow(a: i64, b: i64) { a / b }
675            "#
676            .to_vec(),
677        )?;
678
679        let divz = vm.get_fn("vm_div_by_zero::divz", &[Type::I64, Type::I64])?;
680        let modz = vm.get_fn("vm_div_by_zero::modz", &[Type::I64, Type::I64])?;
681        let overflow = vm.get_fn("vm_div_by_zero::overflow", &[Type::I64, Type::I64])?;
682        let divz: extern "C" fn(i64, i64) -> i64 = unsafe { std::mem::transmute(divz.ptr()) };
683        let modz: extern "C" fn(i64, i64) -> i64 = unsafe { std::mem::transmute(modz.ptr()) };
684        let overflow: extern "C" fn(i64, i64) -> i64 = unsafe { std::mem::transmute(overflow.ptr()) };
685
686        // 正常路径不受守卫影响
687        let _ = dynamic::take_fault();
688        assert_eq!(divz(7, 2), 3);
689        assert_eq!(modz(7, 2), 1);
690        assert!(dynamic::take_fault().is_none());
691
692        // 除零:返回 0 且置 fault,而不是 trap 杀进程
693        assert_eq!(divz(7, 0), 0);
694        assert!(dynamic::take_fault().is_some());
695        assert_eq!(modz(7, 0), 0);
696        assert!(dynamic::take_fault().is_some());
697
698        // INT_MIN / -1 溢出同样被守卫
699        assert_eq!(overflow(i64::MIN, -1), 0);
700        assert!(dynamic::take_fault().is_some());
701        Ok(())
702    }
703
704    #[test]
705    fn constant_divide_by_zero_does_not_crash() -> anyhow::Result<()> {
706        let vm = Vm::with_all()?;
707        vm.import_code(
708            "vm_const_div_zero",
709            br#"
710            pub fn divz(a: i64) { a / 0 }
711            pub fn modz(a: i64) { a % 0 }
712            pub fn divc(a: i64) { a / 7 }
713            "#
714            .to_vec(),
715        )?;
716        let divz: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(vm.get_fn("vm_const_div_zero::divz", &[Type::I64])?.ptr()) };
717        let modz: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(vm.get_fn("vm_const_div_zero::modz", &[Type::I64])?.ptr()) };
718        let divc: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(vm.get_fn("vm_const_div_zero::divc", &[Type::I64])?.ptr()) };
719
720        let _ = dynamic::take_fault();
721        // 常量除零:编译期判定 → 返回 0 + 置 fault,不 trap
722        assert_eq!(divz(42), 0);
723        assert!(dynamic::take_fault().is_some());
724        assert_eq!(modz(42), 0);
725        assert!(dynamic::take_fault().is_some());
726        // 非零常量除数:正常计算,不置 fault(走无守卫快路径)
727        assert_eq!(divc(42), 6);
728        assert!(dynamic::take_fault().is_none());
729        Ok(())
730    }
731
732    #[test]
733    fn dynamic_divide_by_zero_returns_null() -> anyhow::Result<()> {
734        let vm = Vm::with_all()?;
735        vm.import_code(
736            "vm_any_div_by_zero",
737            br#"
738            pub fn divz(a, b) { a / b }
739            "#
740            .to_vec(),
741        )?;
742
743        let divz = vm.get_fn("vm_any_div_by_zero::divz", &[Type::Any, Type::Any])?;
744        let divz: extern "C" fn(*const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(divz.ptr()) };
745        let a = Dynamic::from(7i64);
746        let zero = Dynamic::from(0i64);
747        let _ = dynamic::take_fault();
748        let result = unsafe { &*divz(&a, &zero) };
749        assert!(result.is_null());
750        assert!(dynamic::take_fault().is_some());
751        Ok(())
752    }
753
754    #[test]
755    fn compares_any_with_string_literal_as_string() -> anyhow::Result<()> {
756        let vm = Vm::with_all()?;
757        vm.import_code(
758            "vm_string_compare_any",
759            br#"
760            pub fn any_ne_empty(chat_path) {
761                chat_path != ""
762            }
763            "#
764            .to_vec(),
765        )?;
766
767        let compiled = vm.get_fn("vm_string_compare_any::any_ne_empty", &[Type::Any])?;
768        assert_eq!(compiled.ret_ty(), &Type::Bool);
769
770        let any_ne_empty: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
771        let empty = Dynamic::from("");
772        let non_empty = Dynamic::from("chat");
773
774        assert!(!any_ne_empty(&empty));
775        assert!(any_ne_empty(&non_empty));
776        Ok(())
777    }
778
779    #[test]
780    fn compares_bool_values_and_bool_literals() -> anyhow::Result<()> {
781        let vm = Vm::with_all()?;
782        vm.import_code(
783            "vm_bool_compare",
784            br#"
785            pub fn eq_true(value: bool) {
786                value == true
787            }
788
789            pub fn ne_false(value: bool) {
790                value != false
791            }
792
793            pub fn literal_left(value: bool) {
794                true == value
795            }
796
797            pub fn eq_pair(left: bool, right: bool) {
798                left == right
799            }
800
801            pub fn logic_pair(left: bool, right: bool) {
802                (left && right) || (left == true && right != false)
803            }
804            "#
805            .to_vec(),
806        )?;
807
808        let compiled = vm.get_fn("vm_bool_compare::eq_true", &[Type::Bool])?;
809        assert_eq!(compiled.ret_ty(), &Type::Bool);
810        let eq_true: extern "C" fn(bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
811        assert!(eq_true(true));
812        assert!(!eq_true(false));
813
814        let compiled = vm.get_fn("vm_bool_compare::ne_false", &[Type::Bool])?;
815        assert_eq!(compiled.ret_ty(), &Type::Bool);
816        let ne_false: extern "C" fn(bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
817        assert!(ne_false(true));
818        assert!(!ne_false(false));
819
820        let compiled = vm.get_fn("vm_bool_compare::literal_left", &[Type::Bool])?;
821        assert_eq!(compiled.ret_ty(), &Type::Bool);
822        let literal_left: extern "C" fn(bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
823        assert!(literal_left(true));
824        assert!(!literal_left(false));
825
826        let compiled = vm.get_fn("vm_bool_compare::eq_pair", &[Type::Bool, Type::Bool])?;
827        assert_eq!(compiled.ret_ty(), &Type::Bool);
828        let eq_pair: extern "C" fn(bool, bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
829        assert!(eq_pair(true, true));
830        assert!(eq_pair(false, false));
831        assert!(!eq_pair(true, false));
832        assert!(!eq_pair(false, true));
833
834        let compiled = vm.get_fn("vm_bool_compare::logic_pair", &[Type::Bool, Type::Bool])?;
835        assert_eq!(compiled.ret_ty(), &Type::Bool);
836        let logic_pair: extern "C" fn(bool, bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
837        assert!(logic_pair(true, true));
838        assert!(!logic_pair(true, false));
839        assert!(!logic_pair(false, true));
840        assert!(!logic_pair(false, false));
841        Ok(())
842    }
843
844    #[test]
845    fn parenthesized_expression_can_call_any_method() -> anyhow::Result<()> {
846        let vm = Vm::with_all()?;
847        vm.import_code(
848            "vm_parenthesized_method_call",
849            br#"
850            pub fn run(value) {
851                (value + 2).to_i64()
852            }
853            "#
854            .to_vec(),
855        )?;
856
857        let compiled = vm.get_fn("vm_parenthesized_method_call::run", &[Type::Any])?;
858        assert_eq!(compiled.ret_ty(), &Type::I64);
859        let run: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
860        let value = Dynamic::from(40i64);
861
862        assert_eq!(run(&value), 42);
863        Ok(())
864    }
865
866    #[test]
867    fn casts_any_float_to_i32_without_zeroing() -> anyhow::Result<()> {
868        let vm = Vm::with_all()?;
869        vm.import_code(
870            "vm_any_float_to_i32",
871            br#"
872            pub fn direct(value) {
873                value as i32
874            }
875
876            pub fn map_field(value) {
877                let field = value.v;
878                field as i32
879            }
880
881            pub fn damage(attacker, def_rate) {
882                let x = attacker.atk * (1.0 - def_rate);
883                x as i32
884            }
885            "#
886            .to_vec(),
887        )?;
888
889        let compiled = vm.get_fn("vm_any_float_to_i32::direct", &[Type::Any])?;
890        assert_eq!(compiled.ret_ty(), &Type::I32);
891        let direct: extern "C" fn(*const Dynamic) -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
892        let value = Dynamic::from(9.5f64);
893        assert_eq!(direct(&value), 9);
894
895        let compiled = vm.get_fn("vm_any_float_to_i32::map_field", &[Type::Any])?;
896        assert_eq!(compiled.ret_ty(), &Type::I32);
897        let map_field: extern "C" fn(*const Dynamic) -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
898        let value = dynamic::map!("v"=> 9.5f64);
899        assert_eq!(map_field(&value), 9);
900
901        let compiled = vm.get_fn("vm_any_float_to_i32::damage", &[Type::Any, Type::Any])?;
902        assert_eq!(compiled.ret_ty(), &Type::I32);
903        let damage: extern "C" fn(*const Dynamic, *const Dynamic) -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
904        let attacker = dynamic::map!("atk"=> 64i64);
905        let def_rate = Dynamic::from(0.17f64);
906        assert_eq!(damage(&attacker, &def_rate), 53);
907        Ok(())
908    }
909
910    #[test]
911    fn binary_imm_promotes_integer_literals_for_float_left_values() -> anyhow::Result<()> {
912        let vm = Vm::with_all()?;
913        vm.import_code(
914            "vm_float_binary_imm",
915            br#"
916            pub fn add_f32(value: f32) {
917                value + 1i32
918            }
919
920            pub fn sub_f32(value: f32) {
921                value - 1i32
922            }
923
924            pub fn mul_f32(value: f32) {
925                value * 2i32
926            }
927
928            pub fn div_f32(value: f32) {
929                value / 2i32
930            }
931
932            pub fn gt_f32(value: f32) {
933                value > 2i32
934            }
935            "#
936            .to_vec(),
937        )?;
938
939        let compiled = vm.get_fn("vm_float_binary_imm::add_f32", &[Type::F32])?;
940        assert_eq!(compiled.ret_ty(), &Type::F32);
941        let add_f32: extern "C" fn(f32) -> f32 = unsafe { std::mem::transmute(compiled.ptr()) };
942        assert_eq!(add_f32(2.5), 3.5);
943
944        let compiled = vm.get_fn("vm_float_binary_imm::sub_f32", &[Type::F32])?;
945        assert_eq!(compiled.ret_ty(), &Type::F32);
946        let sub_f32: extern "C" fn(f32) -> f32 = unsafe { std::mem::transmute(compiled.ptr()) };
947        assert_eq!(sub_f32(2.5), 1.5);
948
949        let compiled = vm.get_fn("vm_float_binary_imm::mul_f32", &[Type::F32])?;
950        assert_eq!(compiled.ret_ty(), &Type::F32);
951        let mul_f32: extern "C" fn(f32) -> f32 = unsafe { std::mem::transmute(compiled.ptr()) };
952        assert_eq!(mul_f32(2.5), 5.0);
953
954        let compiled = vm.get_fn("vm_float_binary_imm::div_f32", &[Type::F32])?;
955        assert_eq!(compiled.ret_ty(), &Type::F32);
956        let div_f32: extern "C" fn(f32) -> f32 = unsafe { std::mem::transmute(compiled.ptr()) };
957        assert_eq!(div_f32(5.0), 2.5);
958
959        let compiled = vm.get_fn("vm_float_binary_imm::gt_f32", &[Type::F32])?;
960        assert_eq!(compiled.ret_ty(), &Type::Bool);
961        let gt_f32: extern "C" fn(f32) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
962        assert!(gt_f32(2.5));
963        assert!(!gt_f32(1.5));
964        Ok(())
965    }
966
967    #[test]
968    fn any_keys_returns_map_keys_and_empty_list_for_other_values() -> anyhow::Result<()> {
969        let vm = Vm::with_all()?;
970        vm.import_code(
971            "vm_any_keys",
972            br#"
973            pub fn map_keys(value) {
974                let keys = value.keys();
975                keys.len() == 2 && keys.contains("alpha") && keys.contains("beta")
976            }
977
978            pub fn non_map_keys(value) {
979                value.keys().len() == 0
980            }
981            "#
982            .to_vec(),
983        )?;
984
985        let compiled = vm.get_fn("vm_any_keys::map_keys", &[Type::Any])?;
986        assert_eq!(compiled.ret_ty(), &Type::Bool);
987        let map_keys: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
988        let value = dynamic::map!("alpha"=> 1i64, "beta"=> 2i64);
989        assert!(map_keys(&value));
990
991        let compiled = vm.get_fn("vm_any_keys::non_map_keys", &[Type::Any])?;
992        assert_eq!(compiled.ret_ty(), &Type::Bool);
993        let non_map_keys: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
994        let value = Dynamic::from("alpha");
995        assert!(non_map_keys(&value));
996        Ok(())
997    }
998
999    #[test]
1000    fn any_logic_comparisons_use_bool_abi() -> anyhow::Result<()> {
1001        let vm = Vm::with_all()?;
1002        vm.import_code(
1003            "vm_any_logic_abi",
1004            br#"
1005            pub fn ne_empty(value) {
1006                value != ""
1007            }
1008
1009            pub fn eq_empty(value) {
1010                value == ""
1011            }
1012
1013            pub fn less_than_ten(value) {
1014                value < 10
1015            }
1016
1017            pub fn contains_key(value) {
1018                value.contains("alpha") == true
1019            }
1020            "#
1021            .to_vec(),
1022        )?;
1023
1024        let compiled = vm.get_fn("vm_any_logic_abi::ne_empty", &[Type::Any])?;
1025        assert_eq!(compiled.ret_ty(), &Type::Bool);
1026        let ne_empty: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1027        assert!(ne_empty(&Dynamic::from("x")));
1028        assert!(!ne_empty(&Dynamic::from("")));
1029
1030        let compiled = vm.get_fn("vm_any_logic_abi::eq_empty", &[Type::Any])?;
1031        assert_eq!(compiled.ret_ty(), &Type::Bool);
1032        let eq_empty: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1033        assert!(eq_empty(&Dynamic::from("")));
1034        assert!(!eq_empty(&Dynamic::from("x")));
1035
1036        let compiled = vm.get_fn("vm_any_logic_abi::less_than_ten", &[Type::Any])?;
1037        assert_eq!(compiled.ret_ty(), &Type::Bool);
1038        let less_than_ten: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1039        assert!(less_than_ten(&Dynamic::from(4i64)));
1040        assert!(!less_than_ten(&Dynamic::from(14i64)));
1041
1042        let compiled = vm.get_fn("vm_any_logic_abi::contains_key", &[Type::Any])?;
1043        assert_eq!(compiled.ret_ty(), &Type::Bool);
1044        let contains_key: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1045        assert!(contains_key(&dynamic::map!("alpha"=> 1i64)));
1046        assert!(!contains_key(&dynamic::map!("beta"=> 1i64)));
1047        Ok(())
1048    }
1049
1050    #[test]
1051    fn string_methods_work_on_static_string_and_any_string_values() -> anyhow::Result<()> {
1052        let vm = Vm::with_all()?;
1053        vm.import_code(
1054            "vm_string_methods",
1055            br#"
1056            pub fn static_string_methods(text: string) {
1057                let parts = text.split(",");
1058                text.starts_with("alpha")
1059                    && text.is_string()
1060                    && !text.is_null()
1061                    && parts.len() == 2
1062                    && parts.get_idx(0) == "alpha"
1063                    && parts.get_idx(1) == "beta"
1064            }
1065
1066            pub fn any_string_methods(value) {
1067                let parts = value.split(",");
1068                value.starts_with("alpha")
1069                    && value.is_string()
1070                    && !value.is_null()
1071                    && parts.len() == 2
1072                    && parts.get_idx(0) == "alpha"
1073                    && parts.get_idx(1) == "beta"
1074            }
1075
1076            pub fn any_null_methods(value) {
1077                value.is_null() && !value.is_string()
1078            }
1079            "#
1080            .to_vec(),
1081        )?;
1082
1083        let compiled = vm.get_fn("vm_string_methods::static_string_methods", &[Type::Str])?;
1084        assert_eq!(compiled.ret_ty(), &Type::Bool);
1085        let static_string_methods: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1086        let text = Dynamic::from("alpha,beta");
1087        assert!(static_string_methods(&text));
1088
1089        let compiled = vm.get_fn("vm_string_methods::any_string_methods", &[Type::Any])?;
1090        assert_eq!(compiled.ret_ty(), &Type::Bool);
1091        let any_string_methods: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1092        assert!(any_string_methods(&text));
1093
1094        let compiled = vm.get_fn("vm_string_methods::any_null_methods", &[Type::Any])?;
1095        assert_eq!(compiled.ret_ty(), &Type::Bool);
1096        let any_null_methods: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1097        let value = Dynamic::Null;
1098        assert!(any_null_methods(&value));
1099        Ok(())
1100    }
1101
1102    #[test]
1103    fn static_string_add_uses_direct_strcat() -> anyhow::Result<()> {
1104        let vm = Vm::with_all()?;
1105        vm.import_code(
1106            "vm_static_strcat",
1107            br#"
1108            pub fn join(left: string, right: string) {
1109                left + right
1110            }
1111
1112            pub fn suffix(left: string) {
1113                left + "-tail"
1114            }
1115
1116            pub fn append_local() {
1117                let text: string = "alpha";
1118                text += "-beta";
1119                text += "-tail";
1120                text
1121            }
1122
1123            pub fn append_local_assign() {
1124                let text: string = "alpha";
1125                text = text + "-beta";
1126                text = text + "-tail";
1127                text
1128            }
1129
1130            pub fn append_arg(text: string) {
1131                text += "-tail";
1132                text
1133            }
1134
1135            pub fn append_arg_assign(text: string) {
1136                text = text + "-tail";
1137                text
1138            }
1139
1140            pub fn append_any(value) {
1141                value += "-tail";
1142                value
1143            }
1144
1145            pub fn add_sub_assign_form() {
1146                let x = 10i64;
1147                x = x + 1i64;
1148                x = x - 2i64;
1149                x
1150            }
1151            "#
1152            .to_vec(),
1153        )?;
1154
1155        let compiled = vm.get_fn("vm_static_strcat::join", &[Type::Str, Type::Str])?;
1156        assert_eq!(compiled.ret_ty(), &Type::Str);
1157        let join: extern "C" fn(*const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1158        let left = Dynamic::from("alpha");
1159        let right = Dynamic::from("-beta");
1160        let result = unsafe { &*join(&left, &right) };
1161        assert!(matches!(result, Dynamic::StringBuf(_)));
1162        assert_eq!(result.as_str(), "alpha-beta");
1163
1164        let compiled = vm.get_fn("vm_static_strcat::suffix", &[Type::Str])?;
1165        assert_eq!(compiled.ret_ty(), &Type::Str);
1166        let suffix: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1167        let result = unsafe { &*suffix(&left) };
1168        assert!(matches!(result, Dynamic::StringBuf(_)));
1169        assert_eq!(result.as_str(), "alpha-tail");
1170
1171        let compiled = vm.get_fn("vm_static_strcat::append_local", &[])?;
1172        assert_eq!(compiled.ret_ty(), &Type::Str);
1173        let append_local: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1174        let result = unsafe { &*append_local() };
1175        assert!(matches!(result, Dynamic::StringBuf(_)));
1176        assert_eq!(result.as_str(), "alpha-beta-tail");
1177
1178        let compiled = vm.get_fn("vm_static_strcat::append_local_assign", &[])?;
1179        assert_eq!(compiled.ret_ty(), &Type::Str);
1180        let append_local_assign: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1181        let result = unsafe { &*append_local_assign() };
1182        assert!(matches!(result, Dynamic::StringBuf(_)));
1183        assert_eq!(result.as_str(), "alpha-beta-tail");
1184
1185        let compiled = vm.get_fn("vm_static_strcat::append_arg", &[Type::Str])?;
1186        assert_eq!(compiled.ret_ty(), &Type::Str);
1187        let append_arg: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1188        let input = Dynamic::from("alpha");
1189        let result = unsafe { &*append_arg(&input) };
1190        assert_eq!(result.as_str(), "alpha-tail");
1191        assert_eq!(input.as_str(), "alpha");
1192
1193        let compiled = vm.get_fn("vm_static_strcat::append_arg_assign", &[Type::Str])?;
1194        assert_eq!(compiled.ret_ty(), &Type::Str);
1195        let append_arg_assign: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1196        let input = Dynamic::from("alpha");
1197        let result = unsafe { &*append_arg_assign(&input) };
1198        assert_eq!(result.as_str(), "alpha-tail");
1199        assert_eq!(input.as_str(), "alpha");
1200
1201        let compiled = vm.get_fn("vm_static_strcat::append_any", &[Type::Any])?;
1202        assert_eq!(compiled.ret_ty(), &Type::Str);
1203        let append_any: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1204        let input = Dynamic::from("alpha");
1205        let result = unsafe { &*append_any(&input) };
1206        assert_eq!(result.as_str(), "alpha-tail");
1207        assert_eq!(input.as_str(), "alpha");
1208
1209        let compiled = vm.get_fn("vm_static_strcat::add_sub_assign_form", &[])?;
1210        assert_eq!(compiled.ret_ty(), &Type::I64);
1211        let add_sub_assign_form: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
1212        assert_eq!(add_sub_assign_form(), 9);
1213        Ok(())
1214    }
1215
1216    #[test]
1217    fn primitive_type_check_methods_call_any_runtime() -> anyhow::Result<()> {
1218        let vm = Vm::with_all()?;
1219        vm.import_code(
1220            "vm_primitive_type_check_methods",
1221            br#"
1222            pub fn int_checks() {
1223                !42i64.is_list()
1224                    && !42i64.is_map()
1225                    && !42i64.is_string()
1226                    && !42i64.is_null()
1227            }
1228
1229            pub fn bool_checks() {
1230                !true.is_list() && !true.is_map() && !true.is_null()
1231            }
1232            "#
1233            .to_vec(),
1234        )?;
1235
1236        let compiled = vm.get_fn("vm_primitive_type_check_methods::int_checks", &[])?;
1237        assert_eq!(compiled.ret_ty(), &Type::Bool);
1238        let int_checks: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1239        assert!(int_checks());
1240
1241        let compiled = vm.get_fn("vm_primitive_type_check_methods::bool_checks", &[])?;
1242        assert_eq!(compiled.ret_ty(), &Type::Bool);
1243        let bool_checks: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1244        assert!(bool_checks());
1245        Ok(())
1246    }
1247
1248    #[test]
1249    fn for_loop_iterates_any_list_and_map_values() -> anyhow::Result<()> {
1250        let vm = Vm::with_all()?;
1251        vm.import_code(
1252            "vm_for_any_collections",
1253            br#"
1254            pub fn list_sum(items) {
1255                let total = 0i64;
1256                for item in items {
1257                    total += item;
1258                }
1259                total
1260            }
1261
1262            pub fn map_sum(data) {
1263                let total = 0i64;
1264                for (key, value) in data {
1265                    total += value;
1266                }
1267                total
1268            }
1269            "#
1270            .to_vec(),
1271        )?;
1272
1273        let compiled = vm.get_fn("vm_for_any_collections::list_sum", &[Type::Any])?;
1274        assert_eq!(compiled.ret_ty(), &Type::I64);
1275        let list_sum: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
1276        let items = Dynamic::list(vec![1i64.into(), 2i64.into(), 3i64.into()]);
1277        assert_eq!(list_sum(&items), 6);
1278
1279        let compiled = vm.get_fn("vm_for_any_collections::map_sum", &[Type::Any])?;
1280        assert_eq!(compiled.ret_ty(), &Type::I64);
1281        let map_sum: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
1282        let data = dynamic::map!("a"=> 4i64, "b"=> 5i64);
1283        assert_eq!(map_sum(&data), 9);
1284        Ok(())
1285    }
1286
1287    #[test]
1288    fn compares_concrete_value_with_string_literal_as_string() -> anyhow::Result<()> {
1289        let vm = Vm::with_all()?;
1290        vm.import_code(
1291            "vm_string_compare_imm",
1292            br#"
1293            pub fn int_eq_str(value: i64) {
1294                value == "42"
1295            }
1296
1297            pub fn int_to_str(value: i64) {
1298                value + ""
1299            }
1300            "#
1301            .to_vec(),
1302        )?;
1303
1304        let compiled = vm.get_fn("vm_string_compare_imm::int_eq_str", &[Type::I64])?;
1305        assert_eq!(compiled.ret_ty(), &Type::Bool);
1306
1307        let int_eq_str: extern "C" fn(i64) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1308
1309        let compiled = vm.get_fn("vm_string_compare_imm::int_to_str", &[Type::I64])?;
1310        assert_eq!(compiled.ret_ty(), &Type::Str);
1311        let int_to_str: extern "C" fn(i64) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1312        let text = int_to_str(42);
1313        assert_eq!(unsafe { &*text }.as_str(), "42");
1314
1315        assert!(int_eq_str(42));
1316        assert!(!int_eq_str(7));
1317        Ok(())
1318    }
1319
1320    #[test]
1321    fn concatenates_string_with_integer_values() -> anyhow::Result<()> {
1322        let vm = Vm::with_all()?;
1323        vm.import_code(
1324            "vm_string_concat_integer",
1325            br#"
1326            pub fn idx_key(idx: i64) {
1327                "" + idx
1328            }
1329
1330            pub fn level_text(level: i64) {
1331                "" + level + " level"
1332            }
1333
1334            pub fn gold_text(currency) {
1335                "" + currency.gold
1336            }
1337            "#
1338            .to_vec(),
1339        )?;
1340
1341        let compiled = vm.get_fn("vm_string_concat_integer::idx_key", &[Type::I64])?;
1342        let idx_key: extern "C" fn(i64) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1343        let result = unsafe { &*idx_key(7) };
1344        assert!(matches!(result, Dynamic::StringBuf(_)));
1345        assert_eq!(result.as_str(), "7");
1346
1347        let compiled = vm.get_fn("vm_string_concat_integer::level_text", &[Type::I64])?;
1348        let level_text: extern "C" fn(i64) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1349        let result = unsafe { &*level_text(12) };
1350        assert_eq!(result.as_str(), "12 level");
1351
1352        let compiled = vm.get_fn("vm_string_concat_integer::gold_text", &[Type::Any])?;
1353        let gold_text: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1354        let currency = dynamic::map!("gold"=> 345i64);
1355        let result = unsafe { &*gold_text(&currency) };
1356        assert_eq!(result.as_str(), "345");
1357        Ok(())
1358    }
1359
1360    #[test]
1361    fn coerces_string_concat_to_i64_without_unimplemented_log() -> anyhow::Result<()> {
1362        let vm = Vm::with_all()?;
1363        vm.import_code(
1364            "vm_string_concat_to_i64",
1365            br#"
1366            pub fn run(idx: i64) {
1367                ("" + idx) as i64
1368            }
1369            "#
1370            .to_vec(),
1371        )?;
1372
1373        let compiled = vm.get_fn("vm_string_concat_to_i64::run", &[Type::I64])?;
1374        assert_eq!(compiled.ret_ty(), &Type::I64);
1375        let run: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
1376        assert_eq!(run(7), 7);
1377        Ok(())
1378    }
1379
1380    #[test]
1381    fn casts_dynamic_string_numbers_to_ints_and_floats() -> anyhow::Result<()> {
1382        let vm = Vm::with_all()?;
1383        vm.import_code(
1384            "vm_string_number_casts",
1385            br#"
1386            pub fn limit_i64(req) {
1387                req["@query"].limit as i64
1388            }
1389
1390            pub fn limit_i32(req) {
1391                req["@query"].limit as i32
1392            }
1393
1394            pub fn price_f64(req) {
1395                req["@query"].price as f64
1396            }
1397
1398            pub fn price_f32(req) {
1399                req["@query"].price as f32
1400            }
1401
1402            pub fn literal_i64() {
1403                "42" as i64
1404            }
1405
1406            pub fn literal_f64() {
1407                "3.5" as f64
1408            }
1409
1410            pub fn bad_number(req) {
1411                req["@query"].bad as i64
1412            }
1413            "#
1414            .to_vec(),
1415        )?;
1416
1417        let req = dynamic::map!("@query"=> dynamic::map!("limit"=> "50", "price"=> "3.5", "bad"=> "nope"));
1418
1419        let limit_i64 = vm.get_fn("vm_string_number_casts::limit_i64", &[Type::Any])?;
1420        assert_eq!(limit_i64.ret_ty(), &Type::I64);
1421        let limit_i64: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(limit_i64.ptr()) };
1422        assert_eq!(limit_i64(&req), 50);
1423
1424        let limit_i32 = vm.get_fn("vm_string_number_casts::limit_i32", &[Type::Any])?;
1425        assert_eq!(limit_i32.ret_ty(), &Type::I32);
1426        let limit_i32: extern "C" fn(*const Dynamic) -> i32 = unsafe { std::mem::transmute(limit_i32.ptr()) };
1427        assert_eq!(limit_i32(&req), 50);
1428
1429        let price_f64 = vm.get_fn("vm_string_number_casts::price_f64", &[Type::Any])?;
1430        assert_eq!(price_f64.ret_ty(), &Type::F64);
1431        let price_f64: extern "C" fn(*const Dynamic) -> f64 = unsafe { std::mem::transmute(price_f64.ptr()) };
1432        assert_eq!(price_f64(&req), 3.5);
1433
1434        let price_f32 = vm.get_fn("vm_string_number_casts::price_f32", &[Type::Any])?;
1435        assert_eq!(price_f32.ret_ty(), &Type::F32);
1436        let price_f32: extern "C" fn(*const Dynamic) -> f32 = unsafe { std::mem::transmute(price_f32.ptr()) };
1437        assert_eq!(price_f32(&req), 3.5);
1438
1439        let literal_i64 = vm.get_fn("vm_string_number_casts::literal_i64", &[])?;
1440        assert_eq!(literal_i64.ret_ty(), &Type::I64);
1441        let literal_i64: extern "C" fn() -> i64 = unsafe { std::mem::transmute(literal_i64.ptr()) };
1442        assert_eq!(literal_i64(), 42);
1443
1444        let literal_f64 = vm.get_fn("vm_string_number_casts::literal_f64", &[])?;
1445        assert_eq!(literal_f64.ret_ty(), &Type::F64);
1446        let literal_f64: extern "C" fn() -> f64 = unsafe { std::mem::transmute(literal_f64.ptr()) };
1447        assert_eq!(literal_f64(), 3.5);
1448
1449        let bad_number = vm.get_fn("vm_string_number_casts::bad_number", &[Type::Any])?;
1450        assert_eq!(bad_number.ret_ty(), &Type::I64);
1451        let bad_number: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(bad_number.ptr()) };
1452        assert_eq!(bad_number(&req), 0);
1453        Ok(())
1454    }
1455
1456    #[test]
1457    fn unifies_explicit_return_and_tail_integer_widths() -> anyhow::Result<()> {
1458        let vm = Vm::with_all()?;
1459        vm.import_code(
1460            "vm_return_integer_widths",
1461            br#"
1462            pub fn selected(flag, slot) {
1463                if flag {
1464                    return slot;
1465                }
1466                0
1467            }
1468            "#
1469            .to_vec(),
1470        )?;
1471
1472        let compiled = vm.get_fn("vm_return_integer_widths::selected", &[Type::Bool, Type::I64])?;
1473        assert_eq!(compiled.ret_ty(), &Type::I64);
1474        let selected: extern "C" fn(bool, i64) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
1475
1476        assert_eq!(selected(true, 7), 7);
1477        assert_eq!(selected(false, 7), 0);
1478        Ok(())
1479    }
1480
1481    #[test]
1482    fn root_contains_string_concat_is_bool_condition() -> anyhow::Result<()> {
1483        let vm = Vm::with_all()?;
1484        vm.import_code(
1485            "vm_root_contains_condition",
1486            br#"
1487            pub fn exists(user_id) {
1488                if root::contains("redis/user/" + user_id) {
1489                    return 1;
1490                }
1491                0
1492            }
1493            "#
1494            .to_vec(),
1495        )?;
1496
1497        assert_eq!(vm.infer("root::contains", &[Type::Any])?, Type::Bool);
1498        let compiled = vm.get_fn("vm_root_contains_condition::exists", &[Type::Any])?;
1499        assert_eq!(compiled.ret_ty(), &Type::I64);
1500        Ok(())
1501    }
1502
1503    #[test]
1504    fn root_add_map_can_be_printed() -> anyhow::Result<()> {
1505        let vm = Vm::with_all()?;
1506        assert_eq!(vm.infer("root::add_map", &[Type::Any])?, Type::Bool);
1507        vm.import_code(
1508            "vm_root_add_map_print",
1509            br#"
1510            pub fn run() {
1511                print(root::add_map("local/world_handlers/til_map_novicevillage"));
1512            }
1513            "#
1514            .to_vec(),
1515        )?;
1516
1517        let compiled = vm.get_fn("vm_root_add_map_print::run", &[])?;
1518        assert!(compiled.ret_ty().is_void());
1519        Ok(())
1520    }
1521
1522    #[test]
1523    fn std_log_accepts_any_and_returns_void() -> anyhow::Result<()> {
1524        let vm = Vm::with_all()?;
1525        vm.import_code(
1526            "vm_std_log",
1527            br#"
1528            pub fn run(value) {
1529                log({ ok: true, value: value });
1530            }
1531            "#
1532            .to_vec(),
1533        )?;
1534
1535        let compiled = vm.get_fn("vm_std_log::run", &[Type::Any])?;
1536        assert!(compiled.ret_ty().is_void());
1537        let run: extern "C" fn(*const Dynamic) = unsafe { std::mem::transmute(compiled.ptr()) };
1538        let value = Dynamic::from(7i64);
1539        run(&value);
1540        Ok(())
1541    }
1542
1543    #[test]
1544    fn unary_not_any_loop_var_is_bool_condition() -> anyhow::Result<()> {
1545        let vm = Vm::with_all()?;
1546        vm.import_code(
1547            "vm_unary_not_any_loop_var",
1548            br#"
1549            pub fn count_missing(flags) {
1550                let missing = 0;
1551                for exists in flags {
1552                    if !exists {
1553                        missing = missing + 1;
1554                    }
1555                }
1556                missing
1557            }
1558            "#
1559            .to_vec(),
1560        )?;
1561
1562        let compiled = vm.get_fn("vm_unary_not_any_loop_var::count_missing", &[Type::Any])?;
1563        assert_eq!(compiled.ret_ty(), &Type::I64);
1564        Ok(())
1565    }
1566
1567    #[test]
1568    fn closure_literal_can_be_called_immediately() -> anyhow::Result<()> {
1569        let vm = Vm::with_all()?;
1570        vm.import_code(
1571            "vm_closure_immediate_call",
1572            br#"
1573            pub fn no_args() {
1574                let r = || { 1i32 }();
1575                r
1576            }
1577
1578            pub fn with_arg() {
1579                |value: i32| { value + 1i32 }(2i32)
1580            }
1581            "#
1582            .to_vec(),
1583        )?;
1584
1585        let compiled = vm.get_fn("vm_closure_immediate_call::no_args", &[])?;
1586        assert_eq!(compiled.ret_ty(), &Type::I32);
1587        let no_args: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
1588        assert_eq!(no_args(), 1);
1589
1590        let compiled = vm.get_fn("vm_closure_immediate_call::with_arg", &[])?;
1591        assert_eq!(compiled.ret_ty(), &Type::I32);
1592        let with_arg: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
1593        assert_eq!(with_arg(), 3);
1594        Ok(())
1595    }
1596
1597    #[test]
1598    fn nested_closure_captures_outer_closure_arg() -> anyhow::Result<()> {
1599        let vm = Vm::with_all()?;
1600        vm.import_code(
1601            "vm_nested_closure_capture",
1602            br#"
1603            pub fn run() {
1604                let reference_label = "reference";
1605                |path: string| {
1606                    let upload_done = |uploaded: bool| {
1607                        if uploaded {
1608                            reference_label + ":" + path
1609                        } else {
1610                            "missing"
1611                        }
1612                    };
1613                    upload_done(true)
1614                }("reference.png")
1615            }
1616            "#
1617            .to_vec(),
1618        )?;
1619
1620        let compiled = vm.get_fn("vm_nested_closure_capture::run", &[])?;
1621        assert_eq!(compiled.ret_ty(), &Type::Any);
1622        let run: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1623        let result = unsafe { &*run() };
1624        assert_eq!(result.as_str(), "reference:reference.png");
1625        Ok(())
1626    }
1627
1628    #[test]
1629    fn semicolon_tail_call_makes_function_void() -> anyhow::Result<()> {
1630        let vm = Vm::with_all()?;
1631        vm.import_code(
1632            "vm_semicolon_tail_void",
1633            br#"
1634            pub fn send_role_select(idx, account_id, selected_slot) {
1635                root::send("local/ui/send_dialog", {
1636                    idx: idx,
1637                    account_id: account_id,
1638                    selected_slot: selected_slot
1639                });
1640            }
1641            "#
1642            .to_vec(),
1643        )?;
1644
1645        let compiled = vm.get_fn("vm_semicolon_tail_void::send_role_select", &[Type::Any, Type::Any, Type::Any])?;
1646        assert_eq!(compiled.ret_ty(), &Type::Void);
1647        Ok(())
1648    }
1649
1650    #[test]
1651    fn bare_return_conflicts_with_non_void_return() -> anyhow::Result<()> {
1652        let vm = Vm::with_all()?;
1653        vm.import_code(
1654            "vm_bare_return_conflict",
1655            br#"
1656            pub fn run(flag) {
1657                if flag {
1658                    return;
1659                }
1660                1
1661            }
1662            "#
1663            .to_vec(),
1664        )?;
1665
1666        let err = match vm.get_fn("vm_bare_return_conflict::run", &[Type::Bool]) {
1667            Ok(_) => panic!("expected mismatched return types to fail"),
1668            Err(err) => err,
1669        };
1670        assert!(format!("{err:#}").contains("返回类型不一致"));
1671        Ok(())
1672    }
1673
1674    #[test]
1675    fn root_get_accepts_string_concat_with_dynamic_field() -> anyhow::Result<()> {
1676        let vm = Vm::with_all()?;
1677        vm.import_code(
1678            "vm_root_get_dynamic_concat",
1679            br#"
1680            pub fn get_action(req) {
1681                root::get("local/game/panel_actions/" + req.idx)
1682            }
1683            "#
1684            .to_vec(),
1685        )?;
1686
1687        root::add("local/game/panel_actions/7", dynamic::map!("id"=> "action-7").into())?;
1688        let compiled = vm.get_fn("vm_root_get_dynamic_concat::get_action", &[Type::Any])?;
1689        assert_eq!(compiled.ret_ty(), &Type::Any);
1690        let get_action: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
1691        let req = dynamic::map!("idx"=> 7i64);
1692        let result = unsafe { &*get_action(&req) };
1693
1694        assert_eq!(result.get_dynamic("id").map(|value| value.as_str().to_string()), Some("action-7".to_string()));
1695        Ok(())
1696    }
1697
1698    #[test]
1699    fn root_add_fn_registers_handler_with_dynamic_field_path_concat() -> anyhow::Result<()> {
1700        let vm = Vm::with_all()?;
1701        vm.import_code(
1702            "vm_registered_panel_action",
1703            br#"
1704            pub fn panel_action(req) {
1705                root::get("local/game/panel_actions/" + req.idx)
1706            }
1707
1708            pub fn register() {
1709                root::add_fn("local/ui/panel_action", "vm_registered_panel_action::panel_action")
1710            }
1711            "#
1712            .to_vec(),
1713        )?;
1714
1715        let compiled = vm.get_fn("vm_registered_panel_action::register", &[])?;
1716        assert_eq!(compiled.ret_ty(), &Type::Bool);
1717        let register: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1718        assert!(register());
1719        Ok(())
1720    }
1721
1722    #[test]
1723    fn std_spawn_runs_named_function_with_tuple_args() -> anyhow::Result<()> {
1724        let zero_path = "local/vm_std_spawn/zero";
1725        let sum_path = "local/vm_std_spawn/sum";
1726        let closure_path = "local/vm_std_spawn/closure";
1727        let closure_vars_path = "local/vm_std_spawn/closure_vars";
1728        let _ = root::remove(zero_path);
1729        let _ = root::remove(sum_path);
1730        let _ = root::remove(closure_path);
1731        let _ = root::remove(closure_vars_path);
1732        let vm = Vm::with_all()?;
1733        vm.import_code(
1734            "vm_std_spawn",
1735            br#"
1736            pub fn zero() {
1737                root::add("local/vm_std_spawn/zero", 1);
1738            }
1739
1740            pub fn job(left, right) {
1741                root::add("local/vm_std_spawn/sum", left + right);
1742            }
1743
1744            pub fn start_zero() {
1745                spawn("vm_std_spawn::zero", ())
1746            }
1747
1748            pub fn start_sum() {
1749                spawn("vm_std_spawn::job", (10, 20))
1750            }
1751
1752            pub fn start_closure() {
1753                spawn(|x, y| {
1754                    root::add("local/vm_std_spawn/closure", x + y);
1755                }, (3, 4))
1756            }
1757
1758            pub fn start_closure_vars() {
1759                let x = 5;
1760                let y = 6;
1761                spawn(|left, right| {
1762                    root::add("local/vm_std_spawn/closure_vars", left + right);
1763                }, (x, y))
1764            }
1765            "#
1766            .to_vec(),
1767        )?;
1768
1769        let compiled = vm.get_fn("vm_std_spawn::start_zero", &[])?;
1770        assert_eq!(compiled.ret_ty(), &Type::Bool);
1771        let start_zero: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1772        assert!(start_zero());
1773
1774        let compiled = vm.get_fn("vm_std_spawn::start_sum", &[])?;
1775        assert_eq!(compiled.ret_ty(), &Type::Bool);
1776        let start_sum: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1777        assert!(start_sum());
1778
1779        let compiled = vm.get_fn("vm_std_spawn::start_closure", &[])?;
1780        assert_eq!(compiled.ret_ty(), &Type::Bool);
1781        let start_closure: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1782        assert!(start_closure());
1783
1784        let compiled = vm.get_fn("vm_std_spawn::start_closure_vars", &[])?;
1785        assert_eq!(compiled.ret_ty(), &Type::Bool);
1786        let start_closure_vars: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1787        assert!(start_closure_vars());
1788
1789        for _ in 0..50 {
1790            let zero_done = root::get(zero_path).ok().and_then(|value| value.as_int()) == Some(1);
1791            let sum_done = root::get(sum_path).ok().and_then(|value| value.as_int()) == Some(30);
1792            let closure_done = root::get(closure_path).ok().and_then(|value| value.as_int()) == Some(7);
1793            let closure_vars_done = root::get(closure_vars_path).ok().and_then(|value| value.as_int()) == Some(11);
1794            if zero_done && sum_done && closure_done && closure_vars_done {
1795                return Ok(());
1796            }
1797            std::thread::sleep(std::time::Duration::from_millis(10));
1798        }
1799
1800        anyhow::bail!("spawned jobs did not write expected results");
1801    }
1802
1803    #[test]
1804    fn native_can_save_and_later_call_closure_callback() -> anyhow::Result<()> {
1805        static SAVED_CALLBACK: Mutex<Option<ZustCallback>> = Mutex::new(None);
1806
1807        extern "C" fn save_callback(callback: *const Dynamic) -> bool {
1808            if callback.is_null() {
1809                return false;
1810            }
1811            let Some(callback) = (unsafe { &*callback }).as_custom::<ZustCallback>().cloned() else {
1812                return false;
1813            };
1814            *SAVED_CALLBACK.lock().unwrap() = Some(callback);
1815            true
1816        }
1817
1818        let path = "local/vm_callback/result";
1819        let _ = root::remove(path);
1820        *SAVED_CALLBACK.lock().unwrap() = None;
1821
1822        let vm = Vm::with_all()?;
1823        vm.add_native_module_ptr("callback_test", "save", &[Type::Any], Type::Bool, save_callback as *const u8)?;
1824        vm.import_code(
1825            "vm_callback",
1826            br#"
1827            pub fn register() {
1828                let n = 41;
1829                callback_test::save(|| {
1830                    root::add("local/vm_callback/result", n + 1);
1831                    true
1832                })
1833            }
1834            "#
1835            .to_vec(),
1836        )?;
1837
1838        let compiled = vm.get_fn("vm_callback::register", &[])?;
1839        assert_eq!(compiled.ret_ty(), &Type::Bool);
1840        let register: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
1841        assert!(register());
1842        assert!(root::get(path).is_err());
1843
1844        let callback = SAVED_CALLBACK.lock().unwrap().clone().expect("callback should be saved");
1845        let result = callback.call0()?;
1846        assert_eq!(result.as_bool(), Some(true));
1847        assert_eq!(root::get(path)?.as_int(), Some(42));
1848        Ok(())
1849    }
1850
1851    #[test]
1852    fn closure_captures_share_state_between_callbacks() -> anyhow::Result<()> {
1853        static SAVED_CALLBACKS: Mutex<Vec<ZustCallback>> = Mutex::new(Vec::new());
1854
1855        extern "C" fn save_callback(callback: *const Dynamic) -> bool {
1856            if callback.is_null() {
1857                return false;
1858            }
1859            let Some(callback) = (unsafe { &*callback }).as_custom::<ZustCallback>().cloned() else {
1860                return false;
1861            };
1862            SAVED_CALLBACKS.lock().unwrap().push(callback);
1863            true
1864        }
1865
1866        SAVED_CALLBACKS.lock().unwrap().clear();
1867
1868        let vm = Vm::with_all()?;
1869        vm.add_native_module_ptr("capture_test", "save", &[Type::Any], Type::Bool, save_callback as *const u8)?;
1870        vm.import_code(
1871            "vm_shared_capture",
1872            br#"
1873            pub fn register() {
1874                let state = {};
1875                state.drag_kind = 0;
1876                capture_test::save(|| {
1877                    state.drag_kind = 2;
1878                    true
1879                });
1880                capture_test::save(|| {
1881                    state.drag_kind
1882                })
1883            }
1884            "#
1885            .to_vec(),
1886        )?;
1887
1888        let register = vm.get_fn("vm_shared_capture::register", &[])?;
1889        let register: extern "C" fn() -> bool = unsafe { std::mem::transmute(register.ptr()) };
1890        assert!(register());
1891
1892        let (writer, reader) = {
1893            let saved = SAVED_CALLBACKS.lock().unwrap();
1894            assert_eq!(saved.len(), 2);
1895            (saved[0].clone(), saved[1].clone())
1896        };
1897        assert_eq!(reader.call0()?.as_int(), Some(0));
1898        assert_eq!(writer.call0()?.as_bool(), Some(true));
1899        assert_eq!(reader.call0()?.as_int(), Some(2));
1900        Ok(())
1901    }
1902
1903    #[test]
1904    fn native_can_save_and_later_call_named_function_callback() -> anyhow::Result<()> {
1905        static SAVED_CALLBACK: Mutex<Option<ZustCallback>> = Mutex::new(None);
1906
1907        extern "C" fn save_callback(callback: *const Dynamic) -> bool {
1908            if callback.is_null() {
1909                return false;
1910            }
1911            let Some(callback) = (unsafe { &*callback }).as_custom::<ZustCallback>().cloned() else {
1912                return false;
1913            };
1914            *SAVED_CALLBACK.lock().unwrap() = Some(callback);
1915            true
1916        }
1917
1918        let path = "local/vm_named_callback/result";
1919        let _ = root::remove(path);
1920        *SAVED_CALLBACK.lock().unwrap() = None;
1921
1922        let vm = Vm::with_all()?;
1923        vm.add_native_module_ptr("callback_test", "save", &[Type::Any], Type::Bool, save_callback as *const u8)?;
1924        vm.import_code(
1925            "vm_named_callback",
1926            br#"
1927            pub fn on_result() {
1928                root::add("local/vm_named_callback/result", "done");
1929                true
1930            }
1931
1932            pub fn register() {
1933                callback_test::save(on_result)
1934            }
1935            "#
1936            .to_vec(),
1937        )?;
1938
1939        let register = vm.get_fn("vm_named_callback::register", &[])?;
1940        let register: extern "C" fn() -> bool = unsafe { std::mem::transmute(register.ptr()) };
1941        assert!(register());
1942        assert!(root::get(path).is_err());
1943
1944        let callback = SAVED_CALLBACK.lock().unwrap().clone().expect("callback should be saved");
1945        assert_eq!(callback.call1(dynamic::map!("text"=> "done"))?.as_bool(), Some(true));
1946        assert_eq!(root::get(path)?.as_str(), "done");
1947        Ok(())
1948    }
1949
1950    #[test]
1951    fn native_callback_can_receive_later_dynamic_args() -> anyhow::Result<()> {
1952        static SAVED_PATH_CALLBACK: Mutex<Option<ZustCallback>> = Mutex::new(None);
1953        static SAVED_SUM_CALLBACK: Mutex<Option<ZustCallback>> = Mutex::new(None);
1954
1955        extern "C" fn save_path_callback(callback: *const Dynamic) -> bool {
1956            if callback.is_null() {
1957                return false;
1958            }
1959            let Some(callback) = (unsafe { &*callback }).as_custom::<ZustCallback>().cloned() else {
1960                return false;
1961            };
1962            *SAVED_PATH_CALLBACK.lock().unwrap() = Some(callback);
1963            true
1964        }
1965
1966        extern "C" fn save_sum_callback(callback: *const Dynamic) -> bool {
1967            if callback.is_null() {
1968                return false;
1969            }
1970            let Some(callback) = (unsafe { &*callback }).as_custom::<ZustCallback>().cloned() else {
1971                return false;
1972            };
1973            *SAVED_SUM_CALLBACK.lock().unwrap() = Some(callback);
1974            true
1975        }
1976
1977        let path_result = "local/vm_callback/path";
1978        let sum_result = "local/vm_callback/sum8";
1979        let _ = root::remove(path_result);
1980        let _ = root::remove(sum_result);
1981        *SAVED_PATH_CALLBACK.lock().unwrap() = None;
1982        *SAVED_SUM_CALLBACK.lock().unwrap() = None;
1983
1984        let vm = Vm::with_all()?;
1985        vm.add_native_module_ptr("callback_test", "save_path", &[Type::Any], Type::Bool, save_path_callback as *const u8)?;
1986        vm.add_native_module_ptr("callback_test", "save_sum", &[Type::Any], Type::Bool, save_sum_callback as *const u8)?;
1987        vm.import_code(
1988            "vm_callback_args",
1989            br#"
1990            pub fn register_path() {
1991                let key = "local/vm_callback/path";
1992                callback_test::save_path(|path| {
1993                    root::add(key, path);
1994                    true
1995                })
1996            }
1997
1998            pub fn register_sum() {
1999                callback_test::save_sum(|a, b, c, d, e, f, g, h| {
2000                    root::add("local/vm_callback/sum8", a + b + c + d + e + f + g + h);
2001                    true
2002                })
2003            }
2004            "#
2005            .to_vec(),
2006        )?;
2007
2008        let register_path = vm.get_fn("vm_callback_args::register_path", &[])?;
2009        let register_path: extern "C" fn() -> bool = unsafe { std::mem::transmute(register_path.ptr()) };
2010        assert!(register_path());
2011
2012        let register_sum = vm.get_fn("vm_callback_args::register_sum", &[])?;
2013        let register_sum: extern "C" fn() -> bool = unsafe { std::mem::transmute(register_sum.ptr()) };
2014        assert!(register_sum());
2015
2016        let path_callback = SAVED_PATH_CALLBACK.lock().unwrap().clone().expect("path callback should be saved");
2017        assert_eq!(path_callback.call1(Dynamic::from("picked.txt"))?.as_bool(), Some(true));
2018        assert_eq!(root::get(path_result)?.as_str(), "picked.txt");
2019
2020        let sum_callback = SAVED_SUM_CALLBACK.lock().unwrap().clone().expect("sum callback should be saved");
2021        let sum_args = (1i64..=8).map(Dynamic::from).collect();
2022        assert_eq!(sum_callback.call(sum_args)?.as_bool(), Some(true));
2023        assert_eq!(root::get(sum_result)?.as_int(), Some(36));
2024        Ok(())
2025    }
2026
2027    #[test]
2028    fn callback_with_16_explicit_args_and_captures() -> anyhow::Result<()> {
2029        static SAVED_SUM16: Mutex<Option<ZustCallback>> = Mutex::new(None);
2030
2031        extern "C" fn save_sum16(callback: *const Dynamic) -> bool {
2032            if callback.is_null() {
2033                return false;
2034            }
2035            let Some(callback) = (unsafe { &*callback }).as_custom::<ZustCallback>().cloned() else {
2036                return false;
2037            };
2038            *SAVED_SUM16.lock().unwrap() = Some(callback);
2039            true
2040        }
2041
2042        let sum16_path = "local/vm_callback/sum16";
2043        let _ = root::remove(sum16_path);
2044        *SAVED_SUM16.lock().unwrap() = None;
2045
2046        let vm = Vm::with_all()?;
2047        vm.add_native_module_ptr("callback_test", "save_sum16", &[Type::Any], Type::Bool, save_sum16 as *const u8)?;
2048        vm.import_code(
2049            "vm_callback_16_args",
2050            br#"
2051            pub fn register_sum16() {
2052                let prefix = "sum=";
2053                callback_test::save_sum16(|a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p| {
2054                    let total = a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p;
2055                    root::add("local/vm_callback/sum16", prefix + total);
2056                    true
2057                })
2058            }
2059            "#
2060            .to_vec(),
2061        )?;
2062
2063        let register = vm.get_fn("vm_callback_16_args::register_sum16", &[])?;
2064        let register: extern "C" fn() -> bool = unsafe { std::mem::transmute(register.ptr()) };
2065        assert!(register());
2066
2067        let callback = SAVED_SUM16.lock().unwrap().clone().expect("sum16 callback saved");
2068        let args: Vec<Dynamic> = (1i64..=16).map(Dynamic::from).collect();
2069        assert_eq!(callback.call(args)?.as_bool(), Some(true));
2070        assert_eq!(root::get(sum16_path)?.as_str(), "sum=136");
2071        Ok(())
2072    }
2073
2074    #[test]
2075    fn spawn_closure_with_16_args() -> anyhow::Result<()> {
2076        let spawn16_path = "local/vm_spawn/spawn16";
2077        let _ = root::remove(spawn16_path);
2078
2079        let vm = Vm::with_all()?;
2080        vm.import_code(
2081            "vm_spawn_16_args",
2082            br#"
2083            pub fn start_spawn16() {
2084                spawn(|a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p| {
2085                    root::add("local/vm_spawn/spawn16", a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p);
2086                }, (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))
2087            }
2088            "#
2089            .to_vec(),
2090        )?;
2091
2092        let compiled = vm.get_fn("vm_spawn_16_args::start_spawn16", &[])?;
2093        assert_eq!(compiled.ret_ty(), &Type::Bool);
2094        let start: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
2095        assert!(start());
2096
2097        for _ in 0..50 {
2098            if root::get(spawn16_path).ok().and_then(|v| v.as_int()) == Some(136) {
2099                return Ok(());
2100            }
2101            std::thread::sleep(std::time::Duration::from_millis(10));
2102        }
2103        anyhow::bail!("spawned job did not write expected result");
2104    }
2105
2106    #[test]
2107    fn spawn_native_closure_avoids_any_boxing() -> anyhow::Result<()> {
2108        let nat_path = "local/vm_spawn_native/result";
2109        let _ = root::remove(nat_path);
2110        let vm = Vm::with_all()?;
2111        vm.import_code(
2112            "vm_spawn_native",
2113            br#"
2114            pub fn start() {
2115                spawn(|x: i64, y: i64| {
2116                    root::add("local/vm_spawn_native/result", x + y);
2117                }, (10i64, 20i64))
2118            }
2119            "#
2120            .to_vec(),
2121        )?;
2122        let compiled = vm.get_fn("vm_spawn_native::start", &[])?;
2123        assert_eq!(compiled.ret_ty(), &Type::Bool);
2124        let start: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
2125        assert!(start());
2126        for _ in 0..50 {
2127            if root::get(nat_path).ok().and_then(|v| v.as_int()) == Some(30) {
2128                return Ok(());
2129            }
2130            std::thread::sleep(std::time::Duration::from_millis(10));
2131        }
2132        anyhow::bail!("spawned native closure did not write expected result");
2133    }
2134
2135    #[test]
2136    fn multi_level_nested_closure_captures() -> anyhow::Result<()> {
2137        let vm = Vm::with_all()?;
2138        vm.import_code(
2139            "vm_multi_level_captures",
2140            br#"
2141            pub fn run() {
2142                let level1 = "L1";
2143                let level2 = "L2";
2144                |path: string| {
2145                    let level3 = "L3";
2146                    let inner = |suffix: string| {
2147                        let level4 = "L4";
2148                        |flag: bool| {
2149                            if flag {
2150                                level1 + "." + level2 + "." + level3 + "." + level4 + "." + path + suffix
2151                            } else {
2152                                "off"
2153                            }
2154                        }(true)
2155                    };
2156                    inner(".ext")
2157                }("file.txt")
2158            }
2159            "#
2160            .to_vec(),
2161        )?;
2162
2163        let compiled = vm.get_fn("vm_multi_level_captures::run", &[])?;
2164        assert_eq!(compiled.ret_ty(), &Type::Any);
2165        let run: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2166        let result = unsafe { &*run() };
2167        assert_eq!(result.as_str(), "L1.L2.L3.L4.file.txt.ext");
2168        Ok(())
2169    }
2170
2171    #[test]
2172    fn root_add_fn_accepts_string_concat_in_registered_handler() -> anyhow::Result<()> {
2173        let vm = Vm::with_all()?;
2174        vm.import_code(
2175            "vm_registered_string_concat",
2176            br#"
2177            pub fn send_panel(idx: i64) {
2178                let idx_key = "" + idx;
2179                idx_key
2180            }
2181            "#
2182            .to_vec(),
2183        )?;
2184
2185        assert!(vm.get_fn_ptr("vm_registered_string_concat::send_panel", &[Type::Any]).is_ok());
2186        Ok(())
2187    }
2188
2189    #[test]
2190    fn root_send_idx_returns_handler_value() -> anyhow::Result<()> {
2191        fn echo_handler(msg: Dynamic) -> Dynamic {
2192            dynamic::map!("type"=> "echo", "id"=> msg.get_dynamic("id").unwrap_or(Dynamic::Null))
2193        }
2194
2195        let vm = Vm::with_all()?;
2196        vm.import_code(
2197            "vm_root_send_idx_return",
2198            br#"
2199            pub fn call(req) {
2200                root::send_idx("local/send_idx_return_handlers", 0, req)
2201            }
2202            "#
2203            .to_vec(),
2204        )?;
2205
2206        root::add_list("local/send_idx_return_handlers")?;
2207        let (mount, name) = root::get_mount("local/send_idx_return_handlers")?;
2208        mount.push(name, root::Object::Native(echo_handler))?;
2209
2210        assert_eq!(vm.infer("root::send_idx", &[Type::Any, Type::I64, Type::Any])?, Type::Any);
2211        let compiled = vm.get_fn("vm_root_send_idx_return::call", &[Type::Any])?;
2212        assert_eq!(compiled.ret_ty(), &Type::Any);
2213        let call: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2214        let req = dynamic::map!("id"=> 42i64);
2215        let result = unsafe { &*call(&req) };
2216
2217        assert_eq!(result.get_dynamic("type").map(|value| value.as_str().to_string()), Some("echo".to_string()));
2218        assert_eq!(result.get_dynamic("id").and_then(|value| value.as_int()), Some(42));
2219        Ok(())
2220    }
2221
2222    #[test]
2223    fn compiles_public_hotspots_with_string_paths_and_keys() -> anyhow::Result<()> {
2224        let vm = Vm::with_all()?;
2225        vm.import_code(
2226            "vm_public_hotspots",
2227            br#"
2228            pub fn public_hotspot(action_map_path, panel_id, action_id, hotspot) {
2229                {
2230                    path: action_map_path,
2231                    panel_id: panel_id,
2232                    action_id: action_id,
2233                    id: hotspot.id
2234                }
2235            }
2236
2237            pub fn public_hotspots(idx, panel_id, hotspots) {
2238                let idx_key = "" + idx;
2239                let action_map_path = "local/game/panel_actions/" + idx_key;
2240
2241                let existing_action_map = root::get(action_map_path);
2242                if !existing_action_map.is_map() {
2243                    root::add_map(action_map_path);
2244                }
2245
2246                if hotspots.is_map() {
2247                    let public_items = {};
2248                    for action_id in hotspots.keys() {
2249                        public_items[action_id] = public_hotspot(action_map_path, panel_id, action_id, hotspots[action_id]);
2250                    }
2251                    return public_items;
2252                }
2253
2254                let public_items = [];
2255                let i = 0;
2256                while i < hotspots.len() {
2257                    let hotspot = hotspots.get_idx(i);
2258                    let item = public_hotspot(action_map_path, panel_id, hotspot.id, hotspot);
2259                    public_items.push(item);
2260                    i = i + 1;
2261                }
2262
2263                public_items
2264            }
2265            "#
2266            .to_vec(),
2267        )?;
2268
2269        assert!(vm.get_fn("vm_public_hotspots::public_hotspots", &[Type::I64, Type::Any, Type::Any]).is_ok());
2270        assert!(vm.get_fn("vm_public_hotspots::public_hotspots", &[Type::Any, Type::Any, Type::Any]).is_ok());
2271        Ok(())
2272    }
2273
2274    #[test]
2275    fn send_panel_calls_public_hotspots_with_dynamic_request() -> anyhow::Result<()> {
2276        let vm = Vm::with_all()?;
2277        vm.import_code(
2278            "vm_send_panel_public_hotspots",
2279            br#"
2280            pub fn ok(value) {
2281                value
2282            }
2283
2284            pub fn panel_from_node(req) {
2285                {
2286                    panel_id: req.panel_id,
2287                    hotspots: req.hotspots
2288                }
2289            }
2290
2291            pub fn public_hotspot(action_map_path, panel_id, action_id, hotspot) {
2292                {
2293                    path: action_map_path,
2294                    panel_id: panel_id,
2295                    action_id: action_id,
2296                    id: hotspot.id
2297                }
2298            }
2299
2300            pub fn public_hotspots(idx, panel_id, hotspots) {
2301                let idx_key = "" + idx;
2302                let action_map_path = "local/game/panel_actions/" + idx_key;
2303
2304                let existing_action_map = root::get(action_map_path);
2305                if !existing_action_map.is_map() {
2306                    root::add_map(action_map_path);
2307                }
2308
2309                if hotspots.is_map() {
2310                    let public_items = {};
2311                    for action_id in hotspots.keys() {
2312                        public_items[action_id] = public_hotspot(action_map_path, panel_id, action_id, hotspots[action_id]);
2313                    }
2314                    return public_items;
2315                }
2316
2317                let public_items = [];
2318                let i = 0;
2319                while i < hotspots.len() {
2320                    let hotspot = hotspots.get_idx(i);
2321                    let item = public_hotspot(action_map_path, panel_id, hotspot.id, hotspot);
2322                    public_items.push(item);
2323                    i = i + 1;
2324                }
2325
2326                public_items
2327            }
2328
2329            pub fn send_panel(req) {
2330                let panel = req.panel;
2331                if !panel.is_map() {
2332                    panel = panel_from_node(req);
2333                }
2334                if !panel.is_map() {
2335                    return ok({
2336                        id: 4,
2337                        type: "panel_rejected",
2338                        reason: "invalid panel"
2339                    });
2340                }
2341                panel.id = 4;
2342                panel.idx = req.idx;
2343                if !panel.contains("type") {
2344                    panel.type = "panel";
2345                }
2346                if panel.contains("hotspots") {
2347                    panel.hotspots = public_hotspots(req.idx, panel.panel_id, panel.hotspots);
2348                }
2349                root::send_idx("local/ws", req.idx, panel);
2350                ok({
2351                    id: 4,
2352                    type: "panel",
2353                    panel_id: panel.panel_id
2354                })
2355            }
2356            "#
2357            .to_vec(),
2358        )?;
2359
2360        let compiled = vm.get_fn("vm_send_panel_public_hotspots::send_panel", &[Type::Any])?;
2361        assert_eq!(compiled.ret_ty(), &Type::Any);
2362        let send_panel: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2363        let req = dynamic::map!(
2364            "idx"=> 7i64,
2365            "panel"=> dynamic::map!(
2366                "panel_id"=> "main",
2367                "hotspots"=> dynamic::map!(
2368                    "open"=> dynamic::map!("id"=> "open")
2369                )
2370            )
2371        );
2372        let result = unsafe { &*send_panel(&req) };
2373
2374        assert_eq!(result.get_dynamic("type").map(|value| value.as_str().to_string()), Some("panel".to_string()));
2375        assert_eq!(result.get_dynamic("panel_id").map(|value| value.as_str().to_string()), Some("main".to_string()));
2376        Ok(())
2377    }
2378
2379    #[test]
2380    fn map_assignment_accepts_string_concat_key() -> anyhow::Result<()> {
2381        let vm = Vm::with_all()?;
2382        vm.import_code(
2383            "vm_string_concat_map_key",
2384            br##"
2385            pub fn write_action(action_map, panel_id, action_id, action) {
2386                action_map[panel_id + "#" + action_id] = action;
2387                action_map[panel_id + "#" + action_id]
2388            }
2389            "##
2390            .to_vec(),
2391        )?;
2392
2393        let compiled = vm.get_fn("vm_string_concat_map_key::write_action", &[Type::Any, Type::Any, Type::Any, Type::Any])?;
2394        let write_action: extern "C" fn(*const Dynamic, *const Dynamic, *const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2395        let action_map = dynamic::map!();
2396        let panel_id: Dynamic = "panel".into();
2397        let action_id: Dynamic = "open".into();
2398        let action = dynamic::map!("id"=> "open");
2399
2400        let result = unsafe { &*write_action(&action_map, &panel_id, &action_id, &action) };
2401
2402        assert_eq!(result.get_dynamic("id").map(|value| value.as_str().to_string()), Some("open".to_string()));
2403        assert_eq!(action_map.get_dynamic("panel#open").and_then(|value| value.get_dynamic("id")).map(|value| value.as_str().to_string()), Some("open".to_string()));
2404        Ok(())
2405    }
2406
2407    #[test]
2408    fn map_get_key_accepts_string_concat_key_variable() -> anyhow::Result<()> {
2409        let vm = Vm::with_all()?;
2410        vm.import_code(
2411            "vm_get_key_string_concat_key",
2412            br##"
2413            pub fn read_action(action_map, panel_id, action_id) {
2414                let action_key = panel_id + "#" + action_id;
2415                action_map.get_key(action_key)
2416            }
2417            "##
2418            .to_vec(),
2419        )?;
2420
2421        let compiled = vm.get_fn("vm_get_key_string_concat_key::read_action", &[Type::Any, Type::Any, Type::Any])?;
2422        let read_action: extern "C" fn(*const Dynamic, *const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2423        let action_map = dynamic::map!("panel#open"=> dynamic::map!("id"=> "open"));
2424        let panel_id: Dynamic = "panel".into();
2425        let action_id: Dynamic = "open".into();
2426
2427        let result = unsafe { &*read_action(&action_map, &panel_id, &action_id) };
2428
2429        assert_eq!(result.get_dynamic("id").map(|value| value.as_str().to_string()), Some("open".to_string()));
2430        Ok(())
2431    }
2432
2433    #[test]
2434    fn map_get_key_accepts_helper_string_key() -> anyhow::Result<()> {
2435        let vm = Vm::with_all()?;
2436        vm.import_code(
2437            "vm_get_key_helper_string_key",
2438            br##"
2439            pub fn make_action_key(panel_id, action_id) {
2440                panel_id + "#" + action_id
2441            }
2442
2443            pub fn read_action(action_map, panel_id, action_id) {
2444                let action_key = make_action_key(panel_id, action_id);
2445                let action = action_map.get_key(action_key);
2446                action
2447            }
2448            "##
2449            .to_vec(),
2450        )?;
2451
2452        let compiled = vm.get_fn("vm_get_key_helper_string_key::read_action", &[Type::Any, Type::Any, Type::Any])?;
2453        let read_action: extern "C" fn(*const Dynamic, *const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2454        let action_map = dynamic::map!("panel#open"=> dynamic::map!("id"=> "open"));
2455        let panel_id: Dynamic = "panel".into();
2456        let action_id: Dynamic = "open".into();
2457
2458        let result = unsafe { &*read_action(&action_map, &panel_id, &action_id) };
2459
2460        assert_eq!(result.get_dynamic("id").map(|value| value.as_str().to_string()), Some("open".to_string()));
2461        Ok(())
2462    }
2463
2464    #[test]
2465    fn map_del_key_removes_string_key_and_returns_removed_value() -> anyhow::Result<()> {
2466        let vm = Vm::with_all()?;
2467        vm.import_code(
2468            "vm_del_key_string_key",
2469            br##"
2470            pub fn remove_action(action_map, panel_id, action_id) {
2471                let action_key = panel_id + "#" + action_id;
2472                let removed = action_map.del_key(action_key);
2473                [removed, action_map.get_key(action_key)]
2474            }
2475            "##
2476            .to_vec(),
2477        )?;
2478
2479        let compiled = vm.get_fn("vm_del_key_string_key::remove_action", &[Type::Any, Type::Any, Type::Any])?;
2480        let remove_action: extern "C" fn(*const Dynamic, *const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2481        let action_map = dynamic::map!("panel#open"=> dynamic::map!("id"=> "open"));
2482        let panel_id: Dynamic = "panel".into();
2483        let action_id: Dynamic = "open".into();
2484
2485        let result = unsafe { &*remove_action(&action_map, &panel_id, &action_id) };
2486
2487        assert_eq!(result.get_idx(0).and_then(|value| value.get_dynamic("id")).map(|value| value.as_str().to_string()), Some("open".to_string()));
2488        assert!(result.get_idx(1).is_some_and(|value| value.is_null()));
2489        assert!(action_map.get_dynamic("panel#open").is_none());
2490        Ok(())
2491    }
2492
2493    #[test]
2494    fn dynamic_field_value_participates_in_or_expression() -> anyhow::Result<()> {
2495        let vm = Vm::with_all()?;
2496        vm.import_code(
2497            "vm_dynamic_field_or",
2498            r#"
2499            pub fn direct_next() {
2500                let choice = {
2501                    label: "颜色",
2502                    next: "color"
2503                };
2504                choice.next
2505            }
2506
2507            pub fn bracket_next() {
2508                let choice = {
2509                    label: "颜色",
2510                    next: "color"
2511                };
2512                choice["next"]
2513            }
2514            "#
2515            .as_bytes()
2516            .to_vec(),
2517        )?;
2518
2519        let compiled = vm.get_fn("vm_dynamic_field_or::direct_next", &[])?;
2520        assert_eq!(compiled.ret_ty(), &Type::Any);
2521        let direct_next: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2522        assert_eq!(unsafe { &*direct_next() }.as_str(), "color");
2523
2524        let compiled = vm.get_fn("vm_dynamic_field_or::bracket_next", &[])?;
2525        assert_eq!(compiled.ret_ty(), &Type::Any);
2526        let bracket_next: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2527        assert_eq!(unsafe { &*bracket_next() }.as_str(), "color");
2528        Ok(())
2529    }
2530
2531    #[test]
2532    fn empty_object_literal_in_if_branch_stays_dynamic() -> anyhow::Result<()> {
2533        let vm = Vm::with_all()?;
2534        vm.import_code(
2535            "vm_if_empty_object_branch",
2536            r#"
2537            pub fn first_note(steps) {
2538                let first = if steps.len() > 0 { steps[0] } else { {} };
2539                let first_note = if first.contains("note") { first.note } else { "fallback" };
2540                first_note
2541            }
2542
2543            pub fn first_ja(steps) {
2544                let first = if steps.len() > 0 { steps[0] } else { {} };
2545                if first.contains("ja") { first.ja } else { "すみません" }
2546            }
2547
2548            pub fn assign_first_note(steps) {
2549                let first = {};
2550                first = if steps.len() > 0 { steps[0] } else { {} };
2551                if first.contains("note") { first.note } else { "fallback" }
2552            }
2553            "#
2554            .as_bytes()
2555            .to_vec(),
2556        )?;
2557
2558        let compiled = vm.get_fn("vm_if_empty_object_branch::first_note", &[Type::Any])?;
2559        assert_eq!(compiled.ret_ty(), &Type::Str);
2560        let first_note: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2561
2562        let empty_steps = Dynamic::list(Vec::new());
2563        assert_eq!(unsafe { &*first_note(&empty_steps) }.as_str(), "fallback");
2564
2565        let mut step = std::collections::BTreeMap::new();
2566        step.insert("note".into(), "hello".into());
2567        let steps = Dynamic::list(vec![Dynamic::map(step)]);
2568        assert_eq!(unsafe { &*first_note(&steps) }.as_str(), "hello");
2569
2570        let compiled = vm.get_fn("vm_if_empty_object_branch::first_ja", &[Type::Any])?;
2571        assert_eq!(compiled.ret_ty(), &Type::Any);
2572        let first_ja: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2573        assert_eq!(unsafe { &*first_ja(&empty_steps) }.as_str(), "すみません");
2574
2575        let compiled = vm.get_fn("vm_if_empty_object_branch::assign_first_note", &[Type::Any])?;
2576        assert_eq!(compiled.ret_ty(), &Type::Any);
2577        let assign_first_note: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2578        assert_eq!(unsafe { &*assign_first_note(&empty_steps) }.as_str(), "fallback");
2579        assert_eq!(unsafe { &*assign_first_note(&steps) }.as_str(), "hello");
2580        Ok(())
2581    }
2582
2583    #[test]
2584    fn list_literal_can_be_function_tail_expression() -> anyhow::Result<()> {
2585        let vm = Vm::with_all()?;
2586        vm.import_code(
2587            "vm_tail_list_literal",
2588            r#"
2589            pub fn numbers() {
2590                [1, 2, 3]
2591            }
2592
2593            pub fn maps() {
2594                [
2595                    {note: "first"},
2596                    {note: "second"}
2597                ]
2598            }
2599
2600            pub fn object_with_maps() {
2601                {
2602                    steps: [
2603                        {note: "first"},
2604                        {note: "second"}
2605                    ]
2606                }
2607            }
2608
2609            pub fn return_maps() {
2610                return [
2611                    {note: "first"},
2612                    {note: "second"}
2613                ];
2614            }
2615
2616            pub fn return_maps_without_semicolon() {
2617                return [
2618                    {note: "first"},
2619                    {note: "second"}
2620                ]
2621            }
2622
2623            pub fn tail_bare_variable() {
2624                let value = [
2625                    {note: "first"},
2626                    {note: "second"}
2627                ];
2628                value
2629            }
2630
2631            pub fn return_bare_variable_without_semicolon() {
2632                let value = [
2633                    {note: "first"},
2634                    {note: "second"}
2635                ];
2636                return value
2637            }
2638
2639            pub fn tail_object_variable() {
2640                let result = {
2641                    steps: [
2642                        {note: "first"},
2643                        {note: "second"}
2644                    ]
2645                };
2646                result
2647            }
2648            "#
2649            .as_bytes()
2650            .to_vec(),
2651        )?;
2652
2653        let compiled = vm.get_fn("vm_tail_list_literal::numbers", &[])?;
2654        assert_eq!(compiled.ret_ty(), &Type::Any);
2655        let numbers: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2656        let result = unsafe { &*numbers() };
2657        assert_eq!(result.len(), 3);
2658        assert_eq!(result.get_idx(1).and_then(|value| value.as_int()), Some(2));
2659
2660        let compiled = vm.get_fn("vm_tail_list_literal::maps", &[])?;
2661        assert_eq!(compiled.ret_ty(), &Type::Any);
2662        let maps: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2663        let result = unsafe { &*maps() };
2664        assert_eq!(result.len(), 2);
2665        assert_eq!(result.get_idx(1).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("second".to_string()));
2666
2667        let compiled = vm.get_fn("vm_tail_list_literal::object_with_maps", &[])?;
2668        assert_eq!(compiled.ret_ty(), &Type::Any);
2669        let object_with_maps: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2670        let result = unsafe { &*object_with_maps() };
2671        let steps = result.get_dynamic("steps").expect("steps");
2672        assert_eq!(steps.len(), 2);
2673        assert_eq!(steps.get_idx(0).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("first".to_string()));
2674
2675        let compiled = vm.get_fn("vm_tail_list_literal::return_maps", &[])?;
2676        assert_eq!(compiled.ret_ty(), &Type::Any);
2677        let return_maps: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2678        let result = unsafe { &*return_maps() };
2679        assert_eq!(result.len(), 2);
2680        assert_eq!(result.get_idx(1).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("second".to_string()));
2681
2682        let compiled = vm.get_fn("vm_tail_list_literal::return_maps_without_semicolon", &[])?;
2683        assert_eq!(compiled.ret_ty(), &Type::Any);
2684        let return_maps_without_semicolon: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2685        let result = unsafe { &*return_maps_without_semicolon() };
2686        assert_eq!(result.len(), 2);
2687        assert_eq!(result.get_idx(0).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("first".to_string()));
2688
2689        let compiled = vm.get_fn("vm_tail_list_literal::tail_bare_variable", &[])?;
2690        assert_eq!(compiled.ret_ty(), &Type::Any);
2691        let tail_bare_variable: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2692        let result = unsafe { &*tail_bare_variable() };
2693        assert_eq!(result.len(), 2);
2694        assert_eq!(result.get_idx(1).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("second".to_string()));
2695
2696        let compiled = vm.get_fn("vm_tail_list_literal::return_bare_variable_without_semicolon", &[])?;
2697        assert_eq!(compiled.ret_ty(), &Type::Any);
2698        let return_bare_variable_without_semicolon: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2699        let result = unsafe { &*return_bare_variable_without_semicolon() };
2700        assert_eq!(result.len(), 2);
2701        assert_eq!(result.get_idx(0).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("first".to_string()));
2702
2703        let compiled = vm.get_fn("vm_tail_list_literal::tail_object_variable", &[])?;
2704        assert_eq!(compiled.ret_ty(), &Type::Any);
2705        let tail_object_variable: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2706        let result = unsafe { &*tail_object_variable() };
2707        let steps = result.get_dynamic("steps").expect("steps");
2708        assert_eq!(steps.len(), 2);
2709        assert_eq!(steps.get_idx(1).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("second".to_string()));
2710        Ok(())
2711    }
2712
2713    #[test]
2714    fn list_return_value_supports_get_idx_method_call() -> anyhow::Result<()> {
2715        let vm = Vm::with_all()?;
2716        vm.import_code(
2717            "vm_returned_list_get_idx",
2718            r#"
2719            pub fn ids() {
2720                [
2721                    "base",
2722                    "2",
2723                    "3"
2724                ]
2725            }
2726
2727            pub fn combinations() {
2728                let result = [];
2729                let values = ids();
2730                let idx = 0;
2731                while idx < values.len() {
2732                    result.push(values.get_idx(idx));
2733                    idx = idx + 1;
2734                }
2735                result
2736            }
2737            "#
2738            .as_bytes()
2739            .to_vec(),
2740        )?;
2741
2742        let compiled = vm.get_fn("vm_returned_list_get_idx::combinations", &[])?;
2743        assert_eq!(compiled.ret_ty(), &Type::Any);
2744        let combinations: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2745        let result = unsafe { &*combinations() };
2746
2747        assert_eq!(result.len(), 3);
2748        assert_eq!(result.get_idx(0).map(|value| value.as_str().to_string()), Some("base".to_string()));
2749        assert_eq!(result.get_idx(2).map(|value| value.as_str().to_string()), Some("3".to_string()));
2750        Ok(())
2751    }
2752
2753    #[test]
2754    fn repeated_deep_step_literals_import_successfully() -> anyhow::Result<()> {
2755        fn extra_page_literal(depth: usize) -> String {
2756            let mut value = "{leaf: \"done\"}".to_string();
2757            for idx in 0..depth {
2758                value = format!("{{kind: \"page\", idx: {idx}, children: [{value}], meta: {{title: \"extra\", visible: true}}}}");
2759            }
2760            value
2761        }
2762
2763        let extra = extra_page_literal(48);
2764        let code = format!(
2765            r#"
2766            pub fn script() {{
2767                return [
2768                    {{ja: "一つ目", note: "first", extra: {extra}}},
2769                    {{ja: "二つ目", note: "second", extra: {extra}}},
2770                    {{ja: "三つ目", note: "third", extra: {extra}}}
2771                ]
2772            }}
2773            "#
2774        );
2775
2776        let vm = Vm::with_all()?;
2777        vm.import_code("vm_repeated_deep_step_literals", code.into_bytes())?;
2778        let compiled = vm.get_fn("vm_repeated_deep_step_literals::script", &[])?;
2779        assert_eq!(compiled.ret_ty(), &Type::Any);
2780        let script: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2781        let result = unsafe { &*script() };
2782        assert_eq!(result.len(), 3);
2783        assert_eq!(result.get_idx(2).and_then(|value| value.get_dynamic("note")).map(|value| value.as_str().to_string()), Some("third".to_string()));
2784        Ok(())
2785    }
2786
2787    #[test]
2788    fn native_import_uses_owning_vm() -> anyhow::Result<()> {
2789        let module_path = std::env::temp_dir().join(format!("zust_vm_import_owner_{}.zs", std::process::id()));
2790        std::fs::write(&module_path, "pub fn value() { 41 }")?;
2791        let module_path = module_path.to_string_lossy().replace('\\', "\\\\").replace('"', "\\\"");
2792
2793        let vm1 = Vm::with_all()?;
2794        vm1.import_code(
2795            "vm_import_owner",
2796            format!(
2797                r#"
2798                pub fn run() {{
2799                    import("vm_imported_owner", "{module_path}");
2800                }}
2801                "#
2802            )
2803            .into_bytes(),
2804        )?;
2805        let compiled = vm1.get_fn("vm_import_owner::run", &[])?;
2806
2807        let vm2 = Vm::with_all()?;
2808        vm2.import_code("vm_import_other", b"pub fn run() { 0 }".to_vec())?;
2809        let _ = vm2.get_fn("vm_import_other::run", &[])?;
2810
2811        let run: extern "C" fn() = unsafe { std::mem::transmute(compiled.ptr()) };
2812        run();
2813
2814        assert!(vm1.get_fn("vm_imported_owner::value", &[]).is_ok());
2815        assert!(vm2.get_fn("vm_imported_owner::value", &[]).is_err());
2816        Ok(())
2817    }
2818
2819    #[test]
2820    fn object_last_field_call_does_not_need_trailing_comma() -> anyhow::Result<()> {
2821        let vm = Vm::with_all()?;
2822        vm.import_code(
2823            "vm_object_last_call_field",
2824            r#"
2825            pub fn extra_page() {
2826                {
2827                    title: "extra",
2828                    pages: [
2829                        {note: "nested"}
2830                    ]
2831                }
2832            }
2833
2834            pub fn data() {
2835                return [
2836                    {
2837                        note: "first",
2838                        choices: ["a", "b"],
2839                        extras: extra_page()
2840                    },
2841                    {
2842                        note: "second",
2843                        choices: ["c"],
2844                        extras: extra_page()
2845                    }
2846                ]
2847            }
2848            "#
2849            .as_bytes()
2850            .to_vec(),
2851        )?;
2852
2853        let compiled = vm.get_fn("vm_object_last_call_field::data", &[])?;
2854        assert_eq!(compiled.ret_ty(), &Type::Any);
2855        let data: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2856        let result = unsafe { &*data() };
2857        assert_eq!(result.len(), 2);
2858        let first = result.get_idx(0).expect("first step");
2859        assert_eq!(first.get_dynamic("extras").and_then(|extras| extras.get_dynamic("title")).map(|title| title.as_str().to_string()), Some("extra".to_string()));
2860        Ok(())
2861    }
2862
2863    #[test]
2864    fn string_return_survives_scope_exit() -> anyhow::Result<()> {
2865        let vm = Vm::with_all()?;
2866        vm.import_code(
2867            "vm_string_return_scope",
2868            r#"
2869            pub fn source_root() {
2870                "../assets/character/男主角换装"
2871            }
2872
2873            pub fn binary_root() {
2874                "character_binary/男主角换装"
2875            }
2876
2877            pub fn runtime_binary_url() {
2878                "/" + binary_root()
2879            }
2880
2881            pub fn action_groups() {
2882                let root = source_root();
2883                let binary_url = runtime_binary_url();
2884                let binary_root = binary_root();
2885                [
2886                    {
2887                        id: "field_bottom",
2888                        source_spine: root + "/战斗外/boy_b.spine",
2889                        skeleton: binary_url + "/战斗外/boy_b/boy_b.skel.bytes",
2890                        export_skeleton: binary_root + "/战斗外/boy_b/boy_b.skel.bytes"
2891                    }
2892                ]
2893            }
2894            "#
2895            .as_bytes()
2896            .to_vec(),
2897        )?;
2898
2899        let compiled = vm.get_fn("vm_string_return_scope::source_root", &[])?;
2900        assert_eq!(compiled.ret_ty(), &Type::Str);
2901        let source_root: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2902        let source_root = unsafe { &*source_root() };
2903        assert_eq!(source_root.as_str(), "../assets/character/男主角换装");
2904
2905        let compiled = vm.get_fn("vm_string_return_scope::action_groups", &[])?;
2906        assert_eq!(compiled.ret_ty(), &Type::Any);
2907        let action_groups: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2908        let groups = unsafe { &*action_groups() };
2909        let first = groups.get_idx(0).expect("first action group");
2910        assert_eq!(first.get_dynamic("source_spine").map(|value| value.as_str().to_string()), Some("../assets/character/男主角换装/战斗外/boy_b.spine".to_string()));
2911        assert_eq!(first.get_dynamic("skeleton").map(|value| value.as_str().to_string()), Some("/character_binary/男主角换装/战斗外/boy_b/boy_b.skel.bytes".to_string()));
2912        Ok(())
2913    }
2914
2915    #[test]
2916    fn dynamic_string_add_uses_any_binary_fast_path() -> anyhow::Result<()> {
2917        let vm = Vm::with_all()?;
2918        vm.import_code(
2919            "vm_dynamic_string_add",
2920            br#"
2921            pub fn concat(left, right) {
2922                left + right
2923            }
2924            "#
2925            .to_vec(),
2926        )?;
2927
2928        let compiled = vm.get_fn("vm_dynamic_string_add::concat", &[Type::Any, Type::Any])?;
2929        assert_eq!(compiled.ret_ty(), &Type::Any);
2930        let concat: extern "C" fn(*const Dynamic, *const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
2931        let left = Dynamic::from("hello");
2932        let right = Dynamic::from(" world");
2933        let result = unsafe { &*concat(&left, &right) };
2934        assert_eq!(result.as_str(), "hello world");
2935        Ok(())
2936    }
2937
2938    #[test]
2939    fn large_dynamic_object_accepts_inline_call_fields() -> anyhow::Result<()> {
2940        let vm = Vm::with_all()?;
2941        let model_count = 180;
2942        let combination_count = 90;
2943        let models = (0..model_count)
2944            .map(|idx| {
2945                format!(
2946                    r#"{{id: "model_{idx}", name: "模型_{idx}", source: "/美术资源/角色/少年/套装_{idx}/模型_{idx}.model.json", parts: [
2947                        {{slot: "hair", path: "/模型/头发/颜色_{idx}/默认.png", z: 10}},
2948                        {{slot: "body", path: "/模型/身体/套装_{idx}/默认.png", z: 1}},
2949                        {{slot: "face", path: "/模型/表情/表情_{idx}/默认.png", z: 20}}
2950                    ]}}"#
2951                )
2952            })
2953            .collect::<Vec<_>>()
2954            .join(",\n");
2955        let combinations = (0..combination_count).map(|idx| format!(r#"{{hair: "color_{idx}", body: "set_{idx}", face: "face_{idx}"}}"#)).collect::<Vec<_>>().join(",\n");
2956        let code = format!(
2957            r#"
2958            pub fn source_root() {{
2959                "/美术资源/角色/少年/默认"
2960            }}
2961
2962            pub fn runtime_boy_url() {{
2963                "/cdn/runtime/角色/少年/少年.model.json"
2964            }}
2965
2966            pub fn parts() {{
2967                [
2968                    {{id: "hair", path: "/模型/头发/黑色/默认.png", z: 10}},
2969                    {{id: "body", path: "/模型/身体/校服/默认.png", z: 1}},
2970                    {{id: "face", path: "/模型/表情/微笑/默认.png", z: 20}}
2971                ]
2972            }}
2973
2974            pub fn action_groups() {{
2975                {{
2976                    idle: [
2977                        {{id: "stand", name: "站立", frames: ["待机/0001.png", "待机/0002.png"]}},
2978                        {{id: "blink", name: "眨眼", frames: ["表情/眨眼/0001.png", "表情/眨眼/0002.png"]}}
2979                    ],
2980                    move: [
2981                        {{id: "walk", name: "行走", frames: ["行走/0001.png", "行走/0002.png"]}},
2982                        {{id: "run", name: "奔跑", frames: ["奔跑/0001.png", "奔跑/0002.png"]}}
2983                    ]
2984                }}
2985            }}
2986
2987            pub fn default_model() {{
2988                {{
2989                    id: "runtime_boy",
2990                    name: "运行时少年",
2991                    skins: [
2992                        {{id: "school", title: "校服", source: "/套装/校服/model.json"}},
2993                        {{id: "casual", title: "便服", source: "/套装/便服/model.json"}}
2994                    ],
2995                    models: [
2996                        {models}
2997                    ]
2998                }}
2999            }}
3000
3001            pub fn first_nine_combinations() {{
3002                [
3003                    {combinations}
3004                ]
3005            }}
3006
3007            pub fn config() {{
3008                {{
3009                    source_root: source_root(),
3010                    runtime_boy_url: runtime_boy_url(),
3011                    parts: parts(),
3012                    action_groups: action_groups(),
3013                    default_model: default_model(),
3014                    first_nine_combinations: first_nine_combinations()
3015                }}
3016            }}
3017
3018            pub fn start() {{
3019                root::add("local/vm_large_inline_call_object/config", {{
3020                    source_root: source_root(),
3021                    runtime_boy_url: runtime_boy_url(),
3022                    parts: parts(),
3023                    action_groups: action_groups(),
3024                    default_model: default_model(),
3025                    first_nine_combinations: first_nine_combinations()
3026                }})
3027            }}
3028            "#
3029        );
3030        vm.import_code("vm_large_inline_call_object", code.into_bytes())?;
3031
3032        let compiled = vm.get_fn("vm_large_inline_call_object::config", &[])?;
3033        assert_eq!(compiled.ret_ty(), &Type::Any);
3034        let config: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3035        let result = unsafe { &*config() };
3036        assert_eq!(result.get_dynamic("source_root").map(|value| value.as_str().to_string()), Some("/美术资源/角色/少年/默认".to_string()));
3037        assert_eq!(result.get_dynamic("first_nine_combinations").map(|value| value.len()), Some(combination_count));
3038
3039        let compiled = vm.get_fn("vm_large_inline_call_object::start", &[])?;
3040        assert_eq!(compiled.ret_ty(), &Type::Bool);
3041        let start: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3042        assert!(start());
3043        let saved = root::get("local/vm_large_inline_call_object/config")?;
3044        assert_eq!(saved.get_dynamic("first_nine_combinations").map(|value| value.len()), Some(combination_count));
3045        Ok(())
3046    }
3047
3048    #[test]
3049    fn http_serve_accepts_inline_config_map() -> anyhow::Result<()> {
3050        let vm = Vm::with_all()?;
3051        vm.import_code(
3052            "vm_http_serve_inline_config",
3053            br#"
3054            pub fn start() {
3055                let server = http::serve({host: "127.0.0.1:5192"});
3056                server
3057            }
3058            "#
3059            .to_vec(),
3060        )?;
3061
3062        let compiled = vm.get_fn("vm_http_serve_inline_config::start", &[])?;
3063        assert_eq!(compiled.ret_ty(), &Type::Any);
3064        Ok(())
3065    }
3066
3067    #[test]
3068    fn http_serve_accepts_variable_and_quoted_static_key() -> anyhow::Result<()> {
3069        let vm = Vm::with_all()?;
3070        vm.import_code(
3071            "vm_http_serve_quoted_static",
3072            br#"
3073            pub fn start(server_addr) {
3074                let http_server = http::serve({
3075                    host: server_addr,
3076                    ws: true,
3077                    upload: "upload",
3078                    "static": {
3079                        path: "/",
3080                        dir: "public/local"
3081                    }
3082                });
3083                http_server
3084            }
3085            "#
3086            .to_vec(),
3087        )?;
3088
3089        let compiled = vm.get_fn("vm_http_serve_quoted_static::start", &[Type::Any])?;
3090        assert_eq!(compiled.ret_ty(), &Type::Any);
3091        Ok(())
3092    }
3093
3094    #[test]
3095    fn oss_helpers_accept_explicit_config() -> anyhow::Result<()> {
3096        let vm = Vm::with_all()?;
3097        vm.import_code(
3098            "vm_oss_explicit_config",
3099            br#"
3100            pub fn upload(oss, bytes) {
3101                oss::upload(oss, "llm/input/audio.wav", bytes)
3102            }
3103
3104            pub fn http_upload(oss, bytes) {
3105                http::upload(oss, "uploads/input.bin", bytes)
3106            }
3107
3108            pub fn link(oss, uploaded) {
3109                oss::signed_url(oss, {oss_url: uploaded, expires: 3600})
3110            }
3111            "#
3112            .to_vec(),
3113        )?;
3114
3115        assert_eq!(vm.get_fn("vm_oss_explicit_config::upload", &[Type::Any, Type::Any])?.ret_ty(), &Type::Any);
3116        assert_eq!(vm.get_fn("vm_oss_explicit_config::http_upload", &[Type::Any, Type::Any])?.ret_ty(), &Type::Any);
3117        assert_eq!(vm.get_fn("vm_oss_explicit_config::link", &[Type::Any, Type::Any])?.ret_ty(), &Type::Any);
3118        Ok(())
3119    }
3120
3121    #[test]
3122    fn load_script_accepts_http_serve_inline_config() -> anyhow::Result<()> {
3123        let vm = Vm::with_all()?;
3124        let (_fn_ptr, ty) = vm.load(
3125            br#"
3126            let server_addr = "127.0.0.1:5192";
3127            let http_server = http::serve({
3128                host: server_addr,
3129                ws: true,
3130                upload: "upload",
3131                "static": {
3132                    path: "/",
3133                    dir: "public/local"
3134                }
3135            });
3136            http_server
3137            "#
3138            .to_vec(),
3139            "arg".into(),
3140        )?;
3141
3142        assert_eq!(ty, Type::Any);
3143        Ok(())
3144    }
3145
3146    #[test]
3147    fn load_script_resolves_import_before_compile() -> anyhow::Result<()> {
3148        let module_path = std::env::temp_dir().join(format!("zust_vm_load_import_{}.zs", std::process::id()));
3149        std::fs::write(&module_path, "pub fn init() { return {ok: true}; }")?;
3150        let module_path = module_path.to_string_lossy().replace('\\', "\\\\").replace('"', "\\\"");
3151
3152        let vm = Vm::with_all()?;
3153        let (_fn_ptr, ty) = vm.load(
3154            format!(
3155                r#"
3156                import("create_scene", "{module_path}");
3157                create_scene::init();
3158                "#
3159            )
3160            .into_bytes(),
3161            "req".into(),
3162        )?;
3163
3164        assert_eq!(ty, Type::Void);
3165        Ok(())
3166    }
3167
3168    #[test]
3169    fn gpu_struct_layout_packs_and_unpacks_dynamic_maps() -> anyhow::Result<()> {
3170        let vm = Vm::with_all()?;
3171        vm.import_code(
3172            "vm_gpu_layout",
3173            br#"
3174            pub struct Params {
3175                a: u32,
3176                b: u32,
3177                c: u32,
3178            }
3179            "#
3180            .to_vec(),
3181        )?;
3182
3183        let layout = vm.gpu_struct_layout("vm_gpu_layout::Params", &[])?;
3184        assert_eq!(layout.size, 16);
3185        assert_eq!(layout.fields.iter().map(|field| (field.name.as_str(), field.offset)).collect::<Vec<_>>(), vec![("a", 0), ("b", 4), ("c", 8)]);
3186
3187        let value = dynamic::map!("a"=> 1u32, "b"=> 2u32, "c"=> 3u32);
3188        let bytes = layout.pack_map(&value)?;
3189        assert_eq!(bytes.len(), 16);
3190        assert_eq!(&bytes[0..4], &1u32.to_ne_bytes());
3191        assert_eq!(&bytes[4..8], &2u32.to_ne_bytes());
3192        assert_eq!(&bytes[8..12], &3u32.to_ne_bytes());
3193
3194        let read = layout.unpack_map(&bytes)?;
3195        assert_eq!(read.get_dynamic("a").and_then(|value| value.as_uint()), Some(1));
3196        assert_eq!(read.get_dynamic("b").and_then(|value| value.as_uint()), Some(2));
3197        assert_eq!(read.get_dynamic("c").and_then(|value| value.as_uint()), Some(3));
3198        Ok(())
3199    }
3200
3201    #[test]
3202    fn root_native_calls_do_not_take_ownership_of_dynamic_args() -> anyhow::Result<()> {
3203        let vm = Vm::with_all()?;
3204        vm.import_code(
3205            "vm_root_clone_bridge",
3206            br#"
3207            pub fn add_then_reuse(arg) {
3208                let user = {
3209                    address: "test-wallet",
3210                    points: 20
3211                };
3212                root::add("local/root-clone-bridge-user", user);
3213                user.points = user.points - 7;
3214                root::add("local/root-clone-bridge-user", user);
3215                {
3216                    user: user,
3217                    points: user.points
3218                }
3219            }
3220
3221            pub fn clone_then_mutate(arg) {
3222                let user = {
3223                    profile: {
3224                        points: 20
3225                    }
3226                };
3227                let copied = user.clone();
3228                copied.profile.points = 13;
3229                user
3230            }
3231            "#
3232            .to_vec(),
3233        )?;
3234
3235        let compiled = vm.get_fn("vm_root_clone_bridge::add_then_reuse", &[Type::Any])?;
3236        assert_eq!(compiled.ret_ty(), &Type::Any);
3237        let add_then_reuse: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3238        let arg = Dynamic::Null;
3239        let result = add_then_reuse(&arg);
3240        let result = unsafe { &*result };
3241
3242        assert_eq!(result.get_dynamic("points").and_then(|value| value.as_int()), Some(13));
3243        let mut json = String::new();
3244        result.to_json(&mut json);
3245        assert!(json.contains("\"points\": 13"));
3246
3247        let clone_then_mutate = vm.get_fn("vm_root_clone_bridge::clone_then_mutate", &[Type::Any])?;
3248        let clone_then_mutate: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(clone_then_mutate.ptr()) };
3249        let result = clone_then_mutate(&arg);
3250        let result = unsafe { &*result };
3251        assert_eq!(result.get_dynamic("profile").unwrap().get_dynamic("points").and_then(|value| value.as_int()), Some(20));
3252        Ok(())
3253    }
3254
3255    struct CounterForTypedReceiver {
3256        value: i64,
3257    }
3258
3259    extern "C" fn counter_for_typed_receiver_get(value: *const Dynamic) -> i64 {
3260        unsafe { &*value }.as_custom::<CounterForTypedReceiver>().map(|counter| counter.value).unwrap_or(-1)
3261    }
3262
3263    struct NavMapForFunctionArg;
3264
3265    extern "C" fn nav_map_for_function_arg_new() -> *const Dynamic {
3266        Box::into_raw(Box::new(Dynamic::custom(NavMapForFunctionArg)))
3267    }
3268
3269    #[derive(Debug, Default)]
3270    struct PropertyForwardingObject {
3271        values: RwLock<BTreeMap<String, Dynamic>>,
3272    }
3273
3274    impl CustomProperty for PropertyForwardingObject {
3275        fn get_key(&self, key: &str) -> Option<Dynamic> {
3276            self.values.read().unwrap().get(key).cloned()
3277        }
3278
3279        fn set_key(&self, key: &str, value: Dynamic) -> bool {
3280            self.values.write().unwrap().insert(key.to_string(), value);
3281            true
3282        }
3283    }
3284
3285    extern "C" fn property_forwarding_object_new() -> *const Dynamic {
3286        Box::into_raw(Box::new(Dynamic::custom_with_properties(PropertyForwardingObject::default())))
3287    }
3288
3289    #[test]
3290    fn typed_receiver_method_call_dispatches_with_type_hint() -> anyhow::Result<()> {
3291        let vm = Vm::with_all()?;
3292        vm.add_empty_type("Counter")?;
3293        let counter_ty = vm.get_symbol("Counter", Vec::new())?;
3294        vm.add_native_method_ptr("Counter", "get", &[counter_ty], Type::I64, counter_for_typed_receiver_get as *const u8)?;
3295        vm.import_code(
3296            "vm_typed_receiver_method",
3297            br#"
3298            pub fn run(value) {
3299                value::<Counter>::get()
3300            }
3301            "#
3302            .to_vec(),
3303        )?;
3304
3305        let compiled = vm.get_fn("vm_typed_receiver_method::run", &[Type::Any])?;
3306        assert_eq!(compiled.ret_ty(), &Type::I64);
3307        let run: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3308        let value = Dynamic::custom(CounterForTypedReceiver { value: 42 });
3309
3310        assert_eq!(run(&value), 42);
3311        Ok(())
3312    }
3313
3314    #[test]
3315    fn native_custom_object_can_be_passed_to_zs_function() -> anyhow::Result<()> {
3316        let vm = Vm::with_all()?;
3317        vm.add_empty_type("NavMap")?;
3318        vm.add_native_method_ptr("NavMap", "new", &[], Type::Any, nav_map_for_function_arg_new as *const u8)?;
3319        vm.import_code(
3320            "vm_native_custom_arg",
3321            br#"
3322            pub fn add_nav_spawns(world, navmap) {
3323                navmap
3324            }
3325
3326            pub fn run(world) {
3327                let navmap = NavMap::new();
3328                let with_spawns = add_nav_spawns(world, navmap);
3329                with_spawns
3330            }
3331            "#
3332            .to_vec(),
3333        )?;
3334
3335        let compiled = vm.get_fn("vm_native_custom_arg::run", &[Type::Any])?;
3336        assert_eq!(compiled.ret_ty(), &Type::Any);
3337        let run: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3338        let world = Dynamic::Null;
3339        let result = run(&world);
3340        let result = unsafe { &*result };
3341
3342        assert!(result.as_custom::<NavMapForFunctionArg>().is_some());
3343        Ok(())
3344    }
3345
3346    #[test]
3347    fn any_field_assignment_forwards_to_custom_properties() -> anyhow::Result<()> {
3348        let vm = Vm::with_all()?;
3349        vm.add_empty_type("Dialog")?;
3350        vm.add_native_method_ptr("Dialog", "new", &[], Type::Any, property_forwarding_object_new as *const u8)?;
3351        vm.import_code(
3352            "vm_custom_property_forwarding",
3353            br#"
3354            pub fn run() {
3355                let dialog = Dialog::new();
3356                dialog.file_mode = 3;
3357                dialog.file_mode
3358            }
3359            "#
3360            .to_vec(),
3361        )?;
3362
3363        let compiled = vm.get_fn("vm_custom_property_forwarding::run", &[])?;
3364        assert_eq!(compiled.ret_ty(), &Type::Any);
3365        let run: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3366        let result = unsafe { &*run() };
3367
3368        assert_eq!(result.as_int(), Some(3));
3369        Ok(())
3370    }
3371
3372    #[test]
3373    fn native_custom_object_typed_local_can_be_passed_to_zs_function() -> anyhow::Result<()> {
3374        let vm = Vm::with_all()?;
3375        vm.add_empty_type("NavMap")?;
3376        let _nav_map_ty = vm.get_symbol("NavMap", Vec::new())?;
3377        vm.add_native_method_ptr("NavMap", "new", &[], Type::Any, nav_map_for_function_arg_new as *const u8)?;
3378        vm.import_code(
3379            "vm_native_custom_typed_arg",
3380            br#"
3381            pub fn add_nav_spawns(world, navmap) {
3382                navmap
3383            }
3384
3385            pub fn run(world) {
3386                let navmap: NavMap = NavMap::new();
3387                let with_spawns = add_nav_spawns(world, navmap);
3388                with_spawns
3389            }
3390            "#
3391            .to_vec(),
3392        )?;
3393
3394        let compiled = vm.get_fn("vm_native_custom_typed_arg::run", &[Type::Any])?;
3395        let run: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3396        let world = Dynamic::Null;
3397        let result = run(&world);
3398        let result = unsafe { &*result };
3399
3400        assert!(result.as_custom::<NavMapForFunctionArg>().is_some());
3401        Ok(())
3402    }
3403
3404    // ---- 新增边界条件测试 ----
3405
3406    #[test]
3407    fn dynamic_type_checks_on_null_and_primitive_values() -> anyhow::Result<()> {
3408        let vm = Vm::with_all()?;
3409        vm.import_code(
3410            "vm_dynamic_type_checks",
3411            br#"
3412            pub fn is_list_on_int() {
3413                let x = 42i64;
3414                x.is_list()
3415            }
3416
3417            pub fn is_map_on_int() {
3418                let x = 42i64;
3419                x.is_map()
3420            }
3421
3422            pub fn is_null_on_int() {
3423                let x = 42i64;
3424                x.is_null()
3425            }
3426            "#
3427            .to_vec(),
3428        )?;
3429
3430        let compiled = vm.get_fn("vm_dynamic_type_checks::is_list_on_int", &[])?;
3431        assert_eq!(compiled.ret_ty(), &Type::Bool);
3432        let is_list_on_int: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3433        assert!(!is_list_on_int());
3434
3435        let compiled = vm.get_fn("vm_dynamic_type_checks::is_map_on_int", &[])?;
3436        assert_eq!(compiled.ret_ty(), &Type::Bool);
3437        let is_map_on_int: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3438        assert!(!is_map_on_int());
3439
3440        let compiled = vm.get_fn("vm_dynamic_type_checks::is_null_on_int", &[])?;
3441        assert_eq!(compiled.ret_ty(), &Type::Bool);
3442        let is_null_on_int: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3443        assert!(!is_null_on_int());
3444        Ok(())
3445    }
3446
3447    #[test]
3448    fn void_and_null_are_false_in_boolean_context() -> anyhow::Result<()> {
3449        let vm = Vm::with_all()?;
3450        vm.import_code(
3451            "vm_void_bool_context",
3452            br#"
3453            pub fn run() {
3454                let items = [1i32, 2i32];
3455                let ok1 = !(items.push(3i32) && false);
3456                let ok2 = !(true && items.push(4i32));
3457                let ok3 = null || true;
3458                let ok4 = null || items.len() == 4;
3459                ok1 && ok2 && ok3 && ok4
3460            }
3461            "#
3462            .to_vec(),
3463        )?;
3464
3465        let compiled = vm.get_fn("vm_void_bool_context::run", &[])?;
3466        assert_eq!(compiled.ret_ty(), &Type::Bool);
3467        let run: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3468        assert!(run());
3469        Ok(())
3470    }
3471
3472    #[test]
3473    fn empty_for_loop_range_has_zero_iterations() -> anyhow::Result<()> {
3474        let vm = Vm::with_all()?;
3475        vm.import_code(
3476            "vm_empty_for_range",
3477            br#"
3478            pub fn empty_exclusive() {
3479                let count = 0i32;
3480                for i in 0..0 {
3481                    count += i;
3482                }
3483                count
3484            }
3485
3486            pub fn single_inclusive_iteration() {
3487                let count = 0i32;
3488                for i in 5..=5 {
3489                    count += i;
3490                }
3491                count
3492            }
3493            "#
3494            .to_vec(),
3495        )?;
3496
3497        // 无后缀 range 字面量(0..0 / 5..=5)默认 I64,累加器随复合赋值提升为 I64
3498        let compiled = vm.get_fn("vm_empty_for_range::empty_exclusive", &[])?;
3499        assert_eq!(compiled.ret_ty(), &Type::I64);
3500        let empty_exclusive: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3501        assert_eq!(empty_exclusive(), 0);
3502
3503        let compiled = vm.get_fn("vm_empty_for_range::single_inclusive_iteration", &[])?;
3504        assert_eq!(compiled.ret_ty(), &Type::I64);
3505        let single_inclusive: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3506        assert_eq!(single_inclusive(), 5);
3507        Ok(())
3508    }
3509
3510    #[test]
3511    fn for_loop_range_accepts_dynamic_i64_bounds() -> anyhow::Result<()> {
3512        let vm = Vm::with_all()?;
3513        vm.import_code(
3514            "vm_dynamic_for_range",
3515            br#"
3516            pub fn main() {
3517                let view = {};
3518                view.grid_min_x = -2i64;
3519                view.grid_max_x = 2i64;
3520
3521                let end_x = view.grid_max_x + 1i64;
3522                let count = 0i64;
3523
3524                for x in view.grid_min_x..end_x {
3525                    count += 1i64;
3526                }
3527
3528                count
3529            }
3530            "#
3531            .to_vec(),
3532        )?;
3533
3534        let compiled = vm.get_fn("vm_dynamic_for_range::main", &[])?;
3535        assert_eq!(compiled.ret_ty(), &Type::I64);
3536        let main: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3537        assert_eq!(main(), 5);
3538        Ok(())
3539    }
3540
3541    #[test]
3542    fn map_contains_key_on_non_existent_and_nested_keys() -> anyhow::Result<()> {
3543        let vm = Vm::with_all()?;
3544        vm.import_code(
3545            "vm_map_contains",
3546            br#"
3547            pub fn contains_existing(data) {
3548                data.contains("name")
3549            }
3550
3551            pub fn contains_missing(data) {
3552                data.contains("nothing")
3553            }
3554            "#
3555            .to_vec(),
3556        )?;
3557
3558        let compiled = vm.get_fn("vm_map_contains::contains_existing", &[Type::Any])?;
3559        assert_eq!(compiled.ret_ty(), &Type::Bool);
3560        let contains_existing: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3561        let data = dynamic::map!("name"=> "test");
3562        assert!(contains_existing(&data));
3563
3564        let compiled = vm.get_fn("vm_map_contains::contains_missing", &[Type::Any])?;
3565        assert_eq!(compiled.ret_ty(), &Type::Bool);
3566        let contains_missing: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
3567        assert!(!contains_missing(&data));
3568        Ok(())
3569    }
3570
3571    #[test]
3572    fn list_pop_on_empty_list_returns_null() -> anyhow::Result<()> {
3573        let vm = Vm::with_all()?;
3574        vm.import_code(
3575            "vm_pop_empty",
3576            br#"
3577            pub fn pop_new_list() {
3578                let items = [];
3579                let value = items.pop();
3580                let still_empty = items.len() == 0;
3581                {value: value, empty: still_empty}
3582            }
3583
3584            pub fn pop_until_empty() {
3585                let items = [1i64, 2i64];
3586                items.pop();
3587                let last = items.pop();
3588                let drained = items.pop();
3589                {last: last, drained: drained}
3590            }
3591            "#
3592            .to_vec(),
3593        )?;
3594
3595        let compiled = vm.get_fn("vm_pop_empty::pop_new_list", &[])?;
3596        assert_eq!(compiled.ret_ty(), &Type::Any);
3597        let pop_new_list: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3598        let result = unsafe { &*pop_new_list() };
3599        assert!(result.get_dynamic("value").is_some_and(|v| v.is_null()));
3600        assert_eq!(result.get_dynamic("empty").and_then(|v| v.as_bool()), Some(true));
3601
3602        let compiled = vm.get_fn("vm_pop_empty::pop_until_empty", &[])?;
3603        assert_eq!(compiled.ret_ty(), &Type::Any);
3604        let pop_until_empty: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3605        let result = unsafe { &*pop_until_empty() };
3606        assert_eq!(result.get_dynamic("last").and_then(|v| v.as_int()), Some(1));
3607        assert!(result.get_dynamic("drained").is_some_and(|v| v.is_null()));
3608        Ok(())
3609    }
3610
3611    #[test]
3612    fn void_function_with_multiple_code_paths() -> anyhow::Result<()> {
3613        let vm = Vm::with_all()?;
3614        vm.import_code(
3615            "vm_void_multi_path",
3616            br#"
3617            pub fn log_if_positive(value: i64) {
3618                if value > 0 {
3619                    print(value);
3620                    return;
3621                }
3622                if value < 0 {
3623                    print(-value);
3624                    return;
3625                }
3626                print(0);
3627            }
3628            "#
3629            .to_vec(),
3630        )?;
3631
3632        let compiled = vm.get_fn("vm_void_multi_path::log_if_positive", &[Type::I64])?;
3633        assert!(compiled.ret_ty().is_void());
3634        Ok(())
3635    }
3636
3637    #[test]
3638    fn any_method_call_chain_on_returned_dynamic_value() -> anyhow::Result<()> {
3639        let vm = Vm::with_all()?;
3640        vm.import_code(
3641            "vm_any_method_chain",
3642            br#"
3643            pub fn get_tags(data) {
3644                let tags = data.tags;
3645                if tags.is_list() {
3646                    return tags.len();
3647                }
3648                0
3649            }
3650            "#
3651            .to_vec(),
3652        )?;
3653
3654        let compiled = vm.get_fn("vm_any_method_chain::get_tags", &[Type::Any])?;
3655        assert_eq!(compiled.ret_ty(), &Type::I64);
3656        let get_tags: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3657        let data = dynamic::map!("tags"=> Dynamic::list(vec!["a".into(), "b".into(), "c".into()]));
3658        assert_eq!(get_tags(&data), 3);
3659
3660        let empty_data = Dynamic::Null;
3661        assert_eq!(get_tags(&empty_data), 0);
3662        Ok(())
3663    }
3664
3665    #[test]
3666    fn infers_any_arg_function_return_before_body_compile() -> anyhow::Result<()> {
3667        let vm = Vm::with_all()?;
3668        vm.import_code(
3669            "vm_infer_any_arg_return",
3670            br#"
3671            pub fn caller(candidate) {
3672                let center = polygon_center(candidate.visualPolygon);
3673                center[0]
3674            }
3675
3676            pub fn polygon_center(point_list) {
3677                let total_x = 0;
3678                let total_y = 0;
3679                let count = 0;
3680                if point_list.is_list() {
3681                    for point in point_list {
3682                        if point.is_list() && point.len() >= 2 {
3683                            total_x += point[0];
3684                            total_y += point[1];
3685                            count += 1;
3686                        }
3687                    }
3688                }
3689                if count == 0 {
3690                    return [0, 0];
3691                }
3692                [total_x / count, total_y / count]
3693            }
3694            "#
3695            .to_vec(),
3696        )?;
3697
3698        let compiled = vm.get_fn("vm_infer_any_arg_return::caller", &[Type::Any])?;
3699        assert_eq!(compiled.ret_ty(), &Type::Any);
3700        let caller: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
3701        let candidate = dynamic::map!(
3702            "visualPolygon"=> Dynamic::list(vec![
3703                Dynamic::list(vec![2i64.into(), 4i64.into()]),
3704                Dynamic::list(vec![6i64.into(), 8i64.into()]),
3705            ])
3706        );
3707        let result = unsafe { &*caller(&candidate) };
3708        assert_eq!(result.as_int(), Some(4));
3709        Ok(())
3710    }
3711
3712    #[test]
3713    fn recursive_factorial_keeps_static_return_type() -> anyhow::Result<()> {
3714        let vm = Vm::with_all()?;
3715        vm.import_code(
3716            "vm_recursive_factorial",
3717            br#"
3718            fn factorial(n: i64) {
3719                if n <= 1 {
3720                    return 1;
3721                }
3722                n * factorial(n - 1)
3723            }
3724
3725            pub fn run(n: i64) {
3726                factorial(n)
3727            }
3728            "#
3729            .to_vec(),
3730        )?;
3731
3732        let compiled = vm.get_fn("vm_recursive_factorial::run", &[Type::I64])?;
3733        assert_eq!(compiled.ret_ty(), &Type::I64);
3734        let run: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3735        assert_eq!(run(5), 120);
3736        Ok(())
3737    }
3738
3739    #[test]
3740    fn explicit_const_generic_function_calls_generate_distinct_variants() -> anyhow::Result<()> {
3741        let vm = Vm::with_all()?;
3742        vm.import_code(
3743            "vm_generic_const_variants",
3744            br#"
3745            fn value<N>() {
3746                N
3747            }
3748
3749            pub fn two() {
3750                value::<2>()
3751            }
3752
3753            pub fn three() {
3754                value::<3>()
3755            }
3756            "#
3757            .to_vec(),
3758        )?;
3759
3760        let compiled = vm.get_fn("vm_generic_const_variants::two", &[])?;
3761        assert_eq!(compiled.ret_ty(), &Type::I32);
3762        let two: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
3763        assert_eq!(two(), 2);
3764
3765        let compiled = vm.get_fn("vm_generic_const_variants::three", &[])?;
3766        assert_eq!(compiled.ret_ty(), &Type::I32);
3767        let three: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
3768        assert_eq!(three(), 3);
3769        Ok(())
3770    }
3771
3772    #[test]
3773    fn generic_function_body_resolves_private_generic_helper_after_import() -> anyhow::Result<()> {
3774        let vm = Vm::with_all()?;
3775        vm.import_code(
3776            "vm_generic_private_helper",
3777            br#"
3778            fn helper<N>() {
3779                N
3780            }
3781
3782            pub fn bench<N>() {
3783                helper::<N>()
3784            }
3785            "#
3786            .to_vec(),
3787        )?;
3788
3789        let compiled = vm.get_fn_with_params("vm_generic_private_helper::bench", &[], &[Type::ConstInt(7)])?;
3790        assert_eq!(compiled.ret_ty(), &Type::I32);
3791        let run: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
3792        assert_eq!(run(), 7);
3793        Ok(())
3794    }
3795
3796    #[test]
3797    fn const_generic_repeat_array_initializes_all_items() -> anyhow::Result<()> {
3798        let vm = Vm::with_all()?;
3799        vm.import_code(
3800            "vm_generic_repeat_array",
3801            br#"
3802            fn bench<N>() {
3803                let is_prime = [true; N];
3804                is_prime[0] = false;
3805                is_prime[1] = false;
3806                let count = 0i64;
3807                for p in 2i64..N {
3808                    if is_prime[p] == true {
3809                        count = count + 1;
3810                        let step = p;
3811                        let j = p * p;
3812                        while j < N {
3813                            is_prime[j] = false;
3814                            j = j + step;
3815                        }
3816                    }
3817                }
3818                count
3819            }
3820
3821            pub fn run() {
3822                bench::<10>()
3823            }
3824
3825            pub fn run_1000() {
3826                bench::<1000>()
3827            }
3828
3829            pub fn run_100000() {
3830                bench::<100000>()
3831            }
3832            "#
3833            .to_vec(),
3834        )?;
3835
3836        let compiled = vm.get_fn("vm_generic_repeat_array::run", &[])?;
3837        assert_eq!(compiled.ret_ty(), &Type::I64);
3838        let run: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3839        assert_eq!(run(), 4);
3840
3841        let compiled = vm.get_fn("vm_generic_repeat_array::run_1000", &[])?;
3842        assert_eq!(compiled.ret_ty(), &Type::I64);
3843        let run_1000: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3844        assert_eq!(run_1000(), 168);
3845
3846        let compiled = vm.get_fn("vm_generic_repeat_array::run_100000", &[])?;
3847        assert_eq!(compiled.ret_ty(), &Type::I64);
3848        let run_100000: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3849        assert_eq!(run_100000(), 9592);
3850        Ok(())
3851    }
3852
3853    #[test]
3854    fn repeat_array_initializes_scalar_patterns() -> anyhow::Result<()> {
3855        let vm = Vm::with_all()?;
3856        vm.import_code(
3857            "vm_repeat_scalar_patterns",
3858            br#"
3859            pub fn count_true() {
3860                let items = [true; 100000];
3861                let count = 0i64;
3862                for idx in 0i64..100000 {
3863                    if items[idx] == true {
3864                        count = count + 1;
3865                    }
3866                }
3867                count
3868            }
3869
3870            pub fn i32_pair() {
3871                let items = [-7i32; 1000];
3872                items[0i64] + items[999i64]
3873            }
3874
3875            pub fn i64_pair() {
3876                let items = [1234567890123i64; 1000];
3877                items[0i64] + items[999i64]
3878            }
3879
3880            pub fn f64_pair() {
3881                let items = [1.5f64; 1000];
3882                items[0i64] + items[999i64]
3883            }
3884            "#
3885            .to_vec(),
3886        )?;
3887
3888        let compiled = vm.get_fn("vm_repeat_scalar_patterns::count_true", &[])?;
3889        assert_eq!(compiled.ret_ty(), &Type::I64);
3890        let count_true: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3891        assert_eq!(count_true(), 100000);
3892
3893        let compiled = vm.get_fn("vm_repeat_scalar_patterns::i32_pair", &[])?;
3894        assert_eq!(compiled.ret_ty(), &Type::I32);
3895        let i32_pair: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
3896        assert_eq!(i32_pair(), -14);
3897
3898        let compiled = vm.get_fn("vm_repeat_scalar_patterns::i64_pair", &[])?;
3899        assert_eq!(compiled.ret_ty(), &Type::I64);
3900        let i64_pair: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3901        assert_eq!(i64_pair(), 2469135780246);
3902
3903        let compiled = vm.get_fn("vm_repeat_scalar_patterns::f64_pair", &[])?;
3904        assert_eq!(compiled.ret_ty(), &Type::F64);
3905        let f64_pair: extern "C" fn() -> f64 = unsafe { std::mem::transmute(compiled.ptr()) };
3906        assert_eq!(f64_pair(), 3.0);
3907        Ok(())
3908    }
3909
3910    #[test]
3911    fn bool_array_store_normalizes_condition_values() -> anyhow::Result<()> {
3912        let vm = Vm::with_all()?;
3913        vm.import_code(
3914            "vm_bool_array_store",
3915            br#"
3916            pub fn run() {
3917                let items = [false; 4];
3918                items[1] = 3i64 > 2i64;
3919                items[2] = 3i64 < 2i64;
3920                if items[1] == true && items[2] == false {
3921                    1i64
3922                } else {
3923                    0i64
3924                }
3925            }
3926            "#
3927            .to_vec(),
3928        )?;
3929
3930        let compiled = vm.get_fn("vm_bool_array_store::run", &[])?;
3931        assert_eq!(compiled.ret_ty(), &Type::I64);
3932        let run: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3933        assert_eq!(run(), 1);
3934        Ok(())
3935    }
3936
3937    #[test]
3938    fn bool_array_large_sequential_writes() -> anyhow::Result<()> {
3939        let vm = Vm::with_all()?;
3940        vm.import_code(
3941            "vm_bool_array_large_writes",
3942            br#"
3943            pub fn run() {
3944                let items = [true; 100000];
3945                for idx in 0i64..100000 {
3946                    items[idx] = false;
3947                }
3948                let count = 0i64;
3949                for idx in 0i64..100000 {
3950                    if items[idx] == false {
3951                        count = count + 1;
3952                    }
3953                }
3954                count
3955            }
3956            "#
3957            .to_vec(),
3958        )?;
3959
3960        let compiled = vm.get_fn("vm_bool_array_large_writes::run", &[])?;
3961        assert_eq!(compiled.ret_ty(), &Type::I64);
3962        let run: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3963        assert_eq!(run(), 100000);
3964        Ok(())
3965    }
3966
3967    #[test]
3968    fn bool_array_sieve_style_indices_stay_in_bounds() -> anyhow::Result<()> {
3969        let vm = Vm::with_all()?;
3970        vm.import_code(
3971            "vm_bool_array_sieve_indices",
3972            br#"
3973            pub fn run() {
3974                let items = [true; 100000];
3975                let writes = 0i64;
3976                for p in 2i64..100000 {
3977                    let step = p;
3978                    let j = p * p;
3979                    while j < 100000 {
3980                        items[j] = false;
3981                        writes = writes + 1;
3982                        j = j + step;
3983                    }
3984                }
3985                writes
3986            }
3987            "#
3988            .to_vec(),
3989        )?;
3990
3991        let compiled = vm.get_fn("vm_bool_array_sieve_indices::run", &[])?;
3992        assert_eq!(compiled.ret_ty(), &Type::I64);
3993        let run: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
3994        assert!(run() > 0);
3995        Ok(())
3996    }
3997
3998    #[test]
3999    fn sieve_style_indices_compute_in_bounds_without_array_write() -> anyhow::Result<()> {
4000        let vm = Vm::with_all()?;
4001        vm.import_code(
4002            "vm_sieve_indices_no_write",
4003            br#"
4004            pub fn run() {
4005                let max_j = 0i64;
4006                for p in 2i64..100000 {
4007                    let step = p;
4008                    let j = p * p;
4009                    while j < 100000 {
4010                        if j < 0i64 {
4011                            return -1i64;
4012                        }
4013                        if j > max_j {
4014                            max_j = j;
4015                        }
4016                        j = j + step;
4017                    }
4018                }
4019                max_j
4020            }
4021            "#
4022            .to_vec(),
4023        )?;
4024
4025        let compiled = vm.get_fn("vm_sieve_indices_no_write::run", &[])?;
4026        assert_eq!(compiled.ret_ty(), &Type::I64);
4027        let run: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4028        assert_eq!(run(), 99999);
4029        Ok(())
4030    }
4031
4032    #[test]
4033    fn dynamic_list_index_sum_uses_static_accumulator_type() -> anyhow::Result<()> {
4034        let vm = Vm::with_all()?;
4035        vm.import_code(
4036            "vm_dynamic_index_sum",
4037            br#"
4038            pub fn sum_list(n: i64) {
4039                let l = [];
4040                for i in 0..n {
4041                    l.push(i);
4042                }
4043                let sum = 0i64;
4044                for j in 0..n {
4045                    sum = sum + l[j];
4046                }
4047                sum
4048            }
4049            "#
4050            .to_vec(),
4051        )?;
4052
4053        let compiled = vm.get_fn("vm_dynamic_index_sum::sum_list", &[Type::I64])?;
4054        let sum_list_id = vm.jit.write().unwrap().compiler.symbols.get_id("vm_dynamic_index_sum::sum_list")?;
4055        let hints = vm.jit.write().unwrap().compiler.inferred_local_type_hints(sum_list_id, &[], &[Type::I64]);
4056        assert!(hints.iter().any(|ty| matches!(ty, Some(Type::List(elem)) if elem.as_ref() == &Type::I64)), "local type hints: {:?}", hints);
4057        assert_eq!(compiled.ret_ty(), &Type::I64);
4058        let sum_list: extern "C" fn(i64) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4059        assert_eq!(sum_list(1000), 499500);
4060        Ok(())
4061    }
4062
4063    #[test]
4064    fn loop_pushed_list_is_typed_vector() -> anyhow::Result<()> {
4065        let vm = Vm::with_all()?;
4066        vm.import_code(
4067            "vm_loop_pushed_list",
4068            br#"
4069            pub fn make(n: i64) {
4070                let l = [];
4071                for i in 0..n {
4072                    l.push(i);
4073                }
4074                l
4075            }
4076            "#
4077            .to_vec(),
4078        )?;
4079        let compiled = vm.get_fn("vm_loop_pushed_list::make", &[Type::I64])?;
4080        let make: extern "C" fn(i64) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4081        let result = unsafe { &*make(3) };
4082        assert!(matches!(result, Dynamic::VecI64(v) if v == &vec![0, 1, 2]), "expected flat VecI64, got: {:?}", result);
4083        Ok(())
4084    }
4085
4086    #[test]
4087    fn inferred_empty_list_uses_typed_dynamic_vector() -> anyhow::Result<()> {
4088        let vm = Vm::with_all()?;
4089        vm.import_code(
4090            "vm_inferred_typed_list",
4091            br#"
4092            pub fn make() {
4093                let l = [];
4094                l.push(1i64);
4095                l
4096            }
4097            "#
4098            .to_vec(),
4099        )?;
4100
4101        let compiled = vm.get_fn("vm_inferred_typed_list::make", &[])?;
4102        assert_eq!(compiled.ret_ty(), &Type::Any);
4103        let make: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4104        let result = unsafe { &*make() };
4105        assert!(matches!(result, Dynamic::VecI64(values) if values == &vec![1]), "result: {:?}", result);
4106        Ok(())
4107    }
4108
4109    #[test]
4110    fn for_in_iterates_list_filled_in_same_function() -> anyhow::Result<()> {
4111        let vm = Vm::with_all()?;
4112        vm.import_code(
4113            "vm_for_in_local_pushed_list",
4114            br#"
4115            pub fn sum_i32_items() {
4116                let items = [];
4117                items.push(6000i32);
4118                items.push(4000i32);
4119                let total = 0i32;
4120                for item in items {
4121                    total += item;
4122                }
4123                total
4124            }
4125
4126            pub fn sum_split_bps() {
4127                let splits = [];
4128                splits.push({ bps: "6000" });
4129                splits.push({ bps: 4000 });
4130                let total = 0i32;
4131                let count = 0i32;
4132                for split in splits {
4133                    total += split.bps as i32;
4134                    count += 1i32;
4135                }
4136                total + count
4137            }
4138            "#
4139            .to_vec(),
4140        )?;
4141
4142        let compiled = vm.get_fn("vm_for_in_local_pushed_list::sum_i32_items", &[])?;
4143        assert_eq!(compiled.ret_ty(), &Type::I32);
4144        let sum_i32_items: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
4145        assert_eq!(sum_i32_items(), 10000);
4146
4147        let compiled = vm.get_fn("vm_for_in_local_pushed_list::sum_split_bps", &[])?;
4148        assert_eq!(compiled.ret_ty(), &Type::I32);
4149        let sum_split_bps: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
4150        assert_eq!(sum_split_bps(), 10002);
4151        Ok(())
4152    }
4153
4154    #[test]
4155    fn inferred_list_shortcuts_cover_scalar_types() -> anyhow::Result<()> {
4156        let vm = Vm::with_all()?;
4157        vm.import_code(
4158            "vm_inferred_list_shortcuts",
4159            br#"
4160            pub fn second_bool() {
4161                let l = [];
4162                l.push(true);
4163                l.push(false);
4164                l[1]
4165            }
4166
4167            pub fn first_u8() {
4168                let l = [];
4169                l.push(7u8);
4170                l[0]
4171            }
4172
4173            pub fn sum_i32(n: i64) {
4174                let l = [];
4175                for i in 0..n {
4176                    l.push(i as i32);
4177                }
4178                let sum = 0i32;
4179                for j in 0..n {
4180                    sum = sum + l[j];
4181                }
4182                sum
4183            }
4184
4185            pub fn sum_f32(n: i64) {
4186                let l = [];
4187                for i in 0..n {
4188                    l.push(i as f32);
4189                }
4190                let sum = 0f32;
4191                for j in 0..n {
4192                    sum = sum + l[j];
4193                }
4194                sum
4195            }
4196
4197            pub fn second_str() {
4198                let l = [];
4199                l.push("first");
4200                l.push("second");
4201                l[1]
4202            }
4203            "#
4204            .to_vec(),
4205        )?;
4206
4207        let compiled = vm.get_fn("vm_inferred_list_shortcuts::second_bool", &[])?;
4208        let second_bool_id = vm.jit.write().unwrap().compiler.symbols.get_id("vm_inferred_list_shortcuts::second_bool")?;
4209        let hints = vm.jit.write().unwrap().compiler.inferred_local_type_hints(second_bool_id, &[], &[]);
4210        assert!(hints.iter().any(|ty| matches!(ty, Some(Type::List(elem)) if elem.as_ref() == &Type::Bool)), "bool local type hints: {:?}", hints);
4211        assert_eq!(compiled.ret_ty(), &Type::Bool);
4212        let second_bool: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
4213        assert!(!second_bool());
4214
4215        let compiled = vm.get_fn("vm_inferred_list_shortcuts::first_u8", &[])?;
4216        let first_u8_id = vm.jit.write().unwrap().compiler.symbols.get_id("vm_inferred_list_shortcuts::first_u8")?;
4217        let hints = vm.jit.write().unwrap().compiler.inferred_local_type_hints(first_u8_id, &[], &[]);
4218        assert!(hints.iter().any(|ty| matches!(ty, Some(Type::List(elem)) if elem.as_ref() == &Type::U8)), "u8 local type hints: {:?}", hints);
4219        assert_eq!(compiled.ret_ty(), &Type::U8);
4220        let first_u8: extern "C" fn() -> u8 = unsafe { std::mem::transmute(compiled.ptr()) };
4221        assert_eq!(first_u8(), 7);
4222
4223        let compiled = vm.get_fn("vm_inferred_list_shortcuts::sum_i32", &[Type::I64])?;
4224        let sum_i32_id = vm.jit.write().unwrap().compiler.symbols.get_id("vm_inferred_list_shortcuts::sum_i32")?;
4225        let hints = vm.jit.write().unwrap().compiler.inferred_local_type_hints(sum_i32_id, &[], &[Type::I64]);
4226        assert!(hints.iter().any(|ty| matches!(ty, Some(Type::List(elem)) if elem.as_ref() == &Type::I32)), "i32 local type hints: {:?}", hints);
4227        assert_eq!(compiled.ret_ty(), &Type::I32);
4228        let sum_i32: extern "C" fn(i64) -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
4229        assert_eq!(sum_i32(100), 4950);
4230
4231        let compiled = vm.get_fn("vm_inferred_list_shortcuts::sum_f32", &[Type::I64])?;
4232        let sum_f32_id = vm.jit.write().unwrap().compiler.symbols.get_id("vm_inferred_list_shortcuts::sum_f32")?;
4233        let hints = vm.jit.write().unwrap().compiler.inferred_local_type_hints(sum_f32_id, &[], &[Type::I64]);
4234        assert!(hints.iter().any(|ty| matches!(ty, Some(Type::List(elem)) if elem.as_ref() == &Type::F32)), "f32 local type hints: {:?}", hints);
4235        assert_eq!(compiled.ret_ty(), &Type::F32);
4236        let sum_f32: extern "C" fn(i64) -> f32 = unsafe { std::mem::transmute(compiled.ptr()) };
4237        assert_eq!(sum_f32(10), 45.0);
4238
4239        let compiled = vm.get_fn("vm_inferred_list_shortcuts::second_str", &[])?;
4240        let second_str_id = vm.jit.write().unwrap().compiler.symbols.get_id("vm_inferred_list_shortcuts::second_str")?;
4241        let hints = vm.jit.write().unwrap().compiler.inferred_local_type_hints(second_str_id, &[], &[]);
4242        assert!(hints.iter().any(|ty| matches!(ty, Some(Type::List(elem)) if elem.as_ref() == &Type::Str)), "str local type hints: {:?}", hints);
4243        assert_eq!(compiled.ret_ty(), &Type::Str);
4244        let second_str: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4245        let result = unsafe { &*second_str() };
4246        assert_eq!(result.as_str(), "second");
4247        Ok(())
4248    }
4249
4250    #[test]
4251    fn inferred_list_supports_bracket_set_idx() -> anyhow::Result<()> {
4252        let vm = Vm::with_all()?;
4253        vm.import_code(
4254            "vm_inferred_list_set_idx",
4255            br#"
4256            pub fn swap_first_two() {
4257                let items = [];
4258                items.push(1i64);
4259                items.push(2i64);
4260                let j = 0i64;
4261                let a = items[j];
4262                let b = items[j + 1];
4263                items[j] = b;
4264                items[j + 1] = a;
4265                items[0] * 10i64 + items[1]
4266            }
4267
4268            pub fn replace_string() {
4269                let items = [];
4270                items.push("old");
4271                items[0] = "new";
4272                items[0]
4273            }
4274            "#
4275            .to_vec(),
4276        )?;
4277
4278        let compiled = vm.get_fn("vm_inferred_list_set_idx::swap_first_two", &[])?;
4279        assert_eq!(compiled.ret_ty(), &Type::I64);
4280        let swap_first_two: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4281        assert_eq!(swap_first_two(), 21);
4282
4283        let compiled = vm.get_fn("vm_inferred_list_set_idx::replace_string", &[])?;
4284        assert_eq!(compiled.ret_ty(), &Type::Str);
4285        let replace_string: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4286        let result = unsafe { &*replace_string() };
4287        assert_eq!(result.as_str(), "new");
4288        Ok(())
4289    }
4290
4291    #[test]
4292    fn root_get_returns_null_for_missing_key_which_compares_correctly() -> anyhow::Result<()> {
4293        let vm = Vm::with_all()?;
4294        vm.import_code(
4295            "vm_root_get_missing",
4296            br#"
4297            pub fn check_missing() {
4298                let existing = root::get("local/vm_root_get_missing_test");
4299                if existing.is_map() {
4300                    return false;
4301                }
4302                true
4303            }
4304            "#
4305            .to_vec(),
4306        )?;
4307
4308        let compiled = vm.get_fn("vm_root_get_missing::check_missing", &[])?;
4309        assert_eq!(compiled.ret_ty(), &Type::Bool);
4310        let check_missing: extern "C" fn() -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
4311        assert!(check_missing());
4312        Ok(())
4313    }
4314
4315    #[test]
4316    fn map_get_key_on_null_map_returns_null() -> anyhow::Result<()> {
4317        let vm = Vm::with_all()?;
4318        vm.import_code(
4319            "vm_get_key_null_map",
4320            br#"
4321            pub fn get_key_null(data) {
4322                data.get_key("missing")
4323            }
4324            "#
4325            .to_vec(),
4326        )?;
4327
4328        let compiled = vm.get_fn("vm_get_key_null_map::get_key_null", &[Type::Any])?;
4329        assert_eq!(compiled.ret_ty(), &Type::Any);
4330        let get_key_null: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4331
4332        let data_map = dynamic::map!("exists"=> 1i64);
4333        let missing = unsafe { &*get_key_null(&data_map) };
4334        assert!(missing.is_null());
4335
4336        let null = Dynamic::Null;
4337        let result = unsafe { &*get_key_null(&null) };
4338        assert!(result.is_null());
4339        Ok(())
4340    }
4341
4342    #[test]
4343    fn keys_on_empty_map_returns_empty_list() -> anyhow::Result<()> {
4344        let vm = Vm::with_all()?;
4345        vm.import_code(
4346            "vm_keys_empty_map",
4347            br#"
4348            pub fn empty_map_keys() {
4349                let data = {};
4350                data.keys().len()
4351            }
4352            "#
4353            .to_vec(),
4354        )?;
4355
4356        let compiled = vm.get_fn("vm_keys_empty_map::empty_map_keys", &[])?;
4357        assert_eq!(compiled.ret_ty(), &Type::I32);
4358        let empty_map_keys: extern "C" fn() -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
4359        assert_eq!(empty_map_keys(), 0);
4360        Ok(())
4361    }
4362
4363    #[test]
4364    fn cast_between_all_integer_widths() -> anyhow::Result<()> {
4365        let vm = Vm::with_all()?;
4366        vm.import_code(
4367            "vm_cast_integer_widths",
4368            br#"
4369            pub fn i64_to_i32(value: i64) {
4370                value as i32
4371            }
4372
4373            pub fn i32_to_i64(value: i32) {
4374                value as i64
4375            }
4376
4377            pub fn u32_to_i64(value: u32) {
4378                value as i64
4379            }
4380            "#
4381            .to_vec(),
4382        )?;
4383
4384        let compiled = vm.get_fn("vm_cast_integer_widths::i64_to_i32", &[Type::I64])?;
4385        assert_eq!(compiled.ret_ty(), &Type::I32);
4386        let i64_to_i32: extern "C" fn(i64) -> i32 = unsafe { std::mem::transmute(compiled.ptr()) };
4387        assert_eq!(i64_to_i32(42), 42);
4388
4389        let compiled = vm.get_fn("vm_cast_integer_widths::i32_to_i64", &[Type::I32])?;
4390        assert_eq!(compiled.ret_ty(), &Type::I64);
4391        let i32_to_i64: extern "C" fn(i32) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4392        assert_eq!(i32_to_i64(-1), -1);
4393
4394        let compiled = vm.get_fn("vm_cast_integer_widths::u32_to_i64", &[Type::U32])?;
4395        assert_eq!(compiled.ret_ty(), &Type::I64);
4396        let u32_to_i64: extern "C" fn(u32) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4397        assert_eq!(u32_to_i64(42), 42);
4398        Ok(())
4399    }
4400
4401    #[test]
4402    fn boolean_literals_in_complex_expression_trees() -> anyhow::Result<()> {
4403        let vm = Vm::with_all()?;
4404        vm.import_code(
4405            "vm_complex_boolean",
4406            br#"
4407            pub fn exclusive_or(a: bool, b: bool) {
4408                (a && !b) || (!a && b)
4409            }
4410
4411            pub fn implies(a: bool, b: bool) {
4412                !a || b
4413            }
4414            "#
4415            .to_vec(),
4416        )?;
4417
4418        let compiled = vm.get_fn("vm_complex_boolean::exclusive_or", &[Type::Bool, Type::Bool])?;
4419        assert_eq!(compiled.ret_ty(), &Type::Bool);
4420        let exclusive_or: extern "C" fn(bool, bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
4421        assert!(exclusive_or(true, false));
4422        assert!(exclusive_or(false, true));
4423        assert!(!exclusive_or(true, true));
4424        assert!(!exclusive_or(false, false));
4425
4426        let compiled = vm.get_fn("vm_complex_boolean::implies", &[Type::Bool, Type::Bool])?;
4427        assert_eq!(compiled.ret_ty(), &Type::Bool);
4428        let implies: extern "C" fn(bool, bool) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
4429        assert!(implies(false, true));
4430        assert!(implies(false, false));
4431        assert!(implies(true, true));
4432        assert!(!implies(true, false));
4433        Ok(())
4434    }
4435
4436    #[test]
4437    fn concrete_struct_method_returning_self_type() -> anyhow::Result<()> {
4438        let vm = Vm::with_all()?;
4439        vm.import_code(
4440            "vm_struct_method_self",
4441            br#"
4442            pub struct Vec3 {
4443                x: f64,
4444                y: f64,
4445                z: f64,
4446            }
4447
4448            impl Vec3 {
4449                pub fn add(self: Vec3, other: Vec3) {
4450                    Vec3{x: self.x + other.x, y: self.y + other.y, z: self.z + other.z}
4451                }
4452            }
4453
4454            pub fn run() {
4455                let v1 = Vec3{x: 1.0f64, y: 2.0f64, z: 3.0f64};
4456                let v2 = Vec3{x: 4.0f64, y: 5.0f64, z: 6.0f64};
4457                let sum = v1.add(v2);
4458                sum.x + sum.y + sum.z
4459            }
4460            "#
4461            .to_vec(),
4462        )?;
4463
4464        let compiled = vm.get_fn("vm_struct_method_self::run", &[])?;
4465        assert_eq!(compiled.ret_ty(), &Type::F64);
4466        let run: extern "C" fn() -> f64 = unsafe { std::mem::transmute(compiled.ptr()) };
4467        assert_eq!(run(), 21.0);
4468        Ok(())
4469    }
4470
4471    #[test]
4472    fn deep_nested_struct_access_with_multiple_field_levels() -> anyhow::Result<()> {
4473        let vm = Vm::with_all()?;
4474        vm.import_code(
4475            "vm_deep_nested_struct",
4476            br#"
4477            pub struct A {
4478                value: i64,
4479            }
4480
4481            pub struct B {
4482                a: A,
4483            }
4484
4485            pub struct C {
4486                b: B,
4487            }
4488
4489            pub fn direct_access() {
4490                let c = C{b: B{a: A{value: 99}}};
4491                c.b.a.value
4492            }
4493
4494            pub fn via_variable() {
4495                let c = C{b: B{a: A{value: 77}}};
4496                let b = c.b;
4497                let a = b.a;
4498                a.value
4499            }
4500            "#
4501            .to_vec(),
4502        )?;
4503
4504        let compiled = vm.get_fn("vm_deep_nested_struct::direct_access", &[])?;
4505        assert_eq!(compiled.ret_ty(), &Type::I64);
4506        let direct_access: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4507        assert_eq!(direct_access(), 99);
4508
4509        let compiled = vm.get_fn("vm_deep_nested_struct::via_variable", &[])?;
4510        assert_eq!(compiled.ret_ty(), &Type::I64);
4511        let via_variable: extern "C" fn() -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4512        assert_eq!(via_variable(), 77);
4513        Ok(())
4514    }
4515
4516    #[test]
4517    fn array_index_with_dynamic_value_via_method() -> anyhow::Result<()> {
4518        let vm = Vm::with_all()?;
4519        vm.import_code(
4520            "vm_array_idx_dynamic",
4521            br#"
4522            pub fn get_by_idx(list, idx) {
4523                list.get_idx(idx)
4524            }
4525            "#
4526            .to_vec(),
4527        )?;
4528
4529        let compiled = vm.get_fn("vm_array_idx_dynamic::get_by_idx", &[Type::Any, Type::I64])?;
4530        assert_eq!(compiled.ret_ty(), &Type::Any);
4531        let get_by_idx: extern "C" fn(*const Dynamic, i64) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4532
4533        let list = Dynamic::list(vec!["a".into(), "b".into()]);
4534        let first = unsafe { &*get_by_idx(&list, 0) };
4535        assert_eq!(first.as_str(), "a");
4536
4537        let out = unsafe { &*get_by_idx(&list, 10) };
4538        assert!(out.is_null());
4539        Ok(())
4540    }
4541
4542    #[test]
4543    fn dynamic_field_access_with_optional_or_fallback() -> anyhow::Result<()> {
4544        let vm = Vm::with_all()?;
4545        vm.import_code(
4546            "vm_dynamic_or_fallback",
4547            br#"
4548            pub fn with_fallback(data) {
4549                if data.contains("name") { data.name } else { "unknown" }
4550            }
4551
4552            pub fn with_fallback_missing(data) {
4553                if data.contains("nickname") { data.nickname } else { "unnamed" }
4554            }
4555            "#
4556            .to_vec(),
4557        )?;
4558
4559        let compiled = vm.get_fn("vm_dynamic_or_fallback::with_fallback", &[Type::Any])?;
4560        assert_eq!(compiled.ret_ty(), &Type::Any);
4561        let with_fallback: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4562        let data = dynamic::map!("name"=> "Alice");
4563        let result = unsafe { &*with_fallback(&data) };
4564        assert_eq!(result.as_str(), "Alice");
4565
4566        let compiled = vm.get_fn("vm_dynamic_or_fallback::with_fallback_missing", &[Type::Any])?;
4567        let with_fallback_missing: extern "C" fn(*const Dynamic) -> *const Dynamic = unsafe { std::mem::transmute(compiled.ptr()) };
4568        let result = unsafe { &*with_fallback_missing(&data) };
4569        assert_eq!(result.as_str(), "unnamed");
4570        Ok(())
4571    }
4572
4573    #[test]
4574    fn for_in_loop_iterates_over_list_and_map_directly() -> anyhow::Result<()> {
4575        let vm = Vm::with_all()?;
4576        vm.import_code(
4577            "vm_for_in_collection",
4578            br#"
4579            pub fn sum_list(items) {
4580                let total = 0i64;
4581                for item in items {
4582                    total = total + 1;
4583                }
4584                total
4585            }
4586
4587            pub fn count_map_keys(data) {
4588                let count = 0i64;
4589                for key in data.keys() {
4590                    count = count + 1;
4591                }
4592                count
4593            }
4594
4595            pub fn for_in_list_works(items) {
4596                let exists = false;
4597                for item in items {
4598                    exists = true;
4599                }
4600                exists
4601            }
4602
4603            pub fn for_in_map_values_works(data) {
4604                let exists = false;
4605                for value in data {
4606                    exists = true;
4607                }
4608                exists
4609            }
4610            "#
4611            .to_vec(),
4612        )?;
4613
4614        let compiled = vm.get_fn("vm_for_in_collection::sum_list", &[Type::Any])?;
4615        assert_eq!(compiled.ret_ty(), &Type::I64);
4616        let sum_list: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4617        let items = Dynamic::list(vec![Dynamic::from(1i64), Dynamic::from(2i64), Dynamic::from(3i64)]);
4618        assert_eq!(sum_list(&items), 3);
4619
4620        let data = dynamic::map!("x"=> 1i64, "y"=> 2i64);
4621        let compiled = vm.get_fn("vm_for_in_collection::count_map_keys", &[Type::Any])?;
4622        assert_eq!(compiled.ret_ty(), &Type::I64);
4623        let count_map_keys: extern "C" fn(*const Dynamic) -> i64 = unsafe { std::mem::transmute(compiled.ptr()) };
4624        assert_eq!(count_map_keys(&data), 2);
4625
4626        let compiled = vm.get_fn("vm_for_in_collection::for_in_list_works", &[Type::Any])?;
4627        assert_eq!(compiled.ret_ty(), &Type::Bool);
4628        let for_in_list_works: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
4629        let empty = Dynamic::list(Vec::new());
4630        assert!(!for_in_list_works(&empty));
4631        assert!(for_in_list_works(&items));
4632
4633        let compiled = vm.get_fn("vm_for_in_collection::for_in_map_values_works", &[Type::Any])?;
4634        assert_eq!(compiled.ret_ty(), &Type::Bool);
4635        let for_in_map_values_works: extern "C" fn(*const Dynamic) -> bool = unsafe { std::mem::transmute(compiled.ptr()) };
4636        let empty_map = dynamic::map!();
4637        assert!(!for_in_map_values_works(&empty_map));
4638        assert!(for_in_map_values_works(&data));
4639
4640        Ok(())
4641    }
4642
4643    #[test]
4644    fn concurrent_100_threads_no_memory_leak() -> anyhow::Result<()> {
4645        let vm = Vm::with_all()?;
4646        vm.import_code(
4647            "vm_stress",
4648            br#"
4649            pub fn heavy_alloc(idx: i64) {
4650                let items = [];
4651                let i = 0;
4652                while i < 50 {
4653                    items.push({
4654                        id: i + idx,
4655                        name: "item-" + i,
4656                        tags: ["tag-a", "tag-b", "tag-c"],
4657                        meta: {
4658                            created: 1234567890i64,
4659                            score: (i * 3.14f64) as i64,
4660                            extra: "prefix/" + i + "/" + idx
4661                        }
4662                    });
4663                    i = i + 1;
4664                }
4665                items
4666            }
4667
4668            pub fn string_concat_stress() {
4669                let i = 0;
4670                let result = "";
4671                while i < 200 {
4672                    result = result + "data-" + i + ",";
4673                    i = i + 1;
4674                }
4675                result
4676            }
4677            "#
4678            .to_vec(),
4679        )?;
4680
4681        let (heavy_ptr, _) = vm.get_fn_ptr("vm_stress::heavy_alloc", &[Type::I64])?;
4682        let (concat_ptr, _) = vm.get_fn_ptr("vm_stress::string_concat_stress", &[])?;
4683
4684        let threads: usize = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(4).max(100);
4685        let iters_per_thread = 200;
4686        let total_calls = threads * iters_per_thread * 2;
4687
4688        let before = current_rss_kb();
4689        eprintln!("threads={threads} iters_per_thread={iters_per_thread} total_calls={total_calls} rss_before={before}KB");
4690
4691        // Round 1: first concurrent execution (arena warm-up)
4692        run_stress_round(threads, iters_per_thread, heavy_ptr as usize, concat_ptr as usize);
4693        let r1 = current_rss_kb();
4694        eprintln!("rss_after_round1={r1}KB");
4695
4696        // Round 2: should stabilize (no unbounded growth)
4697        run_stress_round(threads, iters_per_thread, heavy_ptr as usize, concat_ptr as usize);
4698        let r2 = current_rss_kb();
4699        eprintln!("rss_after_round2={r2}KB");
4700
4701        // Round 3: final check
4702        run_stress_round(threads, iters_per_thread, heavy_ptr as usize, concat_ptr as usize);
4703        let r3 = current_rss_kb();
4704        eprintln!("rss_after_round3={r3}KB");
4705
4706        // Round 4: confirm that any one-time allocator growth has settled.
4707        run_stress_round(threads, iters_per_thread, heavy_ptr as usize, concat_ptr as usize);
4708        let r4 = current_rss_kb();
4709        eprintln!("rss_after_round4={r4}KB");
4710
4711        // Allocator/arena growth is allowed during warm-up, but it must settle.
4712        let d12 = r2.saturating_sub(r1);
4713        let d23 = r3.saturating_sub(r2);
4714        let d34 = r4.saturating_sub(r3);
4715        eprintln!("delta_r1→r2={d12}KB delta_r2→r3={d23}KB delta_r3→r4={d34}KB");
4716
4717        // The last interval must be small to prove the growth is not continuing.
4718        let max_growth_kb = 20 * 1024;
4719        assert!(d34 < max_growth_kb, "memory keeps growing after allocator warm-up: round1={r1} round2={r2} round3={r3} round4={r4} delta12={d12}KB delta23={d23}KB delta34={d34}KB (max stable growth={max_growth_kb}KB)");
4720
4721        Ok(())
4722    }
4723
4724    fn run_stress_round(threads: usize, iters: usize, heavy_ptr: usize, concat_ptr: usize) {
4725        std::thread::scope(|scope| {
4726            let mut handles = Vec::with_capacity(threads);
4727            for t in 0..threads {
4728                let heavy_ptr = heavy_ptr;
4729                let concat_ptr = concat_ptr;
4730                handles.push(scope.spawn(move || {
4731                    let heavy_fn: extern "C" fn(i64) -> *const Dynamic = unsafe { std::mem::transmute(heavy_ptr as *const u8) };
4732                    let concat_fn: extern "C" fn() -> *const Dynamic = unsafe { std::mem::transmute(concat_ptr as *const u8) };
4733                    for i in 0..iters {
4734                        // heavy_alloc: drop returned value to free heap allocation
4735                        let r_ptr = heavy_fn((t * iters + i) as i64);
4736                        assert!(!r_ptr.is_null());
4737                        unsafe {
4738                            let r = &*r_ptr;
4739                            assert!(r.len() > 0, "heavy_alloc returned empty list");
4740                            drop(Box::from_raw(r_ptr as *mut Dynamic));
4741                        }
4742
4743                        // concat: same, drop returned value
4744                        let s_ptr = concat_fn();
4745                        assert!(!s_ptr.is_null());
4746                        unsafe {
4747                            let s = &*s_ptr;
4748                            assert!(s.len() > 0, "string_concat_stress returned empty");
4749                            drop(Box::from_raw(s_ptr as *mut Dynamic));
4750                        }
4751                    }
4752                }));
4753            }
4754            for h in handles {
4755                h.join().unwrap();
4756            }
4757        });
4758    }
4759
4760    fn current_rss_kb() -> u64 {
4761        // macOS: use ps
4762        let pid = std::process::id();
4763        if let Ok(output) = std::process::Command::new("ps").args(["-p", &pid.to_string(), "-o", "rss="]).output() {
4764            if let Ok(s) = String::from_utf8(output.stdout) {
4765                if let Some(kb) = s.trim().parse::<u64>().ok() {
4766                    return kb;
4767                }
4768            }
4769        }
4770        // Linux fallback: /proc/self/statm
4771        if let Ok(statm) = std::fs::read_to_string("/proc/self/statm") {
4772            let parts: Vec<&str> = statm.split_whitespace().collect();
4773            if let Some(rss_pages) = parts.get(1).and_then(|s| s.parse::<u64>().ok()) {
4774                return rss_pages * 4; // pages (4KB) → KB
4775            }
4776        }
4777        0
4778    }
4779}