5

I'm using wasm_bindgen built with wasm-pack. I have a Rust function I expose to JS:

#[wasm_bindgen]
pub async fn validate_registration_token(backend_api_domain: String, token: String) -> Result<JsValue, JsValue> {

    console::log_1(&"backend_api_domain=".clone().into());
    console::log_1(&backend_api_domain.clone().into());
    console::log_1(&"token=".clone().into());
    console::log_1(&backend_api_domain.clone().into());

    let api_endpoint_get_guest_info = format!(
        "{backend_api_domain}/weddings/{token}/guests/registration/{registration_token}",
        backend_api_domain = backend_api_domain.clone(),
        token = token.clone(),
        registration_token = registration_token.clone()
    );

    console::log_1(&api_endpoint_get_guest_info.clone().into());

    let res = reqwest::Client::new()
        .get(&api_endpoint_get_guest_info)
        .send()
        .await
        .unwrap();

    let text = res.text().await.unwrap();

    let promise = js_sys::Promise::resolve(&true.into());
    let result = wasm_bindgen_futures::JsFuture::from(promise).await.unwrap();
    Ok(result)
}

In HTML / JavaScript, I call the Rust function:

<button
    type="button"
    class="btn submit"
    onclick="wam.validate_registration_token('http://localhost:80', 'mytoken')">
    Send
</button>

When launching the app, clicking on the Send button will call my Rust function, but both String params seem to be blank / missing.

Here's the console trace from function above:

backend_api_domain=

token=

/weddings//guests/registration/AAAA

I am not sure what I am doing wrong here. Should I change the way I call the Rust function from JavaScript?

Here's the full code example to reproduce

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
BenjaminC
  • 169
  • 7
  • Did you check the JS console to see if it reports on any errors? – djc Mar 27 '20 at 09:26
  • There is no error in the JS console :( – BenjaminC Mar 27 '20 at 10:11
  • It looks like your question might be answered by the answers of [JavaScript string is empty when passed to Rust WebAssembly module](https://stackoverflow.com/q/56248080/155423), which ultimately cites [Passing a JavaScript string to a Rust function compiled to WebAssembly](https://stackoverflow.com/q/49014610/155423) as the answer. If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Mar 27 '20 at 20:45
  • Hey @Shepmaster thanks for your comment. Though I might have missed something here but the comments linked here are not leveraging `wasm_bindgen` usage though. While in my case I am using it. Also in `wasm_bindgen` examples, there is an example with string but it looks like what I did aside the `async` in Rust. Did I miss anything? – BenjaminC Mar 27 '20 at 22:37

1 Answers1

4

Finally managed to fix the issue ! Thanks to help from user Pauan in rust discord. My mistake is to not init WASM properly in JS.

the return value from await init('./front_bg.wasm') is the raw WebAssembly exports (which you generally shouldn't use) whereas the ./front.js module wraps those exports so that they will work properly so you have to use the functions defined in ./front.js, not the functions returned from init

see https://discordapp.com/channels/442252698964721669/443151097398296587/693385649750933564

Changed script tag in HTML to this one:

import init, * as wam from './front.js';
const run = async () => {
    await init('./front_bg.wasm');
    window.wam = wam;
};
run();

Thanks !

BenjaminC
  • 169
  • 7