Skip to content

Commit bdd0387

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. For some reason the changes that address the aforementioned issues start to tip the std library tests over the V8 stack size limit, causing text/template TestMaxExecDepth to fail randomly locally and on CI. This had previously been addressed by @neelance in 1f89545; the --stack_size flag was passed to NodeJS which in turn passed the value onto V8. But per nodejs/node#14567 (comment) it was pointed out that the value of ulimit -s must be >= the value of --stack_size for the --stack_size to make any sort of sense. Hence this commit also harmonises the setting of ulimit -s during the CI test run with the value of --stack_size that is passed to NodeJS (which in turn passes that value onto V8) when running either gopherjs test or gopherjs run. Fixes #661.
1 parent 2b1d432 commit bdd0387

File tree

5 files changed

+129
-5
lines changed

5 files changed

+129
-5
lines changed

circle.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ machine:
33
version: 6.2.2
44
environment:
55
SOURCE_MAP_SUPPORT: false
6+
GOPHERJS_STACK_SIZE: 10000
67

78
dependencies:
89
pre:
@@ -18,7 +19,7 @@ test:
1819
- go tool vet *.go # Go package in root directory.
1920
- for d in */; do echo $d; done | grep -v tests/ | grep -v third_party/ | xargs go tool vet # All subdirectories except "tests", "third_party".
2021
- >
21-
gopherjs test --short --minify
22+
ulimit -s $GOPHERJS_STACK_SIZE && ulimit -s && gopherjs test --short --minify
2223
github.com/gopherjs/gopherjs/tests
2324
github.com/gopherjs/gopherjs/tests/main
2425
github.com/gopherjs/gopherjs/js

compiler/expressions.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,12 @@ func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType typ
11121112
}
11131113

11141114
exprType := c.p.TypeOf(expr)
1115+
1116+
// whenever expr has an underlying *types.Interface type we need to $copyIntf
1117+
if _, isIntf := exprType.Underlying().(*types.Interface); isIntf {
1118+
return c.formatExpr("$copyIntf(%e)", expr)
1119+
}
1120+
11151121
if types.Identical(exprType, desiredType) {
11161122
return c.translateExpr(expr)
11171123
}
@@ -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 $copyIntf = 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 TestStructValIntfMethodCall(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+
}

tool.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,24 @@ func runNode(script string, args []string, dir string, quiet bool) error {
749749
}
750750
}
751751

752+
// set a default stack size;
753+
//
754+
// Per https://github.com/nodejs/node/issues/14567#issuecomment-319367412
755+
// ulimit -s needs to be at least this value
756+
//
757+
// because this probably isn't critical for everyday usage (it's only a real
758+
// issue with massively recursive code, like the tests in text/template) we
759+
// don't test to see if this is the case. Instead we ensure it's the case
760+
// via GOPHERJS_STACK_SIZE which is set as part of the CI build
761+
//
762+
stackSize := "10000"
763+
764+
if ss, ok := os.LookupEnv("GOPHERJS_STACK_SIZE"); ok {
765+
stackSize = ss
766+
}
767+
752768
if runtime.GOOS != "windows" {
753-
allArgs = append(allArgs, "--stack_size=10000", script)
769+
allArgs = append(allArgs, "--stack_size="+stackSize, script)
754770
}
755771

756772
allArgs = append(allArgs, args...)

0 commit comments

Comments
 (0)