Skip to content

Commit da2b585

Browse files
committed
完善 wasm4 的支持
1 parent a9dee85 commit da2b585

File tree

11 files changed

+473
-18
lines changed

11 files changed

+473
-18
lines changed

.github/workflows/publish.yml

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jobs:
2626
- run: make wasm-js
2727
- run: make wasm-wasip1
2828
- run: make -C waroot/examples/snake publish
29+
- run: make -C waroot/examples/w4-snake publish
30+
- run: make -C waroot/examples/w4-2048 publish
2931

3032
- name: Deploy
3133
uses: peaceiris/actions-gh-pages@v3

internal/app/appbuild/appbuild.go

+29
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
package appbuild
44

55
import (
6+
_ "embed"
67
"fmt"
78
"os"
89
"path/filepath"
10+
"strings"
911

1012
"wa-lang.org/wa/internal/3rdparty/cli"
1113
"wa-lang.org/wa/internal/app/appbase"
@@ -15,6 +17,18 @@ import (
1517
"wa-lang.org/wa/internal/wat/watutil"
1618
)
1719

20+
//go:embed assets/favicon.ico
21+
var favicon_ico string
22+
23+
//go:embed assets/index.html
24+
var w4index_html string
25+
26+
//go:embed assets/wasm4.js
27+
var w4js string
28+
29+
//go:embed assets/wasm4.css
30+
var w4css string
31+
1832
var CmdBuild = &cli.Command{
1933
Name: "build",
2034
Usage: "compile Wa source code",
@@ -209,6 +223,21 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
209223
os.Exit(1)
210224
}
211225

226+
if opt.TargetOS == config.WaOS_wasm4 {
227+
icoOutfile := filepath.Join(filepath.Dir(outfile), "favicon.ico")
228+
w4JsOutfile := filepath.Join(filepath.Dir(outfile), "wasm4.js")
229+
w4CssOutfile := filepath.Join(filepath.Dir(outfile), "wasm4.css")
230+
w4IndexOutfile := filepath.Join(filepath.Dir(outfile), "index.html")
231+
232+
wasm4Cart := filepath.Base(outfile)
233+
wasm4JsCode := strings.Replace(w4js, `"cart.wasm"`, `"`+wasm4Cart+`"`, -1)
234+
235+
os.WriteFile(icoOutfile, []byte(favicon_ico), 0666)
236+
os.WriteFile(w4JsOutfile, []byte(wasm4JsCode), 0666)
237+
os.WriteFile(w4CssOutfile, []byte(w4css), 0666)
238+
os.WriteFile(w4IndexOutfile, []byte(w4index_html), 0666)
239+
}
240+
212241
// 主函数
213242
mainFunc := manifest.MainPkg + ".main"
214243

4.19 KB
Binary file not shown.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
6+
<link rel="shortcut icon" href="/favicon.ico">
7+
<title>Wa-lang/WASM-4 Game</title>
8+
<style>wasm4.css</style>
9+
</head>
10+
<body>
11+
<script src="wasm4.js"></script>
12+
<wasm4-app></wasm4-app>
13+
</body>
14+
</html>

internal/app/appbuild/assets/wasm4.css

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/appbuild/assets/wasm4.js

+394
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/apprun/apprun.go

+9-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"wa-lang.org/wa/internal/3rdparty/cli"
1616
"wa-lang.org/wa/internal/app/appbase"
1717
"wa-lang.org/wa/internal/app/appbuild"
18-
"wa-lang.org/wa/internal/config"
1918
"wa-lang.org/wa/internal/wat/watutil"
2019
"wa-lang.org/wa/internal/wazero"
2120
)
@@ -88,27 +87,13 @@ func CmdRunAction(c *cli.Context) error {
8887
return nil
8988
}
9089

91-
if opt.Config().WaOS == config.WaOS_wasm4 {
92-
fmt.Println("please use `w4` to run wasm4 game!")
93-
os.Exit(0)
94-
return nil
95-
}
96-
9790
var appArgs []string
9891
if c.NArg() > 1 {
9992
appArgs = c.Args().Slice()[1:]
10093
}
10194

102-
m, err := wazero.BuildModule(input, wasmBytes, appArgs...)
103-
if err != nil {
104-
fmt.Println("wazero.BuildModule:", err)
105-
os.Exit(1)
106-
return nil
107-
}
108-
defer m.Close()
109-
11095
// Web 模式启动服务器
111-
if (m.HasUnknownImportFunc() || c.Bool("web")) && !c.Bool("console") {
96+
if (wazero.HasUnknownImportFunc(wasmBytes) || c.Bool("web")) && !c.Bool("console") {
11297
var addr = c.String("http")
11398
if strings.HasPrefix(addr, ":") {
11499
addr = "localhost" + addr
@@ -145,6 +130,14 @@ func CmdRunAction(c *cli.Context) error {
145130
return nil
146131
}
147132

133+
m, err := wazero.BuildModule(input, wasmBytes, appArgs...)
134+
if err != nil {
135+
fmt.Println("wazero.BuildModule:", err)
136+
os.Exit(1)
137+
return nil
138+
}
139+
defer m.Close()
140+
148141
stdout, stderr, err := m.RunMain(mainFunc)
149142
if err != nil {
150143
if len(stdout) > 0 {

internal/wazero/module.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,17 @@ func (p *Module) Close() error {
114114
}
115115

116116
// 是否包含用户自定义的宿主函数
117-
func (p *Module) HasUnknownImportFunc() bool {
118-
for _, importedFunc := range p.wazeroCompileModule.ImportedFunctions() {
117+
func HasUnknownImportFunc(wasmBytes []byte) bool {
118+
wazeroCtx := context.Background()
119+
rt := wazero.NewRuntime(wazeroCtx)
120+
121+
var err error
122+
wazeroCompileModule, err := rt.CompileModule(wazeroCtx, wasmBytes)
123+
if err != nil {
124+
return false
125+
}
126+
127+
for _, importedFunc := range wazeroCompileModule.ImportedFunctions() {
119128
moduleName, _, isImport := importedFunc.Import()
120129
if !isImport {
121130
continue

waroot/examples/w4-2048/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/output

waroot/examples/w4-2048/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ dev:
66
run:
77
go run ../../../main.go run -target=wasm4 .
88

9+
publish:
10+
go run ../../../main.go build -target=wasm4 .
11+
-rm -rf ../../../docs/w4-2048/
12+
mkdir -p ../../../docs/w4-2048
13+
cp ./output/* ../../../docs/w4-2048/
14+
915
clean:

waroot/examples/w4-snake/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ dev:
66
run:
77
go run ../../../main.go run -target=wasm4 .
88

9+
publish:
10+
go run ../../../main.go build -target=wasm4 .
11+
-rm -rf ../../../docs/w4-snake/
12+
mkdir -p ../../../docs/w4-snake
13+
cp ./output/* ../../../docs/w4-snake/
14+
915
clean:

0 commit comments

Comments
 (0)