From 2ad27d9d1effb0eee002eba04c051f7f674b1e3f Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 6 Sep 2024 17:45:14 -0700 Subject: [PATCH] fixed #204 :tada: --- Makefile | 12 ++++++++---- repl/repl.go | 1 + wasm/grol_wasm.html | 17 ++++++++++++++--- wasm/wasm_main.go | 3 +-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index cb9217bb..5cda33b9 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,7 @@ 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 $(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 $(TINYGO_STACKS) -no-debug -o wasm/grol.wasm -tags "$(GO_BUILD_TAGS)" ./wasm @@ -50,11 +50,15 @@ wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html wasm/gro sleep 3 open http://localhost:8080/ + +#WASM_GO:=/opt/homebrew/Cellar/go/1.23.1/bin/go +WASM_GO:=go + 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) + GOOS=js GOARCH=wasm $(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 @@ -67,10 +71,10 @@ install: wasm/wasm_exec.js: Makefile # cp "$(shell tinygo env TINYGOROOT)/targets/wasm_exec.js" ./wasm/ - cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/ + cp "$(shell $(WASM_GO) env GOROOT)/misc/wasm/wasm_exec.js" ./wasm/ wasm/wasm_exec.html: - cp "$(shell go env GOROOT)/misc/wasm/wasm_exec.html" ./wasm/ + cp "$(shell $(WASM_GO) env GOROOT)/misc/wasm/wasm_exec.html" ./wasm/ test: grol CGO_ENABLED=0 go test -tags $(GO_BUILD_TAGS) ./... diff --git a/repl/repl.go b/repl/repl.go index c67cb89a..f4790a28 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -380,6 +380,7 @@ func EvalOne(ctx context.Context, s *eval.State, what string, out io.Writer, opt }() } s.SetContext(ctx, options.MaxDuration) + defer s.Cancel() continuation, errs, formatted = evalOne(s, what, out, options) return } diff --git a/wasm/grol_wasm.html b/wasm/grol_wasm.html index 2b330668..24a401ff 100644 --- a/wasm/grol_wasm.html +++ b/wasm/grol_wasm.html @@ -60,11 +60,15 @@ function formatError(error) { return `Error: ${error.message}`; } + let isRunning = false async function run() { + if (isRunning) return; // Prevent running multiple times concurrently + isRunning = true; + document.getElementById("runButton").disabled = true; // Disable button during execution try { // console.clear(); console.log('In run') - go.run(inst); + go.run(inst); var input = document.getElementById('input').value var compact = document.getElementById('compact').checked // Call the grol function with the input @@ -78,6 +82,8 @@ document.getElementById('errors').value = output.errors.join("\n"); if (output.image !== undefined) { document.getElementById('image').src = output.image; + } else { + document.getElementById('image').src = ""; } } else { document.getElementById('errors').value = "Unexpected runtime error, see JS console"; @@ -88,7 +94,12 @@ const formattedError = formatError(e); document.getElementById('errors').value = formattedError; } finally { - inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance + inst = await WebAssembly.instantiate(mod, go.importObject) + console.log('Instance reset:', inst) + if (isRunning) { + isRunning = false; // Allow running again after reset + document.getElementById("runButton").disabled = false; // Re-enable the button + } } resizeTextarea(document.getElementById('input')); resizeTextarea(document.getElementById('output')); @@ -96,7 +107,7 @@ } document.addEventListener('DOMContentLoaded', (event) => { document.getElementById('input').addEventListener('keydown', function (e) { - if (e.key === 'Enter') { + if (e.key === 'Enter' && !isRunning) { run(); } }); diff --git a/wasm/wasm_main.go b/wasm/wasm_main.go index 126c7999..c10242f8 100644 --- a/wasm/wasm_main.go +++ b/wasm/wasm_main.go @@ -88,7 +88,6 @@ func main() { } prev := debug.SetMemoryLimit(WasmMemLimit) log.Infof("Grol wasm main %s - prev memory limit %d now %d", grolVersion, prev, WasmMemLimit) - done := make(chan struct{}, 0) global := js.Global() global.Set("grol", js.FuncOf(jsEval)) global.Set("grolVersion", js.ValueOf(grolVersion)) @@ -98,5 +97,5 @@ func main() { if err != nil { log.Critf("Error initializing extensions: %v", err) } - <-done + select {} }