提交 5657e9b9 编写于 作者: chai2010's avatar chai2010

目标程序内置 wat2wasm 命令

上级 95ce7234
......@@ -3,3 +3,5 @@
module github.com/wa-lang/wa
go 1.17
require github.com/wa-lang/wabt-go v1.1.0 // indirect
// 版权 @2019 凹语言 作者。保留所有权利。
package app
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/wa-lang/wa/internal/config"
"github.com/wa-lang/wa/internal/logger"
"github.com/wa-lang/wabt-go"
)
var wat2wasmPath string
func RunWasm(filename string) (stdoutStderr []byte, err error) {
dst := filename
if strings.HasSuffix(filename, ".wat") {
dst += ".wasm"
if stdoutStderr, err = RunWat2Wasm(filename, "-o", dst); err != nil {
return stdoutStderr, err
}
}
return nil, fmt.Errorf("TODO: run wasm")
}
func InstallWat2wasm(path string) error {
if path == "" {
path, _ = os.Getwd()
}
var exePath string
if isDir(path) {
if runtime.GOOS == "windows" {
exePath = filepath.Join(path, "wat2wasm.exe")
} else {
exePath = filepath.Join(path, "wat2wasm")
}
} else {
exePath = path
}
if err := os.MkdirAll(filepath.Dir(exePath), 0777); err != nil {
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
}
if err := os.WriteFile(exePath, wabt.LoadWat2Wasm(), 0777); err != nil {
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
return err
}
return nil
}
func RunWat2Wasm(args ...string) (stdoutStderr []byte, err error) {
if wat2wasmPath == "" {
logger.Tracef(&config.EnableTrace_app, "wat2wasm not found")
return nil, errors.New("wat2wasm not found")
}
cmd := exec.Command(wat2wasmPath, args...)
stdoutStderr, err = cmd.CombinedOutput()
return
}
func init() {
var baseName = "wat2wasm"
if runtime.GOOS == "windows" {
baseName += ".exe"
}
// 1. exe 同级目录存在 wat2wasm ?
wat2wasmPath = filepath.Join(curExeDir(), baseName)
if exeExists(wat2wasmPath) {
return
}
// 2. 当前目录存在 wat2wasm ?
cwd, _ := os.Getwd()
wat2wasmPath = filepath.Join(cwd, baseName)
if exeExists(wat2wasmPath) {
return
}
// 3. 本地系统存在 wat2wasm ?
if s, _ := exec.LookPath(baseName); s != "" {
wat2wasmPath = s
return
}
// 4. wat2wasm 安装到同级目录存 ?
if err := os.WriteFile(wat2wasmPath, wabt.LoadWat2Wasm(), 0777); err != nil {
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
return
}
}
// 是否为目录
func isDir(path string) bool {
if fi, _ := os.Lstat(path); fi != nil && fi.IsDir() {
return true
}
return false
}
// exe 文件存在
func exeExists(path string) bool {
fi, err := os.Lstat(path)
if err != nil {
return false
}
if !fi.Mode().IsRegular() {
return false
}
return true
}
// 当前执行程序所在目录
func curExeDir() string {
s, err := os.Executable()
if err != nil {
logger.Panicf("os.Executable() failed: %+v", err)
}
return filepath.Dir(s)
}
......@@ -72,7 +72,7 @@ func main() {
cliApp.Commands = []*cli.Command{
{
Name: "init",
Usage: "init a sketch app",
Usage: "init a sketch wa module",
ArgsUsage: "app-name",
Flags: []cli.Flag{
&cli.StringFlag{
......@@ -280,6 +280,10 @@ func main() {
Usage: "set output file",
Value: "a.out.wat",
},
&cli.BoolFlag{
Name: "run",
Usage: "run wat or wasm",
},
},
Action: func(c *cli.Context) error {
outfile := c.String("output")
......@@ -303,7 +307,30 @@ func main() {
os.Exit(1)
}
} else {
fmt.Println(string(output))
if c.Bool("run") {
outfile = "a.out.wat"
err := os.WriteFile(outfile, []byte(output), 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
fmt.Println(string(output))
}
}
if c.Bool("run") {
stdoutStderr, err := app.RunWasm(outfile)
if err != nil {
if len(stdoutStderr) > 0 {
fmt.Println(stdoutStderr)
}
fmt.Println(err)
os.Exit(1)
}
if len(stdoutStderr) > 0 {
fmt.Println(string(stdoutStderr))
}
}
return nil
......@@ -325,6 +352,26 @@ func main() {
return nil
},
},
{
Hidden: true,
Name: "install-wat2wasm",
Usage: "install-wat2wasm tool",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
Usage: "set output dir",
Value: "",
},
},
Action: func(c *cli.Context) error {
outdir := c.String("dir")
if err := app.InstallWat2wasm(outdir); err != nil {
fmt.Println(err)
os.Exit(1)
}
return nil
},
},
}
cliApp.Run(os.Args)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册