wasmerio / wasmer-go

🐹🕸️ WebAssembly runtime for Go
https://pkg.go.dev/github.com/wasmerio/wasmer-go
MIT License
2.84k stars 161 forks source link

Exports from WASM Loader not being imported #316

Closed alexpitsikoulis closed 2 years ago

alexpitsikoulis commented 2 years ago

Thanks for the bug report!

Describe the bug

When trying to import the __getString function from the loader in the module it says the function does not exist. Logs from the AssemblyScript show that it is being exported but only the functions I defined in my Assembly Script file are available.

Steps to reproduce

index.ts (assemblyscript module entry file)

export function add(a: i32, b: i32): i32 {
  return a + b;
}

export function helloWorld(): string {
  return "hello world";
}

index.js (from assembly script folder)

const fs = require("fs");
const loader = require("@assemblyscript/loader");

const wasmModule = loader.instantiateSync(fs.readFileSync(__dirname + "/build/optimized.wasm"));

module.exports = wasmModule.exports;

main.go

package main

import (
    "fmt"
    "io/ioutil"

    wasmer "github.com/wasmerio/wasmer-go/wasmer"
)

func main() {
    wasmBytes, _ := ioutil.ReadFile("../../../dev/web-assembly/build/optimized.wasm")

    engine := wasmer.NewEngine()
    store := wasmer.NewStore(engine)

    module, _ := wasmer.NewModule(store, wasmBytes)

    importObject := wasmer.NewImportObject()
    instance, _ := wasmer.NewInstance(module, importObject)

    add, _ := instance.Exports.GetFunction("add")
    helloWorld, _ := instance.Exports.GetFunction("helloWorld")
    __getString, _ := instance.Exports.GetFunction("__getString") // error happens here, __getString not found even though it is exported and available in a separate node app

    result, _ := add(1, 2)

    doHelloWorld := func() string {
        strPtr, _ := helloWorld()
        str, _ := __getString(strPtr)
        return str
    }

    fmt.Println(result)
    fmt.Println(helloWorld())
    fmt.Println(doHelloWorld())
}

If applicable, add a link to a test case (as a zip file or link to a repository we can clone).

Expected behavior

__getString should be included with the other exported functions

Actual behavior

Exports in go only show add, helloWorld (the 2 functions i defined in my assembly script file), and memory

Add any other context about the problem here.

MaxGraey commented 2 years ago

__getString is js glue function (not a wasm method) and implemented in loader: https://github.com/AssemblyScript/assemblyscript/blob/main/lib/loader/index.js#L168

alexpitsikoulis commented 2 years ago

So what could I do , if anything, to be able to read string values from the web assembly module?

MaxGraey commented 2 years ago

So what could I do

You need to port the js code from loader to Go