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

/// Constructs a Julia function declaration in the form of a stringified Julia expression.
///
/// # Syntax
/// decl_jl! {
///     pub extern "Julia" fn <libname> :: <func> ( <arg: Type> * ) -> OutType;
/// }
///
/// # Example
/// ```
/// let square = decl_jl! {
///     pub extern "Julia" fn libsquare :: square(x: Float64) -> Float64;
/// };
/// "function square(x::Float64, )
///     ccall((:square, \"libsquare\"), Float64, (Float64,), x)
/// end"
/// ```
#[macro_export]
macro_rules! decl_jl {
    {
        pub extern "Julia" fn $lib:ident :: $name:ident ( $( $arg:ident : $type:ty ),* ) -> $out:ty;
    } => {
        concat!(
                "function ", stringify!($name), "(", $( stringify!($arg), "::", stringify!($type), ", ", )* ")\n",
                    "    ccall((:", stringify!($name), ", \"", stringify!($lib), "\"), ", stringify!($out),
                    ", (", $( stringify!($type), ",", )* "), ",  $( stringify!($arg), ",", )* ")\n",
                "end"
            )
    };
    {
        $(
            pub extern "Julia" fn $lib:ident :: $name:ident ( $( $arg:ident : $type:ty ),* ) -> $out:ty;
        )*
    } => {
        vec![
            $(
                decl_jl! { pub extern "Julia" fn $lib:ident :: $name:ident ( $( $arg:ident : $type:ty ),* ) -> $out:ty; }
            ),*
        ]
    }
}

/// Turns Rust functions into Julia extern functions.
///
/// # Syntax
/// ```
/// extern_jl! {
///     extern "Julia" <libname> :: <StructName> {
///         pub fn <func> ( <arg: Type> ) -> OutType {
///             <body>
///         }
///     }
/// }
///
/// # Example
/// ```
/// extern_jl! {
///     extern "Julia" libsquare :: Square {
///         pub fn square(x: Float64) -> Float64 {
///             x * x
///         }
///     }
/// }
///
/// fn main() {
///     let mut jl = Julia::new().unwrap()
///
///     let sqr = Square::new();
///     sqr.decl(&mut jl);
///
///     jl.eval_string("assert(square(5.0) == 25.0)");
/// }
/// ```
#[macro_export]
macro_rules! extern_jl {
    {
        extern "Julia" $lib:ident :: $struct:ident {
            $(
                pub fn $name:ident ( $( $arg:ident : $type:ty ),* ) -> $out:ty $body:block
            )*
        }
    } => {
        $(
            #[no_mangle]
            pub extern "C" fn $name ( $( $arg : $type ),* ) -> $out $body
        )*

        struct $struct {
            $(
                pub $name: &'static str,
            )*
        }

        impl $struct {
            pub fn new() -> $struct {
                $struct {
                    $(
                        $name: decl_jl! {
                            pub extern "Julia" fn $lib :: $name ( $( $arg : $type ),* ) -> $out;
                        },
                    )*
                }
            }

            pub fn decl(self, jl: &mut $crate::api::Julia) -> $crate::error::Result<$crate::api::Value> {
                let mut decl = String::new();

                $(
                    decl.push_str(self.$name);
                )*
                jl.load(&mut decl.as_bytes(), Some(concat!( "jl-decl-", stringify!($lib), ".jl" )))
            }
        }
    }
}