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