From 1de9ad77d258636bdabc1db52e81ac607fcbb9b5 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Mon, 22 Jul 2024 11:20:36 -0700 Subject: [PATCH] Tinygo, take two (#55) * switching to tinygo (can't yet because of syscall/js.finalizeRef not implemented but can be used for ref/testing) * correct git describe * Add test and fix/workaround for https://github.com/tinygo-org/tinygo/issues/4353 * make tiny_test to create binary to debug for TINY_TEST_PACKAGE * fixed go-cmp with tinygo - well needs https://github.com/tinygo-org/tinygo/pull/4356 --- .gitignore | 1 + Makefile | 34 +++++++++++++++++++++++++--------- repl/repl_test.go | 15 +++++++++++++++ wasm/wasm_main.go | 6 ++++++ 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 6193e425..d16164e8 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ grol.wasm grol_tiny.wasm wasm/grol_tiny.wasm wasm/index.html +tiny_test diff --git a/Makefile b/Makefile index 32e99c02..db601a87 100644 --- a/Makefile +++ b/Makefile @@ -11,17 +11,31 @@ grol: Makefile *.go */*.go $(GEN) CGO_ENABLED=0 go build -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . ls -lh grol -tinygo: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html +tinygo-tests: Makefile *.go */*.go $(GEN) + CGO_ENABLED=0 tinygo test $(TINYGO_STACKS) -tags "$(GO_BUILD_TAGS)" -v ./... + +TINY_TEST_PACKAGE:=./ast +tiny_test: + # Make a binary that can be debugged, use + # make TINY_TEST_PACKAGE=. + # to set the package to test to . for instance + -rm -f $@ + CGO_ENABLED=0 tinygo test -tags "no_net,no_json" -c -o $@ -x $(TINY_TEST_PACKAGE) + ./tiny_test -test.v + +tinygo: Makefile *.go */*.go $(GEN) CGO_ENABLED=0 tinygo build -o grol.tiny -tags "$(GO_BUILD_TAGS)" . strip grol.tiny ls -lh grol.tiny +TINYGO_STACKS:=-stack-size=40mb + wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html wasm/grol_wasm.html # GOOS=wasip1 GOARCH=wasm go build -o grol.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . - GOOS=js GOARCH=wasm go build -o wasm/grol.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" ./wasm +# GOOS=js GOARCH=wasm go build -o wasm/grol.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" ./wasm # GOOS=wasip1 GOARCH=wasm tinygo build -target=wasi -no-debug -o grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . # Tiny go generates errors https://github.com/tinygo-org/tinygo/issues/1140 -# GOOS=js GOARCH=wasm tinygo build -no-debug -o wasm/test.wasm -tags "$(GO_BUILD_TAGS)" ./wasm + GOOS=js GOARCH=wasm tinygo build $(TINYGO_STACKS) -no-debug -o wasm/grol.wasm -tags "$(GO_BUILD_TAGS)" ./wasm echo 'Grol' > wasm/index.html cat wasm/grol_wasm.html >> wasm/index.html echo '' >> wasm/index.html @@ -31,17 +45,19 @@ wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html wasm/gro sleep 3 open http://localhost:8080/ -GIT_TAG=$(shell git describe --tags --abbrev=0) +GIT_TAG=$(shell git describe --tags --always --dirty) # used to copy to site a release version wasm-release: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html @echo "Building wasm release GIT_TAG=$(GIT_TAG)" - GOOS=js GOARCH=wasm go install -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" grol.io/grol/wasm@$(GIT_TAG) - mv "$(shell go env GOPATH)/bin/js_wasm/wasm" wasm/grol.wasm +# GOOS=js GOARCH=wasm go install -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" grol.io/grol/wasm@$(GIT_TAG) + # No buildinfo and no tinygo install so we set version old style: + GOOS=js GOARCH=wasm tinygo build $(TINYGO_STACKS) -o wasm/grol.wasm -no-debug -ldflags="-X main.TinyGoVersion=$(GIT_TAG)" -tags "$(GO_BUILD_TAGS)" ./wasm +# mv "$(shell go env GOPATH)/bin/js_wasm/wasm" wasm/grol.wasm ls -lh wasm/*.wasm wasm/wasm_exec.js: Makefile -# cp "$(shell tinygo env TINYGOROOT)/targets/wasm_exec.js" ./wasm/ - cp "$(shell tinygo env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/ + cp "$(shell tinygo env TINYGOROOT)/targets/wasm_exec.js" ./wasm/ +# cp "$(shell tinygo env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/ wasm/wasm_exec.html: cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.html" ./wasm/ @@ -76,4 +92,4 @@ lint: .golangci.yml .golangci.yml: Makefile curl -fsS -o .golangci.yml https://raw.githubusercontent.com/fortio/workflows/main/golangci.yml -.PHONY: all lint generate test clean run build wasm tinygo wasm-release +.PHONY: all lint generate test clean run build wasm tinygo wasm-release tiny_test tinygo-tests diff --git a/repl/repl_test.go b/repl/repl_test.go index b99ee3d1..66d3cea3 100644 --- a/repl/repl_test.go +++ b/repl/repl_test.go @@ -29,6 +29,21 @@ Factorial of 5 is 120` + " \n120\n" // there is an extra space before \n that vs } } +func TestEvalString50(t *testing.T) { + s := ` +fact=func(n) { // function + if (n<=1) { + return 1 + } + n*fact(n-1) +} +fact(50.)` + expected := "30414093201713376000000000000000000000000000000000000000000000000\n" + if got, errs := repl.EvalString(s); got != expected || len(errs) > 0 { + t.Errorf("EvalString() got %v\n---\n%s\n---want---\n%s\n---", errs, got, expected) + } +} + func TestEvalStringParsingError(t *testing.T) { s := `.` expected := "" diff --git a/wasm/wasm_main.go b/wasm/wasm_main.go index ab56135e..84f7b48f 100644 --- a/wasm/wasm_main.go +++ b/wasm/wasm_main.go @@ -8,6 +8,7 @@ Web assembly main for grol, exposing grol (repl.EvalString for now) to JS package main import ( + "runtime" "syscall/js" "fortio.org/cli" @@ -33,9 +34,14 @@ func jsEval(this js.Value, args []js.Value) interface{} { return result } +var TinyGoVersion string + func main() { cli.Main() // just to get version etc _, grolVersion, _ := version.FromBuildInfoPath("grol.io/grol") + if TinyGoVersion != "" { // tinygo doesn't have modules info in buildinfo nor tinygo install... + grolVersion = TinyGoVersion + " " + runtime.Compiler + runtime.Version() + " " + runtime.GOARCH + " " + runtime.GOOS + } log.Infof("Grol wasm main %s", grolVersion) done := make(chan struct{}, 0) global := js.Global()