1use std::collections::HashMap;
2use std::sync::atomic::Ordering;
3use std::sync::Arc;
4use std::{future::Future, pin::Pin};
5
6use crate::harness::VmHarness;
7use crate::mcp::VmMcpClientHandle;
8use crate::BuiltinId;
9
10use super::{
11 VmAtomicHandle, VmChannelHandle, VmClosure, VmError, VmGenerator, VmRange, VmRngHandle,
12 VmStream, VmSyncPermitHandle,
13};
14
15pub type VmAsyncBuiltinFn = Arc<
22 dyn Fn(
23 crate::vm::AsyncBuiltinCtx,
24 Vec<VmValue>,
25 ) -> Pin<Box<dyn Future<Output = Result<VmValue, VmError>> + Send>>
26 + Send
27 + Sync,
28>;
29
30type Shared<T> = Arc<T>;
31
32pub type DictMap = imbl::OrdMap<String, VmValue>;
44
45pub fn string_char_count(text: &str) -> usize {
51 if text.is_ascii() {
52 text.len()
53 } else {
54 text.chars().count()
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct StructLayout {
61 struct_name: String,
62 field_names: Vec<String>,
63 field_indexes: HashMap<String, usize>,
64}
65
66impl StructLayout {
67 pub fn new(struct_name: impl Into<String>, field_names: Vec<String>) -> Self {
68 let mut deduped = Vec::with_capacity(field_names.len());
69 let mut field_indexes = HashMap::with_capacity(field_names.len());
70 for field_name in field_names {
71 if field_indexes.contains_key(&field_name) {
72 continue;
73 }
74 let index = deduped.len();
75 field_indexes.insert(field_name.clone(), index);
76 deduped.push(field_name);
77 }
78
79 Self {
80 struct_name: struct_name.into(),
81 field_names: deduped,
82 field_indexes,
83 }
84 }
85
86 pub fn from_map(struct_name: impl Into<String>, fields: &crate::value::DictMap) -> Self {
87 Self::new(struct_name, fields.keys().cloned().collect())
88 }
89
90 pub fn struct_name(&self) -> &str {
91 &self.struct_name
92 }
93
94 pub fn field_names(&self) -> &[String] {
95 &self.field_names
96 }
97
98 pub fn field_index(&self, field_name: &str) -> Option<usize> {
99 if self.field_names.len() <= 8 {
100 return self
101 .field_names
102 .iter()
103 .position(|candidate| candidate == field_name);
104 }
105 self.field_indexes.get(field_name).copied()
106 }
107
108 pub fn with_appended_field(&self, field_name: String) -> Self {
109 if self.field_indexes.contains_key(&field_name) {
110 return self.clone();
111 }
112 let mut field_names = self.field_names.clone();
113 field_names.push(field_name);
114 Self::new(self.struct_name.clone(), field_names)
115 }
116}
117
118#[derive(Debug, Clone)]
120pub struct VmEnumVariant {
121 pub enum_name: Shared<str>,
122 pub variant: Shared<str>,
123 pub fields: Shared<Vec<VmValue>>,
124}
125
126impl VmEnumVariant {
127 pub fn has_enum_name(&self, enum_name: &str) -> bool {
128 self.enum_name.as_ref() == enum_name
129 }
130
131 pub fn is_variant(&self, enum_name: &str, variant: &str) -> bool {
132 self.has_enum_name(enum_name) && self.variant.as_ref() == variant
133 }
134}
135
136#[derive(Debug, Clone)]
143pub struct VmBuiltinRefId {
144 pub id: BuiltinId,
145 pub name: Shared<str>,
146}
147
148#[derive(Debug, Clone)]
161pub enum VmValue {
162 Int(i64),
163 Float(f64),
164 Decimal(rust_decimal::Decimal),
172 String(Shared<str>),
173 Bytes(Shared<Vec<u8>>),
174 Bool(bool),
175 Nil,
176 List(Shared<Vec<VmValue>>),
177 Dict(Shared<DictMap>),
178 Closure(Shared<VmClosure>),
179 BuiltinRef(Shared<str>),
183 BuiltinRefId(Shared<VmBuiltinRefId>),
188 Duration(i64),
189 EnumVariant(Shared<VmEnumVariant>),
190 StructInstance {
191 layout: Shared<StructLayout>,
192 fields: Shared<Vec<Option<VmValue>>>,
193 },
194 TaskHandle(Shared<str>),
195 Channel(Shared<VmChannelHandle>),
196 Atomic(Shared<VmAtomicHandle>),
197 Rng(Shared<VmRngHandle>),
198 SyncPermit(Shared<VmSyncPermitHandle>),
199 McpClient(Shared<VmMcpClientHandle>),
200 Set(Shared<Vec<VmValue>>),
201 Generator(Shared<VmGenerator>),
202 Stream(Shared<VmStream>),
203 Range(Shared<VmRange>),
207 Iter(crate::vm::iter::VmIterHandle),
209 Pair(Shared<(VmValue, VmValue)>),
214 Harness(Shared<VmHarness>),
219}
220
221static ASCII_CHAR_STRINGS: std::sync::LazyLock<[Arc<str>; 128]> = std::sync::LazyLock::new(|| {
230 std::array::from_fn(|byte| {
231 let mut buffer = [0u8; 4];
232 Arc::from((byte as u8 as char).encode_utf8(&mut buffer))
233 })
234});
235
236impl VmValue {
237 pub fn string(value: impl AsRef<str>) -> Self {
245 VmValue::String(Arc::from(value.as_ref()))
246 }
247
248 pub fn char_value(ch: char) -> Self {
252 if ch.is_ascii() {
253 return VmValue::String(Arc::clone(&ASCII_CHAR_STRINGS[ch as usize]));
254 }
255 let mut buffer = [0u8; 4];
256 VmValue::String(Arc::from(ch.encode_utf8(&mut buffer)))
257 }
258
259 pub fn chars_list(text: &str) -> Self {
264 VmValue::List(Shared::new(text.chars().map(VmValue::char_value).collect()))
265 }
266
267 pub fn enum_variant(
268 enum_name: impl Into<Shared<str>>,
269 variant: impl Into<Shared<str>>,
270 fields: Vec<VmValue>,
271 ) -> Self {
272 VmValue::EnumVariant(Shared::new(VmEnumVariant {
273 enum_name: enum_name.into(),
274 variant: variant.into(),
275 fields: Shared::new(fields),
276 }))
277 }
278
279 pub fn task_handle(id: impl Into<Shared<str>>) -> Self {
280 VmValue::TaskHandle(id.into())
281 }
282
283 pub fn range(range: VmRange) -> Self {
285 VmValue::Range(Shared::new(range))
286 }
287
288 pub fn builtin_ref_id(id: BuiltinId, name: impl Into<Shared<str>>) -> Self {
290 VmValue::BuiltinRefId(Shared::new(VmBuiltinRefId {
291 id,
292 name: name.into(),
293 }))
294 }
295
296 pub fn dict(entries: impl IntoIterator<Item = (String, VmValue)>) -> Self {
302 VmValue::Dict(Shared::new(entries.into_iter().collect::<DictMap>()))
303 }
304
305 pub fn dict_map(map: DictMap) -> Self {
307 VmValue::Dict(Shared::new(map))
308 }
309
310 pub fn channel(handle: VmChannelHandle) -> Self {
311 VmValue::Channel(Shared::new(handle))
312 }
313
314 pub fn atomic(handle: VmAtomicHandle) -> Self {
315 VmValue::Atomic(Shared::new(handle))
316 }
317
318 pub fn rng(handle: VmRngHandle) -> Self {
319 VmValue::Rng(Shared::new(handle))
320 }
321
322 pub fn sync_permit(handle: VmSyncPermitHandle) -> Self {
323 VmValue::SyncPermit(Shared::new(handle))
324 }
325
326 pub fn mcp_client(handle: VmMcpClientHandle) -> Self {
327 VmValue::McpClient(Shared::new(handle))
328 }
329
330 pub fn generator(generator: VmGenerator) -> Self {
331 VmValue::Generator(Shared::new(generator))
332 }
333
334 pub fn stream(stream: VmStream) -> Self {
335 VmValue::Stream(Shared::new(stream))
336 }
337
338 pub fn harness(handle: VmHarness) -> Self {
339 VmValue::Harness(Shared::new(handle))
340 }
341
342 pub fn struct_instance(
343 struct_name: impl Into<Shared<str>>,
344 fields: crate::value::DictMap,
345 ) -> Self {
346 Self::struct_instance_from_map(struct_name.into().to_string(), fields)
347 }
348
349 pub fn is_truthy(&self) -> bool {
350 match self {
351 VmValue::Bool(b) => *b,
352 VmValue::Nil => false,
353 VmValue::Int(n) => *n != 0,
354 VmValue::Float(n) => *n != 0.0,
355 VmValue::Decimal(d) => *d != rust_decimal::Decimal::ZERO,
356 VmValue::String(s) => !s.is_empty(),
357 VmValue::Bytes(bytes) => !bytes.is_empty(),
358 VmValue::List(l) => !l.is_empty(),
359 VmValue::Dict(d) => !d.is_empty(),
360 VmValue::Closure(_) => true,
361 VmValue::BuiltinRef(_) => true,
362 VmValue::BuiltinRefId(_) => true,
363 VmValue::Duration(ms) => *ms != 0,
364 VmValue::EnumVariant(_) => true,
365 VmValue::StructInstance { .. } => true,
366 VmValue::TaskHandle(_) => true,
367 VmValue::Channel(_) => true,
368 VmValue::Atomic(_) => true,
369 VmValue::Rng(_) => true,
370 VmValue::SyncPermit(_) => true,
371 VmValue::McpClient(_) => true,
372 VmValue::Set(s) => !s.is_empty(),
373 VmValue::Generator(_) => true,
374 VmValue::Stream(_) => true,
375 VmValue::Range(_) => true,
378 VmValue::Iter(_) => true,
379 VmValue::Pair(_) => true,
380 VmValue::Harness(_) => true,
381 }
382 }
383
384 pub fn type_name(&self) -> &'static str {
385 match self {
386 VmValue::String(_) => "string",
387 VmValue::Bytes(_) => "bytes",
388 VmValue::Int(_) => "int",
389 VmValue::Float(_) => "float",
390 VmValue::Decimal(_) => "decimal",
391 VmValue::Bool(_) => "bool",
392 VmValue::Nil => "nil",
393 VmValue::List(_) => "list",
394 VmValue::Dict(_) => "dict",
395 VmValue::Closure(_) => "closure",
396 VmValue::BuiltinRef(_) => "builtin",
397 VmValue::BuiltinRefId(_) => "builtin",
398 VmValue::Duration(_) => "duration",
399 VmValue::EnumVariant(_) => "enum",
400 VmValue::StructInstance { .. } => "struct",
401 VmValue::TaskHandle(_) => "task_handle",
402 VmValue::Channel(_) => "channel",
403 VmValue::Atomic(_) => "atomic",
404 VmValue::Rng(_) => "rng",
405 VmValue::SyncPermit(_) => "sync_permit",
406 VmValue::McpClient(_) => "mcp_client",
407 VmValue::Set(_) => "set",
408 VmValue::Generator(_) => "generator",
409 VmValue::Stream(_) => "stream",
410 VmValue::Range(_) => "range",
411 VmValue::Iter(_) => "iter",
412 VmValue::Pair(_) => "pair",
413 VmValue::Harness(h) => h.type_name(),
414 }
415 }
416
417 pub fn as_str_cow(&self) -> std::borrow::Cow<'_, str> {
423 match self {
424 VmValue::String(s) => std::borrow::Cow::Borrowed(s),
425 other => std::borrow::Cow::Owned(other.display()),
426 }
427 }
428
429 pub fn struct_name(&self) -> Option<&str> {
430 match self {
431 VmValue::StructInstance { layout, .. } => Some(layout.struct_name()),
432 _ => None,
433 }
434 }
435
436 pub fn struct_field(&self, field_name: &str) -> Option<&VmValue> {
437 match self {
438 VmValue::StructInstance { layout, fields } => layout
439 .field_index(field_name)
440 .and_then(|index| fields.get(index))
441 .and_then(Option::as_ref),
442 _ => None,
443 }
444 }
445
446 pub fn struct_fields_map(&self) -> Option<crate::value::DictMap> {
447 match self {
448 VmValue::StructInstance { layout, fields } => {
449 Some(struct_fields_to_map(layout, fields))
450 }
451 _ => None,
452 }
453 }
454
455 pub fn struct_instance_from_map(
456 struct_name: impl Into<String>,
457 fields: crate::value::DictMap,
458 ) -> Self {
459 let layout = Shared::new(StructLayout::from_map(struct_name, &fields));
460 let slots = layout
461 .field_names()
462 .iter()
463 .map(|name| fields.get(name).cloned())
464 .collect();
465 VmValue::StructInstance {
466 layout,
467 fields: Shared::new(slots),
468 }
469 }
470
471 pub fn struct_instance_with_layout(
472 struct_name: impl Into<String>,
473 field_names: Vec<String>,
474 field_values: crate::value::DictMap,
475 ) -> Self {
476 let layout = Shared::new(StructLayout::new(struct_name, field_names));
477 let fields = layout
478 .field_names()
479 .iter()
480 .map(|name| field_values.get(name).cloned())
481 .collect();
482 VmValue::StructInstance {
483 layout,
484 fields: Shared::new(fields),
485 }
486 }
487
488 pub fn struct_instance_with_property(&self, field_name: &str, value: VmValue) -> Option<Self> {
489 let VmValue::StructInstance { layout, fields } = self else {
490 return None;
491 };
492
493 let mut new_fields = fields.as_ref().clone();
494 let layout = match layout.field_index(field_name) {
495 Some(index) => {
496 if index >= new_fields.len() {
497 new_fields.resize(index + 1, None);
498 }
499 new_fields[index] = Some(value);
500 Shared::clone(layout)
501 }
502 None => {
503 let new_layout = Shared::new(layout.with_appended_field(field_name.to_string()));
504 new_fields.push(Some(value));
505 new_layout
506 }
507 };
508
509 Some(VmValue::StructInstance {
510 layout,
511 fields: Shared::new(new_fields),
512 })
513 }
514
515 pub fn display(&self) -> String {
516 let mut out = String::new();
517 self.write_display(&mut out);
518 out
519 }
520
521 pub fn write_display(&self, out: &mut String) {
524 use std::fmt::Write;
525
526 match self {
527 VmValue::Int(n) => {
528 let _ = write!(out, "{n}");
529 }
530 VmValue::Float(n) => {
531 if *n == (*n as i64) as f64 && n.abs() < 1e15 {
532 let _ = write!(out, "{n:.1}");
533 } else {
534 let _ = write!(out, "{n}");
535 }
536 }
537 VmValue::Decimal(d) => {
542 let _ = write!(out, "{d}");
543 }
544 VmValue::String(s) => out.push_str(s),
545 VmValue::Bytes(bytes) => {
546 const MAX_PREVIEW_BYTES: usize = 32;
547
548 out.push_str("b\"");
549 for byte in bytes.iter().take(MAX_PREVIEW_BYTES) {
550 let _ = write!(out, "{byte:02x}");
551 }
552 if bytes.len() > MAX_PREVIEW_BYTES {
553 let _ = write!(out, "...+{}", bytes.len() - MAX_PREVIEW_BYTES);
554 }
555 out.push('"');
556 }
557 VmValue::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
558 VmValue::Nil => out.push_str("nil"),
559 VmValue::List(items) => {
560 out.push('[');
561 crate::value::recursion::guard_recursion(|| {
562 for (i, item) in items.iter().enumerate() {
563 if i > 0 {
564 out.push_str(", ");
565 }
566 item.write_display(out);
567 }
568 });
569 out.push(']');
570 }
571 VmValue::Dict(map) => {
572 out.push('{');
573 crate::value::recursion::guard_recursion(|| {
574 for (i, (k, v)) in map.iter().enumerate() {
575 if i > 0 {
576 out.push_str(", ");
577 }
578 out.push_str(k);
579 out.push_str(": ");
580 v.write_display(out);
581 }
582 });
583 out.push('}');
584 }
585 VmValue::Closure(c) => {
586 let names: Vec<&str> = c.func.param_names().collect();
587 let _ = write!(out, "<fn({})>", names.join(", "));
588 }
589 VmValue::BuiltinRef(name) => {
590 let _ = write!(out, "<builtin {name}>");
591 }
592 VmValue::BuiltinRefId(r) => {
593 let _ = write!(out, "<builtin {}>", r.name);
594 }
595 VmValue::Duration(ms) => {
596 let sign = if *ms < 0 { "-" } else { "" };
597 let abs_ms = ms.unsigned_abs();
598 if abs_ms >= 604_800_000 && abs_ms % 604_800_000 == 0 {
599 let _ = write!(out, "{}{}w", sign, abs_ms / 604_800_000);
600 } else if abs_ms >= 86_400_000 && abs_ms % 86_400_000 == 0 {
601 let _ = write!(out, "{}{}d", sign, abs_ms / 86_400_000);
602 } else if abs_ms >= 3_600_000 && abs_ms % 3_600_000 == 0 {
603 let _ = write!(out, "{}{}h", sign, abs_ms / 3_600_000);
604 } else if abs_ms >= 60_000 && abs_ms % 60_000 == 0 {
605 let _ = write!(out, "{}{}m", sign, abs_ms / 60_000);
606 } else if abs_ms >= 1000 && abs_ms % 1000 == 0 {
607 let _ = write!(out, "{}{}s", sign, abs_ms / 1000);
608 } else {
609 let _ = write!(out, "{sign}{abs_ms}ms");
610 }
611 }
612 VmValue::EnumVariant(enum_variant) => {
613 if enum_variant.fields.is_empty() {
614 let _ = write!(out, "{}.{}", enum_variant.enum_name, enum_variant.variant);
615 } else {
616 let _ = write!(out, "{}.{}(", enum_variant.enum_name, enum_variant.variant);
617 crate::value::recursion::guard_recursion(|| {
618 for (i, v) in enum_variant.fields.iter().enumerate() {
619 if i > 0 {
620 out.push_str(", ");
621 }
622 v.write_display(out);
623 }
624 });
625 out.push(')');
626 }
627 }
628 VmValue::StructInstance { layout, fields } => {
629 let _ = write!(out, "{} {{", layout.struct_name());
630 crate::value::recursion::guard_recursion(|| {
631 for (i, (k, v)) in struct_fields_to_map(layout, fields).iter().enumerate() {
632 if i > 0 {
633 out.push_str(", ");
634 }
635 out.push_str(k);
636 out.push_str(": ");
637 v.write_display(out);
638 }
639 });
640 out.push('}');
641 }
642 VmValue::TaskHandle(id) => {
643 let _ = write!(out, "<task:{id}>");
644 }
645 VmValue::Channel(ch) => {
646 let _ = write!(out, "<channel:{}>", ch.name);
647 }
648 VmValue::Atomic(a) => {
649 let _ = write!(out, "<atomic:{}>", a.value.load(Ordering::SeqCst));
650 }
651 VmValue::Rng(_) => {
652 out.push_str("<rng>");
653 }
654 VmValue::SyncPermit(p) => {
655 let _ = write!(out, "<sync_permit:{}:{}>", p.kind(), p.key());
656 }
657 VmValue::McpClient(c) => {
658 let _ = write!(out, "<mcp_client:{}>", c.name);
659 }
660 VmValue::Set(items) => {
661 out.push_str("set(");
662 crate::value::recursion::guard_recursion(|| {
663 for (i, item) in items.iter().enumerate() {
664 if i > 0 {
665 out.push_str(", ");
666 }
667 item.write_display(out);
668 }
669 });
670 out.push(')');
671 }
672 VmValue::Generator(g) => {
673 if g.is_done() {
674 out.push_str("<generator (done)>");
675 } else {
676 out.push_str("<generator>");
677 }
678 }
679 VmValue::Stream(s) => {
680 if s.is_done() {
681 out.push_str("<stream (done)>");
682 } else {
683 out.push_str("<stream>");
684 }
685 }
686 VmValue::Range(r) => {
689 let _ = write!(out, "{} to {}", r.start, r.end);
690 if !r.inclusive {
691 out.push_str(" exclusive");
692 }
693 }
694 VmValue::Iter(h) => {
695 if matches!(&*h.lock(), crate::vm::iter::VmIter::Exhausted) {
696 out.push_str("<iter (exhausted)>");
697 } else {
698 out.push_str("<iter>");
699 }
700 }
701 VmValue::Harness(h) => {
702 let _ = write!(out, "<{}>", h.type_name());
703 }
704 VmValue::Pair(p) => {
705 out.push('(');
706 crate::value::recursion::guard_recursion(|| {
707 p.0.write_display(out);
708 out.push_str(", ");
709 p.1.write_display(out);
710 });
711 out.push(')');
712 }
713 }
714 }
715
716 pub fn as_dict(&self) -> Option<&DictMap> {
718 if let VmValue::Dict(d) = self {
719 Some(d)
720 } else {
721 None
722 }
723 }
724
725 pub fn as_int(&self) -> Option<i64> {
726 if let VmValue::Int(n) = self {
727 Some(*n)
728 } else {
729 None
730 }
731 }
732
733 pub fn as_bytes(&self) -> Option<&[u8]> {
734 if let VmValue::Bytes(bytes) = self {
735 Some(bytes.as_slice())
736 } else {
737 None
738 }
739 }
740}
741
742pub fn struct_fields_to_map(
743 layout: &StructLayout,
744 fields: &[Option<VmValue>],
745) -> crate::value::DictMap {
746 layout
747 .field_names()
748 .iter()
749 .enumerate()
750 .filter_map(|(index, name)| {
751 fields
752 .get(index)
753 .and_then(Option::as_ref)
754 .map(|value| (name.clone(), value.clone()))
755 })
756 .collect()
757}
758
759pub type VmBuiltinFn =
761 Arc<dyn Fn(&[VmValue], &mut String) -> Result<VmValue, VmError> + Send + Sync>;