Skip to main content

vm/
lib.rs

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