Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Wasm-bindgen does not pass String from Rust to JS #3224

Closed
wdanilo opened this issue Jan 4, 2023 · 2 comments
Closed

Wasm-bindgen does not pass String from Rust to JS #3224

wdanilo opened this issue Jan 4, 2023 · 2 comments
Labels

Comments

@wdanilo
Copy link
Contributor

wdanilo commented Jan 4, 2023

Describe the Bug

Hi, I've got such a code:

#[wasm_bindgen]
pub fn test() -> String {
    "hello".to_string()
}

When compiling it with wasm-pack --target=web and calling it from JS, I've got undefined as a result. It seems that I'm not the only one facing this issue and it seems that compiling everything with --target=nodejs makes it working: https://stackoverflow.com/questions/70079979/how-do-i-return-a-string-from-a-rust-function-in-nodejs-webassembly-using-wasm-b

I'm not sure whether this issue is connected more with wasm-pack or wasm-bindgen, but as wam-pack is not very actively maintained, I prefer to ask here first for any help / guidance. Thank you :)

@wdanilo wdanilo added the bug label Jan 4, 2023
@Liamolucko
Copy link
Collaborator

It's hard to say what exactly the issue is without seeing how you're instantiating the wasm module on the JS side, as well as where you're running the JS, etc.

If you're doing the same thing as the Stack Overflow question you linked, using WebAssembly.instantiate on the raw wasm file, the problem is that you should be importing the JS wrapper around it instead. The API exported by the raw wasm isn't meant to be used directly, which is why it returns undefined instead of the string you expect.

@wdanilo
Copy link
Contributor Author

wdanilo commented Jan 12, 2023

You are right, the Stack Overflow code is wrong and I didn't notice it. I am using the JS wrapper. The setup here is pretty complex. Let me clean it a little bit and I link it with a proper description in a few days. Thank you for the help.

@rustwasm rustwasm locked and limited conversation to collaborators Jan 17, 2024
@daxpedda daxpedda converted this issue into discussion #3792 Jan 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests

2 participants