Commit 4be3851d authored by Denys Smirnov's avatar Denys Smirnov Committed by Brad Fitzpatrick

syscall/js: add Wrapper interface to support external Value wrapper types

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: default avatarRichard Musiol <neelance@gmail.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 5538ecad
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment