Skip to content

Commit

Permalink
fixed #204 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Sep 7, 2024
1 parent 8414f83 commit 2ad27d9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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) ./...
Expand Down
1 change: 1 addition & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 14 additions & 3 deletions wasm/grol_wasm.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -88,15 +94,20 @@
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'));
resizeTextarea(document.getElementById('errors'));
}
document.addEventListener('DOMContentLoaded', (event) => {
document.getElementById('input').addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
if (e.key === 'Enter' && !isRunning) {
run();
}
});
Expand Down
3 changes: 1 addition & 2 deletions wasm/wasm_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -98,5 +97,5 @@ func main() {
if err != nil {
log.Critf("Error initializing extensions: %v", err)
}
<-done
select {}
}

0 comments on commit 2ad27d9

Please sign in to comment.