Last active
July 14, 2024 01:25
-
-
Save anzdaddy/bea6bca240ec34b9160a646bf13b7fb9 to your computer and use it in GitHub Desktop.
Go wasm test code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"github.com/anzx/envelope-compose-go/internal/rpc/wasm/client/gowasmer" | |
) | |
func main() { | |
wasmBytes, err := os.ReadFile("../server/main.wasm") | |
if err != nil { | |
log.Fatal(err) | |
} | |
instance, err := gowasmer.NewInstance(wasmBytes) | |
if err != nil { | |
log.Fatal(err) | |
} | |
getFunc := func(name string) func(...any) any { | |
f := instance.Get(name).(func([]any) any) | |
return func(a ...any) any { return f(a) } | |
} | |
getch := getFunc("getch") | |
sum := getFunc("sum") | |
upper := getFunc("upper") | |
a, b := 2, 3 | |
word := "hello" | |
fmt.Printf("sum(%#v, %#v) -> %#v\n", a, b, sum(a, b)) | |
u := upper(word).(string) | |
fmt.Printf("len(upper(%#v)) -> 0x%x\n", word, len(u)) | |
fmt.Printf("upper(%#v) -> %#v\n", word, u) | |
fmt.Printf("getch(%#v, %#v) -> %#v\n", word, a, getch(word, a)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"strings" | |
"syscall/js" | |
"github.com/mattn/gowasmer/wasmutil" | |
) | |
func main() { | |
global := js.Global() | |
wrap := func(f any) js.Func { return js.FuncOf(wasmutil.Wrap(f)) } | |
global.Set("getch", wrap(func(x string, i int) int { | |
return int(x[i]) | |
})) | |
global.Set("sum", wrap(func(a, b int) int { | |
return a + b | |
})) | |
global.Set("upper", wrap(func(x string) string { | |
return strings.ToUpper(x) | |
})) | |
<-make(chan struct{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment