Skip to content

Commit a9dcbab

Browse files
neelanceRichard Musiol
authored and
Richard Musiol
committed
syscall/js: extend ValueOf to support arrays and objects
This commits adds []interface{} and map[string]interface{} as quick ways to create JavaScript arrays and objects. They correspond to the JavaScript notations [...] and {...}. A type alias can be used for a concise notation. Change-Id: I98bb08dbef1e0f3bd3d65c732d6b09e1520026ba Reviewed-on: https://go-review.googlesource.com/126855 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 6d0f757 commit a9dcbab

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/syscall/js/js.go

+26-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ var (
6464
valueGlobal = predefValue(5)
6565
memory = predefValue(6) // WebAssembly linear memory
6666
jsGo = predefValue(7) // instance of the Go class in JavaScript
67+
68+
objectConstructor = valueGlobal.Get("Object")
69+
arrayConstructor = valueGlobal.Get("Array")
6770
)
6871

6972
// Undefined returns the JavaScript value "undefined".
@@ -83,15 +86,17 @@ func Global() Value {
8386

8487
// ValueOf returns x as a JavaScript value:
8588
//
86-
// | Go | JavaScript |
87-
// | --------------------- | --------------------- |
88-
// | js.Value | [its value] |
89-
// | js.TypedArray | [typed array] |
90-
// | js.Callback | function |
91-
// | nil | null |
92-
// | bool | boolean |
93-
// | integers and floats | number |
94-
// | string | string |
89+
// | Go | JavaScript |
90+
// | ---------------------- | ---------------------- |
91+
// | js.Value | [its value] |
92+
// | js.TypedArray | typed array |
93+
// | js.Callback | function |
94+
// | nil | null |
95+
// | bool | boolean |
96+
// | integers and floats | number |
97+
// | string | string |
98+
// | []interface{} | new array |
99+
// | map[string]interface{} | new object |
95100
func ValueOf(x interface{}) Value {
96101
switch x := x.(type) {
97102
case Value:
@@ -138,6 +143,18 @@ func ValueOf(x interface{}) Value {
138143
return floatValue(x)
139144
case string:
140145
return makeValue(stringVal(x))
146+
case []interface{}:
147+
a := arrayConstructor.New(len(x))
148+
for i, s := range x {
149+
a.SetIndex(i, s)
150+
}
151+
return a
152+
case map[string]interface{}:
153+
o := objectConstructor.New()
154+
for k, v := range x {
155+
o.Set(k, v)
156+
}
157+
return o
141158
default:
142159
panic("ValueOf: invalid value")
143160
}

src/syscall/js/js_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,21 @@ func TestType(t *testing.T) {
254254
}
255255
}
256256

257+
type object = map[string]interface{}
258+
type array = []interface{}
259+
260+
func TestValueOf(t *testing.T) {
261+
a := js.ValueOf(array{0, array{0, 42, 0}, 0})
262+
if got := a.Index(1).Index(1).Int(); got != 42 {
263+
t.Errorf("got %v, want %v", got, 42)
264+
}
265+
266+
o := js.ValueOf(object{"x": object{"y": 42}})
267+
if got := o.Get("x").Get("y").Int(); got != 42 {
268+
t.Errorf("got %v, want %v", got, 42)
269+
}
270+
}
271+
257272
func TestCallback(t *testing.T) {
258273
c := make(chan struct{})
259274
cb := js.NewCallback(func(args []js.Value) {

0 commit comments

Comments
 (0)