Skip to main content

vm/
lib.rs

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