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, VmSet,
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 HarnStr = arcstr::ArcStr;
43
44pub type DictMap = imbl::OrdMap<HarnStr, VmValue>;
56
57pub fn intern_key(key: &str) -> HarnStr {
68 const MAX_INTERNED_KEY_LEN: usize = 64;
69 const MAX_INTERNED_KEYS: usize = 8192;
70 static INTERNED_KEYS: std::sync::LazyLock<parking_lot::Mutex<HashMap<Box<str>, HarnStr>>> =
71 std::sync::LazyLock::new(|| parking_lot::Mutex::new(HashMap::new()));
72
73 if key.len() > MAX_INTERNED_KEY_LEN {
74 return HarnStr::from(key);
75 }
76 let mut table = INTERNED_KEYS.lock();
77 if let Some(existing) = table.get(key) {
78 return existing.clone();
79 }
80 let interned = HarnStr::from(key);
81 if table.len() < MAX_INTERNED_KEYS {
82 table.insert(Box::from(key), interned.clone());
83 }
84 interned
85}
86
87pub trait IntoDictKey {
95 fn into_dict_key(self) -> HarnStr;
96}
97
98impl IntoDictKey for String {
99 fn into_dict_key(self) -> HarnStr {
100 intern_key(&self)
101 }
102}
103
104impl IntoDictKey for &str {
105 fn into_dict_key(self) -> HarnStr {
106 intern_key(self)
107 }
108}
109
110impl IntoDictKey for HarnStr {
111 fn into_dict_key(self) -> HarnStr {
112 self
113 }
114}
115
116pub fn string_char_count(text: &str) -> usize {
122 if text.is_ascii() {
123 text.len()
124 } else {
125 text.chars().count()
126 }
127}
128
129#[derive(Debug, Clone, PartialEq, Eq)]
131pub struct StructLayout {
132 struct_name: String,
133 field_names: Vec<String>,
134 field_indexes: HashMap<String, usize>,
135}
136
137impl StructLayout {
138 pub fn new(struct_name: impl Into<String>, field_names: Vec<String>) -> Self {
139 let mut deduped = Vec::with_capacity(field_names.len());
140 let mut field_indexes = HashMap::with_capacity(field_names.len());
141 for field_name in field_names {
142 if field_indexes.contains_key(&field_name) {
143 continue;
144 }
145 let index = deduped.len();
146 field_indexes.insert(field_name.clone(), index);
147 deduped.push(field_name);
148 }
149
150 Self {
151 struct_name: struct_name.into(),
152 field_names: deduped,
153 field_indexes,
154 }
155 }
156
157 pub fn from_map(struct_name: impl Into<String>, fields: &crate::value::DictMap) -> Self {
158 Self::new(
159 struct_name,
160 fields.keys().map(|key| key.to_string()).collect(),
161 )
162 }
163
164 pub fn struct_name(&self) -> &str {
165 &self.struct_name
166 }
167
168 pub fn field_names(&self) -> &[String] {
169 &self.field_names
170 }
171
172 pub fn field_index(&self, field_name: &str) -> Option<usize> {
173 if self.field_names.len() <= 8 {
174 return self
175 .field_names
176 .iter()
177 .position(|candidate| candidate == field_name);
178 }
179 self.field_indexes.get(field_name).copied()
180 }
181
182 pub fn with_appended_field(&self, field_name: String) -> Self {
183 if self.field_indexes.contains_key(&field_name) {
184 return self.clone();
185 }
186 let mut field_names = self.field_names.clone();
187 field_names.push(field_name);
188 Self::new(self.struct_name.clone(), field_names)
189 }
190}
191
192#[derive(Debug, Clone)]
194pub struct VmEnumVariant {
195 pub enum_name: HarnStr,
196 pub variant: HarnStr,
197 pub fields: Shared<Vec<VmValue>>,
198}
199
200impl VmEnumVariant {
201 pub fn has_enum_name(&self, enum_name: &str) -> bool {
202 self.enum_name.as_str() == enum_name
203 }
204
205 pub fn is_variant(&self, enum_name: &str, variant: &str) -> bool {
206 self.has_enum_name(enum_name) && self.variant.as_str() == variant
207 }
208}
209
210#[derive(Debug, Clone)]
217pub struct VmBuiltinRefId {
218 pub id: BuiltinId,
219 pub name: HarnStr,
220}
221
222#[derive(Debug, Clone)]
229pub struct StructInstanceData {
230 pub layout: Shared<StructLayout>,
231 pub fields: Shared<Vec<Option<VmValue>>>,
232}
233
234#[derive(Debug, Clone)]
249pub enum VmValue {
250 Int(i64),
251 Float(f64),
252 Decimal(Shared<rust_decimal::Decimal>),
260 String(HarnStr),
261 Bytes(Shared<Vec<u8>>),
262 Bool(bool),
263 Nil,
264 List(Shared<Vec<VmValue>>),
265 Dict(Shared<DictMap>),
266 Closure(Shared<VmClosure>),
267 BuiltinRef(HarnStr),
271 BuiltinRefId(Shared<VmBuiltinRefId>),
276 Duration(i64),
277 EnumVariant(Shared<VmEnumVariant>),
278 StructInstance(Shared<StructInstanceData>),
279 TaskHandle(HarnStr),
280 Channel(Shared<VmChannelHandle>),
281 Atomic(Shared<VmAtomicHandle>),
282 Rng(Shared<VmRngHandle>),
283 SyncPermit(Shared<VmSyncPermitHandle>),
284 McpClient(Shared<VmMcpClientHandle>),
285 Set(Shared<VmSet>),
286 Generator(Shared<VmGenerator>),
287 Stream(Shared<VmStream>),
288 Range(Shared<VmRange>),
292 Iter(crate::vm::iter::VmIterHandle),
294 Pair(Shared<(VmValue, VmValue)>),
299 Harness(Shared<VmHarness>),
304}
305
306static ASCII_CHAR_STRINGS: std::sync::LazyLock<[HarnStr; 128]> = std::sync::LazyLock::new(|| {
315 std::array::from_fn(|byte| {
316 let mut buffer = [0u8; 4];
317 HarnStr::from((byte as u8 as char).encode_utf8(&mut buffer))
318 })
319});
320
321impl VmValue {
322 pub fn string(value: impl AsRef<str>) -> Self {
330 VmValue::String(HarnStr::from(value.as_ref()))
331 }
332
333 pub fn decimal(value: rust_decimal::Decimal) -> Self {
338 VmValue::Decimal(Shared::new(value))
339 }
340
341 pub fn char_value(ch: char) -> Self {
345 if ch.is_ascii() {
346 return VmValue::String(ASCII_CHAR_STRINGS[ch as usize].clone());
347 }
348 let mut buffer = [0u8; 4];
349 VmValue::String(HarnStr::from(ch.encode_utf8(&mut buffer)))
350 }
351
352 pub fn chars_list(text: &str) -> Self {
357 VmValue::List(Shared::new(text.chars().map(VmValue::char_value).collect()))
358 }
359
360 pub fn enum_variant(
361 enum_name: impl Into<HarnStr>,
362 variant: impl Into<HarnStr>,
363 fields: Vec<VmValue>,
364 ) -> Self {
365 VmValue::EnumVariant(Shared::new(VmEnumVariant {
366 enum_name: enum_name.into(),
367 variant: variant.into(),
368 fields: Shared::new(fields),
369 }))
370 }
371
372 pub fn task_handle(id: impl Into<HarnStr>) -> Self {
373 VmValue::TaskHandle(id.into())
374 }
375
376 pub fn range(range: VmRange) -> Self {
378 VmValue::Range(Shared::new(range))
379 }
380
381 pub fn builtin_ref_id(id: BuiltinId, name: impl Into<HarnStr>) -> Self {
383 VmValue::BuiltinRefId(Shared::new(VmBuiltinRefId {
384 id,
385 name: name.into(),
386 }))
387 }
388
389 pub fn dict<K: IntoDictKey>(entries: impl IntoIterator<Item = (K, VmValue)>) -> Self {
395 VmValue::Dict(Shared::new(
396 entries
397 .into_iter()
398 .map(|(k, v)| (k.into_dict_key(), v))
399 .collect::<DictMap>(),
400 ))
401 }
402
403 pub fn dict_map(map: DictMap) -> Self {
405 VmValue::Dict(Shared::new(map))
406 }
407
408 pub fn set(values: impl IntoIterator<Item = VmValue>) -> Self {
411 VmValue::Set(Shared::new(values.into_iter().collect::<VmSet>()))
412 }
413
414 pub fn set_value(set: VmSet) -> Self {
416 VmValue::Set(Shared::new(set))
417 }
418
419 pub fn channel(handle: VmChannelHandle) -> Self {
420 VmValue::Channel(Shared::new(handle))
421 }
422
423 pub fn atomic(handle: VmAtomicHandle) -> Self {
424 VmValue::Atomic(Shared::new(handle))
425 }
426
427 pub fn rng(handle: VmRngHandle) -> Self {
428 VmValue::Rng(Shared::new(handle))
429 }
430
431 pub fn sync_permit(handle: VmSyncPermitHandle) -> Self {
432 VmValue::SyncPermit(Shared::new(handle))
433 }
434
435 pub fn mcp_client(handle: VmMcpClientHandle) -> Self {
436 VmValue::McpClient(Shared::new(handle))
437 }
438
439 pub fn generator(generator: VmGenerator) -> Self {
440 VmValue::Generator(Shared::new(generator))
441 }
442
443 pub fn stream(stream: VmStream) -> Self {
444 VmValue::Stream(Shared::new(stream))
445 }
446
447 pub fn harness(handle: VmHarness) -> Self {
448 VmValue::Harness(Shared::new(handle))
449 }
450
451 pub fn struct_instance(
452 struct_name: impl Into<Shared<str>>,
453 fields: crate::value::DictMap,
454 ) -> Self {
455 Self::struct_instance_from_map(struct_name.into().to_string(), fields)
456 }
457
458 pub fn is_truthy(&self) -> bool {
459 match self {
460 VmValue::Bool(b) => *b,
461 VmValue::Nil => false,
462 VmValue::Int(n) => *n != 0,
463 VmValue::Float(n) => *n != 0.0,
464 VmValue::Decimal(d) => **d != rust_decimal::Decimal::ZERO,
465 VmValue::String(s) => !s.is_empty(),
466 VmValue::Bytes(bytes) => !bytes.is_empty(),
467 VmValue::List(l) => !l.is_empty(),
468 VmValue::Dict(d) => !d.is_empty(),
469 VmValue::Closure(_) => true,
470 VmValue::BuiltinRef(_) => true,
471 VmValue::BuiltinRefId(_) => true,
472 VmValue::Duration(ms) => *ms != 0,
473 VmValue::EnumVariant(_) => true,
474 VmValue::StructInstance(_) => true,
475 VmValue::TaskHandle(_) => true,
476 VmValue::Channel(_) => true,
477 VmValue::Atomic(_) => true,
478 VmValue::Rng(_) => true,
479 VmValue::SyncPermit(_) => true,
480 VmValue::McpClient(_) => true,
481 VmValue::Set(s) => !s.is_empty(),
482 VmValue::Generator(_) => true,
483 VmValue::Stream(_) => true,
484 VmValue::Range(_) => true,
487 VmValue::Iter(_) => true,
488 VmValue::Pair(_) => true,
489 VmValue::Harness(_) => true,
490 }
491 }
492
493 pub fn type_name(&self) -> &'static str {
494 match self {
495 VmValue::String(_) => "string",
496 VmValue::Bytes(_) => "bytes",
497 VmValue::Int(_) => "int",
498 VmValue::Float(_) => "float",
499 VmValue::Decimal(_) => "decimal",
500 VmValue::Bool(_) => "bool",
501 VmValue::Nil => "nil",
502 VmValue::List(_) => "list",
503 VmValue::Dict(_) => "dict",
504 VmValue::Closure(_) => "closure",
505 VmValue::BuiltinRef(_) => "builtin",
506 VmValue::BuiltinRefId(_) => "builtin",
507 VmValue::Duration(_) => "duration",
508 VmValue::EnumVariant(_) => "enum",
509 VmValue::StructInstance(_) => "struct",
510 VmValue::TaskHandle(_) => "task_handle",
511 VmValue::Channel(_) => "channel",
512 VmValue::Atomic(_) => "atomic",
513 VmValue::Rng(_) => "rng",
514 VmValue::SyncPermit(_) => "sync_permit",
515 VmValue::McpClient(_) => "mcp_client",
516 VmValue::Set(_) => "set",
517 VmValue::Generator(_) => "generator",
518 VmValue::Stream(_) => "stream",
519 VmValue::Range(_) => "range",
520 VmValue::Iter(_) => "iter",
521 VmValue::Pair(_) => "pair",
522 VmValue::Harness(h) => h.type_name(),
523 }
524 }
525
526 pub fn as_str_cow(&self) -> std::borrow::Cow<'_, str> {
532 match self {
533 VmValue::String(s) => std::borrow::Cow::Borrowed(s.as_str()),
534 other => std::borrow::Cow::Owned(other.display()),
535 }
536 }
537
538 pub fn struct_data(&self) -> Option<&StructInstanceData> {
542 match self {
543 VmValue::StructInstance(data) => Some(data),
544 _ => None,
545 }
546 }
547
548 pub fn struct_name(&self) -> Option<&str> {
549 match self {
550 VmValue::StructInstance(data) => Some(data.layout.struct_name()),
551 _ => None,
552 }
553 }
554
555 pub fn struct_field(&self, field_name: &str) -> Option<&VmValue> {
556 match self {
557 VmValue::StructInstance(data) => data
558 .layout
559 .field_index(field_name)
560 .and_then(|index| data.fields.get(index))
561 .and_then(Option::as_ref),
562 _ => None,
563 }
564 }
565
566 pub fn struct_fields_map(&self) -> Option<crate::value::DictMap> {
567 match self {
568 VmValue::StructInstance(data) => Some(struct_fields_to_map(&data.layout, &data.fields)),
569 _ => None,
570 }
571 }
572
573 pub fn struct_instance_from_map(
574 struct_name: impl Into<String>,
575 fields: crate::value::DictMap,
576 ) -> Self {
577 let layout = Shared::new(StructLayout::from_map(struct_name, &fields));
578 let slots = layout
579 .field_names()
580 .iter()
581 .map(|name| fields.get(name.as_str()).cloned())
582 .collect();
583 VmValue::StructInstance(Shared::new(StructInstanceData {
584 layout,
585 fields: Shared::new(slots),
586 }))
587 }
588
589 pub fn struct_instance_with_layout(
590 struct_name: impl Into<String>,
591 field_names: Vec<String>,
592 field_values: crate::value::DictMap,
593 ) -> Self {
594 let layout = Shared::new(StructLayout::new(struct_name, field_names));
595 let fields = layout
596 .field_names()
597 .iter()
598 .map(|name| field_values.get(name.as_str()).cloned())
599 .collect();
600 VmValue::StructInstance(Shared::new(StructInstanceData {
601 layout,
602 fields: Shared::new(fields),
603 }))
604 }
605
606 pub fn struct_instance_with_property(&self, field_name: &str, value: VmValue) -> Option<Self> {
607 let VmValue::StructInstance(data) = self else {
608 return None;
609 };
610 let (layout, fields) = (&data.layout, &data.fields);
611
612 let mut new_fields = fields.as_ref().clone();
613 let layout = match layout.field_index(field_name) {
614 Some(index) => {
615 if index >= new_fields.len() {
616 new_fields.resize(index + 1, None);
617 }
618 new_fields[index] = Some(value);
619 Shared::clone(layout)
620 }
621 None => {
622 let new_layout = Shared::new(layout.with_appended_field(field_name.to_string()));
623 new_fields.push(Some(value));
624 new_layout
625 }
626 };
627
628 Some(VmValue::StructInstance(Shared::new(StructInstanceData {
629 layout,
630 fields: Shared::new(new_fields),
631 })))
632 }
633
634 pub fn display(&self) -> String {
635 let mut out = String::new();
636 self.write_display(&mut out);
637 out
638 }
639
640 pub fn write_display(&self, out: &mut String) {
643 use std::fmt::Write;
644
645 match self {
646 VmValue::Int(n) => {
647 let _ = write!(out, "{n}");
648 }
649 VmValue::Float(n) => {
650 if *n == (*n as i64) as f64 && n.abs() < 1e15 {
651 let _ = write!(out, "{n:.1}");
652 } else {
653 let _ = write!(out, "{n}");
654 }
655 }
656 VmValue::Decimal(d) => {
661 let _ = write!(out, "{d}");
662 }
663 VmValue::String(s) => out.push_str(s),
664 VmValue::Bytes(bytes) => {
665 const MAX_PREVIEW_BYTES: usize = 32;
666
667 out.push_str("b\"");
668 for byte in bytes.iter().take(MAX_PREVIEW_BYTES) {
669 let _ = write!(out, "{byte:02x}");
670 }
671 if bytes.len() > MAX_PREVIEW_BYTES {
672 let _ = write!(out, "...+{}", bytes.len() - MAX_PREVIEW_BYTES);
673 }
674 out.push('"');
675 }
676 VmValue::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
677 VmValue::Nil => out.push_str("nil"),
678 VmValue::List(items) => {
679 out.push('[');
680 crate::value::recursion::guard_recursion(|| {
681 for (i, item) in items.iter().enumerate() {
682 if i > 0 {
683 out.push_str(", ");
684 }
685 item.write_display(out);
686 }
687 });
688 out.push(']');
689 }
690 VmValue::Dict(map) => {
691 out.push('{');
692 crate::value::recursion::guard_recursion(|| {
693 for (i, (k, v)) in map.iter().enumerate() {
694 if i > 0 {
695 out.push_str(", ");
696 }
697 out.push_str(k);
698 out.push_str(": ");
699 v.write_display(out);
700 }
701 });
702 out.push('}');
703 }
704 VmValue::Closure(c) => {
705 let names: Vec<&str> = c.func.param_names().collect();
706 let _ = write!(out, "<fn({})>", names.join(", "));
707 }
708 VmValue::BuiltinRef(name) => {
709 let _ = write!(out, "<builtin {name}>");
710 }
711 VmValue::BuiltinRefId(r) => {
712 let _ = write!(out, "<builtin {}>", r.name);
713 }
714 VmValue::Duration(ms) => {
715 let sign = if *ms < 0 { "-" } else { "" };
716 let abs_ms = ms.unsigned_abs();
717 if abs_ms >= 604_800_000 && abs_ms % 604_800_000 == 0 {
718 let _ = write!(out, "{}{}w", sign, abs_ms / 604_800_000);
719 } else if abs_ms >= 86_400_000 && abs_ms % 86_400_000 == 0 {
720 let _ = write!(out, "{}{}d", sign, abs_ms / 86_400_000);
721 } else if abs_ms >= 3_600_000 && abs_ms % 3_600_000 == 0 {
722 let _ = write!(out, "{}{}h", sign, abs_ms / 3_600_000);
723 } else if abs_ms >= 60_000 && abs_ms % 60_000 == 0 {
724 let _ = write!(out, "{}{}m", sign, abs_ms / 60_000);
725 } else if abs_ms >= 1000 && abs_ms % 1000 == 0 {
726 let _ = write!(out, "{}{}s", sign, abs_ms / 1000);
727 } else {
728 let _ = write!(out, "{sign}{abs_ms}ms");
729 }
730 }
731 VmValue::EnumVariant(enum_variant) => {
732 if enum_variant.fields.is_empty() {
733 let _ = write!(out, "{}.{}", enum_variant.enum_name, enum_variant.variant);
734 } else {
735 let _ = write!(out, "{}.{}(", enum_variant.enum_name, enum_variant.variant);
736 crate::value::recursion::guard_recursion(|| {
737 for (i, v) in enum_variant.fields.iter().enumerate() {
738 if i > 0 {
739 out.push_str(", ");
740 }
741 v.write_display(out);
742 }
743 });
744 out.push(')');
745 }
746 }
747 VmValue::StructInstance(data) => {
748 let (layout, fields) = (&data.layout, &data.fields);
749 let _ = write!(out, "{} {{", layout.struct_name());
750 crate::value::recursion::guard_recursion(|| {
751 for (i, (k, v)) in struct_fields_to_map(layout, fields).iter().enumerate() {
752 if i > 0 {
753 out.push_str(", ");
754 }
755 out.push_str(k);
756 out.push_str(": ");
757 v.write_display(out);
758 }
759 });
760 out.push('}');
761 }
762 VmValue::TaskHandle(id) => {
763 let _ = write!(out, "<task:{id}>");
764 }
765 VmValue::Channel(ch) => {
766 let _ = write!(out, "<channel:{}>", ch.name);
767 }
768 VmValue::Atomic(a) => {
769 let _ = write!(out, "<atomic:{}>", a.value.load(Ordering::SeqCst));
770 }
771 VmValue::Rng(_) => {
772 out.push_str("<rng>");
773 }
774 VmValue::SyncPermit(p) => {
775 let _ = write!(out, "<sync_permit:{}:{}>", p.kind(), p.key());
776 }
777 VmValue::McpClient(c) => {
778 let _ = write!(out, "<mcp_client:{}>", c.name);
779 }
780 VmValue::Set(items) => {
781 out.push_str("set(");
782 crate::value::recursion::guard_recursion(|| {
783 for (i, item) in items.iter().enumerate() {
784 if i > 0 {
785 out.push_str(", ");
786 }
787 item.write_display(out);
788 }
789 });
790 out.push(')');
791 }
792 VmValue::Generator(g) => {
793 if g.is_done() {
794 out.push_str("<generator (done)>");
795 } else {
796 out.push_str("<generator>");
797 }
798 }
799 VmValue::Stream(s) => {
800 if s.is_done() {
801 out.push_str("<stream (done)>");
802 } else {
803 out.push_str("<stream>");
804 }
805 }
806 VmValue::Range(r) => {
809 let _ = write!(out, "{} to {}", r.start, r.end);
810 if !r.inclusive {
811 out.push_str(" exclusive");
812 }
813 }
814 VmValue::Iter(h) => {
815 if matches!(&*h.lock(), crate::vm::iter::VmIter::Exhausted) {
816 out.push_str("<iter (exhausted)>");
817 } else {
818 out.push_str("<iter>");
819 }
820 }
821 VmValue::Harness(h) => {
822 let _ = write!(out, "<{}>", h.type_name());
823 }
824 VmValue::Pair(p) => {
825 out.push('(');
826 crate::value::recursion::guard_recursion(|| {
827 p.0.write_display(out);
828 out.push_str(", ");
829 p.1.write_display(out);
830 });
831 out.push(')');
832 }
833 }
834 }
835
836 pub fn as_dict(&self) -> Option<&DictMap> {
838 if let VmValue::Dict(d) = self {
839 Some(d)
840 } else {
841 None
842 }
843 }
844
845 pub fn as_int(&self) -> Option<i64> {
846 if let VmValue::Int(n) = self {
847 Some(*n)
848 } else {
849 None
850 }
851 }
852
853 pub fn as_bytes(&self) -> Option<&[u8]> {
854 if let VmValue::Bytes(bytes) = self {
855 Some(bytes.as_slice())
856 } else {
857 None
858 }
859 }
860}
861
862pub fn struct_fields_to_map(
863 layout: &StructLayout,
864 fields: &[Option<VmValue>],
865) -> crate::value::DictMap {
866 layout
867 .field_names()
868 .iter()
869 .enumerate()
870 .filter_map(|(index, name)| {
871 fields
872 .get(index)
873 .and_then(Option::as_ref)
874 .map(|value| (intern_key(name), value.clone()))
875 })
876 .collect()
877}
878
879pub type VmBuiltinFn =
881 Arc<dyn Fn(&[VmValue], &mut String) -> Result<VmValue, VmError> + Send + Sync>;