]> Cypherpunks repositories - gostls13.git/commitdiff
syscall/js: add Wrapper interface to support external Value wrapper types
authorDenys Smirnov <denis.smirnov.91@gmail.com>
Wed, 24 Oct 2018 20:09:00 +0000 (20:09 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 24 Oct 2018 20:37:54 +0000 (20:37 +0000)
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>
src/syscall/js/callback.go
src/syscall/js/js.go
src/syscall/js/typedarray.go

index 9d573074cbd3990de97c4f13bc368dd7a2640863..2801e00b68399f6d485982950066866825411051 100644 (file)
@@ -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
index 9d826c38864f49a58c1aadbed77f8b4326f5a8c2..dc741e79a7e42199466bd8748e78277cf1bf2136 100644 (file)
@@ -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:
index afa15488ec7a4919eacfa74cf5a6cb94701677ad..aa56cf69f3c5ed60c85e546cafa46b3550c0cfe9 100644 (file)
@@ -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