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