summary refs log tree commit diff stats
path: root/lib/prelude.nim
blob: 4d8c7d7f0919825546f41a5f6360a7d1f1dd19e7 (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
#
#
#            Nim's Runtime Library
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This is an include file that simply imports common modules for your
## convenience:
##
## .. code-block:: nim
##   include prelude
##
## Same as:
##
## .. code-block:: nim
##   import os, strutils, times, parseutils, hashes, tables, sets, sequtils
##   when not defined(js): import parseopt

import os, strutils, times, parseutils, hashes, tables, sets, sequtils
when not defined(js): import parseopt
f <rumpf_a@web.de> 2019-11-13 23:29:21 +0100 ARC: closure inside object constructor now works' href='/ahoang/Nim/commit/tests/destructor/tlists.nim?h=devel&id=f22d3c75aa68833ae1b01d747d0c71074ccef310'>f22d3c75a ^
85ffcd80c ^
dfb020b17 ^

76179cbec ^
b8152b29e ^
0f6987a86 ^













d56848878 ^










9fc04a555 ^















dfb020b17 ^
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
           


             
                
     

 
    
                                 




























                                                     








                                          







                      

















                                                                      












                                    









                                              
                               
       
           
                          

                        
                 
               













                                










                                        















                              
                                
discard """
  output: '''
@[1, 2, 3]
Success
@["a", "b", "c"]
Hello
1
2
0'''
  cmd: '''nim c --gc:arc $file'''
"""

import os
import math
import lists
import strutils

proc mkleak() =
  # allocate 1 MB via linked lists
  let numberOfLists = 100
  for i in countUp(1, numberOfLists):
    var leakList = initDoublyLinkedList[string]()
    let numberOfLeaks = 5000
    for j in countUp(1, numberOfLeaks):
      leakList.append(newString(200))

proc mkManyLeaks() =
  for i in 0..0:
    mkleak()
  echo "Success"

iterator foobar(c: string): seq[string] {.closure.} =
  yield @["a", "b", c]

proc tsimpleClosureIterator =
  var myc = "c"
  for it in foobar(myc):
    echo it

type
  LazyList = ref object
    c: proc() {.closure.}

proc tlazyList =
  let dep = @[1, 2, 3]
  var x = LazyList(c: proc () = echo(dep))
  x.c()

type
  Foo = ref object

proc tleakingNewStmt =
  var x: Foo
  for i in 0..10:
    new(x)

iterator infinite(): int {.closure.} =
  var i = 0
  while true:
    yield i
    inc i

iterator take(it: iterator (): int, numToTake: int): int {.closure.} =
  var i = 0
  for x in it():
    if i >= numToTake:
      break
    yield x
    inc i

proc take3 =
  for x in infinite.take(3):
    discard


type
  A = ref object of RootObj
    x: int

  B = ref object of A
    more: string

proc inheritanceBug(param: string) =
  var s: (A, A)
  s[0] = B(more: "a" & param)
  s[1] = B(more: "a" & param)


type
  PAsyncHttpServer = ref object
    value: string

proc serve(server: PAsyncHttpServer) = discard

proc leakObjConstr =
  serve(PAsyncHttpServer(value: "asdas"))

let startMem = getOccupiedMem()
take3()
tlazyList()
inheritanceBug("whatever")
mkManyLeaks()
tsimpleClosureIterator()
tleakingNewStmt()
leakObjConstr()

# bug #12964

type
  Token* = ref object of RootObj
  Li* = ref object of Token

proc bug12964*() =
  var token = Li()
  var tokens = @[Token()]
  tokens.add token

bug12964()

# bug #13119
import streams

proc bug13119 =
  var m = newStringStream("Hello world")
  let buffer = m.readStr(5)
  echo buffer
  m.close

bug13119()

# bug #13105

type
  Result[T, E] = object
    a: T
    b: E
  D = ref object
    x: int
  R = Result[D, int]

proc bug13105 =
  for n in [R(b: 1), R(b: 2)]:
    echo n.b

bug13105()

echo getOccupiedMem() - startMem