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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! # Callback Interface definitions for a `ComponentInterface`.
//!
//! This module converts callback interface definitions from UDL into structures that
//! can be added to a `ComponentInterface`. A declaration in the UDL like this:
//!
//! ```
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##"
//! # namespace example {};
//! callback interface Example {
//!   string hello();
//! };
//! # "##, "crate_name")?;
//! # Ok::<(), anyhow::Error>(())
//! ```
//!
//! Will result in a [`CallbackInterface`] member being added to the resulting
//! [`crate::ComponentInterface`]:
//!
//! ```
//! # let ci = uniffi_bindgen::interface::ComponentInterface::from_webidl(r##"
//! # namespace example {};
//! # callback interface Example {
//! #  string hello();
//! # };
//! # "##, "crate_name")?;
//! let callback = ci.get_callback_interface_definition("Example").unwrap();
//! assert_eq!(callback.name(), "Example");
//! assert_eq!(callback.methods()[0].name(), "hello");
//! # Ok::<(), anyhow::Error>(())
//! ```

use std::iter;

use heck::ToUpperCamelCase;
use uniffi_meta::Checksum;

use super::ffi::{FfiArgument, FfiCallbackFunction, FfiField, FfiFunction, FfiStruct, FfiType};
use super::object::Method;
use super::{AsType, Type, TypeIterator};

#[derive(Debug, Clone, Checksum)]
pub struct CallbackInterface {
    pub(super) name: String,
    pub(super) module_path: String,
    pub(super) methods: Vec<Method>,
    // We don't include the FFIFunc in the hash calculation, because:
    //  - it is entirely determined by the other fields,
    //    so excluding it is safe.
    //  - its `name` property includes a checksum derived from  the very
    //    hash value we're trying to calculate here, so excluding it
    //    avoids a weird circular dependency in the calculation.
    #[checksum_ignore]
    pub(super) ffi_init_callback: FfiFunction,
    #[checksum_ignore]
    pub(super) docstring: Option<String>,
}

impl CallbackInterface {
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn methods(&self) -> Vec<&Method> {
        self.methods.iter().collect()
    }

    pub fn ffi_init_callback(&self) -> &FfiFunction {
        &self.ffi_init_callback
    }

    pub(super) fn derive_ffi_funcs(&mut self) {
        self.ffi_init_callback =
            FfiFunction::callback_init(&self.module_path, &self.name, vtable_name(&self.name));
    }

    /// FfiCallbacks to define for our methods.
    pub fn ffi_callbacks(&self) -> Vec<FfiCallbackFunction> {
        ffi_callbacks(&self.name, &self.methods)
    }

    /// The VTable FFI type
    pub fn vtable(&self) -> FfiType {
        FfiType::Struct(vtable_name(&self.name))
    }

    /// the VTable struct to define.
    pub fn vtable_definition(&self) -> FfiStruct {
        vtable_struct(&self.name, &self.methods)
    }

    /// Vec of (ffi_callback, method) pairs
    pub fn vtable_methods(&self) -> Vec<(FfiCallbackFunction, &Method)> {
        self.methods
            .iter()
            .enumerate()
            .map(|(i, method)| (method_ffi_callback(&self.name, method, i), method))
            .collect()
    }

    pub fn iter_types(&self) -> TypeIterator<'_> {
        Box::new(self.methods.iter().flat_map(Method::iter_types))
    }

    pub fn docstring(&self) -> Option<&str> {
        self.docstring.as_deref()
    }

    pub fn has_async_method(&self) -> bool {
        self.methods.iter().any(Method::is_async)
    }
}

impl AsType for CallbackInterface {
    fn as_type(&self) -> Type {
        Type::CallbackInterface {
            name: self.name.clone(),
            module_path: self.module_path.clone(),
        }
    }
}

impl TryFrom<uniffi_meta::CallbackInterfaceMetadata> for CallbackInterface {
    type Error = anyhow::Error;

    fn try_from(meta: uniffi_meta::CallbackInterfaceMetadata) -> anyhow::Result<Self> {
        Ok(Self {
            name: meta.name,
            module_path: meta.module_path,
            methods: Default::default(),
            ffi_init_callback: Default::default(),
            docstring: meta.docstring.clone(),
        })
    }
}

/// [FfiCallbackFunction] functions for the methods of a callback/trait interface
pub fn ffi_callbacks(trait_name: &str, methods: &[Method]) -> Vec<FfiCallbackFunction> {
    methods
        .iter()
        .enumerate()
        .map(|(i, method)| method_ffi_callback(trait_name, method, i))
        .collect()
}

pub fn method_ffi_callback(trait_name: &str, method: &Method, index: usize) -> FfiCallbackFunction {
    if !method.is_async() {
        FfiCallbackFunction {
            name: method_ffi_callback_name(trait_name, index),
            arguments: iter::once(FfiArgument::new("uniffi_handle", FfiType::UInt64))
                .chain(method.arguments().into_iter().map(Into::into))
                .chain(iter::once(match method.return_type() {
                    Some(t) => FfiArgument::new("uniffi_out_return", FfiType::from(t).reference()),
                    None => FfiArgument::new("uniffi_out_return", FfiType::VoidPointer),
                }))
                .collect(),
            has_rust_call_status_arg: true,
            return_type: None,
        }
    } else {
        let completion_callback =
            ffi_foreign_future_complete(method.return_type().map(FfiType::from));
        FfiCallbackFunction {
            name: method_ffi_callback_name(trait_name, index),
            arguments: iter::once(FfiArgument::new("uniffi_handle", FfiType::UInt64))
                .chain(method.arguments().into_iter().map(Into::into))
                .chain([
                    FfiArgument::new(
                        "uniffi_future_callback",
                        FfiType::Callback(completion_callback.name),
                    ),
                    FfiArgument::new("uniffi_callback_data", FfiType::UInt64),
                    FfiArgument::new(
                        "uniffi_out_return",
                        FfiType::Struct("ForeignFuture".to_owned()).reference(),
                    ),
                ])
                .collect(),
            has_rust_call_status_arg: false,
            return_type: None,
        }
    }
}

/// Result struct to pass to the completion callback for async methods
pub fn foreign_future_ffi_result_struct(return_ffi_type: Option<FfiType>) -> FfiStruct {
    let return_type_name =
        FfiType::return_type_name(return_ffi_type.as_ref()).to_upper_camel_case();
    FfiStruct {
        name: format!("ForeignFutureStruct{return_type_name}"),
        fields: match return_ffi_type {
            Some(return_ffi_type) => vec![
                FfiField::new("return_value", return_ffi_type),
                FfiField::new("call_status", FfiType::RustCallStatus),
            ],
            None => vec![
                // In Rust, `return_value` is `()` -- a ZST.
                // ZSTs are not valid in `C`, but they also take up 0 space.
                // Skip the `return_value` field to make the layout correct.
                FfiField::new("call_status", FfiType::RustCallStatus),
            ],
        },
    }
}

/// Definition for callback functions to complete an async callback interface method
pub fn ffi_foreign_future_complete(return_ffi_type: Option<FfiType>) -> FfiCallbackFunction {
    let return_type_name =
        FfiType::return_type_name(return_ffi_type.as_ref()).to_upper_camel_case();
    FfiCallbackFunction {
        name: format!("ForeignFutureComplete{return_type_name}"),
        arguments: vec![
            FfiArgument::new("callback_data", FfiType::UInt64),
            FfiArgument::new(
                "result",
                FfiType::Struct(format!("ForeignFutureStruct{return_type_name}")),
            ),
        ],
        return_type: None,
        has_rust_call_status_arg: false,
    }
}

/// [FfiStruct] for a callback/trait interface VTable
///
/// This struct has a FfiCallbackFunction field for each method, plus extra fields for special
/// methods
pub fn vtable_struct(trait_name: &str, methods: &[Method]) -> FfiStruct {
    FfiStruct {
        name: vtable_name(trait_name),
        fields: methods
            .iter()
            .enumerate()
            .map(|(i, method)| {
                FfiField::new(
                    method.name(),
                    FfiType::Callback(format!("CallbackInterface{trait_name}Method{i}")),
                )
            })
            .chain([FfiField::new(
                "uniffi_free",
                FfiType::Callback("CallbackInterfaceFree".to_owned()),
            )])
            .collect(),
    }
}

pub fn method_ffi_callback_name(trait_name: &str, index: usize) -> String {
    format!("CallbackInterface{trait_name}Method{index}")
}

pub fn vtable_name(trait_name: &str) -> String {
    format!("VTableCallbackInterface{trait_name}")
}

#[cfg(test)]
mod test {
    use super::super::ComponentInterface;

    #[test]
    fn test_empty_interface() {
        const UDL: &str = r#"
            namespace test{};
            // Weird, but allowed.
            callback interface Testing {};
        "#;
        let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap();
        assert_eq!(ci.callback_interface_definitions().len(), 1);
        assert_eq!(
            ci.get_callback_interface_definition("Testing")
                .unwrap()
                .methods()
                .len(),
            0
        );
    }

    #[test]
    fn test_multiple_interfaces() {
        const UDL: &str = r#"
            namespace test{};
            callback interface One {
                void one();
            };
            callback interface Two {
                u32 two();
                u64 too();
            };
        "#;
        let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap();
        assert_eq!(ci.callback_interface_definitions().len(), 2);

        let callbacks_one = ci.get_callback_interface_definition("One").unwrap();
        assert_eq!(callbacks_one.methods().len(), 1);
        assert_eq!(callbacks_one.methods()[0].name(), "one");

        let callbacks_two = ci.get_callback_interface_definition("Two").unwrap();
        assert_eq!(callbacks_two.methods().len(), 2);
        assert_eq!(callbacks_two.methods()[0].name(), "two");
        assert_eq!(callbacks_two.methods()[1].name(), "too");
    }

    #[test]
    fn test_docstring_callback_interface() {
        const UDL: &str = r#"
            namespace test{};
            /// informative docstring
            callback interface Testing { };
        "#;
        let ci = ComponentInterface::from_webidl(UDL, "crate_name").unwrap();
        assert_eq!(
            ci.get_callback_interface_definition("Testing")
                .unwrap()
                .docstring()
                .unwrap(),
            "informative docstring"
        );
    }
}