forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler: fix handling of struct, array and interface values.
As outlined in gopherjs#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. gopherjs#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 gopherjs#661.
- Loading branch information
Showing
5 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type Struct struct { | ||
Name string | ||
} | ||
|
||
func (s Struct) SetName(n string) { | ||
s.Name = n | ||
} | ||
|
||
type SetName interface { | ||
SetName(n string) | ||
} | ||
|
||
func TestAssignStructValInterface(t *testing.T) { | ||
s := Struct{ | ||
Name: "Rob", | ||
} | ||
|
||
var i1 interface{} = s | ||
var i2 interface{} = i1 | ||
|
||
s.Name = "Pike" | ||
|
||
ss := fmt.Sprintf("%#v", s) | ||
i1s := fmt.Sprintf("%#v", i1) | ||
i2s := fmt.Sprintf("%#v", i2) | ||
|
||
if exp := "tests.Struct{Name:\"Pike\"}"; ss != exp { | ||
t.Fatalf("ss should have been %q; got %q", exp, ss) | ||
} | ||
|
||
iexp := "tests.Struct{Name:\"Rob\"}" | ||
|
||
if i1s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i1s) | ||
} | ||
|
||
if i2s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i2s) | ||
} | ||
} | ||
|
||
func TestStructValIntfMethodCall(t *testing.T) { | ||
var i SetName = Struct{ | ||
Name: "Rob", | ||
} | ||
|
||
i.SetName("Pike") | ||
|
||
is := fmt.Sprintf("%#v", i) | ||
|
||
if exp := "tests.Struct{Name:\"Rob\"}"; is != exp { | ||
t.Fatalf("is should have been %q; got %q", exp, is) | ||
} | ||
} | ||
|
||
func TestAssignArrayInterface(t *testing.T) { | ||
a := [2]int{1, 2} | ||
|
||
var i1 interface{} = a | ||
var i2 interface{} = i1 | ||
|
||
a[0] = 0 | ||
|
||
as := fmt.Sprintf("%#v", a) | ||
i1s := fmt.Sprintf("%#v", i1) | ||
i2s := fmt.Sprintf("%#v", i2) | ||
|
||
if exp := "[2]int{0, 2}"; as != exp { | ||
t.Fatalf("ss should have been %q; got %q", exp, as) | ||
} | ||
|
||
iexp := "[2]int{1, 2}" | ||
|
||
if i1s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i1s) | ||
} | ||
|
||
if i2s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i2s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters