]> Cypherpunks repositories - gostls13.git/commitdiff
syscall/js: turn constant package vars into functions
authorRichard Musiol <mail@richard-musiol.de>
Mon, 25 Jun 2018 10:30:31 +0000 (12:30 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 25 Jun 2018 17:04:01 +0000 (17:04 +0000)
This is so the values can not be changed and the type is easy to see.

Requested on https://go-review.googlesource.com/c/go/+/120561.

Change-Id: If2ed48ca3ba8874074687bfb2375d2f5592e8e0d
Reviewed-on: https://go-review.googlesource.com/120564
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/crypto/rand/rand_js.go
src/net/http/roundtrip_js.go
src/syscall/fs_js.go
src/syscall/js/callback.go
src/syscall/js/js.go
src/syscall/js/js_test.go

index bc54ccd37d1c1b15ee5380ad7591808c921ae2ae..89247693a77cffedc7badda5172b85686fabac65 100644 (file)
@@ -12,7 +12,7 @@ func init() {
        Reader = &reader{}
 }
 
-var jsCrypto = js.Global.Get("crypto")
+var jsCrypto = js.Global().Get("crypto")
 
 // reader implements a pseudorandom generator
 // using JavaScript crypto.getRandomValues method.
index e99d418da43446194c3806c01951dfca1a317e7d..c183f87fff51c0f7a2a2b5f084df6480178bf3ad 100644 (file)
@@ -22,28 +22,28 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
        if useFakeNetwork() {
                return t.roundTrip(req)
        }
-       headers := js.Global.Get("Headers").New()
+       headers := js.Global().Get("Headers").New()
        for key, values := range req.Header {
                for _, value := range values {
                        headers.Call("append", key, value)
                }
        }
 
-       ac := js.Global.Get("AbortController")
-       if ac != js.Undefined {
+       ac := js.Global().Get("AbortController")
+       if ac != js.Undefined() {
                // Some browsers that support WASM don't necessarily support
                // the AbortController. See
                // https://developer.mozilla.org/en-US/docs/Web/API/AbortController#Browser_compatibility.
                ac = ac.New()
        }
 
-       opt := js.Global.Get("Object").New()
+       opt := js.Global().Get("Object").New()
        // See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
        // for options available.
        opt.Set("headers", headers)
        opt.Set("method", req.Method)
        opt.Set("credentials", "same-origin")
-       if ac != js.Undefined {
+       if ac != js.Undefined() {
                opt.Set("signal", ac.Get("signal"))
        }
 
@@ -62,7 +62,7 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
                req.Body.Close()
                opt.Set("body", body)
        }
-       respPromise := js.Global.Call("fetch", req.URL.String(), opt)
+       respPromise := js.Global().Call("fetch", req.URL.String(), opt)
        var (
                respCh = make(chan *Response, 1)
                errCh  = make(chan error, 1)
@@ -90,7 +90,7 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
 
                b := result.Get("body")
                var body io.ReadCloser
-               if b != js.Undefined {
+               if b != js.Undefined() {
                        body = &streamReader{stream: b.Call("getReader")}
                } else {
                        // Fall back to using ArrayBuffer
@@ -122,7 +122,7 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
        respPromise.Call("then", success, failure)
        select {
        case <-req.Context().Done():
-               if ac != js.Undefined {
+               if ac != js.Undefined() {
                        // Abort the Fetch request
                        ac.Call("abort")
                }
@@ -225,7 +225,7 @@ func (r *arrayReader) Read(p []byte) (n int, err error) {
                )
                success := js.NewCallback(func(args []js.Value) {
                        // Wrap the input ArrayBuffer with a Uint8Array
-                       uint8arrayWrapper := js.Global.Get("Uint8Array").New(args[0])
+                       uint8arrayWrapper := js.Global().Get("Uint8Array").New(args[0])
                        value := make([]byte, uint8arrayWrapper.Get("byteLength").Int())
                        js.ValueOf(value).Call("set", uint8arrayWrapper)
                        bCh <- value
index 141d46803c81e3108a773dd6f77c9fe7f2d106e2..64b7b8a1ad52687ecdb214513e57f0f9588f6826 100644 (file)
@@ -15,8 +15,8 @@ import (
 // Provided by package runtime.
 func now() (sec int64, nsec int32)
 
-var jsProcess = js.Global.Get("process")
-var jsFS = js.Global.Get("fs")
+var jsProcess = js.Global().Get("process")
+var jsFS = js.Global().Get("fs")
 var constants = jsFS.Get("constants")
 
 var (
index 2c693240fad488323b0f0070449e77104c4ebcd3..cfcce693cb2ce3a46e1e18a055979e2876a4bf0e 100644 (file)
@@ -8,9 +8,9 @@ package js
 
 import "sync"
 
-var pendingCallbacks = Global.Get("Array").New()
+var pendingCallbacks = Global().Get("Array").New()
 
-var makeCallbackHelper = Global.Call("eval", `
+var makeCallbackHelper = Global().Call("eval", `
        (function(id, pendingCallbacks, resolveCallbackPromise) {
                return function() {
                        pendingCallbacks.push({ id: id, args: arguments });
@@ -19,7 +19,7 @@ var makeCallbackHelper = Global.Call("eval", `
        })
 `)
 
-var makeEventCallbackHelper = Global.Call("eval", `
+var makeEventCallbackHelper = Global().Call("eval", `
        (function(preventDefault, stopPropagation, stopImmediatePropagation, fn) {
                return function(event) {
                        if (preventDefault) {
@@ -118,7 +118,7 @@ func callbackLoop() {
                sleepUntilCallback()
                for {
                        cb := pendingCallbacks.Call("shift")
-                       if cb == Undefined {
+                       if cb == Undefined() {
                                break
                        }
 
@@ -127,7 +127,7 @@ func callbackLoop() {
                        f, ok := callbacks[id]
                        callbacksMu.Unlock()
                        if !ok {
-                               Global.Get("console").Call("error", "call to closed callback")
+                               Global().Get("console").Call("error", "call to closed callback")
                                continue
                        }
 
index cbd0730c643da4ce712da59c24e4226d85b6acf9..93c39652466b3d7644cf07b8e79fa8feef8564d7 100644 (file)
@@ -39,23 +39,29 @@ func (e Error) Error() string {
 }
 
 var (
-       // Undefined is the JavaScript value "undefined". The zero Value equals to Undefined.
-       Undefined = makeValue(0)
-
-       // Null is the JavaScript value "null".
-       Null = makeValue(1)
+       valueUndefined         = makeValue(0)
+       valueNull              = makeValue(1)
+       valueGlobal            = makeValue(2)
+       memory                 = makeValue(3) // WebAssembly linear memory
+       resolveCallbackPromise = makeValue(4) // function that the callback helper uses to resume the execution of Go's WebAssembly code
+)
 
-       // Global is the JavaScript global object, usually "window" or "global".
-       Global = makeValue(2)
+// Undefined returns the JavaScript value "undefined".
+func Undefined() Value {
+       return valueUndefined
+}
 
-       // memory is the WebAssembly linear memory.
-       memory = makeValue(3)
+// Null returns the JavaScript value "null".
+func Null() Value {
+       return valueNull
+}
 
-       // resolveCallbackPromise is a function that the callback helper uses to resume the execution of Go's WebAssembly code.
-       resolveCallbackPromise = makeValue(4)
-)
+// Global returns the JavaScript global object, usually "window" or "global".
+func Global() Value {
+       return valueGlobal
+}
 
-var uint8Array = Global.Get("Uint8Array")
+var uint8Array = valueGlobal.Get("Uint8Array")
 
 // ValueOf returns x as a JavaScript value.
 func ValueOf(x interface{}) Value {
@@ -65,7 +71,7 @@ func ValueOf(x interface{}) Value {
        case Callback:
                return x.enqueueFn
        case nil:
-               return Null
+               return valueNull
        case bool:
                return makeValue(boolVal(x))
        case int:
index 53d21a3f4f3e21c05efa7ee406b0e64670e2ec63..e5e950f3a322885ccab0191f46cb9b1e5b09f3a4 100644 (file)
@@ -12,7 +12,7 @@ import (
        "testing"
 )
 
-var dummys = js.Global.Call("eval", `({
+var dummys = js.Global().Call("eval", `({
        someBool: true,
        someString: "abc\u1234",
        someInt: 42,
@@ -90,16 +90,16 @@ func TestFloat(t *testing.T) {
 }
 
 func TestUndefined(t *testing.T) {
-       dummys.Set("test", js.Undefined)
-       if dummys == js.Undefined || dummys.Get("test") != js.Undefined || dummys.Get("xyz") != js.Undefined {
+       dummys.Set("test", js.Undefined())
+       if dummys == js.Undefined() || dummys.Get("test") != js.Undefined() || dummys.Get("xyz") != js.Undefined() {
                t.Errorf("js.Undefined expected")
        }
 }
 
 func TestNull(t *testing.T) {
        dummys.Set("test1", nil)
-       dummys.Set("test2", js.Null)
-       if dummys == js.Null || dummys.Get("test1") != js.Null || dummys.Get("test2") != js.Null {
+       dummys.Set("test2", js.Null())
+       if dummys == js.Null() || dummys.Get("test1") != js.Null() || dummys.Get("test2") != js.Null() {
                t.Errorf("js.Null expected")
        }
 }
@@ -128,7 +128,7 @@ func TestCall(t *testing.T) {
        if got := dummys.Call("add", i, 2).Int(); got != 42 {
                t.Errorf("got %#v, want %#v", got, 42)
        }
-       if got := dummys.Call("add", js.Global.Call("eval", "40"), 2).Int(); got != 42 {
+       if got := dummys.Call("add", js.Global().Call("eval", "40"), 2).Int(); got != 42 {
                t.Errorf("got %#v, want %#v", got, 42)
        }
 }
@@ -141,17 +141,17 @@ func TestInvoke(t *testing.T) {
 }
 
 func TestNew(t *testing.T) {
-       if got := js.Global.Get("Array").New(42).Length(); got != 42 {
+       if got := js.Global().Get("Array").New(42).Length(); got != 42 {
                t.Errorf("got %#v, want %#v", got, 42)
        }
 }
 
 func TestInstanceOf(t *testing.T) {
-       someArray := js.Global.Get("Array").New()
-       if got, want := someArray.InstanceOf(js.Global.Get("Array")), true; got != want {
+       someArray := js.Global().Get("Array").New()
+       if got, want := someArray.InstanceOf(js.Global().Get("Array")), true; got != want {
                t.Errorf("got %#v, want %#v", got, want)
        }
-       if got, want := someArray.InstanceOf(js.Global.Get("Function")), false; got != want {
+       if got, want := someArray.InstanceOf(js.Global().Get("Function")), false; got != want {
                t.Errorf("got %#v, want %#v", got, want)
        }
 }
@@ -165,7 +165,7 @@ func TestCallback(t *testing.T) {
                c <- struct{}{}
        })
        defer cb.Close()
-       js.Global.Call("setTimeout", cb, 0, 42)
+       js.Global().Call("setTimeout", cb, 0, 42)
        <-c
 }
 
@@ -186,7 +186,7 @@ func TestEventCallback(t *testing.T) {
                })
                defer cb.Close()
 
-               event := js.Global.Call("eval", fmt.Sprintf("({ called: false, %s: function() { this.called = true; } })", name))
+               event := js.Global().Call("eval", fmt.Sprintf("({ called: false, %s: function() { this.called = true; } })", name))
                js.ValueOf(cb).Invoke(event)
                if !event.Get("called").Bool() {
                        t.Errorf("%s not called", name)
@@ -202,5 +202,5 @@ func ExampleNewCallback() {
                fmt.Println("button clicked")
                cb.Close() // close the callback if the button will not be clicked again
        })
-       js.Global.Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)
+       js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)
 }