Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow renaming cosmwasm-schema #1420

Closed
uint opened this issue Sep 12, 2022 · 3 comments · Fixed by #2070
Closed

Allow renaming cosmwasm-schema #1420

uint opened this issue Sep 12, 2022 · 3 comments · Fixed by #2070
Assignees
Milestone

Comments

@uint
Copy link
Contributor

uint commented Sep 12, 2022

Right now, renaming the cosmwasm-schema dependency in a contract would cause cosmwasm-schema macros to break.

One fix (my preference probably) is to add an attribute to all macros that allows manually overriding the path to cosmwasm-schema.

Another is using something like proc-macro-crate to find out the crate name from Cargo.toml, but I really don't like the idea of source code depending on the build system like this.

@webmaster128
Copy link
Member

We have the same issue in packages/derive/src/lib.rs for cosmwasm_std here:

#[proc_macro_attribute]
pub fn entry_point(_attr: TokenStream, mut item: TokenStream) -> TokenStream {
    let cloned = item.clone();
    let function = parse_macro_input!(cloned as syn::ItemFn);
    let name = function.sig.ident.to_string();
    // The first argument is `deps`, the rest is region pointers
    let args = function.sig.inputs.len() - 1;

    // E.g. "ptr0: u32, ptr1: u32, ptr2: u32, "
    let typed_ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{}: u32, ", acc, i));
    // E.g. "ptr0, ptr1, ptr2, "
    let ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{}, ", acc, i));

    let new_code = format!(
        r##"
        #[cfg(target_arch = "wasm32")]
        mod __wasm_export_{name} {{ // new module to avoid conflict of function name
            #[no_mangle]
            extern "C" fn {name}({typed_ptrs}) -> u32 {{
                cosmwasm_std::do_{name}(&super::{name}, {ptrs})
            }}
        }}
    "##,
        name = name,
        typed_ptrs = typed_ptrs,
        ptrs = ptrs
    );
    let entry = TokenStream::from_str(&new_code).unwrap();
    item.extend(entry);
    item
}

@webmaster128
Copy link
Member

One fix (my preference probably) is to add an attribute to all macros that allows manually overriding the path to cosmwasm-schema.

I tend to agree. Like #1539 but with

  • module prefix instead od just cosmwasm std name (i.e. you can put mycrate::foo::__bar as an argument)
  • use named instead of positional arg to allow extensibility

@webmaster128 webmaster128 added this to the 2.1.0 milestone Mar 18, 2024
@uint
Copy link
Contributor Author

uint commented Mar 19, 2024

I tend to agree. Like #1539 but with

  • module prefix instead od just cosmwasm std name (i.e. you can put mycrate::foo::__bar as an argument)
  • use named instead of positional arg to allow extensibility

Agreed on both fronts :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants