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

Cannot import and export function of the same name #1496

Closed
alexcrichton opened this issue Apr 29, 2019 · 2 comments · Fixed by #1506
Closed

Cannot import and export function of the same name #1496

alexcrichton opened this issue Apr 29, 2019 · 2 comments · Fixed by #1506

Comments

@alexcrichton
Copy link
Contributor

Currently this generates invalid JS:

#[wasm_bindgen(module = "foo")]   
extern "C" {                      
    #[wasm_bindgen(js_name = foo)]
    pub fn bar(a: u32);           
}                                 
                                  
#[wasm_bindgen]                   
pub fn foo() {                    
    bar(3);                       
}                                 

as it looks like:

import { foo } from 'foo';                        
import * as wasm from './add_bg';                 
                                                  
export function __wbg_foo_562f86fd7faf408e(arg0) {
    foo(arg0 >>> 0);                              
}                                                 
/**                                               
* @returns {void}                                 
*/                                                
export function foo() {                           
    return wasm.foo();                            
}                                                 

but that imports the name foo and also exports it!

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue May 1, 2019
Run exports through the same identifier generation as imports to ensure
that everything gets a unique identifier and then just make sure all the
appropriate wires are hooked up when dealing with exports and imports.

Closes rustwasm#1496
alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue May 3, 2019
Run exports through the same identifier generation as imports to ensure
that everything gets a unique identifier and then just make sure all the
appropriate wires are hooked up when dealing with exports and imports.

Closes rustwasm#1496
@cretz
Copy link

cretz commented Aug 7, 2019

@alexcrichton - This is now giving an error:

#[wasm_bindgen]
#[derive(Copy, Clone)]
pub enum ImageFormat {
    PNG,
    JPEG,
    GIF,
}

#[wasm_bindgen]
impl ImageFormat {
    #[wasm_bindgen]
    pub fn from_str(s: &str) -> Result<ImageFormat, JsValue> {
        match s {
            "PNG" => Ok(ImageFormat::PNG),
            "JPEG" => Ok(ImageFormat::JPEG),
            "GIF" => Ok(ImageFormat::GIF),
            _ => Err(JsValue::from(js_sys::Error::new(&format!("Unknown format: {}", s)))),
        }
    }
}

The error is

error: cannot shadow already defined class `ImageFormat`

Is this a bug in generating a static method? Should I open a new issue? Am I doing something wrong?

@alexcrichton
Copy link
Contributor Author

@cretz hm that looks like it may be unrelated to this, mind opening a new issue for that?

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.

2 participants