Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why wasm response empty string ? #4

Open
rfyiamcool opened this issue Nov 16, 2022 · 0 comments
Open

why wasm response empty string ? #4

rfyiamcool opened this issue Nov 16, 2022 · 0 comments

Comments

@rfyiamcool
Copy link

question

wasm

I build the code to wasm file.

package main

import (
	"syscall/js"

	"github.com/mattn/gowasmer/wasmutil"
)

func Add(a, b int) int {
	return a + b
}

func String(a string) string {
	println(a)
	return a
}

func MultiString(a, b string) string {
	return a + b
}

func main() {
	c := make(chan struct{})
	js.Global().Set("String", js.FuncOf(wasmutil.Wrap(String)))
	js.Global().Set("Add", js.FuncOf(wasmutil.Wrap(Add)))
	js.Global().Set("MultiString", js.FuncOf(wasmutil.Wrap(MultiString)))
	<-c
}

golang code

go run the code. but the function add in wasm is ok, but String and MultiString response emtpy string ?

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	gowasmer "github.com/mattn/gowasmer"
)

func main() {
	b, err := ioutil.ReadFile("wasm-example")
	if err != nil {
		log.Fatal(err)
	}

	inst, err := gowasmer.NewInstance(b)
	if err != nil {
		log.Fatal(err)
	}

	m := inst.Get("Add")
	r := m.(func([]interface{}) interface{})([]interface{}{1, 3})
	fmt.Printf("1 + 3 = %v\n", r)

	m = inst.Get("String")
	r = m.(func([]interface{}) interface{})([]interface{}{"haha"})
	fmt.Printf("String = %v\n", r)

	m = inst.Get("MultiString")
	r = m.(func([]interface{}) interface{})([]interface{}{"aaa", "bbb"})
	fmt.Printf("MultiString = %v\n", r)
}

run stdout:

1 + 3 = 4

String =
MultiString = 

please provide more example, thank u.

😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant