lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
#![allow(unused_variables)]
//! Platform-specific I/O optimizations and features.
//!
//! This module provides:
//! - Windows IOCP (I/O Completion Ports) support
//! - Linux io_uring high-performance I/O
//! - macOS kqueue event notification
//! - Cross-platform abstraction layers
//! - Platform-specific file system features

use crate::diagnostics::{Error as DiagnosticError, Result};
use crate::eval::value::{
    Value, PrimitiveProcedure, PrimitiveImpl, ThreadSafeEnvironment
};
use crate::effects::Effect;
use std::sync::Arc;
use std::collections::HashMap;

#[cfg(target_os = "linux")]
use nix::sys::epoll::{Epoll, EpollCreateFlags, EpollEvent, EpollFlags, EpollTimeout};
#[cfg(target_os = "linux")]
use nix::unistd::{pipe, read, write};
#[cfg(target_os = "linux")]
use nix::fcntl::{fcntl, FcntlArg, OFlag};

#[cfg(all(target_os = "macos", feature = "advanced-io"))]
use nix::sys::event::{KEvent, EventFilter, FilterFlag, EventFlag, Kqueue};
// Note: EvFlags may not be available in this version of nix

#[cfg(windows)]
use winapi::um::ioapiset::{CreateIoCompletionPort, GetQueuedCompletionStatus};
#[cfg(windows)]
use winapi::um::winnt::{HANDLE, INVALID_HANDLE_VALUE};
#[cfg(windows)]
use winapi::um::minwinbase::OVERLAPPED;

use mio::{Events, Poll, Token, Waker};
use std::time::Duration;

/// Platform-specific I/O handle
#[derive(Debug)]
pub enum PlatformHandle {
    #[cfg(target_os = "linux")]
    Epoll {
        epoll_fd: i32,
        events: Vec<EpollEvent>,
    },
    #[cfg(all(target_os = "macos", feature = "advanced-io"))]
    Kqueue {
        kq_fd: Kqueue,
        events: Vec<KEvent>,
    },
    #[cfg(windows)]
    Iocp {
        handle: HANDLE,
        overlapped: Vec<OVERLAPPED>,
    },
    Mio {
        poll: Poll,
        events: Events,
        waker: Arc<Waker>,
    },
}

/// High-performance I/O operation types
#[derive(Debug, Clone, Copy)]
pub enum IoOperation {
    Read,
    Write,
    Accept,
    Connect,
    Send,
    Receive,
}

/// I/O completion result
#[derive(Debug)]
pub struct IoCompletion {
    pub operation: IoOperation,
    pub bytes_transferred: usize,
    pub result: std::io::Result<()>,
    pub user_data: u64,
}

/// Cross-platform high-performance I/O manager
// SAFETY: HighPerformanceIo is carefully designed to be thread-safe
// The raw pointers are only used in controlled, platform-specific contexts
unsafe impl Send for HighPerformanceIo {}
unsafe impl Sync for HighPerformanceIo {}

#[derive(Debug)]
pub struct HighPerformanceIo {
    pub handle: PlatformHandle,
    pub pending_operations: HashMap<u64, IoOperation>,
    pub next_operation_id: u64,
}

impl HighPerformanceIo {
    pub fn new() -> std::io::Result<Self> {
        let handle = Self::create_platform_handle()?;
        
        Ok(HighPerformanceIo {
            handle,
            pending_operations: HashMap::new(),
            next_operation_id: 1,
        })
    }
    
    #[cfg(target_os = "linux")]
    fn create_platform_handle() -> std::io::Result<PlatformHandle> {
        match Epoll::new(EpollCreateFlags::empty()) {
            Ok(epoll) => Ok(PlatformHandle::Epoll {
                epoll_fd: epoll.as_raw_fd(),
                events: Vec::with_capacity(32),
            }),
            Err(e) => {
                eprintln!("Failed to create epoll, falling back to mio: {}", e);
                Self::create_mio_handle()
            }
        }
    }
    
    #[cfg(all(target_os = "macos", feature = "advanced-io"))]
    fn create_platform_handle() -> std::io::Result<PlatformHandle> {
        match Kqueue::new() {
            Ok(kq) => Ok(PlatformHandle::Kqueue {
                kq_fd: kq,
                events: Vec::with_capacity(32),
            }),
            Err(e) => {
                eprintln!("Failed to create kqueue, falling back to mio: {e}");
                Self::create_mio_handle()
            }
        }
    }
    
    // Fallback for macos without advanced-io feature
    #[cfg(all(target_os = "macos", not(feature = "advanced-io")))]
    fn create_platform_handle() -> std::io::Result<PlatformHandle> {
        Self::create_mio_handle()
    }
    
    #[cfg(windows)]
    fn create_platform_handle() -> std::io::Result<PlatformHandle> {
        unsafe {
            let handle = CreateIoCompletionPort(INVALID_HANDLE_VALUE, std::ptr::null_mut(), 0, 0);
            if handle.is_null() {
                eprintln!("Failed to create IOCP, falling back to mio");
                Self::create_mio_handle()
            } else {
                Ok(PlatformHandle::Iocp {
                    handle,
                    overlapped: Vec::new(),
                })
            }
        }
    }
    
    #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
    fn create_platform_handle() -> std::io::Result<PlatformHandle> {
        Self::create_mio_handle()
    }
    
    fn create_mio_handle() -> std::io::Result<PlatformHandle> {
        let poll = Poll::new()?;
        let events = Events::with_capacity(32);
        let waker = Arc::new(Waker::new(poll.registry(), Token(0))?);
        
        Ok(PlatformHandle::Mio {
            poll,
            events,
            waker,
        })
    }
    
    pub fn submit_operation(&mut self, operation: IoOperation, user_data: Option<u64>) -> u64 {
        let op_id = user_data.unwrap_or_else(|| {
            let id = self.next_operation_id;
            self.next_operation_id += 1;
            id
        });
        
        self.pending_operations.insert(op_id, operation);
        op_id
    }
    
    pub fn poll_completions(&mut self, timeout: Option<Duration>) -> std::io::Result<Vec<IoCompletion>> {
        match &mut self.handle {
            #[cfg(target_os = "linux")]
            PlatformHandle::Epoll { epoll_fd, events } => {
                let fd = *epoll_fd;
                Self::poll_epoll_static(fd, events, timeout, &mut self.pending_operations)
            }
            #[cfg(all(target_os = "macos", feature = "advanced-io"))]
            PlatformHandle::Kqueue { kq_fd, events } => {
                Self::poll_kqueue_static(kq_fd, events, timeout, &mut self.pending_operations)
            }
            #[cfg(windows)]
            PlatformHandle::Iocp { handle, .. } => {
                let h = *handle;
                Self::poll_iocp_static(h, timeout, &mut self.pending_operations)
            }
            PlatformHandle::Mio { poll, events, .. } => {
                Self::poll_mio_static(poll, events, timeout, &mut self.pending_operations)
            }
        }
    }
    
    #[cfg(target_os = "linux")]
    fn poll_epoll_static(epoll_fd: i32, events: &mut Vec<EpollEvent>, timeout: Option<Duration>, pending_operations: &mut HashMap<u64, IoOperation>) -> std::io::Result<Vec<IoCompletion>> {
        events.clear();
        events.resize(32, EpollEvent::empty());
        
        let timeout_ms = timeout.map(|d| d.as_millis() as i32).unwrap_or(-1);
        
        // This is a simplified implementation - real io_uring integration would be more complex
        match nix::sys::epoll::epoll_wait(epoll_fd, events, timeout_ms) {
            Ok(num_events) => {
                let mut completions = Vec::new();
                for i in 0..num_events {
                    let event = &events[i];
                    let user_data = event.data();
                    
                    if let Some(operation) = pending_operations.remove(&user_data) {
                        completions.push(IoCompletion {
                            operation,
                            bytes_transferred: 0, // Would be filled by actual implementation
                            result: Ok(()),
                            user_data,
                        });
                    }
                }
                Ok(completions)
            }
            Err(e) => Err(std::io::Error::from(e)),
        }
    }
    
    #[cfg(all(target_os = "macos", feature = "advanced-io"))]
    fn poll_kqueue_static(kq_fd: &Kqueue, events: &mut Vec<KEvent>, timeout: Option<Duration>, pending_operations: &mut HashMap<u64, IoOperation>) -> std::io::Result<Vec<IoCompletion>> {
        events.clear();
        events.resize(32, KEvent::new(0, EventFilter::EVFILT_READ, EventFlag::empty(), FilterFlag::empty(), 0, 0));
        
        let timeout_spec = timeout.map(|d| {
            libc::timespec {
                tv_sec: d.as_secs() as libc::time_t,
                tv_nsec: d.subsec_nanos() as libc::c_long,
            }
        });
        
        match kq_fd.kevent(&[], events, timeout_spec) {
            Ok(num_events) => {
                let mut completions = Vec::new();
                for event in &events[0..num_events] {
                    let user_data = event.udata() as u64;
                    
                    if let Some(operation) = pending_operations.remove(&user_data) {
                        completions.push(IoCompletion {
                            operation,
                            bytes_transferred: event.data() as usize,
                            result: Ok(()),
                            user_data,
                        });
                    }
                }
                Ok(completions)
            }
            Err(e) => Err(std::io::Error::from(e)),
        }
    }
    
    #[cfg(windows)]
    fn poll_iocp_static(handle: HANDLE, timeout: Option<Duration>, pending_operations: &mut HashMap<u64, IoOperation>) -> std::io::Result<Vec<IoCompletion>> {
        use std::ptr;
        use winapi::shared::minwindef::{DWORD, ULONG_PTR};
        
        let timeout_ms = timeout.map(|d| d.as_millis() as DWORD).unwrap_or(winapi::um::winbase::INFINITE);
        
        let mut bytes_transferred: DWORD = 0;
        let mut completion_key: ULONG_PTR = 0;
        let mut overlapped: *mut OVERLAPPED = ptr::null_mut();
        
        unsafe {
            let result = GetQueuedCompletionStatus(
                handle,
                &mut bytes_transferred,
                &mut completion_key,
                &mut overlapped,
                timeout_ms,
            );
            
            if result != 0 {
                let user_data = completion_key as u64;
                if let Some(operation) = pending_operations.remove(&user_data) {
                    Ok(vec![IoCompletion {
                        operation,
                        bytes_transferred: bytes_transferred as usize,
                        result: Ok(()),
                        user_data,
                    }])
                } else {
                    Ok(vec![])
                }
            } else {
                let error = std::io::Error::last_os_error();
                if error.kind() == std::io::ErrorKind::TimedOut {
                    Ok(vec![])
                } else {
                    Err(error)
                }
            }
        }
    }
    
    fn poll_mio_static(poll: &mut Poll, events: &mut Events, timeout: Option<Duration>, pending_operations: &mut HashMap<u64, IoOperation>) -> std::io::Result<Vec<IoCompletion>> {
        events.clear();
        poll.poll(events, timeout)?;
        
        let mut completions = Vec::new();
        for event in events.iter() {
            let token = event.token().0 as u64;
            if let Some(operation) = pending_operations.remove(&token) {
                completions.push(IoCompletion {
                    operation,
                    bytes_transferred: 0, // MIO doesn't provide this directly
                    result: Ok(()),
                    user_data: token,
                });
            }
        }
        
        Ok(completions)
    }
}

/// Creates platform-specific I/O operation bindings.
pub fn create_platform_io_bindings(env: &Arc<ThreadSafeEnvironment>) {
    // High-performance I/O operations
    bind_high_performance_operations(env);
    
    // Platform detection and capabilities
    bind_platform_detection(env);
    
    // Platform-specific file system features
    bind_platform_fs_features(env);
    
    // Event notification systems
    bind_event_systems(env);
}

// ============= HIGH-PERFORMANCE I/O OPERATIONS =============

fn bind_high_performance_operations(env: &Arc<ThreadSafeEnvironment>) {
    // create-high-performance-io
    env.define("create-high-performance-io".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "create-high-performance-io".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_create_high_performance_io),
        effects: vec![Effect::IO],
    })));
    
    // submit-io-operation
    env.define("submit-io-operation".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "submit-io-operation".to_string(),
        arity_min: 2,
        arity_max: Some(4),
        implementation: PrimitiveImpl::RustFn(primitive_submit_io_operation),
        effects: vec![Effect::IO],
    })));
    
    // poll-io-completions
    env.define("poll-io-completions".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "poll-io-completions".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_poll_io_completions),
        effects: vec![Effect::IO],
    })));
    
    // batch-io-operations
    env.define("batch-io-operations".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "batch-io-operations".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_batch_io_operations),
        effects: vec![Effect::IO],
    })));
}

fn bind_platform_detection(env: &Arc<ThreadSafeEnvironment>) {
    // platform-name
    env.define("platform-name".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "platform-name".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_platform_name),
        effects: vec![Effect::Pure],
    })));
    
    // platform-capabilities
    env.define("platform-capabilities".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "platform-capabilities".to_string(),
        arity_min: 0,
        arity_max: Some(0),
        implementation: PrimitiveImpl::RustFn(primitive_platform_capabilities),
        effects: vec![Effect::Pure],
    })));
    
    // io-backend-available?
    env.define("io-backend-available?".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "io-backend-available?".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_io_backend_available_p),
        effects: vec![Effect::Pure],
    })));
}

fn bind_platform_fs_features(env: &Arc<ThreadSafeEnvironment>) {
    // platform-file-attributes
    env.define("platform-file-attributes".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "platform-file-attributes".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_platform_file_attributes),
        effects: vec![Effect::IO],
    })));
    
    // set-platform-file-attributes
    env.define("set-platform-file-attributes".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "set-platform-file-attributes".to_string(),
        arity_min: 2,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_set_platform_file_attributes),
        effects: vec![Effect::IO],
    })));
    
    // file-system-info
    env.define("file-system-info".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "file-system-info".to_string(),
        arity_min: 1,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_file_system_info),
        effects: vec![Effect::IO],
    })));
}

fn bind_event_systems(env: &Arc<ThreadSafeEnvironment>) {
    // create-event-watcher
    env.define("create-event-watcher".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "create-event-watcher".to_string(),
        arity_min: 0,
        arity_max: Some(1),
        implementation: PrimitiveImpl::RustFn(primitive_create_event_watcher),
        effects: vec![Effect::IO],
    })));
    
    // watch-file-events
    env.define("watch-file-events".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "watch-file-events".to_string(),
        arity_min: 2,
        arity_max: Some(3),
        implementation: PrimitiveImpl::RustFn(primitive_watch_file_events),
        effects: vec![Effect::IO],
    })));
    
    // poll-events
    env.define("poll-events".to_string(), Value::Primitive(Arc::new(PrimitiveProcedure {
        name: "poll-events".to_string(),
        arity_min: 1,
        arity_max: Some(2),
        implementation: PrimitiveImpl::RustFn(primitive_poll_events),
        effects: vec![Effect::IO],
    })));
}

// ============= IMPLEMENTATION FUNCTIONS =============

// === High-performance I/O Operations ===

pub fn primitive_create_high_performance_io(args: &[Value]) -> Result<Value> {
    if args.len() > 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("create-high-performance-io expects 0 or 1 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let backend = if args.len() == 1 {
        extract_string(&args[0], "create-high-performance-io")?
    } else {
        "auto".to_string()
    };
    
    match HighPerformanceIo::new() {
        Ok(hp_io) => Ok(Value::opaque(Box::new(hp_io))),
        Err(e) => Err(Box::new(DiagnosticError::runtime_error(
            format!("Cannot create high-performance I/O: {e}"),
            None,
        ))),
    }
}

pub fn primitive_submit_io_operation(args: &[Value]) -> Result<Value> {
    if args.len() < 2 || args.len() > 4 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("submit-io-operation expects 2 to 4 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let operation_str = extract_string(&args[1], "submit-io-operation")?;
    let operation = match operation_str.as_str() {
        "read" => IoOperation::Read,
        "write" => IoOperation::Write,
        "accept" => IoOperation::Accept,
        "connect" => IoOperation::Connect,
        "send" => IoOperation::Send,
        "receive" => IoOperation::Receive,
        _ => {
            return Err(Box::new(DiagnosticError::runtime_error(
                format!("Unknown I/O operation: {operation_str}"),
                None,
            )));
        }
    };
    
    let user_data = if args.len() > 2 {
        Some(extract_integer(&args[2], "submit-io-operation")? as u64)
    } else {
        None
    };
    
    // Extract high-performance I/O from opaque value
    match &args[0] {
        Value::Opaque(opaque_data) => {
            if let Some(hp_io) = opaque_data.downcast_ref::<HighPerformanceIo>() {
                // This is a simplified implementation - we'd need mutable access
                // In reality, we'd need a different approach for mutability
                Ok(Value::integer(1)) // Placeholder operation ID
            } else {
                Err(Box::new(DiagnosticError::runtime_error(
                    "submit-io-operation requires high-performance I/O handle".to_string(),
                    None,
                )))
            }
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "submit-io-operation requires high-performance I/O handle".to_string(),
            None,
        ))),
    }
}

pub fn primitive_poll_io_completions(args: &[Value]) -> Result<Value> {
    if args.is_empty() || args.len() > 2 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("poll-io-completions expects 1 or 2 arguments, got {}", args.len()),
            None,
        )));
    }
    
    let timeout_ms = if args.len() > 1 {
        Some(extract_integer(&args[1], "poll-io-completions")? as u64)
    } else {
        None
    };
    
    // Extract high-performance I/O from opaque value
    match &args[0] {
        Value::Opaque(opaque_data) => {
            if let Some(_hp_io) = opaque_data.downcast_ref::<HighPerformanceIo>() {
                // This is a simplified implementation
                // In reality, we'd poll for completions and return them
                Ok(Value::Nil) // Placeholder - would return list of completions
            } else {
                Err(Box::new(DiagnosticError::runtime_error(
                    "poll-io-completions requires high-performance I/O handle".to_string(),
                    None,
                )))
            }
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            "poll-io-completions requires high-performance I/O handle".to_string(),
            None,
        ))),
    }
}

pub fn primitive_batch_io_operations(_args: &[Value]) -> Result<Value> {
    // TODO: Implement batched I/O operations
    Err(Box::new(DiagnosticError::runtime_error(
        "batch-io-operations not yet implemented".to_string(),
        None,
    )))
}

// === Platform Detection ===

pub fn primitive_platform_name(_args: &[Value]) -> Result<Value> {
    let platform = if cfg!(target_os = "linux") {
        "linux"
    } else if cfg!(target_os = "macos") {
        "macos"
    } else if cfg!(target_os = "windows") {
        "windows"
    } else if cfg!(target_os = "freebsd") {
        "freebsd"
    } else if cfg!(target_os = "openbsd") {
        "openbsd"
    } else if cfg!(target_os = "netbsd") {
        "netbsd"
    } else {
        "unknown"
    };
    
    Ok(Value::string(platform.to_string()))
}

pub fn primitive_platform_capabilities(_args: &[Value]) -> Result<Value> {
    #[allow(clippy::mutable_key_type)]
    let mut capabilities = HashMap::new();
    
    // I/O backends
    capabilities.insert(
        Value::Symbol(crate::utils::intern_symbol("io-backends")),
        {
            let mut backends = Vec::new();
            backends.push(Value::string("mio".to_string()));
            
            #[cfg(target_os = "linux")]
            {
                backends.push(Value::string("epoll".to_string()));
                backends.push(Value::string("io_uring".to_string()));
            }
            
            #[cfg(target_os = "macos")]
            {
                backends.push(Value::string("kqueue".to_string()));
            }
            
            #[cfg(windows)]
            {
                backends.push(Value::string("iocp".to_string()));
            }
            
            list_to_value(backends)
        }
    );
    
    // Async support
    capabilities.insert(
        Value::Symbol(crate::utils::intern_symbol("async-support")),
        Value::boolean(cfg!(feature = "async"))
    );
    
    // Compression support
    capabilities.insert(
        Value::Symbol(crate::utils::intern_symbol("compression-support")),
        Value::boolean(cfg!(feature = "compression"))
    );
    
    // TLS support
    capabilities.insert(
        Value::Symbol(crate::utils::intern_symbol("tls-support")),
        Value::boolean(cfg!(feature = "tls"))
    );
    
    // Platform features
    capabilities.insert(
        Value::Symbol(crate::utils::intern_symbol("unix-sockets")),
        Value::boolean(cfg!(unix))
    );
    
    capabilities.insert(
        Value::Symbol(crate::utils::intern_symbol("memory-mapping")),
        Value::boolean(true) // memmap2 is always available
    );
    
    Ok(Value::Hashtable(Arc::new(std::sync::RwLock::new(capabilities))))
}

pub fn primitive_io_backend_available_p(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("io-backend-available? expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let backend = extract_string(&args[0], "io-backend-available?")?;
    
    let available = match backend.as_str() {
        "mio" => true,
        "epoll" => cfg!(target_os = "linux"),
        "io_uring" => cfg!(target_os = "linux"), // Would check for actual io_uring support
        "kqueue" => cfg!(target_os = "macos"),
        "iocp" => cfg!(windows),
        _ => false,
    };
    
    Ok(Value::boolean(available))
}

// === Platform File System Features ===

pub fn primitive_platform_file_attributes(args: &[Value]) -> Result<Value> {
    if args.len() != 1 {
        return Err(Box::new(DiagnosticError::runtime_error(
            format!("platform-file-attributes expects 1 argument, got {}", args.len()),
            None,
        )));
    }
    
    let path = extract_string(&args[0], "platform-file-attributes")?;
    
    match std::fs::metadata(&path) {
        Ok(metadata) => {
            #[allow(clippy::mutable_key_type)]
            let mut attributes = HashMap::new();
            
            #[cfg(unix)]
            {
                use std::os::unix::fs::MetadataExt;
#[cfg(target_os = "macos")]
                
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("unix-mode")),
                    Value::integer(metadata.mode() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("unix-uid")),
                    Value::integer(metadata.uid() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("unix-gid")),
                    Value::integer(metadata.gid() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("unix-inode")),
                    Value::integer(metadata.ino() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("unix-device")),
                    Value::integer(metadata.dev() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("unix-nlink")),
                    Value::integer(metadata.nlink() as i64)
                );
            }
            
            #[cfg(windows)]
            {
                use std::os::windows::fs::MetadataExt;
                
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("windows-attributes")),
                    Value::integer(metadata.file_attributes() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("windows-creation-time")),
                    Value::integer(metadata.creation_time() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("windows-last-access-time")),
                    Value::integer(metadata.last_access_time() as i64)
                );
                attributes.insert(
                    Value::Symbol(crate::utils::intern_symbol("windows-last-write-time")),
                    Value::integer(metadata.last_write_time() as i64)
                );
            }
            
            Ok(Value::Hashtable(Arc::new(std::sync::RwLock::new(attributes))))
        }
        Err(e) => Err(Box::new(DiagnosticError::runtime_error(
            format!("Cannot get attributes for '{path}': {e}"),
            None,
        ))),
    }
}

pub fn primitive_set_platform_file_attributes(_args: &[Value]) -> Result<Value> {
    // TODO: Implement platform-specific attribute setting
    Err(Box::new(DiagnosticError::runtime_error(
        "set-platform-file-attributes not yet implemented".to_string(),
        None,
    )))
}

pub fn primitive_file_system_info(_args: &[Value]) -> Result<Value> {
    // TODO: Implement file system information retrieval
    Err(Box::new(DiagnosticError::runtime_error(
        "file-system-info not yet implemented".to_string(),
        None,
    )))
}

// === Event Systems ===

pub fn primitive_create_event_watcher(_args: &[Value]) -> Result<Value> {
    // TODO: Implement event watcher creation
    Err(Box::new(DiagnosticError::runtime_error(
        "create-event-watcher not yet implemented".to_string(),
        None,
    )))
}

pub fn primitive_watch_file_events(_args: &[Value]) -> Result<Value> {
    // TODO: Implement file event watching
    Err(Box::new(DiagnosticError::runtime_error(
        "watch-file-events not yet implemented".to_string(),
        None,
    )))
}

pub fn primitive_poll_events(_args: &[Value]) -> Result<Value> {
    // TODO: Implement event polling
    Err(Box::new(DiagnosticError::runtime_error(
        "poll-events not yet implemented".to_string(),
        None,
    )))
}

// ============= HELPER FUNCTIONS =============

/// Extracts a string from a Value.
fn extract_string(value: &Value, operation: &str) -> Result<String> {
    match value {
        Value::Literal(crate::ast::Literal::String(s)) => Ok(s.clone()),
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires string arguments"),
            None,
        ))),
    }
}

/// Extracts an integer from a Value.
fn extract_integer(value: &Value, operation: &str) -> Result<i64> {
    match value {
        Value::Literal(lit) => {
            if let Some(i) = lit.to_i64() {
                Ok(i)
            } else {
                Err(Box::new(DiagnosticError::runtime_error(
                    format!("{operation} requires integer arguments"),
                    None,
                )))
            }
        }
        _ => Err(Box::new(DiagnosticError::runtime_error(
            format!("{operation} requires integer arguments"),
            None,
        ))),
    }
}

/// Converts a vector of values to a Scheme list.
fn list_to_value(values: Vec<Value>) -> Value {
    values.into_iter().rev().fold(Value::Nil, |acc, val| {
        Value::Pair(Arc::new(val), Arc::new(acc))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_platform_detection() {
        let result = primitive_platform_name(&[]);
        assert!(result.is_ok());
        
        if let Ok(Value::Literal(crate::ast::Literal::String(platform))) = result {
            assert!(!platform.is_empty());
            println!("Detected platform: {}", platform);
        } else {
            panic!("Expected string result");
        }
    }
    
    #[test]
    fn test_platform_capabilities() {
        let result = primitive_platform_capabilities(&[]);
        assert!(result.is_ok());
        
        if let Ok(Value::Hashtable(capabilities)) = result {
            let cap_map = capabilities.read().unwrap();
            assert!(cap_map.contains_key(&Value::Symbol(crate::utils::intern_symbol("io-backends"))));
            assert!(cap_map.contains_key(&Value::Symbol(crate::utils::intern_symbol("async-support"))));
        } else {
            panic!("Expected hashtable result");
        }
    }
    
    #[test]
    fn test_io_backend_availability() {
        let args = vec![Value::string("mio".to_string())];
        let result = primitive_io_backend_available_p(&args);
        assert!(result.is_ok());
        
        if let Ok(Value::Literal(crate::ast::Literal::Boolean(available))) = result {
            assert!(available); // MIO should always be available
        } else {
            panic!("Expected boolean result");
        }
    }
    
    #[test]
    fn test_high_performance_io_creation() {
        let result = primitive_create_high_performance_io(&[]);
        assert!(result.is_ok());
        
        // Just verify we can create the handle without panicking
        if let Ok(Value::Opaque(_)) = result {
            // Success
        } else {
            panic!("Expected opaque handle result");
        }
    }
}