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>
Reader = &reader{}
}
-var jsCrypto = js.Global.Get("crypto")
+var jsCrypto = js.Global().Get("crypto")
// reader implements a pseudorandom generator
// using JavaScript crypto.getRandomValues method.
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"))
}
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)
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
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")
}
)
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
// 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 (
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 });
})
`)
-var makeEventCallbackHelper = Global.Call("eval", `
+var makeEventCallbackHelper = Global().Call("eval", `
(function(preventDefault, stopPropagation, stopImmediatePropagation, fn) {
return function(event) {
if (preventDefault) {
sleepUntilCallback()
for {
cb := pendingCallbacks.Call("shift")
- if cb == Undefined {
+ if cb == Undefined() {
break
}
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
}
}
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 {
case Callback:
return x.enqueueFn
case nil:
- return Null
+ return valueNull
case bool:
return makeValue(boolVal(x))
case int:
"testing"
)
-var dummys = js.Global.Call("eval", `({
+var dummys = js.Global().Call("eval", `({
someBool: true,
someString: "abc\u1234",
someInt: 42,
}
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")
}
}
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)
}
}
}
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)
}
}
c <- struct{}{}
})
defer cb.Close()
- js.Global.Call("setTimeout", cb, 0, 42)
+ js.Global().Call("setTimeout", cb, 0, 42)
<-c
}
})
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)
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)
}