gcemscripten.nim 1.2 KB

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