Skip to content

Provide js_namespace argument on static declarations #194

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

Closed
pepsighan opened this issue May 9, 2018 · 5 comments
Closed

Provide js_namespace argument on static declarations #194

pepsighan opened this issue May 9, 2018 · 5 comments

Comments

@pepsighan
Copy link
Member

When accessing static attributes of a js api, we are only allowed to use it the following way.

type window;
static window: window;
type Document;

#[wasm_bindgen(method, getter, structural)]
fn document(this: &window) -> Document;

But to create a cleaner api allowing something like window::document would require js_namespace argument be allowed on static declarations.

type window;
type Document;

#[wasm_bindgen(js_namespace = window)]
static document: Document;
@alexcrichton
Copy link
Contributor

Hm so it looks like js_namespace works today? Are you thinking that the generated code should generate a module called window though?

@pepsighan
Copy link
Member Author

pepsighan commented May 10, 2018

@alexcrichton No, putting js_namespace on static does not work.

For the second question, yeah, I would love if a module was created.
Why? Its comes down to the choice of syntax. window.document() vs window::document.
Also, you told me using structural creates a shim, I think using js_namespace would directly retrieve the value.

@alexcrichton
Copy link
Contributor

Hm can you gist a full example of what isn't working for you? The simple examples I tried locally all seemed to work :(

@pepsighan
Copy link
Member Author

I am using version 0.2.8.

#[wasm_bindgen]
extern {
    pub type window;
    pub type Document;

    #[wasm_bindgen(js_namespace = window)]
    static document: Document;
}

Compiler error:

error: expected one of `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, or `unsafe`, found `static`
 --> src/lib.rs:8:1
  |
8 | #[wasm_bindgen]
  | ^^^^^^^^^^^^^^^ expected one of 8 possible tokens here

error: expected one of `const`, `crate`, `default`, `extern`, `fn`, `pub`, `type`, `unsafe`, or `}`, found `;`
 --> src/lib.rs:8:1
  |
8 | #[wasm_bindgen]
  | ^^^^^^^^^^^^^^^ expected one of 9 possible tokens here

@alexcrichton
Copy link
Contributor

Aha, there we go!

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

No branches or pull requests

2 participants