Skip to content

Commit 8e4ec44

Browse files
committed
js: Interface() returns js.S / js.M / js.F (#137)
1 parent 7ebd216 commit 8e4ec44

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

compiler/prelude/jsmapping.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ var $internalize = function(v, t, recv) {
235235
case Float64Array:
236236
return new ($sliceType($Float64))(v);
237237
case Array:
238-
return $internalize(v, $sliceType($emptyInterface));
238+
return $internalize(v, $packages["github.com/gopherjs/gopherjs/js"].S);
239239
case Boolean:
240240
return new $Bool(!!v);
241241
case Date:
@@ -244,7 +244,7 @@ var $internalize = function(v, t, recv) {
244244
return new timePkg.Time(timePkg.Unix(new $Int64(0, 0), new $Int64(0, v.getTime() * 1000000)));
245245
}
246246
case Function:
247-
var funcType = $funcType([$sliceType($emptyInterface)], [$packages["github.com/gopherjs/gopherjs/js"].Object], true);
247+
var funcType = $packages["github.com/gopherjs/gopherjs/js"].F;
248248
return new funcType($internalize(v, funcType));
249249
case Number:
250250
return new $Float64(parseFloat(v));
@@ -254,7 +254,7 @@ var $internalize = function(v, t, recv) {
254254
if ($global.Node && v instanceof $global.Node) {
255255
return v;
256256
}
257-
var mapType = $mapType($String, $emptyInterface);
257+
var mapType = $packages["github.com/gopherjs/gopherjs/js"].M;
258258
return new mapType($internalize(v, mapType));
259259
}
260260
case $kindMap:

js/js.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
//
33
// Type conversions between Go types and JavaScript types are performed automatically according to the following table:
44
//
5-
// | Go types | Go interface type | JavaScript type |
6-
// | ------------------------------ | ------------------------------ | --------------- |
7-
// | bool | bool | Boolean |
8-
// | int?, uint?, float? | float64 | Number |
9-
// | string | string | String |
10-
// | [?]int8 | []int8 | Int8Array |
11-
// | [?]int16 | []int16 | Int16Array |
12-
// | [?]int32, [?]int | []int | Int32Array |
13-
// | [?]uint8 | []uint8 | Uint8Array |
14-
// | [?]uint16 | []uint16 | Uint16Array |
15-
// | [?]uint32, [?]uint, [?]uintptr | []uint | Uint32Array |
16-
// | [?]float32 | []float32 | Float32Array |
17-
// | [?]float64 | []float64 | Float64Array |
18-
// | all other slices and arrays | []interface{} | Array |
19-
// | maps, structs | map[string]interface{} | Object |
20-
// | functions | func(...interface{}) js.Object | Function |
21-
// | time.Time | time.Time | Date |
5+
// | Go types | Go interface type | JavaScript type |
6+
// | ------------------------------ | ----------------- | --------------- |
7+
// | bool | bool | Boolean |
8+
// | int?, uint?, float? | float64 | Number |
9+
// | string | string | String |
10+
// | [?]int8 | []int8 | Int8Array |
11+
// | [?]int16 | []int16 | Int16Array |
12+
// | [?]int32, [?]int | []int | Int32Array |
13+
// | [?]uint8 | []uint8 | Uint8Array |
14+
// | [?]uint16 | []uint16 | Uint16Array |
15+
// | [?]uint32, [?]uint, [?]uintptr | []uint | Uint32Array |
16+
// | [?]float32 | []float32 | Float32Array |
17+
// | [?]float64 | []float64 | Float64Array |
18+
// | all other slices and arrays | js.S | Array |
19+
// | maps, structs | js.M | Object |
20+
// | functions | js.F | Function |
21+
// | time.Time | time.Time | Date |
2222
//
2323
// The second column denotes the types that are used when converting to `interface{}`. An exception are DOM elements, those are kept with the js.Object type. Additionally, a pointer to a named type is passed to JavaScript as an object which has wrappers for the type's exported methods. This can be used to provide getter and setter methods for Go fields to JavaScript.
2424
package js
@@ -129,14 +129,20 @@ func Keys(o Object) []string {
129129
return s
130130
}
131131

132+
// S is a simple slice type. It is intended as a shorthand for JavaScript arrays (before conversion).
133+
type S []interface{}
134+
132135
// M is a simple map type. It is intended as a shorthand for JavaScript objects (before conversion).
133136
type M map[string]interface{}
134137

135-
// S is a simple slice type. It is intended as a shorthand for JavaScript arrays (before conversion).
136-
type S []interface{}
138+
// F is a simple func type.
139+
type F func(...interface{}) Object
137140

138141
func init() {
139-
// avoid dead code elimination of Error
142+
// avoid dead code elimination
140143
e := Error{}
141-
_ = e
144+
s := interface{}(S(nil))
145+
m := interface{}(M(nil))
146+
f := interface{}(F(nil))
147+
_, _, _, _ = e, s, m, f
142148
}

js/js_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func TestFunc(t *testing.T) {
212212
t.Fail()
213213
}
214214

215-
add := dummys.Get("add").Interface().(func(...interface{}) js.Object)
215+
add := dummys.Get("add").Interface().(js.F)
216216
var i int64 = 40
217217
if add(i, 2).Int() != 42 {
218218
t.Fail()

0 commit comments

Comments
 (0)