diff options
author | Richard Musiol <mail@richard-musiol.de> | 2018-06-25 12:30:31 +0200 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-06-25 17:04:01 +0000 |
commit | 5881d3048dd851d1e259ecab4e247f50cc10c6a8 (patch) | |
tree | 98054fefaf899b3dcd5b6b7e677073618887826f /src/syscall/js | |
parent | 9c35c1a50306f61949e0c76e5871b1033b3fd84d (diff) | |
download | go-git-5881d3048dd851d1e259ecab4e247f50cc10c6a8.tar.gz |
syscall/js: turn constant package vars into functions
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>
Diffstat (limited to 'src/syscall/js')
-rw-r--r-- | src/syscall/js/callback.go | 10 | ||||
-rw-r--r-- | src/syscall/js/js.go | 34 | ||||
-rw-r--r-- | src/syscall/js/js_test.go | 26 |
3 files changed, 38 insertions, 32 deletions
diff --git a/src/syscall/js/callback.go b/src/syscall/js/callback.go index 2c693240fa..cfcce693cb 100644 --- a/src/syscall/js/callback.go +++ b/src/syscall/js/callback.go @@ -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 } diff --git a/src/syscall/js/js.go b/src/syscall/js/js.go index cbd0730c64..93c3965246 100644 --- a/src/syscall/js/js.go +++ b/src/syscall/js/js.go @@ -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: diff --git a/src/syscall/js/js_test.go b/src/syscall/js/js_test.go index 53d21a3f4f..e5e950f3a3 100644 --- a/src/syscall/js/js_test.go +++ b/src/syscall/js/js_test.go @@ -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) } |