avmnif-rs 0.4.1

Safe NIF toolkit for AtomVM written in Rust
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
//! Port collection macros for AtomVM
//! 
//! Provides safe Rust wrappers around AtomVM's port driver API
//!
//! # Design Philosophy
//!
//! All operations work with any AtomTableOps implementation through dependency injection.
//! No global state, no hardcoded dependencies.

use crate::term::{Term, NifError, TermValue};
use crate::context::{Context, GlobalContext, ContextExt, PlatformData, PortBuilder};
use crate::atom::{AtomTableOps, AtomTable};
use core::ffi::{c_void, c_char, c_int};

// Suppress warnings for unused items since this is a library
#[allow(unused_imports)]
use alloc::boxed::Box;

// AtomVM port types (reuse from context module)
pub type ErlNifEnv = c_void;
pub type ERL_NIF_TERM = u64;

/// Port message type
pub type Message = c_void;

/// Port result enum
#[repr(C)]
pub enum PortResult {
    Continue = 0,
    Terminate = 1,
}

/// Port driver function type signatures
pub type PortInitFn = fn(&mut GlobalContext);
pub type PortDestroyFn = fn(&mut GlobalContext);  
pub type PortCreateFn = fn(&GlobalContext, Term) -> *mut Context;
pub type PortHandlerFn = fn(&mut Context, &Message) -> PortResult;

/// C-compatible function types for FFI boundary
type CPortCreateFn = extern "C" fn(*const GlobalContext, ERL_NIF_TERM) -> *mut Context;
type CPortHandlerFn = extern "C" fn(*mut Context, *const Message) -> PortResult;

/// Port driver registration structure
#[repr(C)]
pub struct AtomVMPortDriver {
    pub name: *const c_char,
    pub init: Option<PortInitFn>,
    pub destroy: Option<PortDestroyFn>,
    pub create_port: CPortCreateFn,
    pub message_handler: CPortHandlerFn,
}

unsafe impl Sync for AtomVMPortDriver {}

// AtomVM Port API FFI declarations
extern "C" {
    /// Send a reply to an Erlang process from port context
    pub fn port_send_reply(
        ctx: *mut Context,
        pid: ERL_NIF_TERM,
        reference: ERL_NIF_TERM,
        reply: ERL_NIF_TERM,
    );
    
    /// Send an async message to an Erlang process from any context (ISR-safe)
    pub fn port_send_message_from_task(
        global: *mut GlobalContext,
        pid: u32,
        message: ERL_NIF_TERM,
    );
    
    /// Parse a generic port message into components
    pub fn parse_port_message(
        message: *const Message,
        pid: *mut ERL_NIF_TERM,
        reference: *mut ERL_NIF_TERM,
        command: *mut ERL_NIF_TERM,
    ) -> c_int;
}

/// Register a port collection with AtomVM
/// 
/// # Usage
/// ```rust,ignore
/// use avmnif_rs::port_collection;
/// 
/// port_collection!(
///     my_port,
///     init = my_port_init,
///     destroy = my_port_destroy,
///     create_port = my_port_create,
///     handler = my_port_handler
/// );
/// ```
#[macro_export]
macro_rules! port_collection {
    (
        $port_name:ident,
        init = $init_fn:ident,
        destroy = $destroy_fn:ident,
        create_port = $create_port_fn:ident,
        handler = $handler_fn:ident
    ) => {
        paste::paste! {
            // Wrapper functions that convert between C and Rust types
            extern "C" fn [<$create_port_fn _wrapper>](
                global: *const $crate::context::GlobalContext,
                opts: $crate::port::ERL_NIF_TERM
            ) -> *mut $crate::context::Context {
                let global_ref = unsafe { &*global };
                let opts_term = $crate::term::Term::from_raw(opts.try_into().unwrap());
                $create_port_fn(global_ref, opts_term)
            }
            
            extern "C" fn [<$handler_fn _wrapper>](
                ctx: *mut $crate::context::Context,
                message: *const $crate::port::Message
            ) -> $crate::port::PortResult {
                let ctx_ref = unsafe { &mut *ctx };
                let message_ref = unsafe { &*message };
                $handler_fn(ctx_ref, message_ref)
            }
            
            // Create the port driver structure using wrapper functions
            static [<$port_name:upper _PORT_DRIVER>]: $crate::port::AtomVMPortDriver = $crate::port::AtomVMPortDriver {
                name: concat!(stringify!($port_name), "\0").as_ptr() as *const core::ffi::c_char,
                init: Some($init_fn),
                destroy: Some($destroy_fn),
                create_port: [<$create_port_fn _wrapper>],
                message_handler: [<$handler_fn _wrapper>],
            };
            
            // Export the port driver registration function
            #[no_mangle]
            pub extern "C" fn [<$port_name _port_driver_init>]() -> *const $crate::port::AtomVMPortDriver {
                &[<$port_name:upper _PORT_DRIVER>]
            }
            
            // Export individual functions for debugging/testing
            #[no_mangle]
            pub extern "C" fn [<$port_name _init>](global: *mut $crate::context::GlobalContext) {
                let global_ref = unsafe { &mut *global };
                $init_fn(global_ref);
            }
            
            #[no_mangle]
            pub extern "C" fn [<$port_name _destroy>](global: *mut $crate::context::GlobalContext) {
                let global_ref = unsafe { &mut *global };
                $destroy_fn(global_ref);
            }
            
            #[no_mangle]
            pub extern "C" fn [<$port_name _create_port>](
                global: *const $crate::context::GlobalContext,
                opts: $crate::port::ERL_NIF_TERM
            ) -> *mut $crate::context::Context {
                [<$create_port_fn _wrapper>](global, opts)
            }
            
            #[no_mangle]
            pub extern "C" fn [<$port_name _message_handler>](
                ctx: *mut $crate::context::Context,
                message: *const $crate::port::Message
            ) -> $crate::port::PortResult {
                [<$handler_fn _wrapper>](ctx, message)
            }
        }
    };
    
    // Version without init/destroy functions
    (
        $port_name:ident,
        create_port = $create_port_fn:ident,
        handler = $handler_fn:ident
    ) => {
        paste::paste! {
            // Wrapper functions that convert between C and Rust types
            extern "C" fn [<$create_port_fn _wrapper>](
                global: *const $crate::context::GlobalContext,
                opts: $crate::port::ERL_NIF_TERM
            ) -> *mut $crate::context::Context {
                let global_ref = unsafe { &*global };
                let opts_term = $crate::term::Term::from_raw(opts.try_into().unwrap());
                $create_port_fn(global_ref, opts_term)
            }
            
            extern "C" fn [<$handler_fn _wrapper>](
                ctx: *mut $crate::context::Context,
                message: *const $crate::port::Message
            ) -> $crate::port::PortResult {
                let ctx_ref = unsafe { &mut *ctx };
                let message_ref = unsafe { &*message };
                $handler_fn(ctx_ref, message_ref)
            }
            
            static [<$port_name:upper _PORT_DRIVER>]: $crate::port::AtomVMPortDriver = $crate::port::AtomVMPortDriver {
                name: concat!(stringify!($port_name), "\0").as_ptr() as *const core::ffi::c_char,
                init: None,
                destroy: None,
                create_port: [<$create_port_fn _wrapper>],
                message_handler: [<$handler_fn _wrapper>],
            };
            
            #[no_mangle]
            pub extern "C" fn [<$port_name _port_driver_init>]() -> *const $crate::port::AtomVMPortDriver {
                &[<$port_name:upper _PORT_DRIVER>]
            }
            
            #[no_mangle]
            pub extern "C" fn [<$port_name _create_port>](
                global: *const $crate::context::GlobalContext,
                opts: $crate::port::ERL_NIF_TERM
            ) -> *mut $crate::context::Context {
                [<$create_port_fn _wrapper>](global, opts)
            }
            
            #[no_mangle]
            pub extern "C" fn [<$port_name _message_handler>](
                ctx: *mut $crate::context::Context,
                message: *const $crate::port::Message
            ) -> $crate::port::PortResult {
                [<$handler_fn _wrapper>](ctx, message)
            }
        }
    };
}

/// Helper functions for port message handling

/// Parse a generic port message into its components
pub fn parse_gen_message(message: &Message) -> Result<(Term, Term, Term), NifError> {
    let mut pid: u64 = 0;
    let mut reference: u64 = 0;
    let mut command: u64 = 0;
    
    let result = unsafe {
        parse_port_message(
            message as *const _ as *const c_void,
            &mut pid,
            &mut reference,
            &mut command,
        )
    };
    
    if result != 0 {
        Ok((
            Term::from_raw(pid.try_into().unwrap()),
            Term::from_raw(reference.try_into().unwrap()),
            Term::from_raw(command.try_into().unwrap()),
        ))
    } else {
        Err(NifError::BadArg)
    }
}

/// Send a reply to an Erlang process
pub fn send_reply(ctx: &Context, pid: Term, reference: Term, reply: Term) {
    unsafe {
        port_send_reply(
            ctx as *const _ as *mut Context,
            pid.raw().try_into().unwrap(),
            reference.raw().try_into().unwrap(),
            reply.raw().try_into().unwrap(),
        );
    }
}

/// Send an async message to an Erlang process (ISR-safe)
pub fn send_async_message(pid: u32, message: Term) {
    unsafe {
        port_send_message_from_task(
            crate::context::get_global_context(),
            pid,
            message.raw().try_into().unwrap(),
        );
    }
}

/// Trait for port data types to implement cleanup and message handling
pub trait PortData: PlatformData {
    /// Called when the port receives a message
    fn handle_message(&mut self, message: &Message) -> PortResult {
        let _ = message; // Suppress unused warning
        PortResult::Continue
    }
    
    /// Get the owner PID for this port (if any)
    fn get_owner_pid(&self) -> Option<u32> {
        None
    }
    
    /// Set the owner PID for this port
    fn set_owner_pid(&mut self, _pid: u32) {}
    
    /// Check if the port is active
    fn is_active(&self) -> bool {
        true
    }
    
    /// Activate/deactivate the port
    fn set_active(&mut self, _active: bool) {}
}

/// Generic port data wrapper with standard functionality
#[repr(C)]
pub struct GenericPortData<T: PortData> {
    pub inner: T,
    pub owner_pid: u32,
    pub active: bool,
}

impl<T: PortData> GenericPortData<T> {
    pub fn new(inner: T) -> Self {
        Self {
            inner,
            owner_pid: 0,
            active: false,
        }
    }
    
    pub fn set_owner(&mut self, pid: u32) {
        self.owner_pid = pid;
        self.active = true;
        self.inner.set_owner_pid(pid);
    }
    
    pub fn deactivate(&mut self) {
        self.active = false;
        self.inner.set_active(false);
        self.inner.cleanup();
    }
    
    pub fn get_inner(&self) -> &T {
        &self.inner
    }
    
    pub fn get_inner_mut(&mut self) -> &mut T {
        &mut self.inner
    }
}

impl<T: PortData> PlatformData for GenericPortData<T> {
    fn cleanup(&mut self) {
        self.deactivate();
    }
}

impl<T: PortData> PortData for GenericPortData<T> {
    fn handle_message(&mut self, message: &Message) -> PortResult {
        if self.active {
            self.inner.handle_message(message)
        } else {
            PortResult::Terminate
        }
    }
    
    fn get_owner_pid(&self) -> Option<u32> {
        if self.owner_pid != 0 {
            Some(self.owner_pid)
        } else {
            None
        }
    }
    
    fn set_owner_pid(&mut self, pid: u32) {
        self.owner_pid = pid;
    }
    
    fn is_active(&self) -> bool {
        self.active
    }
    
    fn set_active(&mut self, active: bool) {
        self.active = active;
    }
}

/// Macro for creating simple port data structures
#[macro_export]
macro_rules! port_data {
    (
        $name:ident {
            $(
                $field:ident: $field_type:ty
            ),* $(,)?
        }
    ) => {
        #[repr(C)]
        pub struct $name {
            $(
                pub $field: $field_type,
            )*
        }
        
        impl $crate::context::PlatformData for $name {}
        impl $crate::port::PortData for $name {}
        
        impl $name {
            pub fn new() -> Self {
                Self {
                    $(
                        $field: Default::default(),
                    )*
                }
            }
        }
        
        impl Default for $name {
            fn default() -> Self {
                Self::new()
            }
        }
    };
}

/// Error handling for port operations
#[derive(Debug, Clone, Copy)]
pub enum PortError {
    /// Invalid message format
    InvalidMessage,
    /// Port not active
    PortInactive,
    /// Hardware error
    HardwareError,
    /// Out of memory
    OutOfMemory,
    /// Generic error
    Generic,
}

impl From<PortError> for PortResult {
    fn from(_error: PortError) -> Self {
        PortResult::Terminate
    }
}

/// Result type for port operations
pub type PortOpResult<T> = Result<T, PortError>;

/// Utility functions for common port operations

/// Extract PID as u32 from Term (for use in async messaging)
pub fn term_to_pid(term: Term) -> PortOpResult<u32> {
    // This would need to be implemented based on actual Term structure
    // For now, return a placeholder
    Ok(term.raw() as u32) // This is obviously wrong, but demonstrates the interface
}

/// Create a standard error reply using any atom table
pub fn create_error_reply<T: AtomTableOps>(reason: &str, table: &T) -> Result<Term, NifError> {
    // Create an error tuple: {error, Reason}
    let error_atom = table.ensure_atom_str("error").map_err(|_| NifError::BadArg)?;
    let reason_atom = table.ensure_atom_str(reason).map_err(|_| NifError::BadArg)?;
    
    // This would use the actual term construction API
    // For now, return a placeholder that shows the pattern
    let _ = (error_atom, reason_atom);
    Ok(Term::from_raw(0)) // Obviously wrong, but demonstrates interface
}

/// Create a standard success reply using any atom table
pub fn create_ok_reply<T: AtomTableOps>(data: Term, table: &T) -> Result<Term, NifError> {
    // Create an ok tuple: {ok, Data}
    let ok_atom = table.ensure_atom_str("ok").map_err(|_| NifError::BadArg)?;
    
    // This would use the actual term construction API
    let _ = (ok_atom, data);
    Ok(Term::from_raw(0)) // Obviously wrong, but demonstrates interface
}

/// Generic standard message handler template
pub fn handle_standard_message<T: PortData>(
    ctx: &mut Context,
    message: &Message,
) -> PortResult {
    // Get the atom table from the global context
    let table = AtomTable::from_global();
    
    let port_data = unsafe {
        let data_ptr = ctx.get_platform_data_as::<GenericPortData<T>>();
        if data_ptr.is_null() {
            return PortResult::Terminate;
        }
        &mut *data_ptr
    };
    
    if let Ok((pid, reference, command)) = parse_gen_message(message) {
        // Convert command to TermValue for pattern matching
        let command_value = match command.to_value() {
            Ok(val) => val,
            Err(_) => {
                if let Ok(reply) = create_error_reply("invalid_command", &table) {
                    send_reply(ctx, pid, reference, reply);
                }
                return PortResult::Continue;
            }
        };
        
        // Handle standard commands using TermValue pattern matching with the table
        if command_value.is_atom_str("start", &table) {
            if let Ok(pid_u32) = term_to_pid(pid) {
                port_data.set_owner(pid_u32);
                if let Ok(reply) = create_ok_reply(Term::from_raw(0), &table) {
                    send_reply(ctx, pid, reference, reply);
                }
                PortResult::Continue
            } else {
                if let Ok(reply) = create_error_reply("invalid_pid", &table) {
                    send_reply(ctx, pid, reference, reply);
                }
                PortResult::Continue
            }
        } else if command_value.is_atom_str("stop", &table) {
            port_data.deactivate();
            if let Ok(reply) = create_ok_reply(Term::from_raw(0), &table) {
                send_reply(ctx, pid, reference, reply);
            }
            PortResult::Terminate
        } else if command_value.is_atom_str("status", &table) {
            let _status = if port_data.is_active() {
                "active"
            } else {
                "inactive"
            };
            if let Ok(reply) = create_ok_reply(Term::from_raw(0), &table) {
                send_reply(ctx, pid, reference, reply);
            }
            PortResult::Continue
        } else {
            // Delegate to the port data's message handler
            port_data.handle_message(message)
        }
    } else {
        PortResult::Terminate
    }
}

/// Create a port with automatic platform data setup
pub fn create_port_with_data<T: PortData>(
    global: &GlobalContext,
    data: T,
) -> *mut Context {
    let wrapped_data = GenericPortData::new(data);
    PortBuilder::new(wrapped_data).build(global)
}

/// Create a port with data and user term
pub fn create_port_with_data_and_term<T: PortData>(
    global: &GlobalContext,
    data: T,
    user_term: Term,
) -> *mut Context {
    let wrapped_data = GenericPortData::new(data);
    PortBuilder::new(wrapped_data).build_with_user_term(global, user_term)
}

/// Safely execute a function with port data
pub fn with_port_data<T: PortData, R, F>(ctx: &Context, f: F) -> Option<R>
where
    F: FnOnce(&GenericPortData<T>) -> R,
{
    unsafe {
        let data_ptr = ctx.get_platform_data_as::<GenericPortData<T>>();
        if data_ptr.is_null() {
            None
        } else {
            Some(f(&*data_ptr))
        }
    }
}

/// Safely execute a function with mutable port data
pub fn with_port_data_mut<T: PortData, R, F>(ctx: &mut Context, f: F) -> Option<R>
where
    F: FnOnce(&mut GenericPortData<T>) -> R,
{
    unsafe {
        let data_ptr = ctx.get_platform_data_as::<GenericPortData<T>>();
        if data_ptr.is_null() {
            None
        } else {
            Some(f(&mut *data_ptr))
        }
    }
}

/// High-level port creation macro that handles common patterns
#[macro_export]
macro_rules! simple_port {
    (
        $port_name:ident,
        data = $data_type:ty,
        init_data = $init_expr:expr
    ) => {
        fn [<$port_name _create>](global: &$crate::context::GlobalContext, opts: $crate::term::Term) -> *mut $crate::context::Context {
            let _ = opts; // suppress unused warning
            let data: $data_type = $init_expr;
            $crate::port::create_port_with_data(global, data)
        }
        
        fn [<$port_name _handler>](ctx: &mut $crate::context::Context, message: &$crate::port::Message) -> $crate::port::PortResult {
            $crate::port::handle_standard_message::<$data_type>(ctx, message)
        }
        
        $crate::port_collection!(
            $port_name,
            create_port = [<$port_name _create>],
            handler = [<$port_name _handler>]
        );
    };
    
    (
        $port_name:ident,
        data = $data_type:ty,
        init_data = $init_expr:expr,
        init = $init_fn:ident,
        destroy = $destroy_fn:ident
    ) => {
        fn [<$port_name _create>](global: &$crate::context::GlobalContext, opts: $crate::term::Term) -> *mut $crate::context::Context {
            let _ = opts; // suppress unused warning
            let data: $data_type = $init_expr;
            $crate::port::create_port_with_data(global, data)
        }
        
        fn [<$port_name _handler>](ctx: &mut $crate::context::Context, message: &$crate::port::Message) -> $crate::port::PortResult {
            $crate::port::handle_standard_message::<$data_type>(ctx, message)
        }
        
        $crate::port_collection!(
            $port_name,
            init = $init_fn,
            destroy = $destroy_fn,
            create_port = [<$port_name _create>],
            handler = [<$port_name _handler>]
        );
    };
}