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