react-native-webassembly icon indicating copy to clipboard operation
react-native-webassembly copied to clipboard

Cannot use string

Open Almaju opened this issue 2 years ago • 4 comments

Everything works fine when I do:

export const add = async () => {
  const {instance} = await WebAssembly.instantiate<{
    add: (a: number, b: number) => number;
  }>(AddNumber);
  return instance.exports.add;
};

But if I try to return a string instead:

export const add = async () => {
  const {instance} = await WebAssembly.instantiate<{
    greet: () => string;
  }>(Greet);
  return instance.exports.greet;
};

Then this is what I get :

 WARN  Possible Unhandled Promise Rejection (id: 0):
Error: getValueAtIndex: index 2 is out of bounds [0, 2)
Error: getValueAtIndex: index 2 is out of bounds [0, 2)
    at RNWebassembly_invoke (native)
    at anonymous (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:123593:66)
    at anonymous (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:122943:42)
    at tryCallOne (/Users/distiller/react-native/packages/react-native/sdks/hermes/build_iphonesimulator/lib/InternalBytecode/InternalBytecode.js:53:16)
    at anonymous (/Users/distiller/react-native/packages/react-native/sdks/hermes/build_iphonesimulator/lib/InternalBytecode/InternalBytecode.js:139:27)
    at apply (native)
    at anonymous (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:34514:26)
    at _callTimer (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:34393:17)
    at _callReactNativeMicrotasksPass (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:34438:17)
    at callReactNativeMicrotasks (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:34644:44)
    at __callReactNativeMicrotasks (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:3288:46)
    at anonymous (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:3062:45)
    at __guard (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:3261:15)
    at flushedQueue (http://localhost:8081/index.bundle//&platform=ios&dev=true&minify=false&modulesOnly=false&runModule=true&app=org.reactjs.native.example.app:3061:21)

Almaju avatar Aug 09 '23 21:08 Almaju

Changing the typing of the instantiate function would not impact the runtime in this way, though the Wasm file being loaded would certainly.

Would you be able to share the file so I can try to replicate?

cawfree avatar Nov 03 '23 00:11 cawfree

hello, @cawfree . I have the same issue. This is the rust code:

#[wasm_bindgen]
pub fn generate_hd_account(phrase: &str, index: &str) -> String

js:

 const module = await WebAssembly.instantiate<{
      generate_hd_account: (phrase: string, index: string) => string;
    }>(HD);

  const account = module.instance.exports.generate_hd_account(
    mnemonic,
    index,
  );

I got this too

Error: getValueAtIndex: index 2 is out of bounds [0, 2)

whyubel1eve avatar Dec 01 '23 07:12 whyubel1eve

Hey @whyubel1eve, thanks for clarification!

Any chance you can share a template project so I can debug this locally?

cawfree avatar Dec 01 '23 11:12 cawfree

@cawfree Thank you for making this library! I also face this problem, just create a wasm package that receive string with rust then you will face this issue.

lednhatkhanh avatar Dec 10 '23 12:12 lednhatkhanh