The Callback and TypedArray are the only JavaScript types supported by
the library, thus they are special-cased in a type switch of ValueOf.
Instead, a Ref interface is defined to allow external wrapper types
to be handled properly by ValueOf.
Change-Id: I03240ba7ec46979336b88389a70b7bcac37fc715
GitHub-Last-Rev:
c8cf08d8ccfaab2af98df9eec8bc7b60dbce2c64
GitHub-Pull-Request: golang/go#28181
Reviewed-on: https://go-review.googlesource.com/c/141644
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Richard Musiol <neelance@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
nextCallbackID uint32 = 1
)
+var _ Wrapper = Callback{} // Callback must implement Wrapper
+
// Callback is a Go function that got wrapped for use as a JavaScript callback.
type Callback struct {
Value // the JavaScript function that queues the callback for execution
// nanHead are the upper 32 bits of a ref which are set if the value is not encoded as an IEEE 754 number (see above).
const nanHead = 0x7FF80000
+// Wrapper is implemented by types that are backed by a JavaScript value.
+type Wrapper interface {
+ // JSValue returns a JavaScript value associated with an object.
+ JSValue() Value
+}
+
// Value represents a JavaScript value. The zero value is the JavaScript value "undefined".
type Value struct {
ref ref
}
+// JSValue implements Wrapper interface.
+func (v Value) JSValue() Value {
+ return v
+}
+
func makeValue(v ref) Value {
return Value{ref: v}
}
// | map[string]interface{} | new object |
func ValueOf(x interface{}) Value {
switch x := x.(type) {
- case Value:
+ case Value: // should precede Wrapper to avoid a loop
return x
- case TypedArray:
- return x.Value
- case Callback:
- return x.Value
+ case Wrapper:
+ return x.JSValue()
case nil:
return valueNull
case bool:
float64Array = Global().Get("Float64Array")
)
+var _ Wrapper = TypedArray{} // TypedArray must implement Wrapper
+
// TypedArray represents a JavaScript typed array.
type TypedArray struct {
Value