File tree Expand file tree Collapse file tree 3 files changed +77
-3
lines changed
gno.land/cmd/gnoland/testdata Expand file tree Collapse file tree 3 files changed +77
-3
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,27 @@ gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot
39
39
stdout '("2-3-42-" string)'
40
40
stdout OK!
41
41
42
+ gnokey maketx call -pkgpath gno.land/r/append -func CopyAppend -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
43
+ stdout OK!
44
+
45
+ gnokey maketx call -pkgpath gno.land/r/append -func PopB -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
46
+ stdout OK!
47
+
48
+ # Call render
49
+ gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
50
+ stdout '("2-3-42-" string)'
51
+ stdout OK!
52
+
53
+ gnokey maketx call -pkgpath gno.land/r/append -func AppendMoreAndC -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
54
+ stdout OK!
55
+
56
+ gnokey maketx call -pkgpath gno.land/r/append -func ReassignC -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
57
+ stdout OK!
58
+
59
+ gnokey maketx call -pkgpath gno.land/r/append -func Render -gas-fee 1000000ugnot -gas-wanted 2000000 -args '' -broadcast -chainid=tendermint_test test1
60
+ stdout '("2-3-42-70-100-" string)'
61
+ stdout OK!
62
+
42
63
-- append.gno --
43
64
package append
44
65
@@ -48,7 +69,9 @@ import (
48
69
49
70
type T struct{ i int }
50
71
51
- var a []T
72
+ var a, b []T
73
+ var c = []T{{i: 100}}
74
+
52
75
53
76
func init() {
54
77
a = make([]T, 0, 1)
@@ -62,6 +85,25 @@ func Append(i int) {
62
85
a = append(a, T{i: i})
63
86
}
64
87
88
+ func CopyAppend() {
89
+ b = append(a, T{i: 50}, T{i: 60})
90
+ }
91
+
92
+ func PopB() {
93
+ b = append(b[:0], b[1:]...)
94
+ }
95
+
96
+ func AppendMoreAndC() {
97
+ // Fill to capacity
98
+ a = append(a, T{i: 70})
99
+ // Above capacity; make new array
100
+ a = append(a, c...)
101
+ }
102
+
103
+ func ReassignC() {
104
+ c[0] = T{i: 200}
105
+ }
106
+
65
107
func Render(path string) string {
66
108
var s string
67
109
for i:=0;i<len(a);i++{
Original file line number Diff line number Diff line change @@ -376,7 +376,7 @@ func UverseNode() *PackageNode {
376
376
if 0 < xvl {
377
377
if xvb .Data == nil {
378
378
for i := 0 ; i < xvl ; i ++ {
379
- list [i ] = xvb .List [xvo + i ].Copy (m .Alloc )
379
+ list [i ] = xvb .List [xvo + i ].DeepCopy (m .Alloc , m . Store )
380
380
}
381
381
} else {
382
382
panic ("should not happen" )
@@ -392,7 +392,7 @@ func UverseNode() *PackageNode {
392
392
if 0 < argsl {
393
393
if argsb .Data == nil {
394
394
for i := 0 ; i < argsl ; i ++ {
395
- list [xvl + i ] = argsb .List [argso + i ].Copy (m .Alloc )
395
+ list [xvl + i ] = argsb .List [argso + i ].DeepCopy (m .Alloc , m . Store )
396
396
}
397
397
} else {
398
398
copyDataToList (
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ type S struct {
4
+ i int
5
+ }
6
+
7
+ func main() {
8
+ sArr := make([]S, 0, 4)
9
+ sArr = append(sArr, S{1}, S{2}, S{3})
10
+ println(sArr[0].i, sArr[1].i, sArr[2].i)
11
+
12
+ newArr := append(sArr[:0], sArr[1:]...)
13
+
14
+ // The append modified the underlying array because it was within capacity.
15
+ println(len(sArr) == 3)
16
+ println(sArr[0].i, sArr[1].i, sArr[2].i)
17
+
18
+ // It generated a new slice that references the same array.
19
+ println(len(newArr) == 2)
20
+ println(newArr[0].i, newArr[1].i)
21
+
22
+ // The struct should have been copied, not referenced.
23
+ println(&sArr[2] != &newArr[1])
24
+ }
25
+
26
+ // Output:
27
+ // 1 2 3
28
+ // true
29
+ // 2 3 3
30
+ // true
31
+ // 2 3
32
+ // true
You can’t perform that action at this time.
0 commit comments