Skip to main content

vm/
lib.rs

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