Skip to content

Commit

Permalink
Add a test demo: for debugging goroutine scheduling issues with igop …
Browse files Browse the repository at this point in the history
…on the web platform.
  • Loading branch information
JiepengTan committed Dec 11, 2024
1 parent 1ac28b8 commit ed72ea4
Show file tree
Hide file tree
Showing 44 changed files with 2,173 additions and 81 deletions.
3 changes: 2 additions & 1 deletion audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,15 @@ func (p *soundMgr) play(media Sound, wait, loop bool) (err error) {
return
}

var doneVal bool
var done chan bool
if wait {
done = make(chan bool, 1)
}
p.addPlayer(sp, done)
sp.Play()
if wait {
waitForChan(done)
waitForChan(done, &doneVal)
}
return
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/ispx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# We build / Copy-paste them before running project
main.wasm
wasm_exec.js
28 changes: 28 additions & 0 deletions cmd/ispx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# ispx for Go+ Builder

## Introduction

This package (ispx) is forked from [goplus/ispx](https://github.com/goplus/ispx) with some modification for fs, so that we can run a project packed in a zip file.

## Prepare

```sh
./build.sh
cp $GOROOT/misc/wasm/wasm_exec.js ./
```

Then put zip file of the project you want to test under directory `ispx/`, with name `test.zip`.

## Run

Serve directory `ispx/` with any HTTP server & open `index.html`

## Upgrade deps

If we want to upgrade deps like [spx](https://github.com/goplus/spx). First Modify `go.mod` to upgrade dependencies, then do

```sh
go mod tidy
go install github.com/goplus/igop/cmd/qexp@latest # `qexp` is required to do `go generate`
go generate # `qexp` will update `pkg/github.com/goplus/spx/export.go`, see detail in `main.go` (`//go:generate qexp ...`)
```
9 changes: 9 additions & 0 deletions cmd/ispx/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off
setlocal

set GOOS=js
set GOARCH=wasm

go build -tags canvas -o main.wasm main.go

endlocal
2 changes: 2 additions & 0 deletions cmd/ispx/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
GOOS=js GOARCH=wasm go build -tags canvas -o main.wasm main.go
54 changes: 54 additions & 0 deletions cmd/ispx/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module github.com/goplus/builder/ispx

go 1.21

require (
github.com/goplus/igop v0.27.1
github.com/goplus/reflectx v1.2.2
github.com/goplus/spx v1.0.1-0.20241029011511-845f2c0e2e74
github.com/hajimehoshi/ebiten/v2 v2.8.0-alpha.3
)

require (
github.com/ajstarks/svgo v0.0.0-20210927141636-6d70534b1098 // indirect
github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect
github.com/ebitengine/hideconsole v1.0.0 // indirect
github.com/ebitengine/oto/v3 v3.3.0-alpha.3 // indirect
github.com/ebitengine/purego v0.8.0-alpha.3 // indirect
github.com/esimov/stackblur-go v1.0.1-0.20190121110005-00e727e3c7a9 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/goplus/canvas v0.1.0 // indirect
github.com/goplus/gogen v1.15.2 // indirect
github.com/goplus/gop v1.2.6 // indirect
github.com/goplus/mod v0.13.10 // indirect
github.com/hajimehoshi/go-mp3 v0.3.4 // indirect
github.com/jezek/xgb v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/qiniu/audio v0.2.1 // indirect
github.com/qiniu/x v1.13.10 // indirect
github.com/srwiley/oksvg v0.0.0-20210519022825-9fc0c575d5fe // indirect
github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780 // indirect
github.com/timandy/routine v1.1.1 // indirect
github.com/visualfc/funcval v0.1.4 // indirect
github.com/visualfc/gid v0.1.0 // indirect
github.com/visualfc/goembed v0.3.2 // indirect
github.com/visualfc/xtype v0.2.0 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/mobile v0.0.0-20220518205345-8578da9835fd // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.23.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)

replace (
github.com/goplus/spx => ../../
github.com/hajimehoshi/oto => github.com/hajimehoshi/oto v1.0.1
github.com/srwiley/oksvg => github.com/qiniu/oksvg v0.2.0-no-charset
golang.org/x/image => golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
golang.org/x/mobile => golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5
golang.org/x/mod => golang.org/x/mod v0.5.1
)
174 changes: 174 additions & 0 deletions cmd/ispx/go.sum

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions cmd/ispx/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<h1>WASM data loading test</h1>
<iframe id="wasmFrame" src="runner.html" width="1600" height="900"></iframe>
<script>
"use strict";

const zipResp = fetch('/test.zip')

const iframe = document.getElementById('wasmFrame');
iframe.addEventListener('load', () => {
const wasmWindow = iframe.contentWindow;
window.wasmWindow = wasmWindow;
console.log('wasmWindow', wasmWindow);

wasmWindow.addEventListener('wasmReady', async () => {
const buffer = await (await (zipResp)).arrayBuffer();
console.log('buffer', buffer);
wasmWindow.startWithZipBuffer(buffer);
});
});
</script>
</body>

</html>
89 changes: 89 additions & 0 deletions cmd/ispx/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

//go:generate qexp -outdir pkg github.com/goplus/spx
//go:generate qexp -outdir pkg github.com/hajimehoshi/ebiten/v2

import (
"archive/zip"
"bytes"
"log"
"syscall/js"

_ "github.com/goplus/builder/ispx/pkg/github.com/goplus/spx"
_ "github.com/goplus/builder/ispx/pkg/github.com/hajimehoshi/ebiten/v2"
"github.com/goplus/builder/ispx/zipfs"
"github.com/goplus/igop"
"github.com/goplus/igop/gopbuild"
_ "github.com/goplus/igop/pkg/fmt"
_ "github.com/goplus/igop/pkg/math"
_ "github.com/goplus/reflectx/icall/icall8192"
spxfs "github.com/goplus/spx/fs"
)

var dataChannel = make(chan []byte)

func loadData(this js.Value, args []js.Value) interface{} {
inputArray := args[0]

// Convert Uint8Array to Go byte slice
length := inputArray.Get("length").Int()
goBytes := make([]byte, length)
js.CopyBytesToGo(goBytes, inputArray)

dataChannel <- goBytes
return nil
}

func main() {
js.Global().Set("goLoadData", js.FuncOf(loadData))

zipData := <-dataChannel

zipReader, err := zip.NewReader(bytes.NewReader(zipData), int64(len(zipData)))
if err != nil {
log.Fatalln("Failed to read zip data:", err)
}
fs := zipfs.NewZipFsFromReader(zipReader)
// Configure spx to load project files from zip-based file system.
spxfs.RegisterSchema("", func(path string) (spxfs.Dir, error) {
return fs.Chrooted(path), nil
})

var mode igop.Mode
ctx := igop.NewContext(mode)

// NOTE(everyone): Keep sync with the config in spx [gop.mod](https://github.com/goplus/spx/blob/main/gop.mod)
gopbuild.RegisterClassFileType(".spx", "Game", []*gopbuild.Class{{Ext: ".spx", Class: "SpriteImpl"}}, "github.com/goplus/spx")

// Register patch for spx to support functions with generic type like `Gopt_Game_Gopx_GetWidget`.
// See details in https://github.com/goplus/builder/issues/765#issuecomment-2313915805
err = gopbuild.RegisterPackagePatch(ctx, "github.com/goplus/spx", `
package spx
import (
. "github.com/goplus/spx"
)
func Gopt_Game_Gopx_GetWidget[T any](sg ShapeGetter, name string) *T {
widget := GetWidget_(sg, name)
if result, ok := widget.(interface{}).(*T); ok {
return result
} else {
panic("GetWidget: type mismatch")
}
}
`)
if err != nil {
log.Fatalln("Failed to register package patch:", err)
}

source, err := gopbuild.BuildFSDir(ctx, fs, "")
if err != nil {
log.Fatalln("Failed to build Go+ source:", err)
}
println("code", string(source))
code, err := ctx.RunFile("main.go", source, nil)
if err != nil {
log.Fatalln("Failed to run Go+ source:", err, " Code:", code)
}
}
Loading

0 comments on commit ed72ea4

Please sign in to comment.