4

I'm new to Rust and WASM and struggling to get a first program running.

[dependencies]
wasm-bindgen = { version = "0.2.63" }

I have the following Rust that compiles to WASM

use wasm_bindgen::prelude::*;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
    // let val = ["Hello", name].join(" ");
    let val = format!("Hello {}", name);
    return val;
}

And my node code (inspired by https://nodejs.dev/learn/nodejs-with-webassembly),

const fs = require("fs");
const wasmBuffer = fs.readFileSync("../pkg/hello_world_bg.wasm");
WebAssembly.instantiate(wasmBuffer)
  .then((wasmModule) => {
    // Exported function live under instance.exports
    const greet = wasmModule.instance.exports.greet;
    console.log(typeof greet);
    const greeting = greet("Simon");
    console.log("x", greeting);
  })
  .catch((err) => console.log(err));

This logs

function
x undefined

I tried two ways of concatenating the strings, or perhaps I am doing something wrong with the return value?

5
  • @DenysSéguret wasm_bindgen would generally take care of the necessary glue to return a String from a function in Rust. rustwasm.github.io/wasm-bindgen/reference/types/string.html Commented Nov 23, 2021 at 11:49
  • Something else might be at play here, but a minimal reproducible example is lacking: please specify relevant dependency versions and remove unrelated code (mod utils;) while ensuring that it is complete and enables other people to reproduce the problem. Commented Nov 23, 2021 at 11:50
  • Unrelated, but the function body of greet only needs to be format!("Hello {}", name). Commented Nov 23, 2021 at 11:51
  • mod utils was put there by cargo generate --git https://github.com/rustwasm/wasm-pack-template - I have removed that import s it does not seem to be essential Commented Nov 23, 2021 at 11:59
  • Thanks @DenysSéguret - starting with wasm-pack build --target nodejs I was able to use the js file produced to call greet. That was simple for me, even though the js file I'm importing is impossible to follow at present Commented Nov 23, 2021 at 12:35

1 Answer 1

3

When using WebInstantiate in node without more boilerplate, just like you did, I got the same result (undefined). What works seamlessly in the browser doesn't work so well in node.

But I got string exchange working when specifically building a node module with

wasm-pack build --target nodejs

With a node module, usage is also much simpler:

const wasmModule = require("./pkg/hello_world.js");
const greet = wasmModule.greet;
const greeting = greet("Simon");
console.log('greeting:', greeting);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.