Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a217ebf

Browse filesBrowse files
committed
Propsed fix for struct and array vals in interface-typed variables
1 parent 2b1d432 commit a217ebf
Copy full SHA for a217ebf

File tree

5 files changed

+129
-5
lines changed
Filter options

5 files changed

+129
-5
lines changed

‎circle.yml

Copy file name to clipboardExpand all lines: circle.yml
+2-1Lines 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

Copy file name to clipboardExpand all lines: compiler/expressions.go
+14-3Lines 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

Copy file name to clipboardExpand all lines: compiler/prelude/prelude.go
+8Lines 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

Copy file name to clipboard
+88Lines 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

Copy file name to clipboardExpand all lines: tool.go
+17-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.