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