Skip to content

Commit 89e2069

Browse files
authored
Merge pull request #1164 from visualfc/reflect_set
compiler/natives/src/reflect: fix reflect.Value.Set kind Struct by flagIndir
2 parents 044c47a + afdb7e6 commit 89e2069

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

compiler/natives/src/reflect/reflect.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,12 +1436,10 @@ func (v Value) Set(x Value) {
14361436
x = x.assignTo("reflect.Set", v.typ, nil)
14371437
if v.flag&flagIndir != 0 {
14381438
switch v.typ.Kind() {
1439-
case Array:
1439+
case Array, Struct:
14401440
jsType(v.typ).Call("copy", js.InternalObject(v.ptr), js.InternalObject(x.ptr))
14411441
case Interface:
14421442
js.InternalObject(v.ptr).Call("$set", js.InternalObject(valueInterface(x, false)))
1443-
case Struct:
1444-
copyStruct(js.InternalObject(v.ptr), js.InternalObject(x.ptr), v.typ)
14451443
default:
14461444
js.InternalObject(v.ptr).Call("$set", x.object())
14471445
}

tests/misc_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,3 +862,29 @@ func TestVersion(t *testing.T) {
862862
t.Fatalf("Got: runtime.Version() returned %q. Want: a valid Go version.", got)
863863
}
864864
}
865+
866+
// https://github.com/gopherjs/gopherjs/issues/1163
867+
func TestReflectSetForEmbed(t *testing.T) {
868+
type Point struct {
869+
x int
870+
y int
871+
}
872+
type Embed struct {
873+
value bool
874+
point Point
875+
}
876+
type A struct {
877+
Embed
878+
}
879+
c := &A{}
880+
c.value = true
881+
c.point = Point{100, 200}
882+
in := reflect.ValueOf(c).Elem()
883+
v := reflect.New(in.Type())
884+
e := v.Elem()
885+
f0 := e.Field(0)
886+
e.Set(in)
887+
if e.Field(0) != f0 {
888+
t.Fatalf("relfect.Set got %v, want %v", f0, e.Field(0))
889+
}
890+
}

0 commit comments

Comments
 (0)