cawfree / react-native-webassembly

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

Unable to bind Webassembly to React Native JSI #14

Closed anudit closed 10 months ago

anudit commented 1 year ago

Hey, I'm trying to get Semaphore to work with react-native.

Minimal Demo: https://github.com/anudit/semaphore-minimal

PS: Would love to help figure this out too, https://twitter.com/cawfree/status/1640335508143321090?s=20

Almaju commented 1 year ago

In my case it was because I didn't have New Architecture enabled. I couldn't make it work with Expo so I went with React Native CLI and then https://reactnative.dev/docs/0.71/the-new-architecture/use-app-template?platform=ios#enable-the-new-architecture

anudit commented 1 year ago

@Almaju Switched newArchEnabled to true on expo, still no luck.

image

grmkris commented 1 year ago

so having same issue myself.. added this to metro.config

assetExts: metroDefault.assetExts.concat(['wasm']),

then created a shim file like this:

import { Worker } from 'react-native-workers'
import * as WebAssembly from 'react-native-webassembly'

// This is a basic shim. Depending on the feature parity between
// web-workers and react-native-workers, you might need to expand this.
global.Worker = Worker

global.WebAssembly = WebAssembly

when trying to run web-assembly code, I am getting this:

 WARN  Possible Unhandled Promise Rejection (id: 1):
Error: TypeError: WebAssembly.compile is not a function (it is undefined)
Error: TypeError: WebAssembly.compile is not a function (it is undefined)
avivash commented 1 year ago

@Almaju Switched newArchEnabled to true on expo, still no luck.

image

bumping, i'm seeing the same thing too 👍🏼

avivash commented 1 year ago

After looking through the source code, it looks like it's using NativeModules, whereas maybe it should instead use TurboModules? I'm fairly new to react native, but it sounds like this is what we want and it was what @cawfree originally mentioned in their post

const {
  NativeModules
} = require('react-native');
currenthandle commented 11 months ago

I'm running into the same issue described: my full implementation

Has anyone found a solution?

0xdavid7 commented 11 months ago

I set the flag newArchEnabled=true in android/gradle.properties and it works on my machine

currenthandle commented 11 months ago

@tranhuyducseven what version of Expo are you using?

I already have this newArchEnabled=true set in android/gradle.properties. And I still seeing the Unable to bind Webassembly to React Native JSI issue:

 ERROR  Error: Unable to bind Webassembly to React Native JSI., js engine: hermes
 ERROR  Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called., js engine: hermes

I tried disabling hermes by adding "jsEngine": "jsc", to app.json then running expo prebuild. I get essentially the same error, except it doesn't mention hermes anymore:

 ERROR  Error: Unable to bind Webassembly to React Native JSI.
 ERROR  Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.

The error occurs as a result of this line:

const module = await WebAssembly.instantiate<{
        add: (a: number, b: number) => number
      }>(bufferSource)

I am seeing the same in iOS testing.

currenthandle commented 11 months ago

It seems that calling WebAssembly is causing the app to throw an exception with then inhibits the app from registering it's self: Expo docs: "Application has not been registered" error

0xdavid7 commented 11 months ago

@currenthandle I follow the react-native document then map into expo, these steps:

  1. npx expo prebuild
  2. set newArchEnabled=true
  3. yarn run:android

Note: You have to run native device or virtual simulator (android studio for android), you can not run with Expo Go!

This is the result:

React Native:

image

Expo:

image

I am not sure about the following config in app.config.ts, you can try it

{ 
     ....
     plugins: ["react-native-webassembly"],
}

Hope it works for you.

cawfree commented 10 months ago

Hey y'all, sorry for missing the discussion.

I recently had a kid and they are just as a big of a distraction as you'd think they'd be. But I'm back now.

This is sounding resolved, thanks to everyone who participated. Please feel free to say if you want this reopened.