Skip to content

Commit

Permalink
use normalized module path in function names
Browse files Browse the repository at this point in the history
  • Loading branch information
bobozaur committed Nov 26, 2024
1 parent 3a72135 commit 2b1a18a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
15 changes: 8 additions & 7 deletions uniffi_macros/src/setup_scaffolding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ use uniffi_meta::UNIFFI_CONTRACT_VERSION;

pub fn setup_scaffolding(namespace: String) -> Result<TokenStream> {
let module_path = mod_path()?;
let ffi_contract_version_ident = format_ident!("ffi_{module_path}_uniffi_contract_version");
let normalized_module_path = module_path.replace("::", "__");
let ffi_contract_version_ident = format_ident!("ffi_{normalized_module_path}_uniffi_contract_version");
let namespace_upper = namespace.to_ascii_uppercase();
let namespace_const_ident = format_ident!("UNIFFI_META_CONST_NAMESPACE_{namespace_upper}");
let namespace_static_ident = format_ident!("UNIFFI_META_NAMESPACE_{namespace_upper}");
let ffi_rustbuffer_alloc_ident = format_ident!("ffi_{module_path}_rustbuffer_alloc");
let ffi_rustbuffer_from_bytes_ident = format_ident!("ffi_{module_path}_rustbuffer_from_bytes");
let ffi_rustbuffer_free_ident = format_ident!("ffi_{module_path}_rustbuffer_free");
let ffi_rustbuffer_reserve_ident = format_ident!("ffi_{module_path}_rustbuffer_reserve");
let reexport_hack_ident = format_ident!("{module_path}_uniffi_reexport_hack");
let ffi_rust_future_scaffolding_fns = rust_future_scaffolding_fns(&module_path);
let ffi_rustbuffer_alloc_ident = format_ident!("ffi_{normalized_module_path}_rustbuffer_alloc");
let ffi_rustbuffer_from_bytes_ident = format_ident!("ffi_{normalized_module_path}_rustbuffer_from_bytes");
let ffi_rustbuffer_free_ident = format_ident!("ffi_{normalized_module_path}_rustbuffer_free");
let ffi_rustbuffer_reserve_ident = format_ident!("ffi_{normalized_module_path}_rustbuffer_reserve");
let reexport_hack_ident = format_ident!("{normalized_module_path}_uniffi_reexport_hack");
let ffi_rust_future_scaffolding_fns = rust_future_scaffolding_fns(&normalized_module_path);

Ok(quote! {
// Unit struct to parameterize the FfiConverter trait.
Expand Down
4 changes: 1 addition & 3 deletions uniffi_macros/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ pub fn mod_path() -> syn::Result<String> {
// This is a nightly feature, tracked at https://github.com/rust-lang/rust/issues/90765
let expanded_module_path = TokenStream::expand_expr(&module_path_invoc)
.map_err(|e| syn::Error::new(Span::call_site(), e))?;
Ok(syn::parse::<syn::LitStr>(expanded_module_path)?
.value()
.replace("::", "_"))
Ok(syn::parse::<syn::LitStr>(expanded_module_path)?.value())
}

pub fn try_read_field(f: &syn::Field) -> TokenStream {
Expand Down
9 changes: 9 additions & 0 deletions uniffi_meta/src/ffi_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,37 @@
/// FFI symbol name for a top-level function
pub fn fn_symbol_name(namespace: &str, name: &str) -> String {
let namespace = namespace.replace("::", "__");
let name = name.to_ascii_lowercase();
format!("uniffi_{namespace}_fn_func_{name}")
}

/// FFI symbol name for an object constructor
pub fn constructor_symbol_name(namespace: &str, object_name: &str, name: &str) -> String {
let namespace = namespace.replace("::", "__");
let object_name = object_name.to_ascii_lowercase();
let name = name.to_ascii_lowercase();
format!("uniffi_{namespace}_fn_constructor_{object_name}_{name}")
}

/// FFI symbol name for an object method
pub fn method_symbol_name(namespace: &str, object_name: &str, name: &str) -> String {
let namespace = namespace.replace("::", "__");
let object_name = object_name.to_ascii_lowercase();
let name = name.to_ascii_lowercase();
format!("uniffi_{namespace}_fn_method_{object_name}_{name}")
}

/// FFI symbol name for the `clone` function for an object.
pub fn clone_fn_symbol_name(namespace: &str, object_name: &str) -> String {
let namespace = namespace.replace("::", "__");
let object_name = object_name.to_ascii_lowercase();
format!("uniffi_{namespace}_fn_clone_{object_name}")
}

/// FFI symbol name for the `free` function for an object.
pub fn free_fn_symbol_name(namespace: &str, object_name: &str) -> String {
let namespace = namespace.replace("::", "__");
let object_name = object_name.to_ascii_lowercase();
format!("uniffi_{namespace}_fn_free_{object_name}")
}
Expand All @@ -50,25 +55,29 @@ pub fn init_callback_vtable_fn_symbol_name(
namespace: &str,
callback_interface_name: &str,
) -> String {
let namespace = namespace.replace("::", "__");
let callback_interface_name = callback_interface_name.to_ascii_lowercase();
format!("uniffi_{namespace}_fn_init_callback_vtable_{callback_interface_name}")
}

/// FFI checksum symbol name for a top-level function
pub fn fn_checksum_symbol_name(namespace: &str, name: &str) -> String {
let namespace = namespace.replace("::", "__");
let name = name.to_ascii_lowercase();
format!("uniffi_{namespace}_checksum_func_{name}")
}

/// FFI checksum symbol name for an object constructor
pub fn constructor_checksum_symbol_name(namespace: &str, object_name: &str, name: &str) -> String {
let namespace = namespace.replace("::", "__");
let object_name = object_name.to_ascii_lowercase();
let name = name.to_ascii_lowercase();
format!("uniffi_{namespace}_checksum_constructor_{object_name}_{name}")
}

/// FFI checksum symbol name for an object method
pub fn method_checksum_symbol_name(namespace: &str, object_name: &str, name: &str) -> String {
let namespace = namespace.replace("::", "__");
let object_name = object_name.to_ascii_lowercase();
let name = name.to_ascii_lowercase();
format!("uniffi_{namespace}_checksum_method_{object_name}_{name}")
Expand Down

0 comments on commit 2b1a18a

Please sign in to comment.