tmisc_vm.nim 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. discard """
  2. targets: "c js"
  3. output: '''
  4. [127, 127, 0, 255][127, 127, 0, 255]
  5. (data: 1)
  6. (2, 1)
  7. (2, 1)
  8. (2, 1)
  9. (f0: 5)
  10. '''
  11. nimout: '''caught Exception
  12. main:begin
  13. main:end
  14. @[{0}]
  15. (width: 0, height: 0, path: "")
  16. @[(width: 0, height: 0, path: ""), (width: 0, height: 0, path: "")]
  17. Done!
  18. foo4
  19. foo4
  20. foo4
  21. (a: 0, b: 0)
  22. (a: 0, b: 0)
  23. (a: 0, b: 0)
  24. z1 m: (lo: 12)
  25. z2 a: (lo: 3)
  26. x1 a: (lo: 3)
  27. x2 a: (lo: 6)
  28. x3 a: (lo: 0)
  29. z3 a: (lo: 3)
  30. x1 a: (lo: 3)
  31. x2 a: (lo: 6)
  32. x3 a: (lo: 0)
  33. (2, 1)
  34. (2, 1)
  35. (2, 1)
  36. (f0: 5)
  37. '''
  38. """
  39. import std/sets
  40. #bug #1009
  41. type
  42. TAggRgba8* = array[4, byte]
  43. template R*(self: TAggRgba8): byte = self[0]
  44. template G*(self: TAggRgba8): byte = self[1]
  45. template B*(self: TAggRgba8): byte = self[2]
  46. template A*(self: TAggRgba8): byte = self[3]
  47. template `R=`*(self: TAggRgba8, val: byte) =
  48. self[0] = val
  49. template `G=`*(self: TAggRgba8, val: byte) =
  50. self[1] = val
  51. template `B=`*(self: TAggRgba8, val: byte) =
  52. self[2] = val
  53. template `A=`*(self: TAggRgba8, val: byte) =
  54. self[3] = val
  55. proc ABGR*(val: int | int64): TAggRgba8 =
  56. var V = val
  57. result.R = byte(V and 0xFF)
  58. V = V shr 8
  59. result.G = byte(V and 0xFF)
  60. V = V shr 8
  61. result.B = byte(V and 0xFF)
  62. result.A = byte((V shr 8) and 0xFF)
  63. const
  64. c1 = ABGR(0xFF007F7F)
  65. echo ABGR(0xFF007F7F).repr, c1.repr
  66. # bug 8740
  67. static:
  68. try:
  69. raise newException(ValueError, "foo")
  70. except Exception:
  71. echo "caught Exception"
  72. except Defect:
  73. echo "caught Defect"
  74. except ValueError:
  75. echo "caught ValueError"
  76. # bug #10538
  77. block:
  78. proc fun1(): seq[int] =
  79. try:
  80. try:
  81. result.add(1)
  82. return
  83. except:
  84. result.add(-1)
  85. finally:
  86. result.add(2)
  87. finally:
  88. result.add(3)
  89. result.add(4)
  90. let x1 = fun1()
  91. const x2 = fun1()
  92. doAssert(x1 == x2)
  93. # bug #11610
  94. proc simpleTryFinally()=
  95. try:
  96. echo "main:begin"
  97. finally:
  98. echo "main:end"
  99. static: simpleTryFinally()
  100. # bug #10981
  101. proc main =
  102. for i in 0..<15:
  103. var someSets = @[initHashSet[int]()]
  104. someSets[^1].incl(0) # <-- segfaults
  105. if i == 0:
  106. echo someSets
  107. static:
  108. main()
  109. # bug #7261
  110. const file = """
  111. sprites.png
  112. size: 1024,1024
  113. format: RGBA8888
  114. filter: Linear,Linear
  115. repeat: none
  116. char/slide_down
  117. rotate: false
  118. xy: 730, 810
  119. size: 204, 116
  120. orig: 204, 116
  121. offset: 0, 0
  122. index: -1
  123. """
  124. type
  125. AtlasPage = object
  126. width, height: int
  127. path: string
  128. CtsStream = object
  129. data: string
  130. pos: int
  131. proc atEnd(stream: CtsStream): bool =
  132. stream.pos >= stream.data.len
  133. proc readChar(stream: var CtsStream): char =
  134. if stream.atEnd:
  135. result = '\0'
  136. else:
  137. result = stream.data[stream.pos]
  138. inc stream.pos
  139. proc readLine(s: var CtsStream, line: var string): bool =
  140. # This is pretty much copied from the standard library:
  141. line.setLen(0)
  142. while true:
  143. var c = readChar(s)
  144. if c == '\c':
  145. c = readChar(s)
  146. break
  147. elif c == '\L': break
  148. elif c == '\0':
  149. if line.len > 0: break
  150. else: return false
  151. line.add(c)
  152. result = true
  153. proc peekLine(s: var CtsStream, line: var string): bool =
  154. let oldPos = s.pos
  155. result = s.readLine(line)
  156. s.pos = oldPos
  157. proc initCtsStream(data: string): CtsStream =
  158. CtsStream(
  159. pos: 0,
  160. data: data
  161. )
  162. # ********************
  163. # Interesting stuff happens here:
  164. # ********************
  165. proc parseAtlas(stream: var CtsStream) =
  166. var pages = @[AtlasPage(), AtlasPage()]
  167. var line = ""
  168. block:
  169. let page = addr pages[^1]
  170. discard stream.peekLine(line)
  171. discard stream.peekLine(line)
  172. echo page[]
  173. echo pages
  174. static:
  175. var stream = initCtsStream(file)
  176. parseAtlas(stream)
  177. echo "Done!"
  178. # bug #12244
  179. type
  180. Apple = object
  181. data: int
  182. func what(x: var Apple) =
  183. x = Apple(data: 1)
  184. func oh_no(): Apple =
  185. what(result)
  186. const
  187. vmCrash = oh_no()
  188. debugEcho vmCrash
  189. # bug #12310
  190. proc someTransform(s: var array[8, uint64]) =
  191. var s1 = 5982491417506315008'u64
  192. s[1] += s1
  193. static:
  194. var state: array[8, uint64]
  195. state[1] = 7105036623409894663'u64
  196. someTransform(state)
  197. doAssert state[1] == 13087528040916209671'u64
  198. import macros
  199. # bug #12670
  200. macro fooImpl(arg: untyped) =
  201. result = quote do:
  202. `arg`
  203. proc foo(): string {.compileTime.} =
  204. fooImpl:
  205. result = "foo"
  206. result.addInt 4
  207. static:
  208. echo foo()
  209. echo foo()
  210. echo foo()
  211. # bug #12488
  212. type
  213. MyObject = object
  214. a,b: int
  215. MyObjectRef = ref MyObject
  216. static:
  217. let x1 = new(MyObject)
  218. echo x1[]
  219. let x2 = new(MyObjectRef)
  220. echo x2[]
  221. let x3 = new(ref MyObject) # cannot generate VM code for ref MyObject
  222. echo x3[]
  223. # bug #19464
  224. type
  225. Wrapper = object
  226. inner: int
  227. proc assign(r: var Wrapper, a: Wrapper) =
  228. r = a
  229. proc myEcho(a: Wrapper) =
  230. var tmp = a
  231. assign(tmp, Wrapper(inner: 0)) # this shouldn't modify `a`
  232. doAssert a.inner == 1
  233. static:
  234. var result: Wrapper
  235. assign(result, Wrapper(inner: 1))
  236. myEcho(result)
  237. when true:
  238. # bug #15974
  239. type Foo = object
  240. f0: int
  241. proc fn(a: var Foo) =
  242. var s: Foo
  243. a = Foo(f0: 2)
  244. s = a
  245. doAssert s.f0 == 2
  246. a = Foo(f0: 3)
  247. doAssert s.f0 == 2
  248. proc test2()=
  249. var a = Foo(f0: 1)
  250. fn(a)
  251. static: test2()
  252. test2()
  253. # bug #12551
  254. type
  255. StUint = object
  256. lo: uint64
  257. func `+=`(x: var Stuint, y: Stuint) =
  258. x.lo += y.lo
  259. func `-`(x, y: Stuint): Stuint =
  260. result.lo = x.lo - y.lo
  261. func `+`(x, y: Stuint): Stuint =
  262. result.lo = x.lo + y.lo
  263. func `-=`(x: var Stuint, y: Stuint) =
  264. x = x - y
  265. func `<`(x, y: Stuint): bool=
  266. x.lo < y.lo
  267. func `==`(x, y: Stuint): bool =
  268. x.lo == y.lo
  269. func `<=`(x, y: Stuint): bool =
  270. x.lo <= y.lo
  271. proc div3n2n(r: var Stuint, b: Stuint) =
  272. var d: Stuint
  273. r = d
  274. r += b
  275. func div2n1n(r: var Stuint, b: Stuint) =
  276. div3n2n(r, b)
  277. func divmodBZ(x, y: Stuint, r: var Stuint)=
  278. div2n1n(r, y)
  279. r.lo = 3
  280. func `mod`(x, y: Stuint): Stuint =
  281. divmodBZ(x, y, result)
  282. func doublemod_internal(a, m: Stuint): Stuint =
  283. result = a
  284. if a >= m - a:
  285. result -= m
  286. result += a
  287. func mulmod_internal(a, b, m: Stuint): Stuint =
  288. var (a, b) = (a, b)
  289. swap(a, b)
  290. debugEcho "x1 a: ", a
  291. a = doublemod_internal(a, m)
  292. debugEcho "x2 a: ", a
  293. a = doublemod_internal(a, m)
  294. debugEcho "x3 a: ", a
  295. func powmod_internal(a, m: Stuint): Stuint =
  296. var a = a
  297. debugEcho "z1 m: ", m
  298. debugEcho "z2 a: ", a
  299. result = mulmod_internal(result, a, m)
  300. debugEcho "z3 a: ", a
  301. a = mulmod_internal(a, a, m)
  302. func powmod*(a, m: Stuint) =
  303. discard powmod_internal(a mod m, m)
  304. static:
  305. var x = Stuint(lo: high(uint64))
  306. var y = Stuint(lo: 12)
  307. powmod(x, y)
  308. # bug #16780
  309. when true:
  310. template swap*[T](a, b: var T) =
  311. var a2 = addr(a)
  312. var b2 = addr(b)
  313. var aOld = a2[]
  314. a2[] = b2[]
  315. b2[] = aOld
  316. proc rather =
  317. block:
  318. var a = 1
  319. var b = 2
  320. swap(a, b)
  321. echo (a,b)
  322. block:
  323. type Foo = ref object
  324. x: int
  325. var a = Foo(x:1)
  326. var b = Foo(x:2)
  327. swap(a, b)
  328. echo (a.x, b.x)
  329. block:
  330. type Foo = object
  331. x: int
  332. var a = Foo(x:1)
  333. var b = Foo(x:2)
  334. swap(a, b)
  335. echo (a.x,b.x)
  336. static: rather()
  337. rather()
  338. # bug #16020
  339. when true:
  340. block:
  341. type Foo = object
  342. f0: int
  343. proc main=
  344. var f = Foo(f0: 3)
  345. var f2 = f.addr
  346. f2[].f0 += 1
  347. f2.f0 += 1
  348. echo f
  349. static: main()
  350. main()
  351. import tables, strutils
  352. # bug #14553
  353. const PpcPatterns = @[("aaaa", "bbbb"), ("aaaaa", "bbbbb"), ("aaaaaa", "bbbbbb"), ("aaaaaaa", "bbbbbbb"), ("aaaaaaaa", "bbbbb")]
  354. static:
  355. var
  356. needSecondIdentifier = initTable[uint32, seq[(string, string)]]()
  357. for (name, pattern) in PpcPatterns:
  358. let
  359. firstPart = 0'u32
  360. lastPart = "test"
  361. needSecondIdentifier.mgetOrPut(firstPart, @[]).add((name, pattern))
  362. doAssert needSecondIdentifier[0] == @[("aaaa", "bbbb"), ("aaaaa", "bbbbb"), ("aaaaaa", "bbbbbb"), ("aaaaaaa", "bbbbbbb"), ("aaaaaaaa", "bbbbb")]
  363. # bug #17864
  364. macro transform*(fn: typed) =
  365. quote do:
  366. `fn`
  367. var map: Table[string, HashSet[string]]
  368. proc publish*(): void {.transform.} =
  369. map["k"] = init_hash_set[string]()
  370. map["k"].incl "d"
  371. publish()