Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 34e6ce6

Browse files
committedJun 4, 2021
wasm test
0 parents  commit 34e6ce6

File tree

13 files changed

+891
-0
lines changed

13 files changed

+891
-0
lines changed
 

‎Taskfile.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
vars:
6+
GREETING: Hello, World!
7+
8+
tasks:
9+
default:
10+
cmds:
11+
- task -l
12+
silent: false
13+
14+
wasm-build:
15+
cmds:
16+
- GOOS=js GOARCH=wasm go build -o main.wasm main.go
17+
desc: wasm build
18+
dir: pkg
19+
silent: false
20+
21+
wasm-run:
22+
cmds:
23+
- GOOS=js GOARCH=wasm go run -exec="$(go env GOROOT)/misc/wasm/go_js_wasm_exec" main.go
24+
desc: wasm run
25+
dir: pkg
26+
silent: false
27+
28+
tinygo-build:
29+
cmds:
30+
- tinygo build -o wasi.wasm -target wasm ./wasi.go
31+
desc: tinygo build wasm
32+
dir: pkg
33+
silent: false
34+
35+
wasmer-run:
36+
cmds:
37+
- wasmer triple-rust.wasm -i triple 4
38+
- wasmer wasi.wasm -i hello "tinygo wasm"
39+
desc: wasmer run
40+
dir: pkg
41+
silent: false
42+
43+
web:
44+
dir: web
45+
deps: [wasm-build]
46+
cmds:
47+
- cp ../pkg/main.wasm ./
48+
- go run main.go
49+
desc: web demo
50+
silent: false
51+
52+
wasi:
53+
cmds:
54+
- go run main.go
55+
dir: wasi
56+
deps: [tinygo-build]
57+
desc: wasi demo
58+
silent: false
59+
60+
wat:
61+
cmds:
62+
- wasm2wat triple-rust.wasm
63+
dir: pkg
64+
desc: use wasm2wat display triple-rust.wasm text
65+
silent: false

‎go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/venjiang/wasm
2+
3+
go 1.16
4+
5+
require (
6+
github.com/mathetake/gasm v0.0.0-20210402230938-71556e50c408
7+
github.com/wasmerio/wasmer-go v1.0.3 // indirect
8+
)

‎go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/mathetake/gasm v0.0.0-20210402230938-71556e50c408 h1:9LyyP70MTpF5M8aGW6VEPcLW2UIST4c+xl18WXLpc2E=
4+
github.com/mathetake/gasm v0.0.0-20210402230938-71556e50c408/go.mod h1:B0hjXHyNgRNRpnitQbCwbvm3H3UQdm70tfR9Mdot5dc=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
9+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
10+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/wasmerio/wasmer-go v1.0.3 h1:9pWIlIqUKxALvFlWK8+Zy90qyqxd+8wlyVG91txh1TU=
12+
github.com/wasmerio/wasmer-go v1.0.3/go.mod h1:0gzVdSfg6pysA6QVp6iVRPTagC6Wq9pOE8J86WKb2Fk=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
15+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎pkg/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"syscall/js"
6+
)
7+
8+
func main() {
9+
done := make(chan int, 0)
10+
// alert
11+
// js.Global().Get("alert")
12+
// alert := js.Global().Get("alert")
13+
// alert.Invoke("Hello WASM 测试!")
14+
// hello
15+
fmt.Println("hello go wasm")
16+
js.Global().Set("hello", js.FuncOf(jsHello))
17+
<-done
18+
}
19+
20+
func jsHello(this js.Value, args []js.Value) interface{} {
21+
msg := args[0].String()
22+
23+
return js.ValueOf(hello(msg))
24+
}
25+
26+
func hello(msg string) string {
27+
return "hello " + msg
28+
}

‎pkg/main.wasm

2.08 MB
Binary file not shown.

‎pkg/triple-rust.wasm

189 Bytes
Binary file not shown.

‎pkg/wasi.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
// This calls a JS function from Go.
4+
func main() {
5+
// println("adding two numbers:", add(2, 3)) // expecting 5
6+
println("load tinygo wasm")
7+
}
8+
9+
// This function is exported to JavaScript, so can be called using
10+
// exports.multiply() in JavaScript.
11+
//export multiply
12+
func multiply(x, y int) int {
13+
return x * y
14+
}

‎pkg/wasi.wasm

9.3 KB
Binary file not shown.

‎wasi/main.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io/ioutil"
7+
8+
// "github.com/mathetake/gasm/wasi"
9+
// "github.com/mathetake/gasm/wasm"
10+
// "github.com/go-interpreter/wagon/exec"
11+
// "github.com/go-interpreter/wagon/wasm"
12+
"github.com/mathetake/gasm/wasi"
13+
"github.com/mathetake/gasm/wasm"
14+
wasmer "github.com/wasmerio/wasmer-go/wasmer"
15+
)
16+
17+
func main() {
18+
// wagon()
19+
// gasm()
20+
// loadGoWasm()
21+
loadRustWasm()
22+
}
23+
24+
func loadGoWasm() {
25+
wasmBytes, _ := ioutil.ReadFile("../pkg/wasi.wasm")
26+
// Create an Engine
27+
engine := wasmer.NewEngine()
28+
// Create a Store
29+
store := wasmer.NewStore(engine)
30+
// Compiles the module
31+
module, err := wasmer.NewModule(store, wasmBytes)
32+
if err != nil {
33+
fmt.Println("Failed to compile module:", err)
34+
}
35+
36+
// Create an empty import object.
37+
importObject := wasmer.NewImportObject()
38+
// Let's instantiate the Wasm module.
39+
instance, err := wasmer.NewInstance(module, importObject)
40+
if err != nil {
41+
panic(fmt.Sprintln("Failed to instantiate the module:", err))
42+
}
43+
// get function
44+
multiply, err := instance.Exports.GetFunction("multiply")
45+
if err != nil {
46+
panic(fmt.Sprintln("Failed to retrieve the `multiply` function:", err))
47+
}
48+
49+
result, err := multiply(3, 5)
50+
fmt.Println(result)
51+
52+
}
53+
54+
func loadRustWasm() {
55+
wasmBytes, _ := ioutil.ReadFile("../pkg/triple-rust.wasm")
56+
// engine
57+
engine := wasmer.NewEngine()
58+
store := wasmer.NewStore(engine)
59+
// Compiles the module
60+
module, err := wasmer.NewModule(store, wasmBytes)
61+
if err != nil {
62+
fmt.Println("Failed to compile module:", err)
63+
}
64+
// Instantiates the module
65+
importObject := wasmer.NewImportObject()
66+
instance, err := wasmer.NewInstance(module, importObject)
67+
if err != nil {
68+
panic(fmt.Sprintln("Failed to instantiate the module:", err))
69+
}
70+
// Gets the `sum` exported function from the WebAssembly instance.
71+
triple, _ := instance.Exports.GetFunction("triple")
72+
var data float64 = 5
73+
result, err := triple(data)
74+
if err != nil {
75+
panic(fmt.Sprintln("Failed to get the `add_one` function:", err))
76+
}
77+
fmt.Println(result)
78+
}
79+
80+
// func wagon() {
81+
// f, _ := os.Open("../static/main.wasm")
82+
// m, _ := wasm.ReadModule(f, nil)
83+
// vm, _ := exec.NewVM(m)
84+
// // result = hello(100, 20)
85+
// // msg := "调用wasm"
86+
// result, err := vm.ExecCode(0, 1)
87+
// fmt.Printf("ret: %v, err: %v", result, err)
88+
// }
89+
90+
func gasm() {
91+
buf, _ := ioutil.ReadFile("../pkg/main.wasm")
92+
mod, _ := wasm.DecodeModule(bytes.NewBuffer(buf))
93+
vm, _ := wasm.NewVM(mod, wasi.New().Modules())
94+
// call
95+
// msg := "调用wasm方法"
96+
// arg := &msg
97+
ret, retTypes, err := vm.ExecExportedFunction("hello", 5)
98+
fmt.Println("ret: %v, retTypes: %v, err: %v", ret, retTypes, err)
99+
100+
}

‎web/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<html>
2+
<script src="wasm_exec.js"></script>
3+
<script>
4+
const go = new Go();
5+
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject)
6+
.then((result) => {
7+
go.run(result.instance);
8+
document.querySelector("h2").innerText=hello("加载Go Wasm");
9+
});
10+
11+
</script>
12+
<h2 id="msg">
13+
</h2>
14+
15+
</html>

‎web/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
"net/http"
7+
)
8+
9+
var (
10+
listen = flag.String("listen", ":8080", "listen address")
11+
dir = flag.String("dir", ".", "directory to serve")
12+
)
13+
14+
func main() {
15+
flag.Parse()
16+
log.Printf("listening on %q...", *listen)
17+
err := http.ListenAndServe(*listen, http.FileServer(http.Dir(*dir)))
18+
log.Fatalln(err)
19+
}

‎web/main.wasm

2.08 MB
Binary file not shown.

0 commit comments

Comments
 (0)