From 6e51d31524d6778564d8eaf79b94119f0a60b927 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 30 Jun 2018 00:02:15 +0900 Subject: [PATCH] Use new GopherWasm API (#634) I plan to merge this right after https://github.com/gopherjs/gopherwasm/pull/3 is merged. --- audio/mp3/decode_js.go | 10 ++++---- audio/mp3/float32_wasm.go | 2 +- ebitenutil/file_js.go | 2 +- examples/contextlost/main.go | 8 +++--- internal/clock/now_js.go | 2 +- internal/devicescale/impl_js.go | 2 +- internal/input/input_js.go | 10 ++++---- internal/opengl/context_js.go | 44 ++++++++++++++++----------------- internal/ui/ui_js.go | 6 ++--- internal/web/js.go | 4 +-- 10 files changed, 45 insertions(+), 45 deletions(-) diff --git a/audio/mp3/decode_js.go b/audio/mp3/decode_js.go index d22a67672..c92d5e85e 100644 --- a/audio/mp3/decode_js.go +++ b/audio/mp3/decode_js.go @@ -159,21 +159,21 @@ func Decode(context *audio.Context, src audio.ReadSeekCloser) (*Stream, error) { return s, nil } -var offlineAudioContextClass = js.Null +var offlineAudioContextClass = js.Null() func init() { - if klass := js.Global.Get("OfflineAudioContext"); klass != js.Undefined { + if klass := js.Global().Get("OfflineAudioContext"); klass != js.Undefined() { offlineAudioContextClass = klass return } - if klass := js.Global.Get("webkitOfflineAudioContext"); klass != js.Undefined { + if klass := js.Global().Get("webkitOfflineAudioContext"); klass != js.Undefined() { offlineAudioContextClass = klass return } } 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") } @@ -205,7 +205,7 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) { } }), js.NewCallback(func(args []js.Value) { 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) } else { // On Safari, error value might be null and it is needed to retry decoding diff --git a/audio/mp3/float32_wasm.go b/audio/mp3/float32_wasm.go index b68c89c63..d05d523f7 100644 --- a/audio/mp3/float32_wasm.go +++ b/audio/mp3/float32_wasm.go @@ -25,7 +25,7 @@ import ( func float32ArrayToSlice(arr js.Value) []float32 { bytes := make([]byte, arr.Length()*4) 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)) var f []float32 diff --git a/ebitenutil/file_js.go b/ebitenutil/file_js.go index bdb0b04c9..8e713290d 100644 --- a/ebitenutil/file_js.go +++ b/ebitenutil/file_js.go @@ -36,7 +36,7 @@ func OpenFile(path string) (ReadSeekCloser, error) { var err error var content js.Value ch := make(chan struct{}) - req := js.Global.Get("XMLHttpRequest").New() + req := js.Global().Get("XMLHttpRequest").New() req.Call("open", "GET", path, true) req.Set("responseType", "arraybuffer") req.Call("addEventListener", "load", func() { diff --git a/examples/contextlost/main.go b/examples/contextlost/main.go index 945ed79f1..4ad15612c 100644 --- a/examples/contextlost/main.go +++ b/examples/contextlost/main.go @@ -44,16 +44,16 @@ var ( ) func update(screen *ebiten.Image) error { - if inpututil.IsKeyJustPressed(ebiten.KeySpace) && js.Global != js.Null { - doc := js.Global.Get("document") + if inpututil.IsKeyJustPressed(ebiten.KeySpace) && js.Global() != js.Null() { + doc := js.Global().Get("document") canvas := doc.Call("getElementsByTagName", "canvas").Index(0) context := canvas.Call("getContext", "webgl") - if context == js.Null { + if context == js.Null() { context = canvas.Call("getContext", "experimental-webgl") } // Edge might not support the extension. See // 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") fmt.Println("Context Lost!") } else { diff --git a/internal/clock/now_js.go b/internal/clock/now_js.go index be95c995b..4d2d29801 100644 --- a/internal/clock/now_js.go +++ b/internal/clock/now_js.go @@ -24,5 +24,5 @@ import ( func now() int64 { // 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)) } diff --git a/internal/devicescale/impl_js.go b/internal/devicescale/impl_js.go index c0566b2e8..8305fcbed 100644 --- a/internal/devicescale/impl_js.go +++ b/internal/devicescale/impl_js.go @@ -21,7 +21,7 @@ import ( ) func impl() float64 { - ratio := js.Global.Get("window").Get("devicePixelRatio").Float() + ratio := js.Global().Get("window").Get("devicePixelRatio").Float() if ratio == 0 { ratio = 1 } diff --git a/internal/input/input_js.go b/internal/input/input_js.go index 3e2e1a15c..b2c46c685 100644 --- a/internal/input/input_js.go +++ b/internal/input/input_js.go @@ -143,8 +143,8 @@ func (i *Input) setMouseCursor(x, y int) { } func (i *Input) UpdateGamepads() { - nav := js.Global.Get("navigator") - if nav.Get("getGamepads") == js.Undefined { + nav := js.Global().Get("navigator") + if nav.Get("getGamepads") == js.Undefined() { return } gamepads := nav.Call("getGamepads") @@ -152,7 +152,7 @@ func (i *Input) UpdateGamepads() { for id := 0; id < l; id++ { i.gamepads[id].valid = false gamepad := gamepads.Index(id) - if gamepad == js.Undefined || gamepad == js.Null { + if gamepad == js.Undefined() || gamepad == js.Null() { continue } i.gamepads[id].valid = true @@ -183,7 +183,7 @@ func (i *Input) UpdateGamepads() { func OnKeyDown(e js.Value) { c := e.Get("code") - if c == js.Undefined { + if c == js.Undefined() { code := e.Get("keyCode").Int() if keyCodeToKeyEdge[code] == KeyUp || keyCodeToKeyEdge[code] == KeyDown || @@ -215,7 +215,7 @@ func OnKeyPress(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. code := e.Get("keyCode").Int() theInput.keyUpEdge(code) diff --git a/internal/opengl/context_js.go b/internal/opengl/context_js.go index 27e686e94..9610e224d 100644 --- a/internal/opengl/context_js.go +++ b/internal/opengl/context_js.go @@ -38,7 +38,7 @@ type ( } ) -var InvalidTexture = Texture(js.Null) +var InvalidTexture = Texture(js.Null()) func getProgramID(p Program) programID { return p.id @@ -69,7 +69,7 @@ var ( func init() { // 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()) FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int()) ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int()) @@ -116,19 +116,19 @@ type context struct { } 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") } // TODO: Define id? - canvas := js.Global.Get("document").Call("querySelector", "canvas") - attr := js.Global.Get("Object").New() + canvas := js.Global().Get("document").Call("querySelector", "canvas") + attr := js.Global().Get("Object").New() attr.Set("alpha", true) attr.Set("premultipliedAlpha", true) gl := canvas.Call("getContext", "webgl", attr) - if gl == js.Null { + if gl == js.Null() { gl = canvas.Call("getContext", "experimental-webgl", attr) - if gl == js.Null { + if gl == js.Null() { return fmt.Errorf("opengl: getContext failed") } } @@ -138,9 +138,9 @@ func Init() error { // Getting an extension might fail after the context is lost, so // it is required to get the extension here. c.loseContext = gl.Call("getExtension", "WEBGL_lose_context") - if c.loseContext != js.Null { + if c.loseContext != js.Null() { // 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") })) } @@ -150,8 +150,8 @@ func Init() error { func (c *Context) Reset() error { c.locationCache = newLocationCache() - c.lastTexture = Texture(js.Null) - c.lastFramebuffer = Framebuffer(js.Null) + c.lastTexture = Texture(js.Null()) + c.lastFramebuffer = Framebuffer(js.Null()) c.lastViewportWidth = 0 c.lastViewportHeight = 0 c.lastCompositeMode = CompositeModeUnknown @@ -176,8 +176,8 @@ func (c *Context) BlendFunc(mode CompositeMode) { func (c *Context) NewTexture(width, height int) (Texture, error) { gl := c.gl t := gl.Call("createTexture") - if t == js.Null { - return Texture(js.Null), errors.New("opengl: glGenTexture failed") + if t == js.Null() { + return Texture(js.Null()), errors.New("opengl: glGenTexture failed") } gl.Call("pixelStorei", unpackAlignment, 4) c.BindTexture(Texture(t)) @@ -224,7 +224,7 @@ func (c *Context) DeleteTexture(t Texture) { return } if c.lastTexture == t { - c.lastTexture = Texture(js.Null) + c.lastTexture = Texture(js.Null()) } 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) 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 @@ -269,7 +269,7 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) { // will be a default framebuffer. // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml if c.lastFramebuffer == f { - c.lastFramebuffer = Framebuffer(js.Null) + c.lastFramebuffer = Framebuffer(js.Null()) c.lastViewportWidth = 0 c.lastViewportHeight = 0 } @@ -279,8 +279,8 @@ func (c *Context) DeleteFramebuffer(f Framebuffer) { func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) { gl := c.gl s := gl.Call("createShader", int(shaderType)) - if s == js.Null { - return Shader(js.Null), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) + if s == js.Null() { + return Shader(js.Null()), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) } 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() { 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 } @@ -301,7 +301,7 @@ func (c *Context) DeleteShader(s Shader) { func (c *Context) NewProgram(shaders []Shader) (Program, error) { gl := c.gl v := gl.Call("createProgram") - if v == js.Null { + if v == js.Null() { return Program{}, errors.New("opengl: glCreateProgram failed") } @@ -352,7 +352,7 @@ func (c *Context) UniformFloat(p Program, location string, v float32) { } var ( - float32Array = js.Global.Get("Float32Array") + float32Array = js.Global().Get("Float32Array") ) func (c *Context) UniformFloats(p Program, location string, v []float32) { @@ -450,7 +450,7 @@ func (c *Context) IsContextLost() bool { } func (c *Context) RestoreContext() { - if c.loseContext != js.Null { + if c.loseContext != js.Null() { c.loseContext.Call("restoreContext") } } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 5c16f1050..4ee26ff20 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -49,8 +49,8 @@ var currentUI = &userInterface{ } var ( - window = js.Global.Get("window") - document = js.Global.Get("document") + window = js.Global().Get("window") + document = js.Global().Get("document") requestAnimationFrame = window.Get("requestAnimationFrame") ) @@ -226,7 +226,7 @@ func (u *userInterface) loop(g GraphicsContext) error { } func init() { - if document.Get("body") == js.Null { + if document.Get("body") == js.Null() { ch := make(chan struct{}) window.Call("addEventListener", "load", js.NewCallback(func([]js.Value) { close(ch) diff --git a/internal/web/js.go b/internal/web/js.go index feccb1f9c..ee33d14cf 100644 --- a/internal/web/js.go +++ b/internal/web/js.go @@ -27,7 +27,7 @@ func IsBrowser() 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") { return false } @@ -35,7 +35,7 @@ func IsIOSSafari() 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") { return false }