mirror of
https://gitee.com/wa-lang/wa.git
synced 2025-12-06 09:18:53 +08:00
删除 wabt 依赖和废弃的代码
This commit is contained in:
5
.github/workflows/publish.yml
vendored
5
.github/workflows/publish.yml
vendored
@@ -30,8 +30,7 @@ jobs:
|
||||
- run: make -C waroot/examples/snake publish
|
||||
|
||||
- run: |
|
||||
go run ./builder
|
||||
cp -r ./_output/wa-wasip1 ./docs
|
||||
GOOS=wasip1 GOARCH=wasm go build -o=./docs/wa-wasip1/bin/wa
|
||||
|
||||
- name: Deploy
|
||||
uses: peaceiris/actions-gh-pages@v3
|
||||
@@ -64,7 +63,7 @@ jobs:
|
||||
|
||||
- name: 'Build Wa Image'
|
||||
run: |
|
||||
go run ./builder
|
||||
go build
|
||||
docker build . --tag ghcr.io/wa-lang/wa:latest
|
||||
docker push ghcr.io/wa-lang/wa:latest
|
||||
|
||||
|
||||
34
.github/workflows/release.yml
vendored
34
.github/workflows/release.yml
vendored
@@ -20,9 +20,6 @@ jobs:
|
||||
with:
|
||||
go-version: 1.17
|
||||
|
||||
# install wat2wasm
|
||||
- run: sudo cp internal/3rdparty/wabt-go/internal/wabt-1.0.29-ubuntu/bin/wat2wasm /usr/local/bin/wa.wat2wasm.exe
|
||||
|
||||
- run: make ci-test-all
|
||||
|
||||
Publish:
|
||||
@@ -37,27 +34,12 @@ jobs:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
# go1.21 support wasip1/wasm
|
||||
go-version: 1.21
|
||||
|
||||
- run: make build-dist
|
||||
|
||||
- name: Run Releaser
|
||||
uses: ncipollo/release-action@v1
|
||||
go-version: 1.17
|
||||
- name: Run GoReleaser
|
||||
uses: goreleaser/goreleaser-action@v4
|
||||
with:
|
||||
token: ${{ secrets.WA_DEPLOY }}
|
||||
artifacts: "_output/*.zip,_output/*.checksums.txt"
|
||||
|
||||
- name: 'Login to GitHub Container Registry'
|
||||
uses: docker/login-action@v1
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{github.actor}}
|
||||
password: ${{secrets.WA_DEPLOY_DOCKER}}
|
||||
|
||||
- name: 'Build Wa Image'
|
||||
run: |
|
||||
go run ./builder
|
||||
docker build . --tag ghcr.io/wa-lang/wa:${{ github.ref_name }}
|
||||
docker push ghcr.io/wa-lang/wa:${{ github.ref_name }}
|
||||
|
||||
distribution: goreleaser
|
||||
version: latest
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.WA_DEPLOY }}
|
||||
|
||||
11
.github/workflows/wa.yml
vendored
11
.github/workflows/wa.yml
vendored
@@ -23,9 +23,6 @@ jobs:
|
||||
- run: go version
|
||||
- run: go env
|
||||
|
||||
# install wat2wasm
|
||||
- run: sudo cp internal/3rdparty/wabt-go/internal/wabt-1.0.29-ubuntu/bin/wat2wasm /usr/local/bin/wa.wat2wasm.exe
|
||||
|
||||
- run: make ci-test-all
|
||||
|
||||
- run: go test -v -coverprofile=profile.cov ./...
|
||||
@@ -35,7 +32,7 @@ jobs:
|
||||
- run: wa -h
|
||||
- run: cd waroot && wa run -target=wasi examples/prime
|
||||
- run: cd waroot && wa run -target=wasi hello.wa
|
||||
- run: cd waroot && wa run -target=unknown hello.wa
|
||||
- run: cd waroot && wa run -target=unknown hello.wa
|
||||
|
||||
- uses: shogo82148/actions-goveralls@v1
|
||||
with:
|
||||
@@ -55,9 +52,6 @@ jobs:
|
||||
- run: go version
|
||||
- run: go env
|
||||
|
||||
# install wat2wasm
|
||||
- run: copy internal/3rdparty/wabt-go/internal/wabt-1.0.29-windows/bin/wat2wasm.exe c:/wa.wat2wasm.exe
|
||||
|
||||
- run: make ci-test-all
|
||||
|
||||
- run: go test -v ./...
|
||||
@@ -83,9 +77,6 @@ jobs:
|
||||
- run: go version
|
||||
- run: go env
|
||||
|
||||
# install wat2wasm
|
||||
- run: sudo cp internal/3rdparty/wabt-go/internal/wabt-1.0.29-macos/bin/wat2wasm /usr/local/bin/wa.wat2wasm.exe
|
||||
|
||||
- run: make ci-test-all
|
||||
|
||||
- run: go test -v ./...
|
||||
|
||||
@@ -11,8 +11,8 @@ RUN apt update
|
||||
RUN apt install -y git
|
||||
|
||||
# go run ./builder
|
||||
COPY ./_output/wa-docker-linux-amd64 /usr/local/wa
|
||||
ENV PATH=${PATH}:/usr/local/wa/bin
|
||||
COPY ./wa /usr/local/wa
|
||||
ENV PATH=${PATH}:/usr/local/bin/wa
|
||||
|
||||
# docker run --platform linux/amd64 --rm -it wa-lang/wa
|
||||
CMD ["/bin/bash"]
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
// 版权 @2022 凹语言 作者。保留所有权利。
|
||||
|
||||
//go:build !wasm
|
||||
// +build !wasm
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"wa-lang.org/wa/internal/config"
|
||||
"wa-lang.org/wa/internal/wabt"
|
||||
"wa-lang.org/wa/internal/wat/watutil"
|
||||
"wa-lang.org/wa/internal/wazero"
|
||||
)
|
||||
|
||||
@@ -20,7 +17,7 @@ func RunCode(cfg *config.Config, filename, code string, args ...string) (stdoutS
|
||||
}
|
||||
|
||||
// wat 编译为 wasm
|
||||
wasmBytes, err := wabt.Wat2Wasm(watBytes)
|
||||
wasmBytes, err := watutil.Wat2Wasm(filename, watBytes)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
404
builder/main.go
404
builder/main.go
@@ -1,404 +0,0 @@
|
||||
// 版权 @2019 凹语言 作者。保留所有权利。
|
||||
|
||||
// 凹语言打包程序: Windows/Linux/macOS/wasm
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"wa-lang.org/wa/internal/3rdparty/gover"
|
||||
"wa-lang.org/wa/internal/3rdparty/wabt-go"
|
||||
"wa-lang.org/wa/internal/version"
|
||||
"wa-lang.org/wa/waroot"
|
||||
)
|
||||
|
||||
const (
|
||||
darwin = "darwin"
|
||||
linux = "linux"
|
||||
windows = "windows"
|
||||
wasip1 = "wasip1"
|
||||
|
||||
amd64 = "amd64"
|
||||
arm64 = "arm64"
|
||||
wasm = "wasm"
|
||||
)
|
||||
|
||||
var (
|
||||
flagOutputDir = flag.String("output", "_output", "set output dir")
|
||||
flagCgoEnabled = flag.Bool("cgo-enabled", false, "enable cgo")
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
b := &Builder{Output: *flagOutputDir}
|
||||
b.GenAll()
|
||||
}
|
||||
|
||||
type Builder struct {
|
||||
Output string // 目标的根目录
|
||||
}
|
||||
|
||||
func NewBuilder(outputDir string) *Builder {
|
||||
return &Builder{Output: outputDir}
|
||||
}
|
||||
|
||||
func (p *Builder) GenAll() {
|
||||
waRoot_darwin_amd64 := p.getWarootPath(darwin, arm64)
|
||||
waRoot_darwin_arm64 := p.getWarootPath(darwin, amd64)
|
||||
waRoot_linux_amd64 := p.getWarootPath(linux, amd64)
|
||||
waRoot_windows_amd64 := p.getWarootPath(windows, amd64)
|
||||
waRoot_wasip1_wasm := p.getWarootPath(wasip1, wasm)
|
||||
|
||||
waRoot_docker := fmt.Sprintf("%s/wa-docker-linux-amd64", p.Output)
|
||||
waRoot_wasip1 := fmt.Sprintf("%s/wa-wasip1", p.Output)
|
||||
|
||||
p.genWarootFiles(waRoot_darwin_amd64)
|
||||
p.genWarootFiles(waRoot_darwin_arm64)
|
||||
p.genWarootFiles(waRoot_linux_amd64)
|
||||
p.genWarootFiles(waRoot_windows_amd64)
|
||||
|
||||
if isWasip1Enabled() {
|
||||
p.genWarootFiles(waRoot_wasip1_wasm)
|
||||
}
|
||||
|
||||
p.genWat2wasmExe()
|
||||
p.genWaExe()
|
||||
|
||||
os.RemoveAll(waRoot_docker)
|
||||
cpDir(waRoot_docker, waRoot_linux_amd64)
|
||||
|
||||
if isWasip1Enabled() {
|
||||
os.RemoveAll(waRoot_wasip1)
|
||||
cpDir(waRoot_wasip1, waRoot_wasip1_wasm)
|
||||
}
|
||||
|
||||
p.zipDir(waRoot_darwin_amd64)
|
||||
p.zipDir(waRoot_darwin_arm64)
|
||||
p.zipDir(waRoot_linux_amd64)
|
||||
p.zipDir(waRoot_windows_amd64)
|
||||
|
||||
if isWasip1Enabled() {
|
||||
p.zipDir(waRoot_wasip1_wasm)
|
||||
}
|
||||
|
||||
os.RemoveAll(waRoot_darwin_amd64)
|
||||
os.RemoveAll(waRoot_darwin_arm64)
|
||||
os.RemoveAll(waRoot_linux_amd64) // keep for build docker image
|
||||
os.RemoveAll(waRoot_windows_amd64)
|
||||
|
||||
if isWasip1Enabled() {
|
||||
os.RemoveAll(waRoot_wasip1_wasm)
|
||||
}
|
||||
|
||||
p.genChecksums()
|
||||
}
|
||||
|
||||
func (p *Builder) genChecksums() {
|
||||
paths := []string{
|
||||
p.getWarootPath(darwin, arm64),
|
||||
p.getWarootPath(darwin, amd64),
|
||||
p.getWarootPath(linux, amd64),
|
||||
p.getWarootPath(windows, amd64),
|
||||
}
|
||||
if isWasip1Enabled() {
|
||||
paths = append(paths, p.getWarootPath(wasip1, wasm))
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
for _, path := range paths {
|
||||
data, err := os.ReadFile(path + ".zip")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Fprintf(&buf, "%x MD5(%s)\n", md5.Sum(data), filepath.Base(path)+".zip")
|
||||
}
|
||||
|
||||
os.WriteFile(
|
||||
filepath.Join(p.Output, "wa-"+version.Version+".checksums.txt"),
|
||||
buf.Bytes(),
|
||||
0666,
|
||||
)
|
||||
}
|
||||
|
||||
func (p *Builder) genWaExe() {
|
||||
CGO_ENABLED := "CGO_ENABLED=0"
|
||||
if *flagCgoEnabled {
|
||||
CGO_ENABLED = "CGO_ENABLED=1"
|
||||
}
|
||||
|
||||
// wasip1/wasm
|
||||
if isWasip1Enabled() {
|
||||
waRootPath := p.getWarootPath(wasip1, wasm)
|
||||
dstpath := filepath.Join(waRootPath, "bin", "wa.wasm")
|
||||
|
||||
cmd := exec.Command("go", "build", "-o", dstpath, "wa-lang.org/wa/cmd/wa-wasm")
|
||||
cmd.Env = append([]string{"GOOS=" + wasip1, "GOARCH=" + wasm, "CGO_ENABLED=0"}, os.Environ()...)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
fmt.Print(string(output))
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// darwin/arm64
|
||||
{
|
||||
waRootPath := p.getWarootPath(darwin, arm64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", "wa")
|
||||
|
||||
cmd := exec.Command("go", "build", "-o", dstpath, "wa-lang.org/wa")
|
||||
cmd.Env = append([]string{"GOOS=" + darwin, "GOARCH=" + arm64, CGO_ENABLED}, os.Environ()...)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
fmt.Print(string(output))
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// darwin/amd64
|
||||
{
|
||||
waRootPath := p.getWarootPath(darwin, amd64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", "wa")
|
||||
|
||||
cmd := exec.Command("go", "build", "-o", dstpath, "wa-lang.org/wa")
|
||||
cmd.Env = append([]string{"GOOS=" + darwin, "GOARCH=" + amd64, CGO_ENABLED}, os.Environ()...)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
fmt.Print(string(output))
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// ubuntu
|
||||
{
|
||||
waRootPath := p.getWarootPath(linux, amd64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", "wa")
|
||||
|
||||
cmd := exec.Command("go", "build", "-o", dstpath, "wa-lang.org/wa")
|
||||
cmd.Env = append([]string{"GOOS=" + linux, "GOARCH=" + amd64, CGO_ENABLED}, os.Environ()...)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
fmt.Print(string(output))
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// windows
|
||||
{
|
||||
waRootPath := p.getWarootPath(windows, amd64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", "wa.exe")
|
||||
|
||||
cmd := exec.Command("go", "build", "-o", dstpath, "wa-lang.org/wa")
|
||||
cmd.Env = append([]string{"GOOS=" + windows, "GOARCH=" + amd64, CGO_ENABLED}, os.Environ()...)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
fmt.Print(string(output))
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Builder) genWat2wasmExe() {
|
||||
// wasm
|
||||
if isWasip1Enabled() {
|
||||
waRootPath := p.getWarootPath(wasip1, wasm)
|
||||
dstpath := filepath.Join(waRootPath, "bin", "wat2wasm.wasm")
|
||||
os.WriteFile(dstpath, []byte(wabt.Wat2wasm_wasm), 0777)
|
||||
}
|
||||
|
||||
// macos/arm64
|
||||
{
|
||||
waRootPath := p.getWarootPath(darwin, arm64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", wabt.Wat2WasmName)
|
||||
os.WriteFile(dstpath, []byte(wabt.Wat2wasm_macos), 0777)
|
||||
}
|
||||
|
||||
// macos/amd64
|
||||
{
|
||||
waRootPath := p.getWarootPath(darwin, amd64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", wabt.Wat2WasmName)
|
||||
os.WriteFile(dstpath, []byte(wabt.Wat2wasm_macos), 0777)
|
||||
}
|
||||
|
||||
// ubuntu
|
||||
{
|
||||
waRootPath := p.getWarootPath(linux, amd64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", wabt.Wat2WasmName)
|
||||
os.WriteFile(dstpath, []byte(wabt.Wat2wasm_ubuntu), 0777)
|
||||
}
|
||||
|
||||
// windows
|
||||
{
|
||||
waRootPath := p.getWarootPath(windows, amd64)
|
||||
dstpath := filepath.Join(waRootPath, "bin", wabt.Wat2WasmName)
|
||||
os.WriteFile(dstpath, []byte(wabt.Wat2wasm_windows), 0777)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Builder) genWarootFiles(waRootPath string) error {
|
||||
os.RemoveAll(waRootPath)
|
||||
os.MkdirAll(waRootPath, 0777)
|
||||
warootfs := waroot.GetRootFS()
|
||||
err := fs.WalkDir(waroot.GetRootFS(), ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if d == nil || d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data, err := fs.ReadFile(warootfs, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dstpath := filepath.Join(waRootPath, path)
|
||||
os.MkdirAll(filepath.Dir(dstpath), 0777)
|
||||
|
||||
// 跳过忽略的文件
|
||||
if s := filepath.Base(path); s == "_keep" || s == ".keep" {
|
||||
return nil
|
||||
}
|
||||
if strings.HasSuffix(path, ".go") {
|
||||
return nil
|
||||
}
|
||||
|
||||
f, err := os.Create(dstpath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Builder) zipDir(dir string) {
|
||||
file, err := os.Create(dir + ".zip")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
w := zip.NewWriter(file)
|
||||
defer w.Close()
|
||||
|
||||
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileInfo, err := file.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 需要保留可执行等标志信息
|
||||
header, err := zip.FileInfoHeader(fileInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
relpath, err := filepath.Rel(dir, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header.Name = filepath.Join("wa", relpath)
|
||||
header.Method = zip.Deflate
|
||||
|
||||
f, err := w.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Builder) getWarootPath(waos, waarch string) string {
|
||||
return fmt.Sprintf("%s/wa_%s_%s-%s", p.Output, version.Version, waos, waarch)
|
||||
}
|
||||
|
||||
func isWasip1Enabled() bool {
|
||||
goversion := strings.TrimPrefix(runtime.Version(), "go")
|
||||
return gover.Compare(goversion, "1.21") >= 0
|
||||
}
|
||||
|
||||
func cpDir(dst, src string) (total int) {
|
||||
entryList, err := os.ReadDir(src)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
log.Fatal("cpDir: ", err)
|
||||
}
|
||||
for _, entry := range entryList {
|
||||
if entry.IsDir() {
|
||||
cpDir(dst+"/"+entry.Name(), src+"/"+entry.Name())
|
||||
} else {
|
||||
srcFname := filepath.Clean(src + "/" + entry.Name())
|
||||
dstFname := filepath.Clean(dst + "/" + entry.Name())
|
||||
|
||||
cpFile(dstFname, srcFname)
|
||||
total++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cpFile(dst, src string) {
|
||||
err := os.MkdirAll(filepath.Dir(dst), 0777)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
log.Fatal("cpFile: ", err)
|
||||
}
|
||||
fsrc, err := os.Open(src)
|
||||
if err != nil {
|
||||
log.Fatal("cpFile: ", err)
|
||||
}
|
||||
defer fsrc.Close()
|
||||
|
||||
fi, err := fsrc.Stat()
|
||||
if err != nil {
|
||||
log.Fatal("cpFile: ", err)
|
||||
}
|
||||
|
||||
fdst, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fi.Mode())
|
||||
if err != nil {
|
||||
log.Fatal("cpFile: ", err)
|
||||
}
|
||||
defer fdst.Close()
|
||||
if _, err = io.Copy(fdst, fsrc); err != nil {
|
||||
log.Fatal("cpFile: ", err)
|
||||
}
|
||||
}
|
||||
2
internal/3rdparty/wabt-go/README.md
vendored
2
internal/3rdparty/wabt-go/README.md
vendored
@@ -1,2 +0,0 @@
|
||||
# wabt-go
|
||||
wabt 工具打包
|
||||
@@ -1,4 +0,0 @@
|
||||
!/wabt-1.0.29-macos/bin/wat2wasm
|
||||
!/wabt-1.0.29-ubuntu/bin/wat2wasm
|
||||
!/wabt-1.0.29-windows/bin/wat2wasm.exe
|
||||
!/wabt-1.0.37-wasm/wat2wasm.wasm
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
25
internal/3rdparty/wabt-go/wabt_all.go
vendored
25
internal/3rdparty/wabt-go/wabt_all.go
vendored
@@ -1,25 +0,0 @@
|
||||
// 版权 @2022 凹语言 作者。保留所有权利。
|
||||
|
||||
package wabt
|
||||
|
||||
import _ "embed"
|
||||
|
||||
// Wabt 版本号
|
||||
const Version = "1.0.29"
|
||||
|
||||
const Wat2WasmName = "wa.wat2wasm.exe"
|
||||
|
||||
//go:embed internal/wabt-1.0.29-macos/bin/wat2wasm
|
||||
var Wat2wasm_macos string
|
||||
|
||||
//go:embed internal/wabt-1.0.29-ubuntu/bin/wat2wasm
|
||||
var Wat2wasm_ubuntu string
|
||||
|
||||
//go:embed internal/wabt-1.0.29-windows/bin/wat2wasm.exe
|
||||
var Wat2wasm_windows string
|
||||
|
||||
// wat2wasm.wasm 版本号
|
||||
const Wat2wasm_wasm_Version = "1.0.37"
|
||||
|
||||
//go:embed internal/wabt-1.0.37-wasm/wat2wasm.wasm
|
||||
var Wat2wasm_wasm string
|
||||
@@ -8,14 +8,6 @@ import (
|
||||
"wa-lang.org/wa/internal/config"
|
||||
)
|
||||
|
||||
// wabt工具
|
||||
func MakeFlag_wabt() *cli.BoolFlag {
|
||||
return &cli.BoolFlag{
|
||||
Name: "wabt",
|
||||
Usage: "use wabt/wat2wasm tool",
|
||||
}
|
||||
}
|
||||
|
||||
// 输出路径
|
||||
func MakeFlag_output() *cli.StringFlag {
|
||||
return &cli.StringFlag{
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
|
||||
// 命令行选项
|
||||
type Option struct {
|
||||
UseWabt bool
|
||||
Debug bool
|
||||
WaBackend string
|
||||
BuilgTags []string
|
||||
@@ -81,10 +80,6 @@ func BuildOptions(c *cli.Context, waBackend ...string) *Option {
|
||||
LD_MaxMemory: c.Int("ld-max-memory"),
|
||||
}
|
||||
|
||||
if c.Bool("wabt") {
|
||||
opt.UseWabt = true
|
||||
}
|
||||
|
||||
opt.TargetArch = "wasm"
|
||||
if len(waBackend) > 0 {
|
||||
opt.WaBackend = waBackend[0]
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"wa-lang.org/wa/internal/backends/compiler_wat"
|
||||
"wa-lang.org/wa/internal/config"
|
||||
"wa-lang.org/wa/internal/loader"
|
||||
"wa-lang.org/wa/internal/wabt"
|
||||
"wa-lang.org/wa/internal/wat/watutil"
|
||||
)
|
||||
|
||||
@@ -20,7 +19,6 @@ var CmdBuild = &cli.Command{
|
||||
Name: "build",
|
||||
Usage: "compile Wa source code",
|
||||
Flags: []cli.Flag{
|
||||
appbase.MakeFlag_wabt(),
|
||||
appbase.MakeFlag_output(),
|
||||
appbase.MakeFlag_target(),
|
||||
appbase.MakeFlag_tags(),
|
||||
@@ -77,7 +75,7 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
fmt.Printf("read %s failed: %v\n", input, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
wasmBytes, err := wat2wasm(opt.UseWabt, input, watData)
|
||||
wasmBytes, err := watutil.Wat2Wasm(input, watData)
|
||||
if err != nil {
|
||||
fmt.Printf("wat2wasm %s failed: %v\n", input, err)
|
||||
os.Exit(1)
|
||||
@@ -116,7 +114,7 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
}
|
||||
|
||||
// wat 编译为 wasm
|
||||
wasmBytes, err := wat2wasm(opt.UseWabt, watOutfile, watOutput)
|
||||
wasmBytes, err := watutil.Wat2Wasm(watOutfile, watOutput)
|
||||
if err != nil {
|
||||
fmt.Printf("wat2wasm %s failed: %v\n", input, err)
|
||||
os.Exit(1)
|
||||
@@ -198,7 +196,7 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
}
|
||||
|
||||
// wat 编译为 wasm
|
||||
wasmBytes, err := wat2wasm(opt.UseWabt, input, watOutput)
|
||||
wasmBytes, err := watutil.Wat2Wasm(input, watOutput)
|
||||
if err != nil {
|
||||
fmt.Printf("wat2wasm %s failed: %v\n", input, err)
|
||||
os.Exit(1)
|
||||
@@ -219,14 +217,6 @@ func BuildApp(opt *appbase.Option, input, outfile string) (mainFunc string, wasm
|
||||
}
|
||||
}
|
||||
|
||||
func wat2wasm(useWabt bool, filename string, watBytes []byte) (wasmBytes []byte, err error) {
|
||||
if useWabt {
|
||||
return wabt.Wat2Wasm(watBytes)
|
||||
} else {
|
||||
return watutil.Wat2Wasm(filename, watBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func buildWat(opt *appbase.Option, filename string) (
|
||||
prog *loader.Program, compiler *compiler_wat.Compiler,
|
||||
watBytes []byte, err error,
|
||||
|
||||
@@ -25,7 +25,6 @@ var CmdRun = &cli.Command{
|
||||
Name: "run",
|
||||
Usage: "compile and run Wa program",
|
||||
Flags: []cli.Flag{
|
||||
appbase.MakeFlag_wabt(),
|
||||
appbase.MakeFlag_target(),
|
||||
appbase.MakeFlag_tags(),
|
||||
&cli.BoolFlag{
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"wa-lang.org/wa/internal/backends/compiler_wat"
|
||||
"wa-lang.org/wa/internal/config"
|
||||
"wa-lang.org/wa/internal/loader"
|
||||
"wa-lang.org/wa/internal/wabt"
|
||||
"wa-lang.org/wa/internal/wat/watutil"
|
||||
"wa-lang.org/wa/internal/wazero"
|
||||
"wa-lang.org/wa/waroot/src"
|
||||
)
|
||||
@@ -90,7 +90,7 @@ func runTest(cfg *config.Config, pkgpath, runPattern string, appArgs ...string)
|
||||
os.WriteFile("a.out.wat", []byte(watOutput), 0666)
|
||||
|
||||
// 编译为 wasm
|
||||
wasmBytes, err := wabt.Wat2Wasm([]byte(watOutput))
|
||||
wasmBytes, err := watutil.Wat2Wasm("a.out.wat", []byte(watOutput))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
// 版权 @2019 凹语言 作者。保留所有权利。
|
||||
|
||||
//go:build !wasm
|
||||
// +build !wasm
|
||||
|
||||
// 凹语言,The Wa Programming Language.
|
||||
package app
|
||||
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
// 版权 @2023 凹语言 作者。保留所有权利。
|
||||
|
||||
package wabt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"wa-lang.org/wa/internal/3rdparty/wabt-go"
|
||||
"wa-lang.org/wa/internal/config"
|
||||
"wa-lang.org/wa/internal/logger"
|
||||
)
|
||||
|
||||
var muWabt sync.Mutex
|
||||
var wat2wasmPath string
|
||||
|
||||
const Wat2WasmName = "wa.wat2wasm.exe"
|
||||
|
||||
func init() {
|
||||
// 1. exe 同级目录存在 wat2wasm ?
|
||||
wat2wasmPath = filepath.Join(curExeDir(), Wat2WasmName)
|
||||
if exeExists(wat2wasmPath) {
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 当前目录存在 wat2wasm ?
|
||||
cwd, _ := os.Getwd()
|
||||
wat2wasmPath = filepath.Join(cwd, Wat2WasmName)
|
||||
if exeExists(wat2wasmPath) {
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 本地系统存在 wat2wasm ?
|
||||
if s, _ := exec.LookPath(Wat2WasmName); s != "" {
|
||||
wat2wasmPath = s
|
||||
return
|
||||
}
|
||||
|
||||
// 4. 查找C盘根目录(为了简化一些测试环境)
|
||||
if runtime.GOOS == "windows" {
|
||||
wat2wasmPath = "c:/" + Wat2WasmName
|
||||
if exeExists(wat2wasmPath) {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
wat2wasmPath = "/usr/local/bin/" + Wat2WasmName
|
||||
if exeExists(wat2wasmPath) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 5. wat2wasm 安装到 exe 所在目录 ?
|
||||
{
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
wat2wasmPath = filepath.Join(curExeDir(), Wat2WasmName)
|
||||
if err := os.WriteFile(wat2wasmPath, []byte(wabt.Wat2wasm_macos), 0777); err != nil {
|
||||
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
|
||||
return
|
||||
}
|
||||
case "linux":
|
||||
wat2wasmPath = filepath.Join(curExeDir(), Wat2WasmName)
|
||||
if err := os.WriteFile(wat2wasmPath, []byte(wabt.Wat2wasm_ubuntu), 0777); err != nil {
|
||||
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
|
||||
return
|
||||
}
|
||||
case "windows":
|
||||
wat2wasmPath = filepath.Join(curExeDir(), Wat2WasmName)
|
||||
if err := os.WriteFile(wat2wasmPath, []byte(wabt.Wat2wasm_windows), 0777); err != nil {
|
||||
logger.Tracef(&config.EnableTrace_app, "install wat2wasm failed: %+v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Wat2Wasm(watBytes []byte) (wasmBytes []byte, err error) {
|
||||
muWabt.Lock()
|
||||
defer muWabt.Unlock()
|
||||
|
||||
if wat2wasmPath == "" {
|
||||
logger.Tracef(&config.EnableTrace_app, "wat2wasm not found")
|
||||
return nil, errors.New("wat2wasm not found")
|
||||
}
|
||||
|
||||
var bufStdout bytes.Buffer
|
||||
var bufStderr bytes.Buffer
|
||||
|
||||
// wat2wasm - --output=-
|
||||
var args = []string{"-", "--output=-"}
|
||||
if config.DebugMode {
|
||||
args = append(args, "--debug-names")
|
||||
}
|
||||
|
||||
cmd := exec.Command(wat2wasmPath, args...)
|
||||
cmd.Stdin = bytes.NewReader(watBytes)
|
||||
cmd.Stdout = &bufStdout
|
||||
cmd.Stderr = &bufStderr
|
||||
|
||||
err = cmd.Run()
|
||||
wasmBytes = bufStdout.Bytes()
|
||||
|
||||
if err != nil && bufStderr.Len() > 0 {
|
||||
err = errors.New(bufStderr.String())
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
// 版权 @2023 凹语言 作者。保留所有权利。
|
||||
|
||||
package wabt_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"wa-lang.org/wa/api"
|
||||
"wa-lang.org/wa/internal/wabt"
|
||||
)
|
||||
|
||||
func TestWat2Wasm(t *testing.T) {
|
||||
for i, tt := range watTests {
|
||||
_, err := wabt.Wat2Wasm([]byte(tt.watCode))
|
||||
if !tMatchErrMsg(err, tt.errMsg) {
|
||||
t.Fatalf("%d: check failed: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkWat2Wasm(b *testing.B) {
|
||||
wat := tBuildWat(t_hello_wa)
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if _, err := wabt.Wat2Wasm([]byte(wat)); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func tMatchErrMsg(err error, errMsg string) bool {
|
||||
if errMsg == "" {
|
||||
return err == nil
|
||||
}
|
||||
return strings.Contains(err.Error(), errMsg)
|
||||
}
|
||||
|
||||
func tBuildWat(waCode string) string {
|
||||
_, watBytes, err := api.BuildFile(api.DefaultConfig(), "main.wa", waCode)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(watBytes)
|
||||
}
|
||||
|
||||
var watTests = []struct {
|
||||
watCode string
|
||||
errMsg string
|
||||
}{
|
||||
{
|
||||
watCode: `(module)`,
|
||||
errMsg: "",
|
||||
},
|
||||
{
|
||||
watCode: `
|
||||
(module
|
||||
(func)
|
||||
(memory 1)
|
||||
)`,
|
||||
errMsg: "",
|
||||
},
|
||||
|
||||
{
|
||||
watCode: tBuildWat(t_hello_wa),
|
||||
errMsg: "",
|
||||
},
|
||||
}
|
||||
|
||||
const t_hello_wa = `
|
||||
// 版权 @2019 凹语言 作者。保留所有权利。
|
||||
|
||||
import "fmt"
|
||||
import "runtime"
|
||||
|
||||
global year: i32 = 2023
|
||||
|
||||
func main {
|
||||
println("你好,凹语言!", runtime.WAOS)
|
||||
println(add(40, 2), year)
|
||||
|
||||
fmt.Println("1+1 =", 1+1)
|
||||
}
|
||||
|
||||
func add(a: i32, b: i32) => i32 {
|
||||
return a+b
|
||||
}
|
||||
`
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"wa-lang.org/wa/api"
|
||||
"wa-lang.org/wa/internal/wabt"
|
||||
"wa-lang.org/wa/internal/wat/watutil"
|
||||
"wa-lang.org/wa/internal/wazero"
|
||||
)
|
||||
|
||||
@@ -54,7 +54,7 @@ func tBuildWasm(t *testing.T, waCode string) []byte {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wasmBytes, err := wabt.Wat2Wasm(watBytes)
|
||||
wasmBytes, err := watutil.Wat2Wasm("a.out.wat", watBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
# 版本日志
|
||||
|
||||
- (dev)
|
||||
- `wa` 命令瘦身 (TODO)
|
||||
- 删除 wabt 工具和 `-wabt` 参数 (TODO)
|
||||
- 去掉 `wa run-wasm` 子命令, 合并到 `wa run` 子命令 (TODO)
|
||||
- 完善 `wa lsp` 子命令 (TODO)
|
||||
- 回复 `brew` 打包支持
|
||||
- v0.14.0 (2024-08-01)
|
||||
- 后端重新实现 wat 到 wasm 的转换工具并默认启用; run/build 临时增加 `-wabt=false` 参数
|
||||
- 简化 `wa run`, 优先命令行执行, 并增加 `-web` 参数
|
||||
|
||||
Reference in New Issue
Block a user