Skip to main content

vm/
lib.rs

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