tmisc_vm.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. discard """
  2. output: '''
  3. [127, 127, 0, 255][127, 127, 0, 255]
  4. (data: 1)
  5. '''
  6. nimout: '''caught Exception
  7. main:begin
  8. main:end
  9. @[{0}]
  10. (width: 0, height: 0, path: "")
  11. @[(width: 0, height: 0, path: ""), (width: 0, height: 0, path: "")]
  12. Done!
  13. foo4
  14. foo4
  15. foo4
  16. (a: 0, b: 0)
  17. (a: 0, b: 0)
  18. (a: 0, b: 0)
  19. '''
  20. """
  21. #bug #1009
  22. type
  23. TAggRgba8* = array[4, byte]
  24. template R*(self: TAggRgba8): byte = self[0]
  25. template G*(self: TAggRgba8): byte = self[1]
  26. template B*(self: TAggRgba8): byte = self[2]
  27. template A*(self: TAggRgba8): byte = self[3]
  28. template `R=`*(self: TAggRgba8, val: byte) =
  29. self[0] = val
  30. template `G=`*(self: TAggRgba8, val: byte) =
  31. self[1] = val
  32. template `B=`*(self: TAggRgba8, val: byte) =
  33. self[2] = val
  34. template `A=`*(self: TAggRgba8, val: byte) =
  35. self[3] = val
  36. proc ABGR*(val: int | int64): TAggRgba8 =
  37. var V = val
  38. result.R = byte(V and 0xFF)
  39. V = V shr 8
  40. result.G = byte(V and 0xFF)
  41. V = V shr 8
  42. result.B = byte(V and 0xFF)
  43. result.A = byte((V shr 8) and 0xFF)
  44. const
  45. c1 = ABGR(0xFF007F7F)
  46. echo ABGR(0xFF007F7F).repr, c1.repr
  47. # bug 8740
  48. static:
  49. try:
  50. raise newException(ValueError, "foo")
  51. except Exception:
  52. echo "caught Exception"
  53. except Defect:
  54. echo "caught Defect"
  55. except ValueError:
  56. echo "caught ValueError"
  57. # bug #10538
  58. block:
  59. proc fun1(): seq[int] =
  60. try:
  61. try:
  62. result.add(1)
  63. return
  64. except:
  65. result.add(-1)
  66. finally:
  67. result.add(2)
  68. finally:
  69. result.add(3)
  70. result.add(4)
  71. let x1 = fun1()
  72. const x2 = fun1()
  73. doAssert(x1 == x2)
  74. # bug #11610
  75. proc simpleTryFinally()=
  76. try:
  77. echo "main:begin"
  78. finally:
  79. echo "main:end"
  80. static: simpleTryFinally()
  81. # bug #10981
  82. import sets
  83. proc main =
  84. for i in 0..<15:
  85. var someSets = @[initHashSet[int]()]
  86. someSets[^1].incl(0) # <-- segfaults
  87. if i == 0:
  88. echo someSets
  89. static:
  90. main()
  91. # bug #7261
  92. const file = """
  93. sprites.png
  94. size: 1024,1024
  95. format: RGBA8888
  96. filter: Linear,Linear
  97. repeat: none
  98. char/slide_down
  99. rotate: false
  100. xy: 730, 810
  101. size: 204, 116
  102. orig: 204, 116
  103. offset: 0, 0
  104. index: -1
  105. """
  106. type
  107. AtlasPage = object
  108. width, height: int
  109. path: string
  110. CtsStream = object
  111. data: string
  112. pos: int
  113. proc atEnd(stream: CtsStream): bool =
  114. stream.pos >= stream.data.len
  115. proc readChar(stream: var CtsStream): char =
  116. if stream.atEnd:
  117. result = '\0'
  118. else:
  119. result = stream.data[stream.pos]
  120. inc stream.pos
  121. proc readLine(s: var CtsStream, line: var string): bool =
  122. # This is pretty much copied from the standard library:
  123. line.setLen(0)
  124. while true:
  125. var c = readChar(s)
  126. if c == '\c':
  127. c = readChar(s)
  128. break
  129. elif c == '\L': break
  130. elif c == '\0':
  131. if line.len > 0: break
  132. else: return false
  133. line.add(c)
  134. result = true
  135. proc peekLine(s: var CtsStream, line: var string): bool =
  136. let oldPos = s.pos
  137. result = s.readLine(line)
  138. s.pos = oldPos
  139. proc initCtsStream(data: string): CtsStream =
  140. CtsStream(
  141. pos: 0,
  142. data: data
  143. )
  144. # ********************
  145. # Interesting stuff happens here:
  146. # ********************
  147. proc parseAtlas(stream: var CtsStream) =
  148. var pages = @[AtlasPage(), AtlasPage()]
  149. var line = ""
  150. block:
  151. let page = addr pages[^1]
  152. discard stream.peekLine(line)
  153. discard stream.peekLine(line)
  154. echo page[]
  155. echo pages
  156. static:
  157. var stream = initCtsStream(file)
  158. parseAtlas(stream)
  159. echo "Done!"
  160. # bug #12244
  161. type
  162. Apple = object
  163. data: int
  164. func what(x: var Apple) =
  165. x = Apple(data: 1)
  166. func oh_no(): Apple =
  167. what(result)
  168. const
  169. vmCrash = oh_no()
  170. debugEcho vmCrash
  171. # bug #12310
  172. proc someTransform(s: var array[8, uint64]) =
  173. var s1 = 5982491417506315008'u64
  174. s[1] += s1
  175. static:
  176. var state: array[8, uint64]
  177. state[1] = 7105036623409894663'u64
  178. someTransform(state)
  179. doAssert state[1] == 13087528040916209671'u64
  180. import macros
  181. # bug #12670
  182. macro fooImpl(arg: untyped) =
  183. result = quote do:
  184. `arg`
  185. proc foo(): string {.compileTime.} =
  186. fooImpl:
  187. result = "foo"
  188. result.addInt 4
  189. static:
  190. echo foo()
  191. echo foo()
  192. echo foo()
  193. # bug #12488
  194. type
  195. MyObject = object
  196. a,b: int
  197. MyObjectRef = ref MyObject
  198. static:
  199. let x1 = new(MyObject)
  200. echo x1[]
  201. let x2 = new(MyObjectRef)
  202. echo x2[]
  203. let x3 = new(ref MyObject) # cannot generate VM code for ref MyObject
  204. echo x3[]