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