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
#![allow(unused_variables)]
//! Callback function system for FFI operations.
//!
//! This module provides support for registering Lambdust functions as callbacks
//! that can be called from C code, as well as managing the lifecycle and
//! safety of these callback functions.

use std::collections::HashMap;
use std::ffi::c_void;
use std::fmt;
use std::ptr;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, SystemTime};

use crate::eval::{Value, Environment};
use crate::ast::Literal;
use crate::diagnostics::Error;
use crate::ffi::c_types::{CType, TypeMarshaller, ConversionError};

/// Errors that can occur during callback operations
#[derive(Debug, Clone)]
pub enum CallbackError {
    /// Callback not found
    NotFound(String),
    /// Invalid callback signature
    InvalidSignature {
        callback: String,
        reason: String,
    },
    /// Callback execution failed
    ExecutionFailed {
        callback: String,
        error: String,
    },
    /// Type conversion error
    ConversionError(ConversionError),
    /// Callback already registered
    AlreadyRegistered(String),
    /// Callback lifetime expired
    Expired(String),
    /// Stack overflow protection
    StackOverflow,
}

impl fmt::Display for CallbackError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CallbackError::NotFound(name) => {
                write!(f, "Callback not found: {name}")
            }
            CallbackError::InvalidSignature { callback, reason } => {
                write!(f, "Invalid callback signature for '{callback}': {reason}")
            }
            CallbackError::ExecutionFailed { callback, error } => {
                write!(f, "Callback '{callback}' execution failed: {error}")
            }
            CallbackError::ConversionError(e) => {
                write!(f, "Callback type conversion error: {e}")
            }
            CallbackError::AlreadyRegistered(name) => {
                write!(f, "Callback '{name}' is already registered")
            }
            CallbackError::Expired(name) => {
                write!(f, "Callback '{name}' has expired")
            }
            CallbackError::StackOverflow => {
                write!(f, "Callback stack overflow protection triggered")
            }
        }
    }
}

impl std::error::Error for CallbackError {}

impl From<ConversionError> for CallbackError {
    fn from(e: ConversionError) -> Self {
        CallbackError::ConversionError(e)
    }
}

impl From<CallbackError> for Error {
    fn from(callback_error: CallbackError) -> Self {
        Error::runtime_error(callback_error.to_string(), None)
    }
}

/// Callback function signature
#[derive(Debug, Clone)]
pub struct CallbackSignature {
    /// Function name
    pub name: String,
    /// Parameter types
    pub parameters: Vec<CType>,
    /// Return type
    pub return_type: CType,
    /// Whether the callback is variadic
    pub variadic: bool,
    /// Calling convention (default: C)
    pub calling_convention: CallingConvention,
}

/// Calling conventions for callbacks
#[derive(Debug, Clone, PartialEq)]
#[derive(Default)]
pub enum CallingConvention {
    /// C calling convention (default)
    #[default]
    C,
    /// Standard call (Windows)
    Stdcall,
    /// Fast call
    Fastcall,
    /// System V ABI (Unix)
    SystemV,
}


/// A registered callback function
#[derive(Debug)]
pub struct CallbackFunction {
    /// Callback signature
    pub signature: CallbackSignature,
    /// The Lambdust function to call
    pub function: Value,
    /// Environment for the function
    pub environment: Arc<Mutex<Environment>>,
    /// When this callback was registered
    pub registered_at: SystemTime,
    /// Expiration time (if any)
    pub expires_at: Option<SystemTime>,
    /// Number of times this callback has been invoked
    pub call_count: Arc<Mutex<u64>>,
    /// Generated C function pointer
    pub c_function_ptr: *const c_void,
    /// Whether this callback is currently active
    pub active: Arc<Mutex<bool>>,
}

impl CallbackFunction {
    /// Check if this callback is expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            SystemTime::now() > expires_at
        } else {
            false
        }
    }

    /// Check if this callback is active
    pub fn is_active(&self) -> bool {
        *self.active.lock().unwrap()
    }

    /// Get the call count
    pub fn call_count(&self) -> u64 {
        *self.call_count.lock().unwrap()
    }

    /// Increment call count
    pub fn increment_call_count(&self) {
        let mut count = self.call_count.lock().unwrap();
        *count += 1;
    }
}

unsafe impl Send for CallbackFunction {}
unsafe impl Sync for CallbackFunction {}

/// Callback registry for managing registered callbacks
#[derive(Debug)]
pub struct CallbackRegistry {
    /// Registered callbacks
    callbacks: RwLock<HashMap<String, Arc<CallbackFunction>>>,
    /// Type marshaller for conversions
    marshaller: Arc<Mutex<TypeMarshaller>>,
    /// Stack depth protection
    stack_depth: Arc<Mutex<usize>>,
    /// Maximum stack depth
    max_stack_depth: usize,
    /// Statistics
    stats: RwLock<CallbackStats>,
}

/// Callback usage statistics
#[derive(Debug, Default, Clone)]
pub struct CallbackStats {
    /// Total number of registered callbacks
    pub total_registered: usize,
    /// Currently active callbacks
    pub currently_active: usize,
    /// Total callback invocations
    pub total_invocations: u64,
    /// Successful invocations
    pub successful_invocations: u64,
    /// Failed invocations
    pub failed_invocations: u64,
    /// Stack overflows prevented
    pub stack_overflows_prevented: u64,
}

impl Default for CallbackRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl CallbackRegistry {
    /// Create a new callback registry
    pub fn new() -> Self {
        Self {
            callbacks: RwLock::new(HashMap::new()),
            marshaller: Arc::new(Mutex::new(TypeMarshaller::new())),
            stack_depth: Arc::new(Mutex::new(0)),
            max_stack_depth: 32, // Reasonable default
            stats: RwLock::new(CallbackStats::default()),
        }
    }

    /// Set maximum stack depth
    pub fn set_max_stack_depth(&mut self, depth: usize) {
        self.max_stack_depth = depth;
    }

    /// Register a callback function
    pub fn register_callback(
        &self,
        signature: CallbackSignature,
        function: Value,
        environment: Arc<Mutex<Environment>>,
        expires_after: Option<Duration>,
    ) -> std::result::Result<*const c_void, CallbackError> {
        // Check if already registered
        {
            let callbacks = self.callbacks.read().unwrap();
            if callbacks.contains_key(&signature.name) {
                return Err(CallbackError::AlreadyRegistered(signature.name.clone()));
            }
        }

        // Calculate expiration time
        let expires_at = expires_after.map(|duration| SystemTime::now() + duration);

        // Generate C function pointer (this is a simplified implementation)
        let c_function_ptr = self.generate_c_function_ptr(&signature)?;

        // Create callback function
        let callback = Arc::new(CallbackFunction {
            signature: signature.clone(),
            function,
            environment,
            registered_at: SystemTime::now(),
            expires_at,
            call_count: Arc::new(Mutex::new(0)),
            c_function_ptr,
            active: Arc::new(Mutex::new(true)),
        });

        // Register the callback
        {
            let mut callbacks = self.callbacks.write().unwrap();
            callbacks.insert(signature.name.clone(), callback);
        }

        // Update statistics
        {
            let mut stats = self.stats.write().unwrap();
            stats.total_registered += 1;
            stats.currently_active = self.callbacks.read().unwrap().len();
        }

        Ok(c_function_ptr)
    }

    /// Unregister a callback
    pub fn unregister_callback(&self, name: &str) -> std::result::Result<(), CallbackError> {
        let callback = {
            let mut callbacks = self.callbacks.write().unwrap();
            callbacks.remove(name)
        };

        if let Some(callback) = callback {
            // Mark as inactive
            {
                let mut active = callback.active.lock().unwrap();
                *active = false;
            }

            // Update statistics
            {
                let mut stats = self.stats.write().unwrap();
                stats.currently_active = self.callbacks.read().unwrap().len();
            }

            Ok(())
        } else {
            Err(CallbackError::NotFound(name.to_string()))
        }
    }

    /// Get a callback by name
    pub fn get_callback(&self, name: &str) -> Option<Arc<CallbackFunction>> {
        let callbacks = self.callbacks.read().unwrap();
        callbacks.get(name).cloned()
    }

    /// List all registered callbacks
    pub fn list_callbacks(&self) -> Vec<String> {
        let callbacks = self.callbacks.read().unwrap();
        callbacks.keys().cloned().collect()
    }

    /// Clean up expired callbacks
    pub fn cleanup_expired(&self) -> usize {
        let expired_names: Vec<String> = {
            let callbacks = self.callbacks.read().unwrap();
            callbacks
                .iter()
                .filter(|(_, callback)| callback.is_expired())
                .map(|(name, _)| name.clone())
                .collect()
        };

        let count = expired_names.len();
        for name in expired_names {
            let _ = self.unregister_callback(&name);
        }

        count
    }

    /// Get callback statistics
    pub fn stats(&self) -> CallbackStats {
        self.stats.read().unwrap().clone()
    }

    /// Generate a C function pointer for the callback
    fn generate_c_function_ptr(&self, _signature: &CallbackSignature) -> std::result::Result<*const c_void, CallbackError> {
        // This is a simplified implementation
        // In a real implementation, this would generate platform-specific
        // assembly stubs or use libffi to create callable function pointers

        // For now, we'll create a dummy function pointer
        // In practice, this would involve:
        // 1. Allocating executable memory
        // 2. Generating assembly code that calls back into Rust
        // 3. Setting up proper stack frame and calling convention
        
        let dummy_ptr = self as *const CallbackRegistry as *const c_void;
        Ok(dummy_ptr)
    }

    /// Execute a callback (called from generated C code)
    ///
    /// # Safety
    /// 
    /// The caller must ensure that:
    /// - `args` is a valid pointer to an array of `arg_count` valid `*const c_void` pointers
    /// - Each pointer in the `args` array points to valid data of the expected type for the callback
    /// - The memory pointed to by `args` and its elements remains valid for the duration of this call
    /// - The callback is thread-safe if called from multiple threads
    pub unsafe fn execute_callback(
        &self,
        name: &str,
        args: *const *const c_void,
        arg_count: usize,
    ) -> std::result::Result<*const c_void, CallbackError> {
        // Stack overflow protection
        {
            let mut depth = self.stack_depth.lock().unwrap();
            if *depth >= self.max_stack_depth {
                let mut stats = self.stats.write().unwrap();
                stats.stack_overflows_prevented += 1;
                return Err(CallbackError::StackOverflow);
            }
            *depth += 1;
        }

        // Ensure stack is decremented on exit
        let _stack_guard = StackGuard::new(Arc::clone(&self.stack_depth));

        // Get the callback
        let callback = self.get_callback(name)
            .ok_or_else(|| CallbackError::NotFound(name.to_string()))?;

        // Check if expired
        if callback.is_expired() {
            return Err(CallbackError::Expired(name.to_string()));
        }

        // Check if active
        if !callback.is_active() {
            return Err(CallbackError::NotFound(name.to_string()));
        }

        // Update statistics
        {
            let mut stats = self.stats.write().unwrap();
            stats.total_invocations += 1;
        }

        // Convert arguments
        let mut scheme_args = Vec::new();
        let marshaller = self.marshaller.lock().unwrap();

        for i in 0..arg_count.min(callback.signature.parameters.len()) {
            let arg_ptr = unsafe { *args.add(i) };
            let param_type = &callback.signature.parameters[i];
            
            // This is simplified - in practice, we'd need to properly
            // convert from C types to Lambdust values
            let value = match param_type {
                CType::CInt => {
                    let int_val = unsafe { *(arg_ptr as *const libc::c_int) };
                    Value::Literal(Literal::integer(int_val as i64))
                }
                CType::CString => {
                    let c_str_ptr = unsafe { *(arg_ptr as *const *const libc::c_char) };
                    if c_str_ptr.is_null() {
                        Value::Literal(Literal::String("".to_string()))
                    } else {
                        let c_str = unsafe { std::ffi::CStr::from_ptr(c_str_ptr) };
                        let rust_str = c_str.to_str()
                            .map_err(|e| CallbackError::ConversionError(
                                ConversionError::StringConversion(e.to_string())
                            ))?;
                        Value::Literal(Literal::String(rust_str.to_string()))
                    }
                }
                _ => Value::Nil, // Simplified
            };
            
            scheme_args.push(value);
        }

        // Execute the callback function
        let result = {
            let env = callback.environment.lock().unwrap();
            // This is simplified - in practice, we'd need to call the evaluator
            // with the callback function and arguments
            match &callback.function {
                Value::Procedure(_) => {
                    // Would call evaluator here
                    Ok(Value::Literal(Literal::integer(0))) // Placeholder
                }
                _ => Err(CallbackError::ExecutionFailed {
                    callback: name.to_string(),
                    error: "Not a function".to_string(),
                })
            }
        };

        match result {
            Ok(return_value) => {
                // Increment call count
                callback.increment_call_count();

                // Update success statistics
                {
                    let mut stats = self.stats.write().unwrap();
                    stats.successful_invocations += 1;
                }

                // Convert return value back to C
                // This is simplified - would need proper conversion
                Ok(ptr::null())
            }
            Err(e) => {
                // Update failure statistics
                {
                    let mut stats = self.stats.write().unwrap();
                    stats.failed_invocations += 1;
                }

                Err(e)
            }
        }
    }
}

/// Stack guard for automatic cleanup
struct StackGuard {
    stack_depth: Arc<Mutex<usize>>,
}

impl StackGuard {
    fn new(stack_depth: Arc<Mutex<usize>>) -> Self {
        Self { stack_depth }
    }
}

impl Drop for StackGuard {
    fn drop(&mut self) {
        let mut depth = self.stack_depth.lock().unwrap();
        if *depth > 0 {
            *depth -= 1;
        }
    }
}

/// Async callback support
#[cfg(feature = "async")]
pub mod async_callbacks {
    use super::*;
    use std::future::Future;
    use std::pin::Pin;
    use tokio::sync::oneshot;

    /// Async callback execution result
    pub type AsyncCallbackResult = Pin<Box<dyn Future<Output = std::result::Result<Value, CallbackError>> + Send>>;

    /// Async callback registry
    #[derive(Debug)]
    pub struct AsyncCallbackRegistry {
        /// Base registry
        base: CallbackRegistry,
        /// Pending async operations
        pending: Arc<Mutex<HashMap<String, oneshot::Sender<Value>>>>,
    }

    impl AsyncCallbackRegistry {
        /// Create a new async callback registry
        pub fn new() -> Self {
            Self {
                base: CallbackRegistry::new(),
                pending: Arc::new(Mutex::new(HashMap::new())),
            }
        }

        /// Register an async callback
        pub async fn register_async_callback(
            &self,
            signature: CallbackSignature,
            function: Value,
            environment: Arc<Mutex<Environment>>,
            expires_after: Option<Duration>,
        ) -> std::result::Result<*const c_void, CallbackError> {
            // For now, delegate to sync implementation
            self.base.register_callback(signature, function, environment, expires_after)
        }

        /// Execute an async callback
        pub async fn execute_async_callback(
            &self,
            _name: &str,
            _args: Vec<Value>,
        ) -> std::result::Result<Value, CallbackError> {
            // This would implement proper async callback execution
            // For now, return a placeholder
            Ok(Value::Nil)
        }
    }

    impl Default for AsyncCallbackRegistry {
        fn default() -> Self {
            Self::new()
        }
    }
}

lazy_static::lazy_static! {
    /// Global callback registry
    pub static ref GLOBAL_CALLBACK_REGISTRY: CallbackRegistry = CallbackRegistry::new();
}

/// Convenience functions for global callback registry
pub fn register_callback(
    signature: CallbackSignature,
    function: Value,
    environment: Arc<Mutex<Environment>>,
    expires_after: Option<Duration>,
) -> std::result::Result<*const c_void, CallbackError> {
    GLOBAL_CALLBACK_REGISTRY.register_callback(signature, function, environment, expires_after)
}

pub fn unregister_callback(name: &str) -> std::result::Result<(), CallbackError> {
    GLOBAL_CALLBACK_REGISTRY.unregister_callback(name)
}

pub fn cleanup_expired_callbacks() -> usize {
    GLOBAL_CALLBACK_REGISTRY.cleanup_expired()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::eval::Environment;

    #[test]
    fn test_callback_registry_creation() {
        let registry = CallbackRegistry::new();
        let stats = registry.stats();
        assert_eq!(stats.currently_active, 0);
        assert_eq!(stats.total_registered, 0);
    }

    #[test]
    fn test_callback_signature() {
        let sig = CallbackSignature {
            name: "test_callback".to_string(),
            parameters: vec![CType::CInt, CType::CString],
            return_type: CType::CInt,
            variadic: false,
            calling_convention: CallingConvention::C,
        };

        assert_eq!(sig.name, "test_callback");
        assert_eq!(sig.parameters.len(), 2);
        assert!(!sig.variadic);
    }

    #[test]
    fn test_callback_function_creation() {
        let signature = CallbackSignature {
            name: "test".to_string(),
            parameters: vec![],
            return_type: CType::Void,
            variadic: false,
            calling_convention: CallingConvention::C,
        };

        let function = Value::Literal(Literal::Number(42.0));
        let environment = Arc::new(Mutex::new(Environment::new(None, 0)));

        let callback = CallbackFunction {
            signature,
            function,
            environment,
            registered_at: SystemTime::now(),
            expires_at: None,
            call_count: Arc::new(Mutex::new(0)),
            c_function_ptr: ptr::null(),
            active: Arc::new(Mutex::new(true)),
        };

        assert!(callback.is_active());
        assert!(!callback.is_expired());
        assert_eq!(callback.call_count(), 0);
    }

    #[test]
    fn test_cleanup_expired() {
        let registry = CallbackRegistry::new();
        
        // This test would need more setup to create expired callbacks
        let cleaned = registry.cleanup_expired();
        assert_eq!(cleaned, 0); // No expired callbacks initially
    }
}