cawfree / react-native-webassembly

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

Error: Exception in HostFunction: function lookup failed #16

Open burhon97 opened 1 year ago

burhon97 commented 1 year ago

I'm trying to read file main.wasm with WebAssembly.instantiate() and get function add() . But got this error:

Error: Exception in HostFunction: function lookup failed

File main.wasm build with go:

Go code:

package main

import (
    "fmt"
    "syscall/js"
)

func add(this js.Value, inputs []js.Value) interface{} {
    a := inputs[0].Float()
    b := inputs[1].Float()
    return a + b
}

func main() {
    fmt.Println("Hello world!")
    ch := make(chan struct{}, 0)
    js.Global().Set("add", js.FuncOf(add))
    <-ch
}

Build wasm file and linking custom assets to iOS and Android project:

GOOS=js GOARCH=wasm go build -o main.wasm
cd ../
react-native-asset

Code of js in React-Native:

import * as WebAssembly from 'react-native-webassembly';
import {Dirs, FileSystem} from 'react-native-file-access';
import {Base64Url} from './src/base64url';

async function wasmTest() {
      const src = `${Dirs.MainBundleDir}`;
      const result =  await FileSystem.readFile(`${src}/main.wasm`, "base64")
      // console.log('result', result);
      const buffer = Base64Url.toBuffer(result);
      console.log('buffer', buffer.length);

      const module = await WebAssembly.instantiate<{
        add: (a: number, b: number) => number;
      }>(new Uint8Array(buffer));

      const secondResult = module.instance.exports.add(300, 200);
      console.log('second result = ', secondResult);
}

Versions

cawfree commented 10 months ago

Sounds like it was unable to find the functions we expect the Wasm to.

I'm unfamiliar with the formatting of this file - can you confirm that it works within a different runtime than react-native-webassembly?