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 use JS strings without copying them into wasm memory #287

Closed
fitzgen opened this issue Jun 21, 2018 · 3 comments
Closed

Cannot use JS strings without copying them into wasm memory #287

fitzgen opened this issue Jun 21, 2018 · 3 comments
Labels
more-types Adding support for more Rust types to cross the boundary

Comments

@fitzgen
Copy link
Member

fitzgen commented Jun 21, 2018

This doesn't work, but it should:

#[wasm_bindgen]
extern {
    #[wasm_bindgen(js_name = String)]
    pub extern type JsString;

    pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
}

This is blocking all the String related bindings in #275.

I haven't dug in very far, but I think the issue is that strings are special cased by wasm-bindgen to always copy in/out of wasm memory, and so we can't reference them as an opaque extern type.

@fitzgen fitzgen added the more-types Adding support for more Rust types to cross the boundary label Jun 21, 2018
@kzvi
Copy link
Contributor

kzvi commented Jun 22, 2018

Aside from js_name not working, this works for me. Here is an example test case of it working (see build.sh): https://github.com/kzvi/wbg_test

@kzvi
Copy link
Contributor

kzvi commented Jun 22, 2018

This line in crates/backend/src/ast.rs makes the assumption that the literal type name of a method in Rust is going to be the same as the type name of a method in Javascript. This is why this is breaking.

In order to fix this, either:

  • Every method declaration needs to be annotated with the JS type name, e.g. #[wasm_bindgen(method, js_class = String)]
  • The code in the "backend" crate would need to have some mutable state to keep track of which class methods are being defined for.

@ohanar
Copy link
Member

ohanar commented Jun 24, 2018

I believe this was resolved in #295.

@ohanar ohanar closed this as completed Jun 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more-types Adding support for more Rust types to cross the boundary
Projects
None yet
Development

No branches or pull requests

3 participants