File: gcemscripten.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (59 lines) | stat: -rw-r--r-- 1,254 bytes parent folder | download | duplicates (4)
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
discard """
  outputsub: "77\n77"
"""

## Check how GC/Alloc works in Emscripten
import strutils

type
  X = ref XObj
  XObj = object
    name: string
    value: int
when defined(allow_print):
  const print = true
else:
  const print = false

proc myResult3*(i:int): X {.exportc.} =
  if print: echo "3"
  new(result)
  if print: echo "3-2"
  result.value = i

proc myResult5*(i:int, x:X):X {.exportc.} =
  if print: echo "5"
  system.GC_fullCollect()
  new(result)
  if print: echo "5-2"
  result.value = i
  x.value = i+1
  if result.value == x.value:
    echo "This should not happen. Just allocated variable points to parameter"

proc myResult2*(val: string, i: int): X {.exportc.} =
  if print: echo "2-1"
  result = myResult3(i)
  if print: echo "2-2"
  system.GC_fullCollect()
  if print: echo "2-3"
  var t = new(X)
  if print: echo "2-4"
  result.name = val
  if t.name == "qwe":
    echo "This should not happen. Variable is GC collected and new one on same place are allocated."
  if print: echo "2-5"

proc myResult4*(val: string, i: int): X {.exportc.} =
  if print: echo "4-1"
  result = myResult5(i, X())
  if print: echo "4-2"

var x = myResult2("qwe", 77)
echo intToStr(x.value)

var x2 = myResult4("qwe", 77)
echo intToStr(x2.value)