cawfree / react-native-webassembly

⚛️ 🏎 WebAssembly for React Native powered by JSI.
https://twitter.com/cawfree
MIT License
292 stars 6 forks source link

Cannot use string #18

Open Almaju opened 1 year ago

Almaju commented 1 year ago

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)
cawfree commented 10 months ago

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?

whyubel1eve commented 9 months ago

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)
cawfree commented 9 months ago

Hey @whyubel1eve, thanks for clarification!

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

lednhatkhanh commented 8 months ago

@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.