Skip to content

Commit 39dbcaf

Browse files
committed
compiler: fix handling of struct, array and interface values.
As outlined in #661, the compiler fails to generate code to copy a struct/array value for an assignment when the target's underlying type is an interface type, whether for explicit variable assignments, implicit function/method parameters etc. Instead, taking the example of explicit variable assignment, the interface variable is assigned a value that contains the same pointer to the source struct/array val (we're in Javascript world, so everything is a pointer). This means that changes to the struct/array value via the source variable are, incorrectly, visible via the target variable. #661 gives a simple example. There is a further issue when interface values are assigned to interface-typed variables: struct/array values are not copied when they should be. Fixes #661.
1 parent bed69dc commit 39dbcaf

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

compiler/expressions.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ func (c *funcContext) makeReceiver(e *ast.SelectorExpr) *expression {
812812

813813
_, isPointer := recvType.Underlying().(*types.Pointer)
814814
methodsRecvType := sel.Obj().Type().(*types.Signature).Recv().Type()
815+
816+
if _, isInterface := methodsRecvType.Underlying().(*types.Interface); isInterface {
817+
return c.formatExpr("$copyInterfaceVal(%e)", x)
818+
}
819+
815820
_, pointerExpected := methodsRecvType.(*types.Pointer)
816821
if !isPointer && pointerExpected {
817822
recvType = types.NewPointer(recvType)
@@ -825,6 +830,7 @@ func (c *funcContext) makeReceiver(e *ast.SelectorExpr) *expression {
825830
if isWrapped(recvType) {
826831
recv = c.formatExpr("new %s(%s)", c.typeName(methodsRecvType), recv)
827832
}
833+
828834
return recv
829835
}
830836

@@ -1130,12 +1136,17 @@ func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType typ
11301136
// wrap JS object into js.Object struct when converting to interface
11311137
return c.formatExpr("new $jsObjectPtr(%e)", expr)
11321138
}
1139+
1140+
switch exprType.Underlying().(type) {
1141+
case *types.Array:
1142+
return c.formatExpr("new %1s($clone(%e, %1s))", c.typeName(exprType), expr)
1143+
case *types.Struct:
1144+
return c.formatExpr("new %1e.constructor.elem($clone(%1e, %s))", expr, c.typeName(exprType))
1145+
}
1146+
11331147
if isWrapped(exprType) {
11341148
return c.formatExpr("new %s(%e)", c.typeName(exprType), expr)
11351149
}
1136-
if _, isStruct := exprType.Underlying().(*types.Struct); isStruct {
1137-
return c.formatExpr("new %1e.constructor.elem(%1e)", expr)
1138-
}
11391150
}
11401151

11411152
return c.translateExpr(expr)

compiler/prelude/prelude.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ var $clone = function(src, type) {
293293
return clone;
294294
};
295295
296+
var $copyInterfaceVal = function(src) {
297+
if (src.constructor.copy) {
298+
return new src.constructor($clone(src.$val, src.constructor));
299+
}
300+
301+
return src;
302+
};
303+
296304
var $pointerOfStructConversion = function(obj, type) {
297305
if(obj.$proxies === undefined) {
298306
obj.$proxies = {};

tests/interface_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type Struct struct {
9+
Name string
10+
}
11+
12+
func (s Struct) SetName(n string) {
13+
s.Name = n
14+
}
15+
16+
type SetName interface {
17+
SetName(n string)
18+
}
19+
20+
func TestAssignStructValInterface(t *testing.T) {
21+
s := Struct{
22+
Name: "Rob",
23+
}
24+
25+
var i1 interface{} = s
26+
var i2 interface{} = i1
27+
28+
s.Name = "Pike"
29+
30+
ss := fmt.Sprintf("%#v", s)
31+
i1s := fmt.Sprintf("%#v", i1)
32+
i2s := fmt.Sprintf("%#v", i2)
33+
34+
if exp := "tests.Struct{Name:\"Pike\"}"; ss != exp {
35+
t.Fatalf("ss should have been %q; got %q", exp, ss)
36+
}
37+
38+
iexp := "tests.Struct{Name:\"Rob\"}"
39+
40+
if i1s != iexp {
41+
t.Fatalf("is should have been %q; got %q", iexp, i1s)
42+
}
43+
44+
if i2s != iexp {
45+
t.Fatalf("is should have been %q; got %q", iexp, i2s)
46+
}
47+
}
48+
49+
func TestStructValInterfaceMethodCall(t *testing.T) {
50+
var i SetName = Struct{
51+
Name: "Rob",
52+
}
53+
54+
i.SetName("Pike")
55+
56+
is := fmt.Sprintf("%#v", i)
57+
58+
if exp := "tests.Struct{Name:\"Rob\"}"; is != exp {
59+
t.Fatalf("is should have been %q; got %q", exp, is)
60+
}
61+
}
62+
63+
func TestAssignArrayInterface(t *testing.T) {
64+
a := [2]int{1, 2}
65+
66+
var i1 interface{} = a
67+
var i2 interface{} = i1
68+
69+
a[0] = 0
70+
71+
as := fmt.Sprintf("%#v", a)
72+
i1s := fmt.Sprintf("%#v", i1)
73+
i2s := fmt.Sprintf("%#v", i2)
74+
75+
if exp := "[2]int{0, 2}"; as != exp {
76+
t.Fatalf("ss should have been %q; got %q", exp, as)
77+
}
78+
79+
iexp := "[2]int{1, 2}"
80+
81+
if i1s != iexp {
82+
t.Fatalf("is should have been %q; got %q", iexp, i1s)
83+
}
84+
85+
if i2s != iexp {
86+
t.Fatalf("is should have been %q; got %q", iexp, i2s)
87+
}
88+
}

0 commit comments

Comments
 (0)