| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
//! Functions to calculate names for FFI symbols |
| 6 |
//! |
| 7 |
//! All of these functions input a `namespace` parameter which is: |
| 8 |
//! - The UDL namespace for UDL-based generation |
| 9 |
//! - The "module path" of the item for proc-macro based generation. Right now this is actually just the |
| 10 |
//! crate name, but we eventually hope to make this the full module path. |
| 11 |
//! |
| 12 |
//! This could cause collisions in the case where you combine UDL and proc-macro generation and you |
| 13 |
//! set the UDL namespace to the name of another crate. This seems so pathological that it's not |
| 14 |
//! worth the code complexity to prevent it. |
| 15 |
|
| 16 |
/// FFI symbol name for a top-level function |
| 17 |
pub fn fn_symbol_name(namespace: &str, name: &str) -> String { |
| 18 |
let namespace = namespace.replace("::", "__"); |
| 19 |
let name = name.to_ascii_lowercase(); |
| 20 |
format!("uniffi_{namespace}_fn_func_{name}") |
| 21 |
} |
| 22 |
|
| 23 |
/// FFI symbol name for an object constructor |
| 24 |
pub fn constructor_symbol_name(namespace: &str, object_name: &str, name: &str) -> String { |
| 25 |
let namespace = namespace.replace("::", "__"); |
| 26 |
let object_name = object_name.to_ascii_lowercase(); |
| 27 |
let name = name.to_ascii_lowercase(); |
| 28 |
format!("uniffi_{namespace}_fn_constructor_{object_name}_{name}") |
| 29 |
} |
| 30 |
|
| 31 |
/// FFI symbol name for an object method |
| 32 |
pub fn method_symbol_name(namespace: &str, object_name: &str, name: &str) -> String { |
| 33 |
let namespace = namespace.replace("::", "__"); |
| 34 |
let object_name = object_name.to_ascii_lowercase(); |
| 35 |
let name = name.to_ascii_lowercase(); |
| 36 |
format!("uniffi_{namespace}_fn_method_{object_name}_{name}") |
| 37 |
} |
| 38 |
|
| 39 |
/// FFI symbol name for the `clone` function for an object. |
| 40 |
pub fn clone_fn_symbol_name(namespace: &str, object_name: &str) -> String { |
| 41 |
let namespace = namespace.replace("::", "__"); |
| 42 |
let object_name = object_name.to_ascii_lowercase(); |
| 43 |
format!("uniffi_{namespace}_fn_clone_{object_name}") |
| 44 |
} |
| 45 |
|
| 46 |
/// FFI symbol name for the `free` function for an object. |
| 47 |
pub fn free_fn_symbol_name(namespace: &str, object_name: &str) -> String { |
| 48 |
let namespace = namespace.replace("::", "__"); |
| 49 |
let object_name = object_name.to_ascii_lowercase(); |
| 50 |
format!("uniffi_{namespace}_fn_free_{object_name}") |
| 51 |
} |
| 52 |
|
| 53 |
/// FFI symbol name for the `init_callback` function for a callback interface |
| 54 |
pub fn init_callback_vtable_fn_symbol_name( |
| 55 |
namespace: &str, |
| 56 |
callback_interface_name: &str, |
| 57 |
) -> String { |
| 58 |
let namespace = namespace.replace("::", "__"); |
| 59 |
let callback_interface_name = callback_interface_name.to_ascii_lowercase(); |
| 60 |
format!("uniffi_{namespace}_fn_init_callback_vtable_{callback_interface_name}") |
| 61 |
} |
| 62 |
|
| 63 |
/// FFI checksum symbol name for a top-level function |
| 64 |
pub fn fn_checksum_symbol_name(namespace: &str, name: &str) -> String { |
| 65 |
let namespace = namespace.replace("::", "__"); |
| 66 |
let name = name.to_ascii_lowercase(); |
| 67 |
format!("uniffi_{namespace}_checksum_func_{name}") |
| 68 |
} |
| 69 |
|
| 70 |
/// FFI checksum symbol name for an object constructor |
| 71 |
pub fn constructor_checksum_symbol_name(namespace: &str, object_name: &str, name: &str) -> String { |
| 72 |
let namespace = namespace.replace("::", "__"); |
| 73 |
let object_name = object_name.to_ascii_lowercase(); |
| 74 |
let name = name.to_ascii_lowercase(); |
| 75 |
format!("uniffi_{namespace}_checksum_constructor_{object_name}_{name}") |
| 76 |
} |
| 77 |
|
| 78 |
/// FFI checksum symbol name for an object method |
| 79 |
pub fn method_checksum_symbol_name(namespace: &str, object_name: &str, name: &str) -> String { |
| 80 |
let namespace = namespace.replace("::", "__"); |
| 81 |
let object_name = object_name.to_ascii_lowercase(); |
| 82 |
let name = name.to_ascii_lowercase(); |
| 83 |
format!("uniffi_{namespace}_checksum_method_{object_name}_{name}") |
| 84 |
} |
| 85 |
|
| 86 |
/// Get the symbol name for a FFI-buffer version of a function |
| 87 |
pub fn ffi_buffer_symbol_name(fn_name: &str) -> String { |
| 88 |
match fn_name.strip_prefix("uniffi_") { |
| 89 |
Some(rest) => format!("uniffi_ffibuffer_{rest}"), |
| 90 |
// this should never happen, but if it does let's try our best to prefix things properl. |
| 91 |
None => format!("uniffi_ffibuffer_{fn_name}"), |
| 92 |
} |
| 93 |
} |