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