Skip to content

Added implementation for a wasm backend #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

MeKo-Christian
Copy link

No description provided.

@gonutz
Copy link
Owner

gonutz commented Apr 20, 2025

Very nice, I just took it for a little test drive.
Already, the original semantics make less sense in the browser context. As always, I said

if window.WasKeyPressed(draw.KeyEscape) {
	window.Close()
}

which in your current implementation will stop accepting input but keep drawing the same state.

What do you think about this instead:

func (w *wasmWindow) Close() {
	js.Global().Get("window").Get("location").Call("reload")
}

which would reload the page on Close? I am not sure what makes the most sense, since we do not want to close the browser tab :-)

@gonutz
Copy link
Owner

gonutz commented Apr 20, 2025

Another thing: how do I play a sound file? I just tried

if window.WasKeyPressed(draw.KeySpace) {
	window.PlaySoundFile("prototype-demo.wav")
}

using your catchy demo tune but this crashes the program with this log:

wasm_exec.js:22 fatal error: all goroutines are asleep - deadlock!
wasm_exec.js:22 
wasm_exec.js:22 goroutine 1 [select (no cases)]:
wasm_exec.js:22 github.com/gonutz/prototype/draw.RunWindow({0x28c8d, 0x5}, 0x320, 0x258, 0x140a030)
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:162 +0x3b
wasm_exec.js:22 main.main()
wasm_exec.js:22 	C:/code/meko/testdrive/main.go:7 +0x7
wasm_exec.js:22 
wasm_exec.js:22 goroutine 6 [chan receive]:
wasm_exec.js:22 github.com/gonutz/prototype/draw.(*wasmWindow).loadSoundFile(0x144c000, {0x2af58, 0x12})
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:239 +0xf
wasm_exec.js:22 github.com/gonutz/prototype/draw.(*wasmWindow).PlaySoundFile(0x144c000, {0x2af58, 0x12})
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:674 +0x2
wasm_exec.js:22 main.main.func1({0x4a248, 0x144c000})
wasm_exec.js:22 	C:/code/meko/testdrive/main.go:13 +0xa
wasm_exec.js:22 github.com/gonutz/prototype/draw.RunWindow.func9({{}, 0x0, 0x0}, {0x140f2d0, 0x1, 0x1})
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:145 +0x7
wasm_exec.js:22 syscall/js.handleEvent()
wasm_exec.js:22 	C:/bin/go/src/syscall/js/func.go:100 +0x23
wasm_exec.js:101 exit code: 2
globalThis.Go.constructor.exit @ wasm_exec.js:101
runtime.wasmExit @ wasm_exec.js:232
$func795 @ main.wasm:0xc22a9
...

where line 239 in windows_wasm.go is this one:

func (w *wasmWindow) loadSoundFile(path string) (js.Value, error) {
	if buffer, ok := w.audioBuffers[path]; ok {
...
		}))
		return nil
	})

	fetchPromise.Call("then", then)
***	<-done // <- panics here   ***

	return result, err
}

@MeKo-Christian
Copy link
Author

if window.WasKeyPressed(draw.KeyEscape) {
	window.Close()
}

which in your current implementation will stop accepting input but keep drawing the same state.

As you mentioned, a close does actually not make much sense in the browser context, so maybe one could leave this just empty and it would be fine.

What do you think about this instead:

func (w *wasmWindow) Close() {
	js.Global().Get("window").Get("location").Call("reload")
}

which would reload the page on Close? I am not sure what makes the most sense, since we do not want to close the browser tab :-)

At least something. If you want, I can fix this in my PR. Then you can keep things clear, but it's up to you.

@MeKo-Christian
Copy link
Author

Another thing: how do I play a sound file? I just tried

if window.WasKeyPressed(draw.KeySpace) {
	window.PlaySoundFile("prototype-demo.wav")
}

using your catchy demo tune but this crashes the program with this log:

wasm_exec.js:22 fatal error: all goroutines are asleep - deadlock!
wasm_exec.js:22 
wasm_exec.js:22 goroutine 1 [select (no cases)]:
wasm_exec.js:22 github.com/gonutz/prototype/draw.RunWindow({0x28c8d, 0x5}, 0x320, 0x258, 0x140a030)
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:162 +0x3b
wasm_exec.js:22 main.main()
wasm_exec.js:22 	C:/code/meko/testdrive/main.go:7 +0x7
wasm_exec.js:22 
wasm_exec.js:22 goroutine 6 [chan receive]:
wasm_exec.js:22 github.com/gonutz/prototype/draw.(*wasmWindow).loadSoundFile(0x144c000, {0x2af58, 0x12})
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:239 +0xf
wasm_exec.js:22 github.com/gonutz/prototype/draw.(*wasmWindow).PlaySoundFile(0x144c000, {0x2af58, 0x12})
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:674 +0x2
wasm_exec.js:22 main.main.func1({0x4a248, 0x144c000})
wasm_exec.js:22 	C:/code/meko/testdrive/main.go:13 +0xa
wasm_exec.js:22 github.com/gonutz/prototype/draw.RunWindow.func9({{}, 0x0, 0x0}, {0x140f2d0, 0x1, 0x1})
wasm_exec.js:22 	C:/code/meko/prototype/draw/window_wasm.go:145 +0x7
wasm_exec.js:22 syscall/js.handleEvent()
wasm_exec.js:22 	C:/bin/go/src/syscall/js/func.go:100 +0x23
wasm_exec.js:101 exit code: 2
globalThis.Go.constructor.exit @ wasm_exec.js:101
runtime.wasmExit @ wasm_exec.js:232
$func795 @ main.wasm:0xc22a9
...

where line 239 in windows_wasm.go is this one:

func (w *wasmWindow) loadSoundFile(path string) (js.Value, error) {
	if buffer, ok := w.audioBuffers[path]; ok {
...
		}))
		return nil
	})

	fetchPromise.Call("then", then)
***	<-done // <- panics here   ***

	return result, err
}

This was on my todo list as well. Actually, that's why I initially wrote the demo in the first place. The audio code was written rather blindly and everything is more or less untested. Either you fix it on your own or I can have a look at it on the weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants