summary refs log tree commit diff stats
path: root/tests/destructor/tmove_objconstr.nim
blob: 875f78283fe139a80f4dd1c8ff17e5efd4204803 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
discard """
output:  '''test created
test destroyed 0
1
2
3
4
Pony is dying!'''
  cmd: '''nim c --newruntime $file'''
"""

# bug #4214
type
  Data = object
    data: string
    rc: int

proc `=destroy`(d: var Data) =
  dec d.rc
  echo d.data, " destroyed ", d.rc

proc `=`(dst: var Data, src: Data) =
  echo src.data, " copied"
  dst.data = src.data & " (copy)"
  dec dst.rc
  inc dst.rc

proc initData(s: string): Data =
  result = Data(data: s, rc: 1)
  echo s, " created"

proc pointlessWrapper(s: string): Data =
  result = initData(s)

proc main =
  var x = pointlessWrapper"test"

when true:
  main()

# bug #985

type
  Pony = object
    name: string

proc `=destroy`(o: var Pony) =
  echo "Pony is dying!"

proc getPony: Pony =
  result.name = "Sparkles"

iterator items(p: Pony): int =
  for i in 1..4:
    yield i

for x in getPony():
  echo x
# XXX this needs to be enabled once top level statements
# produce destructor calls again.
#echo "Pony is dying!"


#------------------------------------------------------------
#-- Move into tuple constructor and move on tuple unpacking
#------------------------------------------------------------

type
  MySeqNonCopyable* = object
    len: int 
    data: ptr UncheckedArray[float]

proc `=destroy`*(m: var MySeqNonCopyable) {.inline.} =
  if m.data != nil:
    deallocShared(m.data)
    m.data = nil

proc `=`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.error.}

proc `=sink`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.inline.} =
  if m.data != m2.data:
    if m.data != nil:
      `=destroy`(m)
    m.len = m2.len
    m.data = m2.data

proc len*(m: MySeqNonCopyable): int {.inline.} = m.len

proc `[]`*(m: MySeqNonCopyable; i: int): float {.inline.} =
  m.data[i.int]

proc `[]=`*(m: var MySeqNonCopyable; i, val: float) {.inline.} =
  m.data[i.int] = val

proc setTo(s: var MySeqNonCopyable, val: float) = 
  for i in 0..<s.len.int:
    s.data[i] = val

proc newMySeq*(size: int, initial_value = 0.0): MySeqNonCopyable =#
  result.len = size
  if size > 0:
    result.data = cast[ptr UncheckedArray[float]](createShared(float, size))

  result.setTo(initial_value)

proc myfunc(x, y: int): (MySeqNonCopyable, MySeqNonCopyable) =
  result = (newMySeq(x, 1.0), newMySeq(y, 5.0))

proc myfunc2(x, y: int): tuple[a: MySeqNonCopyable, b:int, c:MySeqNonCopyable] =
  var cc = newMySeq(y, 5.0)
  (a: case x:
    of 1: 
      let (z1, z2) = myfunc(x,y)
      z2
    elif x > 5: raise newException(ValueError, "new error")
    else: newMySeq(x, 1.0), 
   b: 0, 
   c: block:
        var tmp = if y > 0: move(cc) else: newMySeq(1, 3.0)
        tmp[0] = 5
        tmp 
  )
   

let (seq1, seq2) = myfunc(2, 3)
doAssert seq1.len == 2
doAssert seq1[0] == 1.0
doAssert seq2.len == 3
doAssert seq2[0] == 5.0

var (seq3, i, _) = myfunc2(2, 3)
doAssert seq3.len == 2
doAssert seq3[0] == 1.0

var seq4, seq5: MySeqNonCopyable
(seq4, i, seq5) = myfunc2(2, 3)

seq4 = block:
  var tmp = newMySeq(4, 1.0)
  tmp[0] = 3.0
  tmp

doAssert seq4[0] == 3.0 

import macros

seq4 = 
  if i > 0: newMySeq(2, 5.0) 
  elif i < -100: raise newException(ValueError, "Parse Error")
  else: newMySeq(2, 3.0)

seq4 = 
  case (char) i:
    of 'A', {'W'..'Z'}: newMySeq(2, 5.0) 
    of 'B': quit(-1)
    else: 
      let (x1, x2, x3) = myfunc2(2, 3)
      x3


#------------------------------------------------------------
#-- Move into array constructor
#------------------------------------------------------------

var ii = 1
let arr2 = [newMySeq(2, 5.0), if i > 1: newMySeq(3, 1.0) else: newMySeq(0, 0.0)]
var seqOfSeq2 = @[newMySeq(2, 5.0), newMySeq(3, 1.0)]