From c09493cc02be18a857bf76c8ed1d51ada6561993 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Mon, 15 Jul 2024 17:18:24 -0700 Subject: [PATCH 01/12] add comment about EvalOne's in interactive mode --- repl/repl.go | 1 + 1 file changed, 1 insertion(+) diff --git a/repl/repl.go b/repl/repl.go index 9fc1240f..e123fc29 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -73,6 +73,7 @@ func Interactive(in io.Reader, out io.Writer, options Options) { return } l := prev + scanner.Text() + // errors are already logged and this is the only case that can get contNeeded (EOL instead of EOF mode) contNeeded, _ := EvalOne(s, macroState, l, out, options) if contNeeded { prev = l + "\n" From d8c5cfd94d1c406fce6b421eb66af470cf0966a9 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Mon, 15 Jul 2024 23:15:59 -0700 Subject: [PATCH 02/12] start of wasm support, logger to stdout==web browser console --- .gitignore | 4 ++++ Makefile | 15 ++++++++++++++- go.mod | 2 +- go.sum | 4 ++-- main.go | 6 ++++++ wasm/dev_server.go | 17 +++++++++++++++++ wasm/index.html | 13 +++++++++++++ 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 wasm/dev_server.go create mode 100644 wasm/index.html diff --git a/.gitignore b/.gitignore index d02fd796..0730b5c6 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,7 @@ grol __* # *_string.go dist/ +wasm/wasm_exec.js +wasm/wasm_exec.html +wasm/grol.wasm +wasm/test.wasm diff --git a/Makefile b/Makefile index 01854c5c..caa38c07 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,19 @@ grol: Makefile *.go */*.go $(GEN) CGO_ENABLED=0 go build -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . ls -l grol +wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html + GOOS=js GOARCH=wasm go build -o wasm/test.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . + -pkill wasm + go run ./wasm ./wasm & + sleep 3 + open http://localhost:8080/wasm_exec.html + +wasm/wasm_exec.js: + cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/ + +wasm/wasm_exec.html: + cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.html" ./wasm/ + test: grol CGO_ENABLED=0 go test -tags $(GO_BUILD_TAGS) ./... ./grol *.gr @@ -44,4 +57,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 +.PHONY: all lint generate test clean run build wasm diff --git a/go.mod b/go.mod index f4ff4a09..faab0b77 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.5 require ( fortio.org/cli v1.7.0 - fortio.org/log v1.14.0 + fortio.org/log v1.15.0-pre1 github.com/google/go-cmp v0.6.0 // only for tests ) diff --git a/go.sum b/go.sum index 07df51f6..a58312cb 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ fortio.org/cli v1.7.0 h1:w+uXZLGi4t3Vn/BvbeMuSw84Z1pvNPG9HqeGfpP68cc= fortio.org/cli v1.7.0/go.mod h1:s4vxWz7P7T4cYOWdMF0NA693Nu1gK9OW4KoDj54/Do4= -fortio.org/log v1.14.0 h1:ZkIc3Qqwfs9Dd931k07YzoC+bqCpJKEjVlZwxgXW3Nw= -fortio.org/log v1.14.0/go.mod h1:1tnXMqd5rZAgvSeHJkD2xXpyXRBzdeXtKLZuzNLIwtA= +fortio.org/log v1.15.0-pre1 h1:jg4lj4NMPqd+JFGCo4Y0wXHl2OPMk9BMxt0Dd01xtJw= +fortio.org/log v1.15.0-pre1/go.mod h1:1tnXMqd5rZAgvSeHJkD2xXpyXRBzdeXtKLZuzNLIwtA= fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0= fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U= diff --git a/main.go b/main.go index 17d025b5..bf82aec7 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ package main import ( "flag" "os" + "runtime" "fortio.org/cli" "fortio.org/log" @@ -12,12 +13,17 @@ import ( ) func main() { + if runtime.GOARCH == "wasm" { + log.SetOutput(os.Stdout) + log.Infof("wasm detected, adjusted logger to stdout.") + } showParse := flag.Bool("parse", false, "show parse tree") showEval := flag.Bool("eval", true, "show eval results") sharedState := flag.Bool("shared-state", false, "All files share same interpreter state (default is new state for each)") cli.ArgsHelp = "*.gr files to interpret or no arg for stdin repl..." cli.MaxArgs = -1 cli.Main() + log.Printf("grol %s - welcome!", cli.LongVersion) options := repl.Options{ ShowParse: *showParse, ShowEval: *showEval, diff --git a/wasm/dev_server.go b/wasm/dev_server.go new file mode 100644 index 00000000..259170eb --- /dev/null +++ b/wasm/dev_server.go @@ -0,0 +1,17 @@ +// Not to be used for anything but localhost testing +package main + +import ( + "fmt" + "log" + "net/http" + "os" +) + +func main() { + port := ":8080" + path := os.Args[1] + fmt.Println("Serving", path, "on", port) + fs := http.FileServer(http.Dir(path)) + log.Fatalf("%v", http.ListenAndServe(port, fs)) //nolint:gosec // just a test server +} diff --git a/wasm/index.html b/wasm/index.html new file mode 100644 index 00000000..d1f7d4d8 --- /dev/null +++ b/wasm/index.html @@ -0,0 +1,13 @@ + + + + + + + + From 9396c9b051e054a7783f75195aac0084f20911d2 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 16 Jul 2024 00:02:48 -0700 Subject: [PATCH 03/12] log now autoswitches to stdout if stderr is invalid --- go.mod | 2 +- go.sum | 4 ++-- main.go | 5 ----- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index faab0b77..eec8eef2 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.5 require ( fortio.org/cli v1.7.0 - fortio.org/log v1.15.0-pre1 + fortio.org/log v1.15.0-pre2 github.com/google/go-cmp v0.6.0 // only for tests ) diff --git a/go.sum b/go.sum index a58312cb..562d62f7 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ fortio.org/cli v1.7.0 h1:w+uXZLGi4t3Vn/BvbeMuSw84Z1pvNPG9HqeGfpP68cc= fortio.org/cli v1.7.0/go.mod h1:s4vxWz7P7T4cYOWdMF0NA693Nu1gK9OW4KoDj54/Do4= -fortio.org/log v1.15.0-pre1 h1:jg4lj4NMPqd+JFGCo4Y0wXHl2OPMk9BMxt0Dd01xtJw= -fortio.org/log v1.15.0-pre1/go.mod h1:1tnXMqd5rZAgvSeHJkD2xXpyXRBzdeXtKLZuzNLIwtA= +fortio.org/log v1.15.0-pre2 h1:lVlwcOSHMAva7MGn14ZXwHax8VB06GyQn8L9WTbQJPg= +fortio.org/log v1.15.0-pre2/go.mod h1:1tnXMqd5rZAgvSeHJkD2xXpyXRBzdeXtKLZuzNLIwtA= fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0= fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U= diff --git a/main.go b/main.go index bf82aec7..cd289fc8 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ package main import ( "flag" "os" - "runtime" "fortio.org/cli" "fortio.org/log" @@ -13,10 +12,6 @@ import ( ) func main() { - if runtime.GOARCH == "wasm" { - log.SetOutput(os.Stdout) - log.Infof("wasm detected, adjusted logger to stdout.") - } showParse := flag.Bool("parse", false, "show parse tree") showEval := flag.Bool("eval", true, "show eval results") sharedState := flag.Bool("shared-state", false, "All files share same interpreter state (default is new state for each)") From 3f91e59695344c88ba267a863a304afe61cf4425 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 16 Jul 2024 16:29:15 -0700 Subject: [PATCH 04/12] New tinygo target! 245K instead of 1.8M. Adding -c for equiv of bash -c. --- Makefile | 10 ++++++++-- go.mod | 3 ++- go.sum | 6 ++++-- main.go | 10 ++++++++++ tags.go | 4 ++-- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index caa38c07..b6cbc6b8 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,15 @@ GEN:=object/type_string.go parser/priority_string.go token/type_string.go grol: Makefile *.go */*.go $(GEN) CGO_ENABLED=0 go build -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . - ls -l grol + ls -lh grol + +tinygo: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html + CGO_ENABLED=0 tinygo build -o grol.tiny -tags "$(GO_BUILD_TAGS)" . + strip grol.tiny + ls -lh grol.tiny wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.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/test.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . -pkill wasm go run ./wasm ./wasm & @@ -57,4 +63,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 +.PHONY: all lint generate test clean run build wasm tinygo failing-tests diff --git a/go.mod b/go.mod index eec8eef2..1f68c62a 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,13 @@ go 1.22.5 require ( fortio.org/cli v1.7.0 - fortio.org/log v1.15.0-pre2 + fortio.org/log v1.15.0-pre4 github.com/google/go-cmp v0.6.0 // only for tests ) require ( fortio.org/struct2env v0.4.1 // indirect fortio.org/version v1.0.4 // indirect + github.com/kortschak/goroutine v1.1.2 // indirect golang.org/x/crypto/x509roots/fallback v0.0.0-20240626151235-a6a393ffd658 // indirect; not actually used with our build tags ) diff --git a/go.sum b/go.sum index 562d62f7..a4e84719 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,14 @@ fortio.org/cli v1.7.0 h1:w+uXZLGi4t3Vn/BvbeMuSw84Z1pvNPG9HqeGfpP68cc= fortio.org/cli v1.7.0/go.mod h1:s4vxWz7P7T4cYOWdMF0NA693Nu1gK9OW4KoDj54/Do4= -fortio.org/log v1.15.0-pre2 h1:lVlwcOSHMAva7MGn14ZXwHax8VB06GyQn8L9WTbQJPg= -fortio.org/log v1.15.0-pre2/go.mod h1:1tnXMqd5rZAgvSeHJkD2xXpyXRBzdeXtKLZuzNLIwtA= +fortio.org/log v1.15.0-pre4 h1:jAr6cn7AHhKBBdD6M2WlCqNWnePyCMp9A6++o0Zl0jY= +fortio.org/log v1.15.0-pre4/go.mod h1:t58Spg9njjymvRioh5F6qKGSupEsnMjXLGWIS1i3khE= fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0= fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U= fortio.org/version v1.0.4/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/kortschak/goroutine v1.1.2 h1:lhllcCuERxMIK5cYr8yohZZScL1na+JM5JYPRclWjck= +github.com/kortschak/goroutine v1.1.2/go.mod h1:zKpXs1FWN/6mXasDQzfl7g0LrGFIOiA6cLs9eXKyaMY= golang.org/x/crypto/x509roots/fallback v0.0.0-20240626151235-a6a393ffd658 h1:i7K6wQLN/0oxF7FT3tKkfMCstxoT4VGG36YIB9ZKLzI= golang.org/x/crypto/x509roots/fallback v0.0.0-20240626151235-a6a393ffd658/go.mod h1:kNa9WdvYnzFwC79zRpLRMJbdEFlhyM5RPFBBZp/wWH8= diff --git a/main.go b/main.go index cd289fc8..06363fd3 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" + "fmt" "os" "fortio.org/cli" @@ -12,6 +13,7 @@ import ( ) func main() { + commandFlag := flag.String("c", "", "command/inline script to run instead of interactive mode") showParse := flag.Bool("parse", false, "show parse tree") showEval := flag.Bool("eval", true, "show eval results") sharedState := flag.Bool("shared-state", false, "All files share same interpreter state (default is new state for each)") @@ -24,6 +26,14 @@ func main() { ShowEval: *showEval, } nArgs := len(flag.Args()) + if *commandFlag != "" { + res, errs := repl.EvalString(*commandFlag) + if len(errs) > 0 { + log.Errf("Errors: %v", errs) + } + fmt.Println(res) + return + } if nArgs == 0 { repl.Interactive(os.Stdin, os.Stdout, options) return diff --git a/tags.go b/tags.go index 83a49a2c..6afdbdcd 100644 --- a/tags.go +++ b/tags.go @@ -1,7 +1,7 @@ // Make go install fail if wrong tags are set -//go:build cgo || !no_net || !no_json -// +build cgo !no_net !no_json +//go:build (cgo && !tinygo) || !no_net || !no_json +// +build cgo,!tinygo !no_net !no_json package main From 8934a00bc6dff941d52476e2d55bee592467f74c Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 16 Jul 2024 18:33:06 -0700 Subject: [PATCH 05/12] build 2 variants of wasm --- .gitignore | 7 ++++++- Makefile | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0730b5c6..c88045cb 100644 --- a/.gitignore +++ b/.gitignore @@ -24,7 +24,6 @@ go.work.sum .golangci.yml gorepl -grol .DS_Store __* @@ -34,3 +33,9 @@ wasm/wasm_exec.js wasm/wasm_exec.html wasm/grol.wasm wasm/test.wasm +wasm/grol_tiny.wasm +grol +grol.tiny +grol.wasm +grol_tiny.wasm +wasm/grol_tiny.wasm diff --git a/Makefile b/Makefile index b6cbc6b8..42a2e1d9 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,8 @@ tinygo: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.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/test.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . + GOOS=wasip1 GOARCH=wasm tinygo build -o grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . + GOOS=js GOARCH=wasm tinygo build -o wasm/grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . -pkill wasm go run ./wasm ./wasm & sleep 3 From 6fac899b5089cad42360622037449338a9267f38 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 16 Jul 2024 19:54:37 -0700 Subject: [PATCH 06/12] GOOS=wasip1 doesn't work withou -target, -no-debug makes smaller binaries --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 42a2e1d9..d64adb65 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,9 @@ tinygo: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.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/test.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . - GOOS=wasip1 GOARCH=wasm tinygo build -o grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . - GOOS=js GOARCH=wasm tinygo build -o wasm/grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . + GOOS=wasip1 GOARCH=wasm tinygo build -target=wasi -no-debug -o grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . + GOOS=js GOARCH=wasm tinygo build -no-debug -o wasm/grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . + ls -lh *.wasm wasm/*.wasm -pkill wasm go run ./wasm ./wasm & sleep 3 From b970dbe64524ec2811223deff04420edf7cf4bfe Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 18 Jul 2024 18:18:53 -0700 Subject: [PATCH 07/12] release version of fortio.org/log v1.15.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 1f68c62a..57d76790 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.22.5 require ( fortio.org/cli v1.7.0 - fortio.org/log v1.15.0-pre4 + fortio.org/log v1.15.0 github.com/google/go-cmp v0.6.0 // only for tests ) diff --git a/go.sum b/go.sum index a4e84719..1d4bf9c6 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ fortio.org/cli v1.7.0 h1:w+uXZLGi4t3Vn/BvbeMuSw84Z1pvNPG9HqeGfpP68cc= fortio.org/cli v1.7.0/go.mod h1:s4vxWz7P7T4cYOWdMF0NA693Nu1gK9OW4KoDj54/Do4= -fortio.org/log v1.15.0-pre4 h1:jAr6cn7AHhKBBdD6M2WlCqNWnePyCMp9A6++o0Zl0jY= -fortio.org/log v1.15.0-pre4/go.mod h1:t58Spg9njjymvRioh5F6qKGSupEsnMjXLGWIS1i3khE= +fortio.org/log v1.15.0 h1:DRbZzgZH4av3ZPz6yIcvBwMy4NLH8a5iznRXXEegvJQ= +fortio.org/log v1.15.0/go.mod h1:t58Spg9njjymvRioh5F6qKGSupEsnMjXLGWIS1i3khE= fortio.org/struct2env v0.4.1 h1:rJludAMO5eBvpWplWEQNqoVDFZr4RWMQX7RUapgZyc0= fortio.org/struct2env v0.4.1/go.mod h1:lENUe70UwA1zDUCX+8AsO663QCFqYaprk5lnPhjD410= fortio.org/version v1.0.4 h1:FWUMpJ+hVTNc4RhvvOJzb0xesrlRmG/a+D6bjbQ4+5U= From ce513197a0e892d50d67a500a8daef997151d225 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 19 Jul 2024 13:38:44 -0700 Subject: [PATCH 08/12] finally working :tada: --- Makefile | 18 +++++---- apply.gr | 2 + pi.gr | 16 ++++++++ pi2.gr | 11 ++++++ wasm/dev_server.go | 3 ++ wasm/index.html | 93 ++++++++++++++++++++++++++++++++++++++++------ wasm/wasm_main.go | 44 ++++++++++++++++++++++ 7 files changed, 168 insertions(+), 19 deletions(-) create mode 100644 pi.gr create mode 100644 pi2.gr create mode 100644 wasm/wasm_main.go diff --git a/Makefile b/Makefile index d64adb65..34128c84 100644 --- a/Makefile +++ b/Makefile @@ -17,18 +17,20 @@ tinygo: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html ls -lh grol.tiny wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.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/test.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" . - GOOS=wasip1 GOARCH=wasm tinygo build -target=wasi -no-debug -o grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . - GOOS=js GOARCH=wasm tinygo build -no-debug -o wasm/grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" . - ls -lh *.wasm wasm/*.wasm +# 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=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 + -ls -lh wasm/*.wasm -pkill wasm go run ./wasm ./wasm & sleep 3 - open http://localhost:8080/wasm_exec.html + open http://localhost:8080/ -wasm/wasm_exec.js: - cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" ./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/ wasm/wasm_exec.html: cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.html" ./wasm/ diff --git a/apply.gr b/apply.gr index 2eb43635..66135179 100644 --- a/apply.gr +++ b/apply.gr @@ -12,3 +12,5 @@ apply = func(f, a) { apply(func(x) {2*x}, a) // ^^^ [2, 6, 10, 14] + +log("should be reached, won't") diff --git a/pi.gr b/pi.gr new file mode 100644 index 00000000..2982d9ca --- /dev/null +++ b/pi.gr @@ -0,0 +1,16 @@ +f = func(n, fac, dfac, exp, N) { + // log("Call n=", n, fac, dfac, exp) + if (n>N) { + [fac, dfac, exp] + } else { + dfac = 1.*dfac*(2*n - 1) + exp = exp * 2 + fac = fac * n + f(n+1, fac, dfac, exp, N) + } +} +N = 100 +r = f(1,1.,1.,1.,N) +// log("r", r) +approx = r[0] * r[2] / r[1] +approx * approx / N diff --git a/pi2.gr b/pi2.gr new file mode 100644 index 00000000..2590c660 --- /dev/null +++ b/pi2.gr @@ -0,0 +1,11 @@ +// Γ(x + 1/2) ~ Γ(x)x^(1/2) = (x-1)!√x +// Γ(x + 1/2) = (2x - 1)!! * 2^-x * √π +f=func(i,n, prod) { + //log(i, prod) + if (i==n+1) { + return 1./(prod*prod*n) + } + f(i+1, n, prod * ( 1 - 1./(2*i))) +} +n=200000 +f(1,n,1) diff --git a/wasm/dev_server.go b/wasm/dev_server.go index 259170eb..728f1fd4 100644 --- a/wasm/dev_server.go +++ b/wasm/dev_server.go @@ -1,3 +1,6 @@ +//go:build !wasm +// +build !wasm + // Not to be used for anything but localhost testing package main diff --git a/wasm/index.html b/wasm/index.html index d1f7d4d8..6e9fbd35 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -1,13 +1,84 @@ + - - - - - - + + + + Grol + + + + + + +
+ +
+
+ + +
+
+ + +
+
+ + +
+ + diff --git a/wasm/wasm_main.go b/wasm/wasm_main.go new file mode 100644 index 00000000..679300fd --- /dev/null +++ b/wasm/wasm_main.go @@ -0,0 +1,44 @@ +//go:build wasm +// +build wasm + +/* +Web assembly main for grol, exposing grol (repl.EvalString for now) to JS +*/ + +package main + +import ( + "syscall/js" + + "fortio.org/cli" + "fortio.org/log" + "fortio.org/version" + "grol.io/grol/repl" +) + +func jsEval(this js.Value, args []js.Value) interface{} { + if len(args) != 1 { + return "ERROR: number of arguments doesn't match" + } + input := args[0].String() + res, errs := repl.EvalString(input) + result := make(map[string]any) + result["result"] = res + // transfer errors to []any (!) + anyErrs := make([]any, len(errs)) + for i, v := range errs { + anyErrs[i] = v + } + result["errors"] = anyErrs + return result +} + +func main() { + cli.Main() // just to get version etc + _, grolVersion, _ := version.FromBuildInfoPath("grol.io/grol") + log.Infof("Grol wasm main %s", grolVersion) + done := make(chan struct{}, 0) + global := js.Global() + global.Set("grol", js.FuncOf(jsEval)) + <-done +} From 466deb217549f5c442a049d2127fb3ee4f8bc842 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 19 Jul 2024 14:04:09 -0700 Subject: [PATCH 09/12] proper error handling, auto eval on return, auto resize of output/err --- wasm/index.html | 104 +++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/wasm/index.html b/wasm/index.html index 6e9fbd35..7a44b01a 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -2,82 +2,106 @@ - - Grol + + Grol - - + -
- -
+ async function run() { + try { + // console.clear(); + console.log('In run') + go.run(inst); + var input = document.getElementById('input').value + // Call the grol function with the input + var output = grol(input); + console.log('Eval done:'); + console.log(output); + // Write the result to the output textarea + document.getElementById('output').value = output.result; + document.getElementById('errors').value = output.errors.join("\n"); + resizeTextarea(document.getElementById('output')); + resizeTextarea(document.getElementById('errors')); + } catch (e) { + console.error(e); + } finally { + inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance + } + } + document.addEventListener('DOMContentLoaded', (event) => { + document.getElementById('input').addEventListener('keydown', function (e) { + if (e.key === 'Enter') { + run(); + } + }); + }); + +
+
+ Hit enter or click +
- +
- +
From 98c850e10b59a8c3423115ee2df286b8e0a6cbd8 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 19 Jul 2024 14:19:19 -0700 Subject: [PATCH 10/12] start smaller for input --- wasm/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasm/index.html b/wasm/index.html index 7a44b01a..f0faaec1 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -90,7 +90,7 @@
- +
Hit enter or click From 6c2dc97f1c9291857029753443f02fe3f2312fbd Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 19 Jul 2024 14:44:44 -0700 Subject: [PATCH 11/12] added grolVersion, some fix to font size, new wasm-release target --- Makefile | 9 ++++++++- wasm/index.html | 5 ++++- wasm/wasm_main.go | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 34128c84..aa8fae63 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,13 @@ wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html sleep 3 open http://localhost:8080/ +GIT_TAG:=$(shell git describe --tags --abbrev=0) +# 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 BINPATH=. go install -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" grol.io/grol/wasm@$(GIT_TAG) + 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/ @@ -68,4 +75,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 failing-tests +.PHONY: all lint generate test clean run build wasm tinygo failing-tests wasm-release diff --git a/wasm/index.html b/wasm/index.html index f0faaec1..5a766bac 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -16,7 +16,8 @@ gap: 10px; } - textarea { + textarea, label, div { + font-size: 14px; width: 100%; box-sizing: border-box; } @@ -71,6 +72,7 @@ // Write the result to the output textarea document.getElementById('output').value = output.result; document.getElementById('errors').value = output.errors.join("\n"); + document.getElementById('version').innerHTML = "GROL " + grolVersion; resizeTextarea(document.getElementById('output')); resizeTextarea(document.getElementById('errors')); } catch (e) { @@ -103,6 +105,7 @@
+
GROL
diff --git a/wasm/wasm_main.go b/wasm/wasm_main.go index 679300fd..ab56135e 100644 --- a/wasm/wasm_main.go +++ b/wasm/wasm_main.go @@ -40,5 +40,6 @@ func main() { done := make(chan struct{}, 0) global := js.Global() global.Set("grol", js.FuncOf(jsEval)) + global.Set("grolVersion", js.ValueOf(grolVersion)) <-done } From b4cbc3d22f277b5cc2eb8a0a84249a1dff656d3f Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 19 Jul 2024 14:52:38 -0700 Subject: [PATCH 12/12] working wasm-release target (embbeded version) --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index aa8fae63..2a5711be 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,8 @@ GIT_TAG:=$(shell git describe --tags --abbrev=0) # 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 BINPATH=. go install -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" grol.io/grol/wasm@$(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 ls -lh wasm/*.wasm wasm/wasm_exec.js: Makefile @@ -65,7 +66,7 @@ token/type_string.go: token/token.go clean: - rm -f grol */*_string.go + rm -f grol */*_string.go *.wasm wasm/*.wasm wasm/wasm_exec.html wasm/wasm_exec.js build: grol