-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglobal.go
42 lines (37 loc) · 931 Bytes
/
global.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//go:build js && wasm
package safejs
import (
"fmt"
"syscall/js"
)
// Global returns the JavaScript global object, usually "window" or "global".
func Global() Value {
return Safe(js.Global())
}
// MustGetGlobal fetches the given global, then verifies it is truthy. Panics on error or falsy values.
// This is intended for simple global variable initialization, like preparing classes for later instantiation.
//
// For example:
//
// var jsUint8Array = safejs.MustGetGlobal("Uint8Array")
func MustGetGlobal(property string) Value {
value, err := getGlobal(property)
if err != nil {
panic(err)
}
return value
}
func getGlobal(property string) (Value, error) {
value, err := Global().Get(property)
if err != nil {
return Value{}, err
}
truthy, err := value.Truthy()
if err != nil {
return Value{}, err
}
if !truthy {
return Value{}, fmt.Errorf("global %q is not defined", property)
}
return value, nil
}