ruchy 4.1.1

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
//! Core method dispatch logic
//!
//! Extracted from interpreter_methods.rs for coverage attribution.

#![allow(clippy::unused_self)]
#![allow(clippy::only_used_in_recursion)]
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::expect_used)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::rc_buffer)]

use crate::frontend::ast::{Expr, ExprKind};
use crate::runtime::interpreter::Interpreter;
use crate::runtime::{InterpreterError, Value};
use std::collections::HashMap;
use std::sync::Arc;

impl Interpreter {
    /// Evaluate a method call
    pub(crate) fn eval_method_call(
        &mut self,
        receiver: &Expr,
        method: &str,
        args: &[Expr],
    ) -> Result<Value, InterpreterError> {
        // Special handling for stdlib namespace methods (e.g., Html.parse())
        if let ExprKind::Identifier(namespace) = &receiver.kind {
            // Check if this is a stdlib namespace call before trying to look it up as a variable
            let namespace_method = format!("{namespace}_{method}");

            // Try to evaluate as builtin function first
            let arg_values: Result<Vec<_>, _> =
                args.iter().map(|arg| self.eval_expr(arg)).collect();
            let arg_values = arg_values?;

            if let Ok(Some(result)) =
                crate::runtime::eval_builtin::eval_builtin_function(&namespace_method, &arg_values)
            {
                return Ok(result);
            }
        }

        // Special handling for mutating array methods on simple identifiers
        // e.g., messages.push(item)
        if let ExprKind::Identifier(var_name) = &receiver.kind {
            if method == "push" && args.len() == 1 {
                // Get current array value
                if let Ok(Value::Array(arr)) = self.lookup_variable(var_name) {
                    // Evaluate the argument
                    let arg_value = self.eval_expr(&args[0])?;

                    // Create new array with item added
                    let mut new_arr = arr.to_vec();
                    new_arr.push(arg_value);

                    // Update the variable binding - CRITICAL: Use env_set_mut to update
                    // in parent scopes (e.g., when push is called inside while loops)
                    self.env_set_mut(var_name.clone(), Value::Array(Arc::from(new_arr)));

                    return Ok(Value::Nil); // push returns nil
                }
            } else if method == "pop" && args.is_empty() {
                // Get current array value
                if let Ok(Value::Array(arr)) = self.lookup_variable(var_name) {
                    // Create new array with last item removed
                    let mut new_arr = arr.to_vec();
                    let popped_value = new_arr.pop().unwrap_or(Value::Nil);

                    // Update the variable binding - CRITICAL: Use env_set_mut to update
                    // in parent scopes (e.g., when pop is called inside while loops)
                    self.env_set_mut(var_name.clone(), Value::Array(Arc::from(new_arr)));

                    return Ok(popped_value); // pop returns the removed item
                }
            }
        }

        // Special handling for mutating array methods on ObjectMut fields
        // e.g., self.messages.push(item)
        if let ExprKind::FieldAccess { object, field } = &receiver.kind {
            if let Ok(object_value) = self.eval_expr(object) {
                if let Value::ObjectMut(cell_rc) = object_value {
                    // Check if this is a mutating array method
                    if method == "push" && args.len() == 1 {
                        // Evaluate the argument
                        let arg_value = self.eval_expr(&args[0])?;

                        // Get mutable access to the object
                        let mut obj = cell_rc
                            .lock()
                            .expect("Mutex poisoned: object lock is corrupted");

                        // Get the field value
                        if let Some(field_value) = obj.get(field) {
                            // If it's an array, push to it
                            if let Value::Array(arr) = field_value {
                                let mut new_arr = arr.to_vec();
                                new_arr.push(arg_value);
                                obj.insert(field.clone(), Value::Array(Arc::from(new_arr)));
                                return Ok(Value::Nil); // push returns nil
                            }
                        }
                    }
                }
            }
        }

        let receiver_value = self.eval_expr(receiver)?;

        // Special handling for Module method calls - look up function and call it
        // This allows `mod math { pub fun add(a, b) { ... } }; math.add(1, 2)`
        if let Value::Object(ref obj) = receiver_value {
            if let Some(Value::String(type_name)) = obj.get("__type") {
                if type_name.as_ref() == "Module" {
                    // Look up the function in the module
                    let func_value = obj.get(method).ok_or_else(|| {
                        InterpreterError::RuntimeError(format!(
                            "Module has no function named '{}'",
                            method
                        ))
                    })?;

                    // Evaluate arguments
                    let arg_values: Result<Vec<_>, _> =
                        args.iter().map(|arg| self.eval_expr(arg)).collect();
                    let arg_values = arg_values?;

                    // Call the function using the existing call_function infrastructure
                    return self.call_function(func_value.clone(), &arg_values);
                }
            }
        }

        // Special handling for DataFrame methods with closures - don't pre-evaluate the closure argument
        if matches!(receiver_value, Value::DataFrame { .. }) {
            match method {
                "filter" => return self.eval_dataframe_filter_method(&receiver_value, args),
                "with_column" => {
                    return self.eval_dataframe_with_column_method(&receiver_value, args)
                }
                "transform" => return self.eval_dataframe_transform_method(&receiver_value, args),
                _ => {}
            }
        }

        // Special handling for actor send/ask methods - convert undefined identifiers to messages
        if (method == "send" || method == "ask") && args.len() == 1 {
            // Check if receiver is an actor instance (immutable or mutable)
            let is_actor = match &receiver_value {
                Value::Object(ref obj) => obj.contains_key("__actor"),
                Value::ObjectMut(ref cell) => cell
                    .lock()
                    .expect("Mutex poisoned: object lock is corrupted")
                    .contains_key("__actor"),
                _ => false,
            };

            if is_actor {
                // Try to evaluate the argument as a message
                let arg_value = match &args[0].kind {
                    ExprKind::Identifier(name) => {
                        // Try to evaluate as variable first
                        if let Ok(val) = self.lookup_variable(name) {
                            val
                        } else {
                            // Treat as a zero-argument message constructor
                            let mut message = HashMap::new();
                            message.insert(
                                "__type".to_string(),
                                Value::from_string("Message".to_string()),
                            );
                            message.insert("type".to_string(), Value::from_string(name.clone()));
                            message.insert("data".to_string(), Value::Array(Arc::from(vec![])));
                            Value::Object(Arc::new(message))
                        }
                    }
                    _ => self.eval_expr(&args[0])?,
                };
                return self.dispatch_method_call(&receiver_value, method, &[arg_value], false);
            }
        }

        let arg_values: Result<Vec<_>, _> = args.iter().map(|arg| self.eval_expr(arg)).collect();
        let arg_values = arg_values?;

        // RUNTIME-099 FIX: For mutable method calls on identifiers, ensure variable binding
        // is updated after the method executes (similar to array.push/pop pattern)
        if let ExprKind::Identifier(var_name) = &receiver.kind {
            if matches!(receiver_value, Value::ObjectMut(_)) {
                // Call the mutable method
                let result = self.dispatch_method_call(
                    &receiver_value,
                    method,
                    &arg_values,
                    args.is_empty(),
                )?;

                // Update the variable binding to ensure mutations persist
                // (ObjectMut uses Arc, so this just ensures the binding is current)
                self.set_variable(var_name, receiver_value);

                return Ok(result);
            }

            // RUNTIME-ISSUE-148 FIX: Handle Value::Struct method calls with &mut self
            // Structs use value semantics - method modifications create a new struct that must replace the variable
            if let Value::Struct { name, fields } = &receiver_value {
                // Check if this struct has impl methods (not just generic object methods)
                let qualified_method_name = format!("{}::{}", name, method);
                if self.lookup_variable(&qualified_method_name).is_ok() {
                    // This is a struct with custom methods - use capture version
                    let (result, modified_fields_opt) = self
                        .eval_struct_instance_method_with_self_capture(
                            fields,
                            name,
                            method,
                            &arg_values,
                        )?;

                    // If method modified self, update the variable with modified struct
                    if let Some(modified_fields) = modified_fields_opt {
                        let new_struct = Value::Struct {
                            name: name.clone(),
                            fields: modified_fields,
                        };
                        self.set_variable(var_name, new_struct);
                    }

                    return Ok(result);
                }
            }
        }

        self.dispatch_method_call(&receiver_value, method, &arg_values, args.is_empty())
    }

    // Helper methods for method dispatch (complexity <10 each)

    /// Evaluate a message expression - if it's an undefined identifier, treat as message name
    /// Complexity: ≤5
    pub(crate) fn eval_message_expr(&mut self, message: &Expr) -> Result<Value, InterpreterError> {
        match &message.kind {
            ExprKind::Identifier(name) => {
                // Try to evaluate as variable first
                if let Ok(val) = self.lookup_variable(name) {
                    Ok(val)
                } else {
                    // Treat as a zero-argument message constructor
                    let mut msg_obj = HashMap::new();
                    msg_obj.insert(
                        "__type".to_string(),
                        Value::from_string("Message".to_string()),
                    );
                    msg_obj.insert("type".to_string(), Value::from_string(name.clone()));
                    msg_obj.insert("data".to_string(), Value::Array(Arc::from(vec![])));
                    Ok(Value::Object(Arc::new(msg_obj)))
                }
            }
            _ => self.eval_expr(message),
        }
    }

    pub(crate) fn dispatch_method_call(
        &mut self,
        receiver: &Value,
        method: &str,
        arg_values: &[Value],
        args_empty: bool,
    ) -> Result<Value, InterpreterError> {
        // EVALUATOR-001: Strip turbofish syntax from method names
        // Example: "parse::<i32>" becomes "parse"
        // Turbofish is for type hints only, not used in runtime method lookup
        let base_method = if let Some(pos) = method.find("::") {
            &method[..pos]
        } else {
            method
        };

        match receiver {
            Value::String(s) => self.eval_string_method(s, base_method, arg_values),
            Value::Array(arr) => self.eval_array_method(arr, base_method, arg_values),
            Value::Float(f) => self.eval_float_method(*f, base_method, args_empty),
            Value::Integer(n) => self.eval_integer_method(*n, base_method, arg_values),
            Value::DataFrame { columns } => {
                self.eval_dataframe_method(columns, base_method, arg_values)
            }
            Value::Object(obj) => {
                // Check if this is an actor instance
                if let Some(Value::String(actor_name)) = obj.get("__actor") {
                    self.eval_actor_instance_method(
                        obj,
                        actor_name.as_ref(),
                        base_method,
                        arg_values,
                    )
                }
                // Check if this is a class instance
                else if let Some(Value::String(class_name)) = obj.get("__class") {
                    self.eval_class_instance_method(
                        obj,
                        class_name.as_ref(),
                        base_method,
                        arg_values,
                    )
                }
                // Check if this is a struct instance with impl methods
                else if let Some(Value::String(struct_name)) =
                    obj.get("__struct_type").or_else(|| obj.get("__struct"))
                {
                    self.eval_struct_instance_method(
                        obj,
                        struct_name.as_ref(),
                        base_method,
                        arg_values,
                    )
                }
                // Check if this is a `DataFrame` builder
                else if let Some(Value::String(type_str)) = obj.get("__type") {
                    if type_str.as_ref() == "DataFrameBuilder" {
                        self.eval_dataframe_builder_method(obj, base_method, arg_values)
                    } else {
                        self.eval_object_method(obj, base_method, arg_values, args_empty)
                    }
                } else {
                    self.eval_object_method(obj, base_method, arg_values, args_empty)
                }
            }
            Value::ObjectMut(cell_rc) => {
                // Dispatch mutable objects the same way as immutable ones
                // Safe borrow: We only read metadata fields to determine dispatch
                let obj = cell_rc
                    .lock()
                    .expect("Mutex poisoned: object lock is corrupted");

                // Check if this is an actor instance
                if let Some(Value::String(actor_name)) = obj.get("__actor") {
                    let actor_name = actor_name.clone();
                    drop(obj); // Release borrow before recursive call
                    self.eval_actor_instance_method_mut(
                        cell_rc,
                        actor_name.as_ref(),
                        base_method,
                        arg_values,
                    )
                }
                // Check if this is a class instance
                else if let Some(Value::String(class_name)) = obj.get("__class") {
                    let class_name = class_name.clone();
                    drop(obj); // Release borrow before recursive call
                    self.eval_class_instance_method_mut(
                        cell_rc,
                        class_name.as_ref(),
                        base_method,
                        arg_values,
                    )
                }
                // Check if this is a struct instance with impl methods
                else if let Some(Value::String(struct_name)) =
                    obj.get("__struct_type").or_else(|| obj.get("__struct"))
                {
                    let struct_name = struct_name.clone();
                    drop(obj); // Release borrow before recursive call
                    self.eval_struct_instance_method_mut(
                        cell_rc,
                        struct_name.as_ref(),
                        base_method,
                        arg_values,
                    )
                }
                // ISSUE-116: Check if this is a File object
                else if let Some(Value::String(type_name)) = obj.get("__type") {
                    if type_name.as_ref() == "File" {
                        drop(obj); // Release borrow before recursive call
                        return self.eval_file_method_mut(cell_rc, base_method, arg_values);
                    }
                    drop(obj); // Release borrow before recursive call
                    self.eval_object_method_mut(cell_rc, base_method, arg_values, args_empty)
                } else {
                    drop(obj); // Release borrow before recursive call
                    self.eval_object_method_mut(cell_rc, base_method, arg_values, args_empty)
                }
            }
            Value::Struct { name, fields } => {
                // Dispatch struct instance method call
                self.eval_struct_instance_method(fields, name, base_method, arg_values)
            }
            Value::Class {
                class_name,
                fields,
                methods,
            } => {
                // Dispatch instance method call on Class
                self.eval_class_instance_method_on_class(
                    class_name,
                    fields,
                    methods,
                    base_method,
                    arg_values,
                )
            }
            #[cfg(not(target_arch = "wasm32"))]
            Value::HtmlDocument(doc) => {
                self.eval_html_document_method(doc, base_method, arg_values)
            }
            #[cfg(not(target_arch = "wasm32"))]
            Value::HtmlElement(elem) => {
                self.eval_html_element_method(elem, base_method, arg_values)
            }
            _ => self.eval_generic_method(receiver, base_method, args_empty),
        }
    }

    pub(crate) fn eval_float_method(
        &self,
        f: f64,
        method: &str,
        args_empty: bool,
    ) -> Result<Value, InterpreterError> {
        super::eval_method::eval_float_method(f, method, args_empty)
    }

    pub(crate) fn eval_integer_method(
        &self,
        n: i64,
        method: &str,
        arg_values: &[Value],
    ) -> Result<Value, InterpreterError> {
        super::eval_method::eval_integer_method(n, method, arg_values)
    }

    pub(crate) fn eval_generic_method(
        &self,
        receiver: &Value,
        method: &str,
        args_empty: bool,
    ) -> Result<Value, InterpreterError> {
        super::eval_method::eval_generic_method(receiver, method, args_empty)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frontend::ast::Span;

    fn make_interpreter() -> Interpreter {
        Interpreter::new()
    }

    fn make_expr(kind: ExprKind) -> Expr {
        Expr {
            kind,
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    // Test turbofish stripping
    #[test]
    fn test_dispatch_strips_turbofish() {
        let mut interp = make_interpreter();
        let s = Value::from_string("42".to_string());
        // parse::<i32> should become just "parse"
        let result = interp.dispatch_method_call(&s, "parse::<i32>", &[], false);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), Value::Integer(42));
    }

    // Test dispatch to string method
    #[test]
    fn test_dispatch_to_string_method() {
        let mut interp = make_interpreter();
        let s = Value::from_string("hello".to_string());
        let result = interp.dispatch_method_call(&s, "len", &[], false).unwrap();
        assert_eq!(result, Value::Integer(5));
    }

    // Test dispatch to array method
    #[test]
    fn test_dispatch_to_array_method() {
        let mut interp = make_interpreter();
        let arr = Value::Array(Arc::from(vec![Value::Integer(1), Value::Integer(2)]));
        let result = interp
            .dispatch_method_call(&arr, "len", &[], false)
            .unwrap();
        assert_eq!(result, Value::Integer(2));
    }

    // Test dispatch to float method
    #[test]
    fn test_dispatch_to_float_method() {
        let mut interp = make_interpreter();
        let f = Value::Float(3.7);
        let result = interp.dispatch_method_call(&f, "round", &[], true).unwrap();
        assert_eq!(result, Value::Float(4.0));
    }

    // Test dispatch to integer method
    #[test]
    fn test_dispatch_to_integer_method() {
        let mut interp = make_interpreter();
        let n = Value::Integer(-5);
        let result = interp.dispatch_method_call(&n, "abs", &[], false).unwrap();
        assert_eq!(result, Value::Integer(5));
    }

    // Test eval_message_expr with undefined identifier
    #[test]
    fn test_eval_message_expr_undefined_identifier() {
        let mut interp = make_interpreter();
        let expr = make_expr(ExprKind::Identifier("Increment".to_string()));
        let result = interp.eval_message_expr(&expr).unwrap();

        if let Value::Object(obj) = result {
            assert_eq!(
                obj.get("__type"),
                Some(&Value::from_string("Message".to_string()))
            );
            assert_eq!(
                obj.get("type"),
                Some(&Value::from_string("Increment".to_string()))
            );
        } else {
            panic!("Expected Object");
        }
    }

    // Test eval_message_expr with defined variable
    #[test]
    fn test_eval_message_expr_defined_variable() {
        let mut interp = make_interpreter();
        interp.set_variable("x", Value::Integer(42));
        let expr = make_expr(ExprKind::Identifier("x".to_string()));
        let result = interp.eval_message_expr(&expr).unwrap();
        assert_eq!(result, Value::Integer(42));
    }

    // Test eval_message_expr with literal
    #[test]
    fn test_eval_message_expr_literal() {
        let mut interp = make_interpreter();
        let expr = make_expr(ExprKind::Literal(crate::frontend::ast::Literal::Integer(
            100, None,
        )));
        let result = interp.eval_message_expr(&expr).unwrap();
        assert_eq!(result, Value::Integer(100));
    }

    // Test dispatch to object method - missing type marker
    #[test]
    fn test_dispatch_to_object_missing_type() {
        let mut interp = make_interpreter();
        let obj_map = HashMap::new();
        let obj = Value::Object(Arc::new(obj_map));

        let result = interp.dispatch_method_call(&obj, "test", &[], true);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("missing __type marker"));
    }

    // Test dispatch to object method - unknown type
    #[test]
    fn test_dispatch_to_object_unknown_type() {
        let mut interp = make_interpreter();
        let mut obj_map = HashMap::new();
        obj_map.insert(
            "__type".to_string(),
            Value::from_string("UnknownType".to_string()),
        );
        let obj = Value::Object(Arc::new(obj_map));

        let result = interp.dispatch_method_call(&obj, "test", &[], true);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unknown object type"));
    }

    // Test eval_generic_method
    #[test]
    fn test_eval_generic_method() {
        let interp = make_interpreter();
        let v = Value::Integer(42);
        let result = interp.eval_generic_method(&v, "to_string", false);
        // to_string might not be implemented for Integer in generic method
        // This test verifies the method is called without panic
        assert!(result.is_ok() || result.is_err());
    }

    // Test eval_float_method
    #[test]
    fn test_eval_float_method_ceil() {
        let interp = make_interpreter();
        let result = interp.eval_float_method(3.2, "ceil", true).unwrap();
        assert_eq!(result, Value::Float(4.0));
    }

    #[test]
    fn test_eval_float_method_floor() {
        let interp = make_interpreter();
        let result = interp.eval_float_method(3.8, "floor", true).unwrap();
        assert_eq!(result, Value::Float(3.0));
    }

    // Test eval_integer_method
    #[test]
    fn test_eval_integer_method_abs() {
        let interp = make_interpreter();
        let result = interp.eval_integer_method(-10, "abs", &[]).unwrap();
        assert_eq!(result, Value::Integer(10));
    }

    #[test]
    fn test_eval_integer_method_positive_abs() {
        let interp = make_interpreter();
        let result = interp.eval_integer_method(10, "abs", &[]).unwrap();
        assert_eq!(result, Value::Integer(10));
    }

    // Test push on array variable
    #[test]
    fn test_eval_method_call_push_on_array() {
        let mut interp = make_interpreter();
        interp.set_variable("arr", Value::Array(Arc::from(vec![Value::Integer(1)])));

        let receiver = make_expr(ExprKind::Identifier("arr".to_string()));
        let arg = make_expr(ExprKind::Literal(crate::frontend::ast::Literal::Integer(
            2, None,
        )));
        let result = interp.eval_method_call(&receiver, "push", &[arg]).unwrap();

        // push returns nil
        assert_eq!(result, Value::Nil);

        // Verify array was modified
        let arr = interp.lookup_variable("arr").unwrap();
        if let Value::Array(values) = arr {
            assert_eq!(values.len(), 2);
            assert_eq!(values[1], Value::Integer(2));
        } else {
            panic!("Expected Array");
        }
    }

    // Test pop on array variable
    #[test]
    fn test_eval_method_call_pop_on_array() {
        let mut interp = make_interpreter();
        interp.set_variable(
            "arr",
            Value::Array(Arc::from(vec![Value::Integer(1), Value::Integer(2)])),
        );

        let receiver = make_expr(ExprKind::Identifier("arr".to_string()));
        let result = interp.eval_method_call(&receiver, "pop", &[]).unwrap();

        // pop returns the removed item
        assert_eq!(result, Value::Integer(2));

        // Verify array was modified
        let arr = interp.lookup_variable("arr").unwrap();
        if let Value::Array(values) = arr {
            assert_eq!(values.len(), 1);
        } else {
            panic!("Expected Array");
        }
    }

    // Test pop on empty array
    #[test]
    fn test_eval_method_call_pop_empty_array() {
        let mut interp = make_interpreter();
        interp.set_variable("arr", Value::Array(Arc::from(vec![])));

        let receiver = make_expr(ExprKind::Identifier("arr".to_string()));
        let result = interp.eval_method_call(&receiver, "pop", &[]).unwrap();

        // pop returns nil for empty array
        assert_eq!(result, Value::Nil);
    }

    // Test push on ObjectMut field
    #[test]
    fn test_eval_method_call_push_on_objectmut_field() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        // Create ObjectMut with an array field
        let mut obj = HashMap::new();
        obj.insert(
            "items".to_string(),
            Value::Array(Arc::from(vec![Value::Integer(1)])),
        );
        let obj_mut = Value::ObjectMut(Arc::new(Mutex::new(obj)));
        interp.set_variable("self", obj_mut);

        // Call self.items.push(2)
        let object = Box::new(make_expr(ExprKind::Identifier("self".to_string())));
        let receiver = make_expr(ExprKind::FieldAccess {
            object,
            field: "items".to_string(),
        });
        let arg = make_expr(ExprKind::Literal(crate::frontend::ast::Literal::Integer(
            2, None,
        )));
        let result = interp.eval_method_call(&receiver, "push", &[arg]).unwrap();

        // push returns nil
        assert_eq!(result, Value::Nil);
    }

    // Test Module method call
    #[test]
    fn test_eval_method_call_module() {
        use crate::frontend::ast::Literal;
        use std::cell::RefCell;
        use std::rc::Rc;

        let mut interp = make_interpreter();

        // Create a Module object with a function
        let mut module = HashMap::new();
        module.insert(
            "__type".to_string(),
            Value::from_string("Module".to_string()),
        );

        // Add a simple function that returns 42
        let body = make_expr(ExprKind::Literal(Literal::Integer(42, None)));
        let closure = Value::Closure {
            params: vec![],
            body: Arc::new(body),
            env: Rc::new(RefCell::new(HashMap::new())),
        };
        module.insert("get_answer".to_string(), closure);

        interp.set_variable("math", Value::Object(Arc::new(module)));

        // Call math.get_answer()
        let receiver = make_expr(ExprKind::Identifier("math".to_string()));
        let result = interp
            .eval_method_call(&receiver, "get_answer", &[])
            .unwrap();
        assert_eq!(result, Value::Integer(42));
    }

    // Test Module method not found
    #[test]
    fn test_eval_method_call_module_not_found() {
        let mut interp = make_interpreter();

        let mut module = HashMap::new();
        module.insert(
            "__type".to_string(),
            Value::from_string("Module".to_string()),
        );
        interp.set_variable("math", Value::Object(Arc::new(module)));

        let receiver = make_expr(ExprKind::Identifier("math".to_string()));
        let result = interp.eval_method_call(&receiver, "nonexistent", &[]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("no function named"));
    }

    // Test dispatch to actor with __actor marker
    #[test]
    fn test_dispatch_to_actor_object() {
        let mut interp = make_interpreter();

        let mut actor = HashMap::new();
        actor.insert(
            "__actor".to_string(),
            Value::from_string("Counter".to_string()),
        );
        let obj = Value::Object(Arc::new(actor));

        // Stop method returns true for any actor
        let result = interp
            .dispatch_method_call(&obj, "stop", &[], true)
            .unwrap();
        assert_eq!(result, Value::Bool(true));
    }

    // Test dispatch to class with __class marker
    #[test]
    fn test_dispatch_to_class_object() {
        let mut interp = make_interpreter();

        let mut class_obj = HashMap::new();
        class_obj.insert(
            "__class".to_string(),
            Value::from_string("MyClass".to_string()),
        );
        let obj = Value::Object(Arc::new(class_obj));

        // Should fail because no method registered
        let result = interp.dispatch_method_call(&obj, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to struct with __struct marker
    #[test]
    fn test_dispatch_to_struct_object() {
        let mut interp = make_interpreter();

        let mut struct_obj = HashMap::new();
        struct_obj.insert(
            "__struct".to_string(),
            Value::from_string("Point".to_string()),
        );
        let obj = Value::Object(Arc::new(struct_obj));

        // Should fail because no impl method registered
        let result = interp.dispatch_method_call(&obj, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to struct with __struct_type marker
    #[test]
    fn test_dispatch_to_struct_type_object() {
        let mut interp = make_interpreter();

        let mut struct_obj = HashMap::new();
        struct_obj.insert(
            "__struct_type".to_string(),
            Value::from_string("Point".to_string()),
        );
        let obj = Value::Object(Arc::new(struct_obj));

        // Should fail because no impl method registered
        let result = interp.dispatch_method_call(&obj, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to DataFrameBuilder
    #[test]
    fn test_dispatch_to_dataframe_builder() {
        let mut interp = make_interpreter();

        let mut builder = HashMap::new();
        builder.insert(
            "__type".to_string(),
            Value::from_string("DataFrameBuilder".to_string()),
        );
        let obj = Value::Object(Arc::new(builder));

        // Unknown method on DataFrameBuilder should fail
        let result = interp.dispatch_method_call(&obj, "unknown", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to ObjectMut with __actor marker
    #[test]
    fn test_dispatch_to_actor_objectmut() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        let mut actor = HashMap::new();
        actor.insert(
            "__actor".to_string(),
            Value::from_string("Counter".to_string()),
        );
        let obj = Value::ObjectMut(Arc::new(Mutex::new(actor)));

        // Stop method returns true for any actor
        let result = interp
            .dispatch_method_call(&obj, "stop", &[], true)
            .unwrap();
        assert_eq!(result, Value::Bool(true));
    }

    // Test dispatch to ObjectMut with __class marker
    #[test]
    fn test_dispatch_to_class_objectmut() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        let mut class_obj = HashMap::new();
        class_obj.insert(
            "__class".to_string(),
            Value::from_string("MyClass".to_string()),
        );
        let obj = Value::ObjectMut(Arc::new(Mutex::new(class_obj)));

        // Should fail because no method registered
        let result = interp.dispatch_method_call(&obj, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to ObjectMut with __struct marker
    #[test]
    fn test_dispatch_to_struct_objectmut() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        let mut struct_obj = HashMap::new();
        struct_obj.insert(
            "__struct".to_string(),
            Value::from_string("Point".to_string()),
        );
        let obj = Value::ObjectMut(Arc::new(Mutex::new(struct_obj)));

        // Should fail because no impl method registered
        let result = interp.dispatch_method_call(&obj, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to ObjectMut with File type
    #[test]
    fn test_dispatch_to_file_objectmut() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        let mut file_obj = HashMap::new();
        file_obj.insert("__type".to_string(), Value::from_string("File".to_string()));
        let obj = Value::ObjectMut(Arc::new(Mutex::new(file_obj)));

        // close method should work on File
        let result = interp.dispatch_method_call(&obj, "close", &[], true);
        // May succeed or fail depending on actual File implementation
        assert!(result.is_ok() || result.is_err());
    }

    // Test dispatch to ObjectMut without special markers
    #[test]
    fn test_dispatch_to_generic_objectmut() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        let mut obj_map = HashMap::new();
        obj_map.insert(
            "__type".to_string(),
            Value::from_string("GenericType".to_string()),
        );
        let obj = Value::ObjectMut(Arc::new(Mutex::new(obj_map)));

        let result = interp.dispatch_method_call(&obj, "test", &[], true);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("Unknown object type"));
    }

    // Test dispatch to Value::Struct
    #[test]
    fn test_dispatch_to_value_struct() {
        let mut interp = make_interpreter();

        let fields: HashMap<String, Value> = HashMap::new();
        let v = Value::Struct {
            name: "Point".to_string(),
            fields: Arc::new(fields),
        };

        // Should fail because no impl method registered
        let result = interp.dispatch_method_call(&v, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to Value::Class
    #[test]
    fn test_dispatch_to_value_class() {
        use std::sync::RwLock;

        let mut interp = make_interpreter();

        let v = Value::Class {
            class_name: "Person".to_string(),
            fields: Arc::new(RwLock::new(HashMap::new())),
            methods: Arc::new(HashMap::new()),
        };

        // Unknown method should fail
        let result = interp.dispatch_method_call(&v, "unknown_method", &[], true);
        assert!(result.is_err());
    }

    // Test dispatch to generic value (bool)
    #[test]
    fn test_dispatch_to_bool() {
        let mut interp = make_interpreter();
        let v = Value::Bool(true);

        // to_string should work
        let result = interp.dispatch_method_call(&v, "to_string", &[], true);
        // May not be implemented
        assert!(result.is_ok() || result.is_err());
    }

    // Test actor send with undefined identifier as message
    #[test]
    fn test_method_call_actor_send_undefined_message() {
        let mut interp = make_interpreter();

        // Create actor instance
        let mut actor = HashMap::new();
        actor.insert(
            "__actor".to_string(),
            Value::from_string("Counter".to_string()),
        );
        interp.set_variable("counter", Value::Object(Arc::new(actor)));

        // Call counter.send(Increment) where Increment is undefined
        let receiver = make_expr(ExprKind::Identifier("counter".to_string()));
        let arg = make_expr(ExprKind::Identifier("Increment".to_string()));

        // This will try to process the message
        let result = interp.eval_method_call(&receiver, "send", &[arg]);
        // Will fail because no handler for the constructed message
        assert!(result.is_err());
    }

    // Test actor ask with defined variable as message
    #[test]
    fn test_method_call_actor_ask_defined_variable() {
        let mut interp = make_interpreter();

        // Create actor instance
        let mut actor = HashMap::new();
        actor.insert(
            "__actor".to_string(),
            Value::from_string("Echo".to_string()),
        );
        interp.set_variable("echo", Value::Object(Arc::new(actor)));

        // Define message variable
        interp.set_variable("msg", Value::Integer(42));

        // Call echo.ask(msg) where msg is defined
        let receiver = make_expr(ExprKind::Identifier("echo".to_string()));
        let arg = make_expr(ExprKind::Identifier("msg".to_string()));

        // The ask method with a simple value echoes it back (default behavior)
        let result = interp.eval_method_call(&receiver, "ask", &[arg]).unwrap();
        // Echo behavior returns the message itself
        assert_eq!(result, Value::Integer(42));
    }

    // Test actor send with ObjectMut actor
    #[test]
    fn test_method_call_objectmut_actor_send() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        // Create mutable actor instance
        let mut actor = HashMap::new();
        actor.insert(
            "__actor".to_string(),
            Value::from_string("Counter".to_string()),
        );
        interp.set_variable("counter", Value::ObjectMut(Arc::new(Mutex::new(actor))));

        // Call counter.send(Increment)
        let receiver = make_expr(ExprKind::Identifier("counter".to_string()));
        let arg = make_expr(ExprKind::Identifier("Increment".to_string()));

        let result = interp.eval_method_call(&receiver, "send", &[arg]);
        // Will fail because no handler
        assert!(result.is_err());
    }

    // Test non-actor with send method (not special handling)
    #[test]
    fn test_method_call_non_actor_send() {
        let mut interp = make_interpreter();

        // Create non-actor object
        let obj = HashMap::new();
        interp.set_variable("obj", Value::Object(Arc::new(obj)));

        // Call obj.send(x) - should not use actor special handling
        let receiver = make_expr(ExprKind::Identifier("obj".to_string()));
        let arg = make_expr(ExprKind::Literal(crate::frontend::ast::Literal::Integer(
            1, None,
        )));

        let result = interp.eval_method_call(&receiver, "send", &[arg]);
        // Will fail with missing type marker
        assert!(result.is_err());
    }

    // Test method call with ObjectMut identifier (RUNTIME-099 fix)
    #[test]
    fn test_method_call_objectmut_identifier() {
        use std::sync::Mutex;

        let mut interp = make_interpreter();

        // Create ObjectMut with __type
        let mut obj = HashMap::new();
        obj.insert(
            "__type".to_string(),
            Value::from_string("SomeType".to_string()),
        );
        interp.set_variable("obj", Value::ObjectMut(Arc::new(Mutex::new(obj))));

        let receiver = make_expr(ExprKind::Identifier("obj".to_string()));
        let result = interp.eval_method_call(&receiver, "test", &[]);
        assert!(result.is_err());
    }

    // Test method call with Struct identifier (RUNTIME-ISSUE-148 fix)
    #[test]
    fn test_method_call_struct_identifier_with_impl() {
        use crate::frontend::ast::Literal;
        use std::cell::RefCell;
        use std::rc::Rc;

        let mut interp = make_interpreter();

        // Register a method for Point struct
        let body = make_expr(ExprKind::Literal(Literal::Integer(99, None)));
        let closure = Value::Closure {
            params: vec![("self".to_string(), None)],
            body: Arc::new(body),
            env: Rc::new(RefCell::new(HashMap::new())),
        };
        interp.set_variable("Point::get_x", closure);

        // Create struct value
        let fields: HashMap<String, Value> = HashMap::new();
        let struct_val = Value::Struct {
            name: "Point".to_string(),
            fields: Arc::new(fields),
        };
        interp.set_variable("p", struct_val);

        let receiver = make_expr(ExprKind::Identifier("p".to_string()));
        let result = interp.eval_method_call(&receiver, "get_x", &[]).unwrap();
        assert_eq!(result, Value::Integer(99));
    }
}