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

mod malloc_buf;

pub use encode::{Encode, EncodeArguments, Encoding};
pub use message::{Message, MessageArguments, MessageError};

pub use message::send_message as __send_message;
pub use message::send_super_message as __send_super_message;

#[macro_use]
mod macros;

pub mod runtime;
pub mod declare;
pub mod rc;
mod encode;
mod message;

//#[cfg(test)]
//mod test_utils;



#[macro_export]
macro_rules!objc_block {
    (move | $ ( $ arg_ident: ident: $ arg_ty: ty), * | $ (: $ return_ty: ty) ? $ body: block) => {
        {
            #[repr(C)]
            struct BlockDescriptor {
                reserved: std::os::raw::c_ulong,
                size: std::os::raw::c_ulong,
                copy_helper: extern "C" fn(*mut std::os::raw::c_void, *const std::os::raw::c_void),
                dispose_helper: extern "C" fn(*mut std::os::raw::c_void),
            }
            
            static DESCRIPTOR: BlockDescriptor = BlockDescriptor {
                reserved: 0,
                size: std::mem::size_of::<BlockLiteral>() as std::os::raw::c_ulong,
                copy_helper,
                dispose_helper,
            };
            
            #[allow(unused_unsafe)]
            extern "C" fn copy_helper(dst: *mut std::os::raw::c_void, src: *const std::os::raw::c_void) {
                unsafe {
                    std::ptr::write(
                        &mut (*(dst as *mut BlockLiteral)).inner as *mut _,
                        (&*(src as *const BlockLiteral)).inner.clone()
                    );
                }
            }
            
            #[allow(unused_unsafe)]
            extern "C" fn dispose_helper(src: *mut std::os::raw::c_void) {
                unsafe {
                    std::ptr::drop_in_place(src as *mut BlockLiteral);
                }
            }
            
            #[allow(unused_unsafe)]
            extern "C" fn invoke(literal: *mut BlockLiteral, $ ( $ arg_ident: $ arg_ty), *) $ ( -> $ return_ty) ? {
                let literal = unsafe {&mut *literal};
                literal.inner.lock().unwrap()( $ ( $ arg_ident), *)
            }
            
            #[repr(C)]
            struct BlockLiteral {
                isa: *const std::os::raw::c_void,
                flags: std::os::raw::c_int,
                reserved: std::os::raw::c_int,
                invoke: extern "C" fn(*mut BlockLiteral, $ ( $ arg_ty), *) $ ( -> $ return_ty) ?,
                descriptor: *const BlockDescriptor,
                inner: ::std::sync::Arc<::std::sync::Mutex<dyn Fn( $ ( $ arg_ty), *) $ ( -> $ return_ty) ? >>,
            }
            
            #[allow(unused_unsafe)]
            BlockLiteral {
                isa: unsafe {_NSConcreteStackBlock.as_ptr() as *const std::os::raw::c_void},
                flags: 1 << 25,
                reserved: 0,
                invoke,
                descriptor: &DESCRIPTOR,
                inner: ::std::sync::Arc::new(::std::sync::Mutex::new(move | $ ( $ arg_ident: $ arg_ty), * | {
                    $ body
                }))
            }
        }
    }
}



#[macro_export]
macro_rules!objc_block_invoke {
    ( $ inp: expr, invoke ( $ ( ($ arg_ident: expr): $ arg_ty: ty), *) $ ( -> $ return_ty: ty) ?) => {
        {
            #[repr(C)]
            struct BlockLiteral {
                isa: *const std::os::raw::c_void,
                flags: std::os::raw::c_int,
                reserved: std::os::raw::c_int,
                invoke: extern "C" fn(*mut BlockLiteral, $ ( $ arg_ty), *) $ ( -> $ return_ty) ?,
            }
            
            let block: &mut BlockLiteral = &mut *( $ inp as *mut _);
            (block.invoke)(block, $ ( $ arg_ident), *)
        }
    }
}