gcemscripten.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. discard """
  2. outputsub: "77\n77"
  3. retries: 2
  4. """
  5. ## Check how GC/Alloc works in Emscripten
  6. import strutils
  7. type
  8. X = ref XObj
  9. XObj = object
  10. name: string
  11. value: int
  12. when defined(allow_print):
  13. const print = true
  14. else:
  15. const print = false
  16. proc myResult3*(i:int): X {.exportc.} =
  17. if print: echo "3"
  18. new(result)
  19. if print: echo "3-2"
  20. result.value = i
  21. proc myResult5*(i:int, x:X):X {.exportc.} =
  22. if print: echo "5"
  23. system.GC_fullCollect()
  24. new(result)
  25. if print: echo "5-2"
  26. result.value = i
  27. x.value = i+1
  28. if result.value == x.value:
  29. echo "This should not happen. Just allocated variable points to parameter"
  30. proc myResult2*(val: string, i: int): X {.exportc.} =
  31. if print: echo "2-1"
  32. result = myResult3(i)
  33. if print: echo "2-2"
  34. system.GC_fullCollect()
  35. if print: echo "2-3"
  36. var t = new(X)
  37. if print: echo "2-4"
  38. result.name = val
  39. if t.name == "qwe":
  40. echo "This should not happen. Variable is GC collected and new one on same place are allocated."
  41. if print: echo "2-5"
  42. proc myResult4*(val: string, i: int): X {.exportc.} =
  43. if print: echo "4-1"
  44. result = myResult5(i, X())
  45. if print: echo "4-2"
  46. var x = myResult2("qwe", 77)
  47. echo intToStr(x.value)
  48. var x2 = myResult4("qwe", 77)
  49. echo intToStr(x2.value)