Use new GopherWasm API (#634)

I plan to merge this right after
https://github.com/gopherjs/gopherwasm/pull/3 is merged.
This commit is contained in:
Hajime Hoshi 2018-06-30 00:02:15 +09:00 committed by GitHub
parent abdf52bc74
commit 6e51d31524
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 45 additions and 45 deletions

View File

@ -159,21 +159,21 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) {
return s, nil return s, nil
} }
var offlineAudioContextClass = js.Null var offlineAudioContextClass = js.Null()
func init() { func init() {
if klass := js.Global.Get("OfflineAudioContext"); klass != js.Undefined { if klass := js.Global().Get("OfflineAudioContext"); klass != js.Undefined() {
offlineAudioContextClass = klass offlineAudioContextClass = klass
return return
} }
if klass := js.Global.Get("webkitOfflineAudioContext"); klass != js.Undefined { if klass := js.Global().Get("webkitOfflineAudioContext"); klass != js.Undefined() {
offlineAudioContextClass = klass offlineAudioContextClass = klass
return return
} }
} }
func decode(context *audio.Context, buf []byte, try int) (*Stream, error) { func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
if offlineAudioContextClass == js.Null { if offlineAudioContextClass == js.Null() {
return nil, errors.New("audio/mp3: OfflineAudioContext is not available") return nil, errors.New("audio/mp3: OfflineAudioContext is not available")
} }
@ -205,7 +205,7 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
} }
}), js.NewCallback(func(args []js.Value) { }), js.NewCallback(func(args []js.Value) {
err := args[0] err := args[0]
if err != js.Null || err != js.Undefined { if err != js.Null() || err != js.Undefined() {
ch <- fmt.Errorf("audio/mp3: decodeAudioData failed: %v", err) ch <- fmt.Errorf("audio/mp3: decodeAudioData failed: %v", err)
} else { } else {
// On Safari, error value might be null and it is needed to retry decoding // On Safari, error value might be null and it is needed to retry decoding

View File

@ -25,7 +25,7 @@ import (
func float32ArrayToSlice(arr js.Value) []float32 { func float32ArrayToSlice(arr js.Value) []float32 {
bytes := make([]byte, arr.Length()*4) bytes := make([]byte, arr.Length()*4)
buf := arr.Get("buffer").Call("slice", arr.Get("byteOffset"), arr.Get("byteOffset").Int()+arr.Get("byteLength").Int()) buf := arr.Get("buffer").Call("slice", arr.Get("byteOffset"), arr.Get("byteOffset").Int()+arr.Get("byteLength").Int())
js.ValueOf(bytes).Call("set", js.Global.Get("Uint8Array").New(buf)) js.ValueOf(bytes).Call("set", js.Global().Get("Uint8Array").New(buf))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&bytes)) bh := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
var f []float32 var f []float32

View File

@ -36,7 +36,7 @@ func OpenFile(path string) (ReadSeekCloser, error) {
var err error var err error
var content js.Value var content js.Value
ch := make(chan struct{}) ch := make(chan struct{})
req := js.Global.Get("XMLHttpRequest").New() req := js.Global().Get("XMLHttpRequest").New()
req.Call("open", "GET", path, true) req.Call("open", "GET", path, true)
req.Set("responseType", "arraybuffer") req.Set("responseType", "arraybuffer")
req.Call("addEventListener", "load", func() { req.Call("addEventListener", "load", func() {

View File

@ -44,16 +44,16 @@ var (
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if inpututil.IsKeyJustPressed(ebiten.KeySpace) && js.Global != js.Null { if inpututil.IsKeyJustPressed(ebiten.KeySpace) && js.Global() != js.Null() {
doc := js.Global.Get("document") doc := js.Global().Get("document")
canvas := doc.Call("getElementsByTagName", "canvas").Index(0) canvas := doc.Call("getElementsByTagName", "canvas").Index(0)
context := canvas.Call("getContext", "webgl") context := canvas.Call("getContext", "webgl")
if context == js.Null { if context == js.Null() {
context = canvas.Call("getContext", "experimental-webgl") context = canvas.Call("getContext", "experimental-webgl")
} }
// Edge might not support the extension. See // Edge might not support the extension. See
// https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_lose_context // https://developer.mozilla.org/en-US/docs/Web/API/WEBGL_lose_context
if ext := context.Call("getExtension", "WEBGL_lose_context"); ext != js.Null { if ext := context.Call("getExtension", "WEBGL_lose_context"); ext != js.Null() {
ext.Call("loseContext") ext.Call("loseContext")
fmt.Println("Context Lost!") fmt.Println("Context Lost!")
} else { } else {

View File

@ -24,5 +24,5 @@ import (
func now() int64 { func now() int64 {
// time.Now() is not reliable until GopherJS supports performance.now(). // time.Now() is not reliable until GopherJS supports performance.now().
return int64(js.Global.Get("performance").Call("now").Float() * float64(time.Millisecond)) return int64(js.Global().Get("performance").Call("now").Float() * float64(time.Millisecond))
} }

View File

@ -21,7 +21,7 @@ import (
) )
func impl() float64 { func impl() float64 {
ratio := js.Global.Get("window").Get("devicePixelRatio").Float() ratio := js.Global().Get("window").Get("devicePixelRatio").Float()
if ratio == 0 { if ratio == 0 {
ratio = 1 ratio = 1
} }

View File

@ -143,8 +143,8 @@ func (i *Input) setMouseCursor(x, y int) {
} }
func (i *Input) UpdateGamepads() { func (i *Input) UpdateGamepads() {
nav := js.Global.Get("navigator") nav := js.Global().Get("navigator")
if nav.Get("getGamepads") == js.Undefined { if nav.Get("getGamepads") == js.Undefined() {
return return
} }
gamepads := nav.Call("getGamepads") gamepads := nav.Call("getGamepads")
@ -152,7 +152,7 @@ func (i *Input) UpdateGamepads() {
for id := 0; id < l; id++ { for id := 0; id < l; id++ {
i.gamepads[id].valid = false i.gamepads[id].valid = false
gamepad := gamepads.Index(id) gamepad := gamepads.Index(id)
if gamepad == js.Undefined || gamepad == js.Null { if gamepad == js.Undefined() || gamepad == js.Null() {
continue continue
} }
i.gamepads[id].valid = true i.gamepads[id].valid = true
@ -183,7 +183,7 @@ func (i *Input) UpdateGamepads() {
func OnKeyDown(e js.Value) { func OnKeyDown(e js.Value) {
c := e.Get("code") c := e.Get("code")
if c == js.Undefined { if c == js.Undefined() {
code := e.Get("keyCode").Int() code := e.Get("keyCode").Int()
if keyCodeToKeyEdge[code] == KeyUp || if keyCodeToKeyEdge[code] == KeyUp ||
keyCodeToKeyEdge[code] == KeyDown || keyCodeToKeyEdge[code] == KeyDown ||
@ -215,7 +215,7 @@ func OnKeyPress(e js.Value) {
} }
func OnKeyUp(e js.Value) { func OnKeyUp(e js.Value) {
if e.Get("code") == js.Undefined { if e.Get("code") == js.Undefined() {
// Assume that UA is Edge. // Assume that UA is Edge.
code := e.Get("keyCode").Int() code := e.Get("keyCode").Int()
theInput.keyUpEdge(code) theInput.keyUpEdge(code)

View File

@ -38,7 +38,7 @@ type (
} }
) )
var InvalidTexture = Texture(js.Null) var InvalidTexture = Texture(js.Null())
func getProgramID(p Program) programID { func getProgramID(p Program) programID {
return p.id return p.id
@ -69,7 +69,7 @@ var (
func init() { func init() {
// Accessing the prototype is rquired on Safari. // Accessing the prototype is rquired on Safari.
c := js.Global.Get("WebGLRenderingContext").Get("prototype") c := js.Global().Get("WebGLRenderingContext").Get("prototype")
VertexShader = ShaderType(c.Get("VERTEX_SHADER").Int()) VertexShader = ShaderType(c.Get("VERTEX_SHADER").Int())
FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int()) FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int())
ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int()) ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int())
@ -116,19 +116,19 @@ type context struct {
} }
func Init() error { func Init() error {
if js.Global.Get("WebGLRenderingContext") == js.Undefined { if js.Global().Get("WebGLRenderingContext") == js.Undefined() {
return fmt.Errorf("opengl: WebGL is not supported") return fmt.Errorf("opengl: WebGL is not supported")
} }
// TODO: Define id? // TODO: Define id?
canvas := js.Global.Get("document").Call("querySelector", "canvas") canvas := js.Global().Get("document").Call("querySelector", "canvas")
attr := js.Global.Get("Object").New() attr := js.Global().Get("Object").New()
attr.Set("alpha", true) attr.Set("alpha", true)
attr.Set("premultipliedAlpha", true) attr.Set("premultipliedAlpha", true)
gl := canvas.Call("getContext", "webgl", attr) gl := canvas.Call("getContext", "webgl", attr)
if gl == js.Null { if gl == js.Null() {
gl = canvas.Call("getContext", "experimental-webgl", attr) gl = canvas.Call("getContext", "experimental-webgl", attr)
if gl == js.Null { if gl == js.Null() {
return fmt.Errorf("opengl: getContext failed") return fmt.Errorf("opengl: getContext failed")
} }
} }
@ -138,9 +138,9 @@ func Init() error {
// Getting an extension might fail after the context is lost, so // Getting an extension might fail after the context is lost, so
// it is required to get the extension here. // it is required to get the extension here.
c.loseContext = gl.Call("getExtension", "WEBGL_lose_context") c.loseContext = gl.Call("getExtension", "WEBGL_lose_context")
if c.loseContext != js.Null { if c.loseContext != js.Null() {
// This testing function name is temporary. // This testing function name is temporary.
js.Global.Set("_ebiten_loseContextForTesting", js.NewCallback(func([]js.Value) { js.Global().Set("_ebiten_loseContextForTesting", js.NewCallback(func([]js.Value) {
c.loseContext.Call("loseContext") c.loseContext.Call("loseContext")
})) }))
} }
@ -150,8 +150,8 @@ func Init() error {
func (c *Context) Reset() error { func (c *Context) Reset() error {
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = Texture(js.Null) c.lastTexture = Texture(js.Null())
c.lastFramebuffer = Framebuffer(js.Null) c.lastFramebuffer = Framebuffer(js.Null())
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = CompositeModeUnknown c.lastCompositeMode = CompositeModeUnknown
@ -176,8 +176,8 @@ func (c *Context) BlendFunc(mode CompositeMode) {
func (c *Context) NewTexture(width, height int) (Texture, error) { func (c *Context) NewTexture(width, height int) (Texture, error) {
gl := c.gl gl := c.gl
t := gl.Call("createTexture") t := gl.Call("createTexture")
if t == js.Null { if t == js.Null() {
return Texture(js.Null), errors.New("opengl: glGenTexture failed") return Texture(js.Null()), errors.New("opengl: glGenTexture failed")
} }
gl.Call("pixelStorei", unpackAlignment, 4) gl.Call("pixelStorei", unpackAlignment, 4)
c.BindTexture(Texture(t)) c.BindTexture(Texture(t))
@ -224,7 +224,7 @@ func (c *Context) DeleteTexture(t Texture) {
return return
} }
if c.lastTexture == t { if c.lastTexture == t {
c.lastTexture = Texture(js.Null) c.lastTexture = Texture(js.Null())
} }
gl.Call("deleteTexture", js.Value(t)) gl.Call("deleteTexture", js.Value(t))
} }
@ -249,7 +249,7 @@ func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
gl.Call("framebufferTexture2D", framebuffer, colorAttachment0, texture2d, js.Value(t), 0) gl.Call("framebufferTexture2D", framebuffer, colorAttachment0, texture2d, js.Value(t), 0)
if s := gl.Call("checkFramebufferStatus", framebuffer); s.Int() != framebufferComplete.Int() { if s := gl.Call("checkFramebufferStatus", framebuffer); s.Int() != framebufferComplete.Int() {
return Framebuffer(js.Null), errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int())) return Framebuffer(js.Null()), errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int()))
} }
return Framebuffer(f), nil return Framebuffer(f), nil
@ -269,7 +269,7 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) {
// will be a default framebuffer. // will be a default framebuffer.
// https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml
if c.lastFramebuffer == f { if c.lastFramebuffer == f {
c.lastFramebuffer = Framebuffer(js.Null) c.lastFramebuffer = Framebuffer(js.Null())
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
} }
@ -279,8 +279,8 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) {
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) { func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
gl := c.gl gl := c.gl
s := gl.Call("createShader", int(shaderType)) s := gl.Call("createShader", int(shaderType))
if s == js.Null { if s == js.Null() {
return Shader(js.Null), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) return Shader(js.Null()), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
} }
gl.Call("shaderSource", js.Value(s), source) gl.Call("shaderSource", js.Value(s), source)
@ -288,7 +288,7 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error
if !gl.Call("getShaderParameter", js.Value(s), compileStatus).Bool() { if !gl.Call("getShaderParameter", js.Value(s), compileStatus).Bool() {
log := gl.Call("getShaderInfoLog", js.Value(s)) log := gl.Call("getShaderInfoLog", js.Value(s))
return Shader(js.Null), fmt.Errorf("opengl: shader compile failed: %s", log) return Shader(js.Null()), fmt.Errorf("opengl: shader compile failed: %s", log)
} }
return Shader(s), nil return Shader(s), nil
} }
@ -301,7 +301,7 @@ func (c *Context) DeleteShader(s Shader) {
func (c *Context) NewProgram(shaders []Shader) (Program, error) { func (c *Context) NewProgram(shaders []Shader) (Program, error) {
gl := c.gl gl := c.gl
v := gl.Call("createProgram") v := gl.Call("createProgram")
if v == js.Null { if v == js.Null() {
return Program{}, errors.New("opengl: glCreateProgram failed") return Program{}, errors.New("opengl: glCreateProgram failed")
} }
@ -352,7 +352,7 @@ func (c *Context) UniformFloat(p Program, location string, v float32) {
} }
var ( var (
float32Array = js.Global.Get("Float32Array") float32Array = js.Global().Get("Float32Array")
) )
func (c *Context) UniformFloats(p Program, location string, v []float32) { func (c *Context) UniformFloats(p Program, location string, v []float32) {
@ -450,7 +450,7 @@ func (c *Context) IsContextLost() bool {
} }
func (c *Context) RestoreContext() { func (c *Context) RestoreContext() {
if c.loseContext != js.Null { if c.loseContext != js.Null() {
c.loseContext.Call("restoreContext") c.loseContext.Call("restoreContext")
} }
} }

View File

@ -49,8 +49,8 @@ var currentUI = &userInterface{
} }
var ( var (
window = js.Global.Get("window") window = js.Global().Get("window")
document = js.Global.Get("document") document = js.Global().Get("document")
requestAnimationFrame = window.Get("requestAnimationFrame") requestAnimationFrame = window.Get("requestAnimationFrame")
) )
@ -226,7 +226,7 @@ func (u *userInterface) loop(g GraphicsContext) error {
} }
func init() { func init() {
if document.Get("body") == js.Null { if document.Get("body") == js.Null() {
ch := make(chan struct{}) ch := make(chan struct{})
window.Call("addEventListener", "load", js.NewCallback(func([]js.Value) { window.Call("addEventListener", "load", js.NewCallback(func([]js.Value) {
close(ch) close(ch)

View File

@ -27,7 +27,7 @@ func IsBrowser() bool {
} }
func IsIOSSafari() bool { func IsIOSSafari() bool {
ua := js.Global.Get("navigator").Get("userAgent").String() ua := js.Global().Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "iPhone") { if !strings.Contains(ua, "iPhone") {
return false return false
} }
@ -35,7 +35,7 @@ func IsIOSSafari() bool {
} }
func IsAndroidChrome() bool { func IsAndroidChrome() bool {
ua := js.Global.Get("navigator").Get("userAgent").String() ua := js.Global().Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "Android") { if !strings.Contains(ua, "Android") {
return false return false
} }