WIP Game Settings Window, WASM Fixes, Sound FX
* Add sound effect and music support to Doodle. * Fix WASM build to use the 'null' sound driver for now. * Add a Settings button to the main menu; UI for it is WIP.
This commit is contained in:
parent
47cca8c7c6
commit
6d8aa387d7
|
@ -112,6 +112,10 @@ func (s *MainScene) Setup(d *Doodle) error {
|
||||||
Name: "Edit a Level",
|
Name: "Edit a Level",
|
||||||
Func: d.GotoLoadMenu,
|
Func: d.GotoLoadMenu,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "Settings",
|
||||||
|
Func: d.GotoSettingsMenu,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, button := range buttons {
|
for _, button := range buttons {
|
||||||
button := button
|
button := button
|
||||||
|
|
|
@ -72,6 +72,15 @@ func (d *Doodle) GotoPlayMenu() {
|
||||||
d.Goto(scene)
|
d.Goto(scene)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GotoSettingsMenu loads the settings screen.
|
||||||
|
func (d *Doodle) GotoSettingsMenu() {
|
||||||
|
log.Info("Loading the MenuScene to the Settings Menu")
|
||||||
|
scene := &MenuScene{
|
||||||
|
StartupMenu: "settings",
|
||||||
|
}
|
||||||
|
d.Goto(scene)
|
||||||
|
}
|
||||||
|
|
||||||
// Setup the scene.
|
// Setup the scene.
|
||||||
func (s *MenuScene) Setup(d *Doodle) error {
|
func (s *MenuScene) Setup(d *Doodle) error {
|
||||||
s.Supervisor = ui.NewSupervisor()
|
s.Supervisor = ui.NewSupervisor()
|
||||||
|
@ -98,6 +107,10 @@ func (s *MenuScene) Setup(d *Doodle) error {
|
||||||
if err := s.setupLoadWindow(d); err != nil {
|
if err := s.setupLoadWindow(d); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
case "settings":
|
||||||
|
if err := s.setupSettingsWindow(d); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
d.Flash("No Valid StartupMenu Given to MenuScene")
|
d.Flash("No Valid StartupMenu Given to MenuScene")
|
||||||
}
|
}
|
||||||
|
@ -171,6 +184,17 @@ func (s *MenuScene) setupLoadWindow(d *Doodle) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setupLoadWindow sets up the UI for the "New" window.
|
||||||
|
func (s *MenuScene) setupSettingsWindow(d *Doodle) error {
|
||||||
|
window := windows.NewSettingsWindow(windows.Settings{
|
||||||
|
Supervisor: s.Supervisor,
|
||||||
|
Engine: d.Engine,
|
||||||
|
})
|
||||||
|
window.SetButtons(0)
|
||||||
|
s.window = window
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Loop the editor scene.
|
// Loop the editor scene.
|
||||||
func (s *MenuScene) Loop(d *Doodle, ev *event.State) error {
|
func (s *MenuScene) Loop(d *Doodle, ev *event.State) error {
|
||||||
s.Supervisor.Loop(ev)
|
s.Supervisor.Loop(ev)
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
// Package sound provides audio functions for Doodle.
|
// Package sound manages music and sound effects.
|
||||||
package sound
|
package sound
|
||||||
|
|
||||||
import (
|
import "path/filepath"
|
||||||
"path/filepath"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"git.kirsle.net/apps/doodle/pkg/log"
|
// Package globals.
|
||||||
"git.kirsle.net/go/audio/sdl"
|
|
||||||
"github.com/veandco/go-sdl2/mix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Globals.
|
|
||||||
var (
|
var (
|
||||||
// If enabled is false, all sound functions are no-ops.
|
// If enabled is false, all sound functions are no-ops.
|
||||||
Enabled bool
|
Enabled bool
|
||||||
|
@ -18,97 +11,4 @@ var (
|
||||||
// Root folder on disk where sound and music files should live.
|
// Root folder on disk where sound and music files should live.
|
||||||
SoundRoot = filepath.Join("rtp", "sfx")
|
SoundRoot = filepath.Join("rtp", "sfx")
|
||||||
MusicRoot = filepath.Join("rtp", "music")
|
MusicRoot = filepath.Join("rtp", "music")
|
||||||
|
|
||||||
// Cache of loaded music and sound effects.
|
|
||||||
music = map[string]*sdl.Track{}
|
|
||||||
sounds = map[string]*sdl.Track{}
|
|
||||||
mu sync.RWMutex
|
|
||||||
|
|
||||||
engine *sdl.Engine
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Initialize SDL2 Audio at startup.
|
|
||||||
func init() {
|
|
||||||
eng, err := sdl.New(mix.INIT_MP3 | mix.INIT_OGG)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("sound.init(): error initializing SDL2 audio: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = eng.Setup()
|
|
||||||
if err != nil {
|
|
||||||
log.Error("sound.init(): error setting up SDL2 audio: %s", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
engine = eng
|
|
||||||
Enabled = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadMusic loads filename from the MusicRoot into the global music cache.
|
|
||||||
// If the music is already loaded, does nothing.
|
|
||||||
func LoadMusic(filename string) *sdl.Track {
|
|
||||||
if engine == nil || !Enabled {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the music is already loaded.
|
|
||||||
mu.RLock()
|
|
||||||
mus, ok := music[filename]
|
|
||||||
mu.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return mus
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the music in.
|
|
||||||
track, err := engine.LoadMusic(filepath.Join(MusicRoot, filename))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("sound.LoadMusic: failed to load file %s: %s", filename, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
mu.Lock()
|
|
||||||
music[filename] = &track
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
return &track
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadSound loads filename from the SoundRoot into the global SFX cache.
|
|
||||||
// If the sound is already loaded, does nothing.
|
|
||||||
func LoadSound(filename string) *sdl.Track {
|
|
||||||
if engine == nil || !Enabled {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the music is already loaded.
|
|
||||||
mu.RLock()
|
|
||||||
sfx, ok := sounds[filename]
|
|
||||||
mu.RUnlock()
|
|
||||||
if ok {
|
|
||||||
return sfx
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the sound in.
|
|
||||||
log.Info("Loading sound: %s", filename)
|
|
||||||
track, err := engine.LoadSound(filepath.Join(SoundRoot, filename))
|
|
||||||
if err != nil {
|
|
||||||
log.Error("sound.LoadSound: failed to load file %s: %s", filename, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
mu.Lock()
|
|
||||||
sounds[filename] = &track
|
|
||||||
mu.Unlock()
|
|
||||||
|
|
||||||
return &track
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlaySound plays the named sound.
|
|
||||||
func PlaySound(filename string) {
|
|
||||||
log.Debug("Play sound: %s", filename)
|
|
||||||
sound := LoadSound(filename)
|
|
||||||
if sound != nil {
|
|
||||||
sound.Play(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
109
pkg/sound/sound_sdl.go
Normal file
109
pkg/sound/sound_sdl.go
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
//+build !js,!wasm
|
||||||
|
|
||||||
|
package sound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"git.kirsle.net/apps/doodle/pkg/log"
|
||||||
|
"git.kirsle.net/go/audio"
|
||||||
|
"git.kirsle.net/go/audio/sdl"
|
||||||
|
"github.com/veandco/go-sdl2/mix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SDL engine globals.
|
||||||
|
var (
|
||||||
|
engine *sdl.Engine
|
||||||
|
|
||||||
|
// Cache of loaded music and sound effects.
|
||||||
|
music = map[string]*sdl.Track{}
|
||||||
|
sounds = map[string]*sdl.Track{}
|
||||||
|
mu sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize SDL2 Audio at startup.
|
||||||
|
func init() {
|
||||||
|
eng, err := sdl.New(mix.INIT_MP3 | mix.INIT_OGG)
|
||||||
|
if err != nil {
|
||||||
|
log.Error("sound.init(): error initializing SDL2 audio: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = eng.Setup()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("sound.init(): error setting up SDL2 audio: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
engine = eng
|
||||||
|
Enabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadMusic loads filename from the MusicRoot into the global music cache.
|
||||||
|
// If the music is already loaded, does nothing.
|
||||||
|
func LoadMusic(filename string) audio.Playable {
|
||||||
|
if engine == nil || !Enabled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the music is already loaded.
|
||||||
|
mu.RLock()
|
||||||
|
mus, ok := music[filename]
|
||||||
|
mu.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return mus
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the music in.
|
||||||
|
track, err := engine.LoadMusic(filepath.Join(MusicRoot, filename))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("sound.LoadMusic: failed to load file %s: %s", filename, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
music[filename] = &track
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
return &track
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadSound loads filename from the SoundRoot into the global SFX cache.
|
||||||
|
// If the sound is already loaded, does nothing.
|
||||||
|
func LoadSound(filename string) audio.Playable {
|
||||||
|
if engine == nil || !Enabled {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the music is already loaded.
|
||||||
|
mu.RLock()
|
||||||
|
sfx, ok := sounds[filename]
|
||||||
|
mu.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return sfx
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the sound in.
|
||||||
|
log.Info("Loading sound: %s", filename)
|
||||||
|
track, err := engine.LoadSound(filepath.Join(SoundRoot, filename))
|
||||||
|
if err != nil {
|
||||||
|
log.Error("sound.LoadSound: failed to load file %s: %s", filename, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
sounds[filename] = &track
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
return &track
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlaySound plays the named sound.
|
||||||
|
func PlaySound(filename string) {
|
||||||
|
log.Debug("Play sound: %s", filename)
|
||||||
|
sound := LoadSound(filename)
|
||||||
|
if sound != nil {
|
||||||
|
sound.Play(1)
|
||||||
|
}
|
||||||
|
}
|
32
pkg/sound/sound_wasm.go
Normal file
32
pkg/sound/sound_wasm.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
//+build js,wasm
|
||||||
|
|
||||||
|
package sound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.kirsle.net/go/audio"
|
||||||
|
"git.kirsle.net/go/audio/null"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Globals for WASM sound engine.
|
||||||
|
var (
|
||||||
|
engine *null.Engine
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
engine = null.New()
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadMusic loads filename from the MusicRoot into the global music cache.
|
||||||
|
// If the music is already loaded, does nothing.
|
||||||
|
func LoadMusic(filename string) audio.Playable {
|
||||||
|
return null.Playable{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadSound loads filename from the SoundRoot into the global SFX cache.
|
||||||
|
// If the sound is already loaded, does nothing.
|
||||||
|
func LoadSound(filename string) audio.Playable {
|
||||||
|
return null.Playable{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PlaySound plays the named sound.
|
||||||
|
func PlaySound(filename string) {}
|
94
pkg/windows/settings.go
Normal file
94
pkg/windows/settings.go
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
package windows
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
||||||
|
"git.kirsle.net/apps/doodle/pkg/branding"
|
||||||
|
"git.kirsle.net/apps/doodle/pkg/native"
|
||||||
|
"git.kirsle.net/go/render"
|
||||||
|
"git.kirsle.net/go/ui"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Settings window.
|
||||||
|
type Settings struct {
|
||||||
|
// Settings passed in by doodle
|
||||||
|
Supervisor *ui.Supervisor
|
||||||
|
Engine render.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSettingsWindow initializes the window.
|
||||||
|
func NewSettingsWindow(cfg Settings) *ui.Window {
|
||||||
|
window := ui.NewWindow("Game Settings")
|
||||||
|
window.SetButtons(ui.CloseButton)
|
||||||
|
window.Configure(ui.Config{
|
||||||
|
Width: 400,
|
||||||
|
Height: 170,
|
||||||
|
Background: render.Grey,
|
||||||
|
})
|
||||||
|
|
||||||
|
// {
|
||||||
|
// row := ui.NewFrame("Theme Frame")
|
||||||
|
// label := ui.NewLabel(ui.Label{
|
||||||
|
// Text: "Theme:",
|
||||||
|
// Font: balance.MenuFont,
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
|
||||||
|
text := ui.NewLabel(ui.Label{
|
||||||
|
Text: fmt.Sprintf("%s is a drawing-based maze game.\n\n"+
|
||||||
|
"Copyright © %s.\nAll rights reserved.\n\n"+
|
||||||
|
"Version %s",
|
||||||
|
branding.AppName,
|
||||||
|
branding.Copyright,
|
||||||
|
branding.Version,
|
||||||
|
),
|
||||||
|
})
|
||||||
|
window.Pack(text, ui.Pack{
|
||||||
|
Side: ui.N,
|
||||||
|
Padding: 8,
|
||||||
|
})
|
||||||
|
|
||||||
|
frame := ui.NewFrame("Button frame")
|
||||||
|
buttons := []struct {
|
||||||
|
label string
|
||||||
|
f func()
|
||||||
|
}{
|
||||||
|
{"Website", func() {
|
||||||
|
native.OpenURL(branding.Website)
|
||||||
|
}},
|
||||||
|
{"Open Source Licenses", func() {
|
||||||
|
// TODO: open file
|
||||||
|
native.OpenURL("./Open Source Licenses.md")
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
for _, button := range buttons {
|
||||||
|
button := button
|
||||||
|
|
||||||
|
btn := ui.NewButton(button.label, ui.NewLabel(ui.Label{
|
||||||
|
Text: button.label,
|
||||||
|
Font: balance.MenuFont,
|
||||||
|
}))
|
||||||
|
|
||||||
|
btn.Handle(ui.Click, func(ed ui.EventData) error {
|
||||||
|
button.f()
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
btn.Compute(cfg.Engine)
|
||||||
|
cfg.Supervisor.Add(btn)
|
||||||
|
|
||||||
|
frame.Pack(btn, ui.Pack{
|
||||||
|
Side: ui.W,
|
||||||
|
PadX: 4,
|
||||||
|
Expand: true,
|
||||||
|
Fill: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
window.Pack(frame, ui.Pack{
|
||||||
|
Side: ui.N,
|
||||||
|
Padding: 8,
|
||||||
|
})
|
||||||
|
|
||||||
|
return window
|
||||||
|
}
|
|
@ -30,6 +30,12 @@
|
||||||
global.fs = require("fs");
|
global.fs = require("fs");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const enosys = () => {
|
||||||
|
const err = new Error("not implemented");
|
||||||
|
err.code = "ENOSYS";
|
||||||
|
return err;
|
||||||
|
};
|
||||||
|
|
||||||
if (!global.fs) {
|
if (!global.fs) {
|
||||||
let outputBuf = "";
|
let outputBuf = "";
|
||||||
global.fs = {
|
global.fs = {
|
||||||
|
@ -45,27 +51,53 @@
|
||||||
},
|
},
|
||||||
write(fd, buf, offset, length, position, callback) {
|
write(fd, buf, offset, length, position, callback) {
|
||||||
if (offset !== 0 || length !== buf.length || position !== null) {
|
if (offset !== 0 || length !== buf.length || position !== null) {
|
||||||
throw new Error("not implemented");
|
callback(enosys());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const n = this.writeSync(fd, buf);
|
const n = this.writeSync(fd, buf);
|
||||||
callback(null, n);
|
callback(null, n);
|
||||||
},
|
},
|
||||||
open(path, flags, mode, callback) {
|
chmod(path, mode, callback) { callback(enosys()); },
|
||||||
const err = new Error("not implemented");
|
chown(path, uid, gid, callback) { callback(enosys()); },
|
||||||
err.code = "ENOSYS";
|
close(fd, callback) { callback(enosys()); },
|
||||||
callback(err);
|
fchmod(fd, mode, callback) { callback(enosys()); },
|
||||||
},
|
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
||||||
read(fd, buffer, offset, length, position, callback) {
|
fstat(fd, callback) { callback(enosys()); },
|
||||||
const err = new Error("not implemented");
|
fsync(fd, callback) { callback(null); },
|
||||||
err.code = "ENOSYS";
|
ftruncate(fd, length, callback) { callback(enosys()); },
|
||||||
callback(err);
|
lchown(path, uid, gid, callback) { callback(enosys()); },
|
||||||
},
|
link(path, link, callback) { callback(enosys()); },
|
||||||
fsync(fd, callback) {
|
lstat(path, callback) { callback(enosys()); },
|
||||||
callback(null);
|
mkdir(path, perm, callback) { callback(enosys()); },
|
||||||
},
|
open(path, flags, mode, callback) { callback(enosys()); },
|
||||||
|
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
||||||
|
readdir(path, callback) { callback(enosys()); },
|
||||||
|
readlink(path, callback) { callback(enosys()); },
|
||||||
|
rename(from, to, callback) { callback(enosys()); },
|
||||||
|
rmdir(path, callback) { callback(enosys()); },
|
||||||
|
stat(path, callback) { callback(enosys()); },
|
||||||
|
symlink(path, link, callback) { callback(enosys()); },
|
||||||
|
truncate(path, length, callback) { callback(enosys()); },
|
||||||
|
unlink(path, callback) { callback(enosys()); },
|
||||||
|
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!global.process) {
|
||||||
|
global.process = {
|
||||||
|
getuid() { return -1; },
|
||||||
|
getgid() { return -1; },
|
||||||
|
geteuid() { return -1; },
|
||||||
|
getegid() { return -1; },
|
||||||
|
getgroups() { throw enosys(); },
|
||||||
|
pid: -1,
|
||||||
|
ppid: -1,
|
||||||
|
umask() { throw enosys(); },
|
||||||
|
cwd() { throw enosys(); },
|
||||||
|
chdir() { throw enosys(); },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!global.crypto) {
|
if (!global.crypto) {
|
||||||
const nodeCrypto = require("crypto");
|
const nodeCrypto = require("crypto");
|
||||||
global.crypto = {
|
global.crypto = {
|
||||||
|
@ -113,24 +145,19 @@
|
||||||
this._scheduledTimeouts = new Map();
|
this._scheduledTimeouts = new Map();
|
||||||
this._nextCallbackTimeoutID = 1;
|
this._nextCallbackTimeoutID = 1;
|
||||||
|
|
||||||
const mem = () => {
|
|
||||||
// The buffer may change when requesting more memory.
|
|
||||||
return new DataView(this._inst.exports.mem.buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
const setInt64 = (addr, v) => {
|
const setInt64 = (addr, v) => {
|
||||||
mem().setUint32(addr + 0, v, true);
|
this.mem.setUint32(addr + 0, v, true);
|
||||||
mem().setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const getInt64 = (addr) => {
|
const getInt64 = (addr) => {
|
||||||
const low = mem().getUint32(addr + 0, true);
|
const low = this.mem.getUint32(addr + 0, true);
|
||||||
const high = mem().getInt32(addr + 4, true);
|
const high = this.mem.getInt32(addr + 4, true);
|
||||||
return low + high * 4294967296;
|
return low + high * 4294967296;
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadValue = (addr) => {
|
const loadValue = (addr) => {
|
||||||
const f = mem().getFloat64(addr, true);
|
const f = this.mem.getFloat64(addr, true);
|
||||||
if (f === 0) {
|
if (f === 0) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
@ -138,7 +165,7 @@
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
const id = mem().getUint32(addr, true);
|
const id = this.mem.getUint32(addr, true);
|
||||||
return this._values[id];
|
return this._values[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,57 +174,62 @@
|
||||||
|
|
||||||
if (typeof v === "number") {
|
if (typeof v === "number") {
|
||||||
if (isNaN(v)) {
|
if (isNaN(v)) {
|
||||||
mem().setUint32(addr + 4, nanHead, true);
|
this.mem.setUint32(addr + 4, nanHead, true);
|
||||||
mem().setUint32(addr, 0, true);
|
this.mem.setUint32(addr, 0, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (v === 0) {
|
if (v === 0) {
|
||||||
mem().setUint32(addr + 4, nanHead, true);
|
this.mem.setUint32(addr + 4, nanHead, true);
|
||||||
mem().setUint32(addr, 1, true);
|
this.mem.setUint32(addr, 1, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mem().setFloat64(addr, v, true);
|
this.mem.setFloat64(addr, v, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (v) {
|
switch (v) {
|
||||||
case undefined:
|
case undefined:
|
||||||
mem().setFloat64(addr, 0, true);
|
this.mem.setFloat64(addr, 0, true);
|
||||||
return;
|
return;
|
||||||
case null:
|
case null:
|
||||||
mem().setUint32(addr + 4, nanHead, true);
|
this.mem.setUint32(addr + 4, nanHead, true);
|
||||||
mem().setUint32(addr, 2, true);
|
this.mem.setUint32(addr, 2, true);
|
||||||
return;
|
return;
|
||||||
case true:
|
case true:
|
||||||
mem().setUint32(addr + 4, nanHead, true);
|
this.mem.setUint32(addr + 4, nanHead, true);
|
||||||
mem().setUint32(addr, 3, true);
|
this.mem.setUint32(addr, 3, true);
|
||||||
return;
|
return;
|
||||||
case false:
|
case false:
|
||||||
mem().setUint32(addr + 4, nanHead, true);
|
this.mem.setUint32(addr + 4, nanHead, true);
|
||||||
mem().setUint32(addr, 4, true);
|
this.mem.setUint32(addr, 4, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ref = this._refs.get(v);
|
let id = this._ids.get(v);
|
||||||
if (ref === undefined) {
|
if (id === undefined) {
|
||||||
ref = this._values.length;
|
id = this._idPool.pop();
|
||||||
this._values.push(v);
|
if (id === undefined) {
|
||||||
this._refs.set(v, ref);
|
id = this._values.length;
|
||||||
}
|
}
|
||||||
let typeFlag = 0;
|
this._values[id] = v;
|
||||||
|
this._goRefCounts[id] = 0;
|
||||||
|
this._ids.set(v, id);
|
||||||
|
}
|
||||||
|
this._goRefCounts[id]++;
|
||||||
|
let typeFlag = 1;
|
||||||
switch (typeof v) {
|
switch (typeof v) {
|
||||||
case "string":
|
case "string":
|
||||||
typeFlag = 1;
|
|
||||||
break;
|
|
||||||
case "symbol":
|
|
||||||
typeFlag = 2;
|
typeFlag = 2;
|
||||||
break;
|
break;
|
||||||
case "function":
|
case "symbol":
|
||||||
typeFlag = 3;
|
typeFlag = 3;
|
||||||
break;
|
break;
|
||||||
|
case "function":
|
||||||
|
typeFlag = 4;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
mem().setUint32(addr + 4, nanHead | typeFlag, true);
|
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
|
||||||
mem().setUint32(addr, ref, true);
|
this.mem.setUint32(addr, id, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadSlice = (addr) => {
|
const loadSlice = (addr) => {
|
||||||
|
@ -232,11 +264,13 @@
|
||||||
|
|
||||||
// func wasmExit(code int32)
|
// func wasmExit(code int32)
|
||||||
"runtime.wasmExit": (sp) => {
|
"runtime.wasmExit": (sp) => {
|
||||||
const code = mem().getInt32(sp + 8, true);
|
const code = this.mem.getInt32(sp + 8, true);
|
||||||
this.exited = true;
|
this.exited = true;
|
||||||
delete this._inst;
|
delete this._inst;
|
||||||
delete this._values;
|
delete this._values;
|
||||||
delete this._refs;
|
delete this._goRefCounts;
|
||||||
|
delete this._ids;
|
||||||
|
delete this._idPool;
|
||||||
this.exit(code);
|
this.exit(code);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -244,20 +278,25 @@
|
||||||
"runtime.wasmWrite": (sp) => {
|
"runtime.wasmWrite": (sp) => {
|
||||||
const fd = getInt64(sp + 8);
|
const fd = getInt64(sp + 8);
|
||||||
const p = getInt64(sp + 16);
|
const p = getInt64(sp + 16);
|
||||||
const n = mem().getInt32(sp + 24, true);
|
const n = this.mem.getInt32(sp + 24, true);
|
||||||
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func nanotime() int64
|
// func resetMemoryDataView()
|
||||||
"runtime.nanotime": (sp) => {
|
"runtime.resetMemoryDataView": (sp) => {
|
||||||
|
this.mem = new DataView(this._inst.exports.mem.buffer);
|
||||||
|
},
|
||||||
|
|
||||||
|
// func nanotime1() int64
|
||||||
|
"runtime.nanotime1": (sp) => {
|
||||||
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func walltime() (sec int64, nsec int32)
|
// func walltime1() (sec int64, nsec int32)
|
||||||
"runtime.walltime": (sp) => {
|
"runtime.walltime1": (sp) => {
|
||||||
const msec = (new Date).getTime();
|
const msec = (new Date).getTime();
|
||||||
setInt64(sp + 8, msec / 1000);
|
setInt64(sp + 8, msec / 1000);
|
||||||
mem().setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func scheduleTimeoutEvent(delay int64) int32
|
// func scheduleTimeoutEvent(delay int64) int32
|
||||||
|
@ -276,12 +315,12 @@
|
||||||
},
|
},
|
||||||
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
|
||||||
));
|
));
|
||||||
mem().setInt32(sp + 16, id, true);
|
this.mem.setInt32(sp + 16, id, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func clearTimeoutEvent(id int32)
|
// func clearTimeoutEvent(id int32)
|
||||||
"runtime.clearTimeoutEvent": (sp) => {
|
"runtime.clearTimeoutEvent": (sp) => {
|
||||||
const id = mem().getInt32(sp + 8, true);
|
const id = this.mem.getInt32(sp + 8, true);
|
||||||
clearTimeout(this._scheduledTimeouts.get(id));
|
clearTimeout(this._scheduledTimeouts.get(id));
|
||||||
this._scheduledTimeouts.delete(id);
|
this._scheduledTimeouts.delete(id);
|
||||||
},
|
},
|
||||||
|
@ -291,6 +330,18 @@
|
||||||
crypto.getRandomValues(loadSlice(sp + 8));
|
crypto.getRandomValues(loadSlice(sp + 8));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// func finalizeRef(v ref)
|
||||||
|
"syscall/js.finalizeRef": (sp) => {
|
||||||
|
const id = this.mem.getUint32(sp + 8, true);
|
||||||
|
this._goRefCounts[id]--;
|
||||||
|
if (this._goRefCounts[id] === 0) {
|
||||||
|
const v = this._values[id];
|
||||||
|
this._values[id] = null;
|
||||||
|
this._ids.delete(v);
|
||||||
|
this._idPool.push(id);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// func stringVal(value string) ref
|
// func stringVal(value string) ref
|
||||||
"syscall/js.stringVal": (sp) => {
|
"syscall/js.stringVal": (sp) => {
|
||||||
storeValue(sp + 24, loadString(sp + 8));
|
storeValue(sp + 24, loadString(sp + 8));
|
||||||
|
@ -308,6 +359,11 @@
|
||||||
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// func valueDelete(v ref, p string)
|
||||||
|
"syscall/js.valueDelete": (sp) => {
|
||||||
|
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
||||||
|
},
|
||||||
|
|
||||||
// func valueIndex(v ref, i int) ref
|
// func valueIndex(v ref, i int) ref
|
||||||
"syscall/js.valueIndex": (sp) => {
|
"syscall/js.valueIndex": (sp) => {
|
||||||
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
||||||
|
@ -327,10 +383,10 @@
|
||||||
const result = Reflect.apply(m, v, args);
|
const result = Reflect.apply(m, v, args);
|
||||||
sp = this._inst.exports.getsp(); // see comment above
|
sp = this._inst.exports.getsp(); // see comment above
|
||||||
storeValue(sp + 56, result);
|
storeValue(sp + 56, result);
|
||||||
mem().setUint8(sp + 64, 1);
|
this.mem.setUint8(sp + 64, 1);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
storeValue(sp + 56, err);
|
storeValue(sp + 56, err);
|
||||||
mem().setUint8(sp + 64, 0);
|
this.mem.setUint8(sp + 64, 0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -342,10 +398,10 @@
|
||||||
const result = Reflect.apply(v, undefined, args);
|
const result = Reflect.apply(v, undefined, args);
|
||||||
sp = this._inst.exports.getsp(); // see comment above
|
sp = this._inst.exports.getsp(); // see comment above
|
||||||
storeValue(sp + 40, result);
|
storeValue(sp + 40, result);
|
||||||
mem().setUint8(sp + 48, 1);
|
this.mem.setUint8(sp + 48, 1);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
storeValue(sp + 40, err);
|
storeValue(sp + 40, err);
|
||||||
mem().setUint8(sp + 48, 0);
|
this.mem.setUint8(sp + 48, 0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -357,10 +413,10 @@
|
||||||
const result = Reflect.construct(v, args);
|
const result = Reflect.construct(v, args);
|
||||||
sp = this._inst.exports.getsp(); // see comment above
|
sp = this._inst.exports.getsp(); // see comment above
|
||||||
storeValue(sp + 40, result);
|
storeValue(sp + 40, result);
|
||||||
mem().setUint8(sp + 48, 1);
|
this.mem.setUint8(sp + 48, 1);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
storeValue(sp + 40, err);
|
storeValue(sp + 40, err);
|
||||||
mem().setUint8(sp + 48, 0);
|
this.mem.setUint8(sp + 48, 0);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -384,7 +440,7 @@
|
||||||
|
|
||||||
// func valueInstanceOf(v ref, t ref) bool
|
// func valueInstanceOf(v ref, t ref) bool
|
||||||
"syscall/js.valueInstanceOf": (sp) => {
|
"syscall/js.valueInstanceOf": (sp) => {
|
||||||
mem().setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
|
this.mem.setUint8(sp + 24, loadValue(sp + 8) instanceof loadValue(sp + 16));
|
||||||
},
|
},
|
||||||
|
|
||||||
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
||||||
|
@ -392,13 +448,13 @@
|
||||||
const dst = loadSlice(sp + 8);
|
const dst = loadSlice(sp + 8);
|
||||||
const src = loadValue(sp + 32);
|
const src = loadValue(sp + 32);
|
||||||
if (!(src instanceof Uint8Array)) {
|
if (!(src instanceof Uint8Array)) {
|
||||||
mem().setUint8(sp + 48, 0);
|
this.mem.setUint8(sp + 48, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const toCopy = src.subarray(0, dst.length);
|
const toCopy = src.subarray(0, dst.length);
|
||||||
dst.set(toCopy);
|
dst.set(toCopy);
|
||||||
setInt64(sp + 40, toCopy.length);
|
setInt64(sp + 40, toCopy.length);
|
||||||
mem().setUint8(sp + 48, 1);
|
this.mem.setUint8(sp + 48, 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
||||||
|
@ -406,13 +462,13 @@
|
||||||
const dst = loadValue(sp + 8);
|
const dst = loadValue(sp + 8);
|
||||||
const src = loadSlice(sp + 16);
|
const src = loadSlice(sp + 16);
|
||||||
if (!(dst instanceof Uint8Array)) {
|
if (!(dst instanceof Uint8Array)) {
|
||||||
mem().setUint8(sp + 48, 0);
|
this.mem.setUint8(sp + 48, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const toCopy = src.subarray(0, dst.length);
|
const toCopy = src.subarray(0, dst.length);
|
||||||
dst.set(toCopy);
|
dst.set(toCopy);
|
||||||
setInt64(sp + 40, toCopy.length);
|
setInt64(sp + 40, toCopy.length);
|
||||||
mem().setUint8(sp + 48, 1);
|
this.mem.setUint8(sp + 48, 1);
|
||||||
},
|
},
|
||||||
|
|
||||||
"debug": (value) => {
|
"debug": (value) => {
|
||||||
|
@ -424,7 +480,8 @@
|
||||||
|
|
||||||
async run(instance) {
|
async run(instance) {
|
||||||
this._inst = instance;
|
this._inst = instance;
|
||||||
this._values = [ // TODO: garbage collection
|
this.mem = new DataView(this._inst.exports.mem.buffer);
|
||||||
|
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
||||||
NaN,
|
NaN,
|
||||||
0,
|
0,
|
||||||
null,
|
null,
|
||||||
|
@ -433,10 +490,10 @@
|
||||||
global,
|
global,
|
||||||
this,
|
this,
|
||||||
];
|
];
|
||||||
this._refs = new Map();
|
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
|
||||||
this.exited = false;
|
this._ids = new Map(); // mapping from JS values to reference ids
|
||||||
|
this._idPool = []; // unused ids that have been garbage collected
|
||||||
const mem = new DataView(this._inst.exports.mem.buffer)
|
this.exited = false; // whether the Go program has exited
|
||||||
|
|
||||||
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
||||||
let offset = 4096;
|
let offset = 4096;
|
||||||
|
@ -444,7 +501,7 @@
|
||||||
const strPtr = (str) => {
|
const strPtr = (str) => {
|
||||||
const ptr = offset;
|
const ptr = offset;
|
||||||
const bytes = encoder.encode(str + "\0");
|
const bytes = encoder.encode(str + "\0");
|
||||||
new Uint8Array(mem.buffer, offset, bytes.length).set(bytes);
|
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
|
||||||
offset += bytes.length;
|
offset += bytes.length;
|
||||||
if (offset % 8 !== 0) {
|
if (offset % 8 !== 0) {
|
||||||
offset += 8 - (offset % 8);
|
offset += 8 - (offset % 8);
|
||||||
|
@ -458,17 +515,18 @@
|
||||||
this.argv.forEach((arg) => {
|
this.argv.forEach((arg) => {
|
||||||
argvPtrs.push(strPtr(arg));
|
argvPtrs.push(strPtr(arg));
|
||||||
});
|
});
|
||||||
|
argvPtrs.push(0);
|
||||||
|
|
||||||
const keys = Object.keys(this.env).sort();
|
const keys = Object.keys(this.env).sort();
|
||||||
argvPtrs.push(keys.length);
|
|
||||||
keys.forEach((key) => {
|
keys.forEach((key) => {
|
||||||
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
|
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
|
||||||
});
|
});
|
||||||
|
argvPtrs.push(0);
|
||||||
|
|
||||||
const argv = offset;
|
const argv = offset;
|
||||||
argvPtrs.forEach((ptr) => {
|
argvPtrs.forEach((ptr) => {
|
||||||
mem.setUint32(offset, ptr, true);
|
this.mem.setUint32(offset, ptr, true);
|
||||||
mem.setUint32(offset + 4, 0, true);
|
this.mem.setUint32(offset + 4, 0, true);
|
||||||
offset += 8;
|
offset += 8;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user