mirror of
https://github.com/golang/go
synced 2024-11-07 12:26:16 -07:00
js: define Wrapper interface to support external wrappers for JS values
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 Wrapper interface is defined to allow external wrapper types to be handled properly by ValueOf.
This commit is contained in:
parent
be0f3c286b
commit
c8cf08d8cc
@ -20,6 +20,8 @@ var (
|
||||
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
|
||||
|
@ -26,11 +26,22 @@ type ref uint64
|
||||
// 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}
|
||||
}
|
||||
@ -105,12 +116,10 @@ func Global() Value {
|
||||
// | 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:
|
||||
|
@ -22,6 +22,8 @@ var (
|
||||
float64Array = Global().Get("Float64Array")
|
||||
)
|
||||
|
||||
var _ Wrapper = TypedArray{} // TypedArray must implement Wrapper
|
||||
|
||||
// TypedArray represents a JavaScript typed array.
|
||||
type TypedArray struct {
|
||||
Value
|
||||
|
Loading…
Reference in New Issue
Block a user