Skip to content

Commit

Permalink
wa run 默认启动 web 服务
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Nov 21, 2023
1 parent 724d352 commit 3bb78d9
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 8 deletions.
11 changes: 11 additions & 0 deletions internal/app/appbuild/appbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ func BuildApp(opt *appbase.Option, input, outfile string) (wasmBytes []byte, err
fmt.Printf("write %s failed: %v\n", jsOutfile, err)
os.Exit(1)
}

// 生成 index.html 文件
indexHtmlPath := filepath.Join(filepath.Dir(outfile), "index.html")
if !appbase.PathExists(indexHtmlPath) {
htmlOutput := compiler.GenIndexHtml(filepath.Base(jsOutfile))
err = os.WriteFile(indexHtmlPath, []byte(htmlOutput), 0666)
if err != nil {
fmt.Printf("write %s failed: %v\n", indexHtmlPath, err)
os.Exit(1)
}
}
}

// wat 编译为 wasm
Expand Down
56 changes: 56 additions & 0 deletions internal/app/apprun/apprun.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ package apprun

import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"

"wa-lang.org/wa/internal/3rdparty/cli"
"wa-lang.org/wa/internal/app/appbase"
"wa-lang.org/wa/internal/app/appbuild"
"wa-lang.org/wa/internal/config"
"wa-lang.org/wa/internal/wazero"
)

Expand All @@ -18,6 +25,15 @@ var CmdRun = &cli.Command{
Flags: []cli.Flag{
appbase.MakeFlag_target(),
appbase.MakeFlag_tags(),
&cli.StringFlag{
Name: "http",
Usage: "set http address",
Value: ":8000",
},
&cli.BoolFlag{
Name: "noweb",
Usage: "set noweb mode",
},
},
Action: CmdRunAction,
}
Expand All @@ -36,6 +52,30 @@ func CmdRunAction(c *cli.Context) error {
return err
}

// Web 模式启动服务器
if !c.Bool("noweb") && opt.TargetOS == config.WaOS_js && appbase.IsNativeDir(input) {
var addr = c.String("http")
if strings.HasPrefix(addr, ":") {
addr = "localhost" + addr
}
fmt.Printf("listen at http://%s\n", addr)

go func() {
time.Sleep(time.Second * 2)
openBrowser(addr)
}()

http.Handle(
"/", http.FileServer(http.Dir(filepath.Join(input, "output"))),
)
if err := http.ListenAndServe(addr, nil); err != nil {
fmt.Println(err)
os.Exit(1)
}

return nil
}

var appArgs []string
if c.NArg() > 1 {
appArgs = c.Args().Slice()[1:]
Expand All @@ -62,3 +102,19 @@ func CmdRunAction(c *cli.Context) error {
}
return nil
}

func openBrowser(url string) error {
if !strings.HasPrefix(url, "http") {
url = "http://" + url
}
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", url).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
return exec.Command("open", url).Start()
default:
return fmt.Errorf("unsupported platform")
}
}
25 changes: 25 additions & 0 deletions internal/backends/compiler_wat/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ func (p *Compiler) GenJsBind() {
}
}

//go:embed index.html.gotmpl
var index_html_tmpl string

//go:embed js_binding_tmpl.js
var js_binding_tmpl string

Expand Down Expand Up @@ -481,6 +484,28 @@ func (p *Compiler) funcsForJSBinding() []JSFunc {
return funcs
}

func (p *Compiler) GenIndexHtml(jsFilename string) string {
t, err := template.New("index.html").Parse(index_html_tmpl)
if err != nil {
logger.Fatal(err)
}
data := JSModule{
Filename: jsFilename,
Pkg: p.prog.Manifest.MainPkg,
Globals: p.globalsForJsBinding(),
Funcs: p.funcsForJSBinding(),
ImportCode: p.CompileWImportFiles(p.prog),
}

var bf bytes.Buffer
err = t.Execute(&bf, data)
if err != nil {
logger.Fatal(err)
}

return bf.String()
}

func (p *Compiler) GenJSBinding(wasmFilename string) string {
// 模板
t, err := template.New("js").Parse(js_binding_tmpl)
Expand Down
10 changes: 10 additions & 0 deletions internal/backends/compiler_wat/index.html.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
{{- $root := . -}}

<title>{{$root.Pkg}}</title>

<script type="text/javascript" src="./{{$root.Filename}}"></script>

<script>
// waApp.Method();
</script>
16 changes: 8 additions & 8 deletions waroot/examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ ci-test-all:

# loop forever

cd ./brainfuck && make
cd ./expr && make
cd ./hello && make
cd ./misc && make
cd ./pkg && make
cd ./prime && make
cd ./reftoptr && make
cd ./surface && make
#cd ./brainfuck && make
#cd ./expr && make
#cd ./hello && make
#cd ./misc && make
#cd ./pkg && make
#cd ./prime && make
#cd ./reftoptr && make
#cd ./surface && make

# snake
cd ./snake && make publish
Expand Down
1 change: 1 addition & 0 deletions waroot/examples/snake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/output

0 comments on commit 3bb78d9

Please sign in to comment.