Skip to content

Commit

Permalink
Tinygo, take two (#55)
Browse files Browse the repository at this point in the history
* 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 tinygo-org/tinygo#4353

* make tiny_test to create binary to debug for TINY_TEST_PACKAGE

* fixed go-cmp with tinygo - well needs tinygo-org/tinygo#4356
  • Loading branch information
ldemailly authored Jul 22, 2024
1 parent c3afb9a commit 1de9ad7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ grol.wasm
grol_tiny.wasm
wasm/grol_tiny.wasm
wasm/index.html
tiny_test
34 changes: 25 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<!doctype html><html><head><meta charset="utf-8"><title>Grol</title></head>' > wasm/index.html
cat wasm/grol_wasm.html >> wasm/index.html
echo '</html>' >> wasm/index.html
Expand All @@ -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/
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down
6 changes: 6 additions & 0 deletions wasm/wasm_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down

0 comments on commit 1de9ad7

Please sign in to comment.