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
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! count_args {
    () => { 0 };
    ($name:ident) => { 1 };
    ($first:ident, $($rest:ident),*) => {
        1 + count_args!($($rest),*)
    }
}

macro_rules! invoke_fn_impl {
    ($(
        fn $FnName:ident($($Arg:tt: $T:ident),*) -> $ErrName:ident;
    )+) => {
        $(
            /// An invocation error that contains the function name, a mutable reference to the
            /// runtime, passed arguments, and the output type. This allows the caller to retry
            /// the function invocation using the `Retriable` trait.
            pub struct $ErrName<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection> {
                msg: String,
                runtime: std::rc::Rc<core::cell::RefCell<Runtime>>,
                function_name: &'s str,
                $($Arg: $T,)*
                output: core::marker::PhantomData<Output>,
            }

            impl<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection> core::fmt::Debug for $ErrName<'s, $($T,)* Output> {
                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    write!(f, "{}", &self.msg)
                }
            }

            impl<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection> core::fmt::Display for $ErrName<'s, $($T,)* Output> {
                fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                    write!(f, "{}", &self.msg)
                }
            }

            impl<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection> std::error::Error for $ErrName<'s, $($T,)* Output> {
                fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
                    None
                }
            }

            impl<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection> $ErrName<'s, $($T,)* Output> {
                /// Constructs a new invocation error.
                #[allow(clippy::too_many_arguments)]
                pub fn new(err_msg: String, runtime: std::rc::Rc<core::cell::RefCell<Runtime>>, function_name: &'s str, $($Arg: $T),*) -> Self {
                    Self {
                        msg: err_msg,
                        runtime,
                        function_name,
                        $($Arg,)*
                        output: core::marker::PhantomData,
                    }
                }
            }

            impl<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection> $crate::RetryResultExt for core::result::Result<Output, $ErrName<'s, $($T,)* Output>> {
                type Output = Output;

                fn retry(self) -> Self {
                    match self {
                        Ok(output) => Ok(output),
                        Err(err) => {
                            eprintln!("{}", err.msg);
                            while !err.runtime.borrow_mut().update() {
                                // Wait until there has been an update that might fix the error
                            }
                            $crate::Runtime::$FnName(&err.runtime, err.function_name, $(err.$Arg,)*)
                        }
                    }
                }

                fn wait(mut self) -> Self::Output {
                    loop {
                        if let Ok(output) = self {
                            return output;
                        } else {
                            self = self.retry();
                        }
                    }
                }
            }

            impl Runtime {
                /// Invokes the method `method_name` with arguments `args`, in the library compiled
                /// based on the manifest at `manifest_path`.
                ///
                /// If an error occurs when invoking the method, an error message is logged. The
                /// runtime continues looping until the cause of the error has been resolved.
                #[allow(clippy::too_many_arguments, unused_assignments)]
                pub fn $FnName<'s, $($T: ArgumentReflection,)* Output: ReturnTypeReflection>(
                    runtime: &std::rc::Rc<core::cell::RefCell<Runtime>>,
                    function_name: &'s str,
                    $($Arg: $T,)*
                ) -> core::result::Result<Output, $ErrName<'s, $($T,)* Output>> {
                    let runtime_ref = runtime.borrow();
                    match runtime_ref
                        .get_function_definition(function_name)
                        .ok_or_else(|| format!("Failed to obtain function '{}'", function_name))
                        .and_then(|function_info| {
                            // Validate function signature
                            let num_args = $crate::count_args!($($T),*);

                            let arg_types = function_info.prototype.signature.arg_types();
                            if arg_types.len() != num_args {
                                return Err(format!(
                                    "Invalid number of arguments. Expected: {}. Found: {}.",
                                    arg_types.len(),
                                    num_args,
                                ));
                            }

                            #[allow(unused_mut, unused_variables)]
                            let mut idx = 0;
                            $(
                                crate::reflection::equals_argument_type(&runtime_ref, &arg_types[idx], &$Arg)
                                    .map_err(|(expected, found)| {
                                        format!(
                                            "Invalid argument type at index {}. Expected: {}. Found: {}.",
                                            idx,
                                            expected,
                                            found,
                                        )
                                    })?;
                                idx += 1;
                            )*

                            if let Some(return_type) = function_info.prototype.signature.return_type() {
                                crate::reflection::equals_return_type::<Output>(return_type)
                            } else if <() as ReturnTypeReflection>::type_guid() != Output::type_guid() {
                                Err((<() as ReturnTypeReflection>::type_name(), Output::type_name()))
                            } else {
                                Ok(())
                            }.map_err(|(expected, found)| {
                                format!(
                                    "Invalid return type. Expected: {}. Found: {}",
                                    expected,
                                    found,
                                )
                            })?;

                            Ok(function_info)
                        }) {
                        Ok(function_info) => {
                            let function: fn($($T::Marshalled),*) -> Output::Marshalled = unsafe {
                                core::mem::transmute(function_info.fn_ptr)
                            };
                            let result = function($($Arg.marshal()),*);

                            // Marshall the result
                            return Ok(result.marshal_value(runtime.clone()))
                        }
                        Err(e) => Err($ErrName::new(e, runtime.clone(), function_name, $($Arg),*))
                    }
                }
            }
        )+
    }
}

/// Invokes a runtime function and returns a [`Result`] that implements the [`RetryResultExt`]
/// trait.
///
/// The first argument `invoke_fn` receives is a `Runtime` and the second argument is a function
/// string. This must be a `&str`.
///
/// Additional parameters passed to `invoke_fn` are the arguments of the function in the order
/// given.
#[macro_export]
macro_rules! invoke_fn {
    ($Runtime:expr, $FnName:expr) => {
        $crate::Runtime::invoke_fn0(&$Runtime, $FnName)
    };
    ($Runtime:expr, $FnName:expr, $A:expr) => {
        $crate::Runtime::invoke_fn1(&$Runtime, $FnName, $A)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr) => {
        $crate::Runtime::invoke_fn2(&$Runtime, $FnName, $A, $B)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr) => {
        $crate::Runtime::invoke_fn3(&$Runtime, $FnName, $A, $B, $C)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr) => {
        $crate::Runtime::invoke_fn4(&$Runtime, $FnName, $A, $B, $C, $D)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr) => {
        $crate::Runtime::invoke_fn5(&$Runtime, $FnName, $A, $B, $C, $D, $E)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr) => {
        $crate::Runtime::invoke_fn6(&$Runtime, $FnName, $A, $B, $C, $D, $E, $F)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr) => {
        $crate::Runtime::invoke_fn7(&$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr) => {
        $crate::Runtime::invoke_fn8(&$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr) => {
        $crate::Runtime::invoke_fn9(&$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr) => {
        $crate::Runtime::invoke_fn10(&$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J)
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr, $K:expr) => {
        $crate::Runtime::invoke_fn11(
            &$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K,
        )
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr, $K:expr, $L:expr) => {
        $crate::Runtime::invoke_fn12(
            &$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L,
        )
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr, $K:expr, $L:expr, $M:expr) => {
        $crate::Runtime::invoke_fn13(
            &$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M,
        )
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr, $K:expr, $L:expr, $M:expr, $N:expr) => {
        $crate::Runtime::invoke_fn14(
            &$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N,
        )
    };
    ($Runtime:expr, $FnName:expr, $A:expr, $B:expr, $C:expr, $D:expr, $E:expr, $F:expr, $G:expr, $H:expr, $I:expr, $J:expr, $K:expr, $L:expr, $M:expr, $N:expr, $O:expr) => {
        $crate::Runtime::invoke_fn15(
            &$Runtime, $FnName, $A, $B, $C, $D, $E, $F, $G, $H, $I, $J, $K, $L, $M, $N, $O,
        )
    };
}