summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/60_gc
blob: 28c0d00c4c81dd878d0c84e01a437255fc5501ad (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
The `gc()` function allows controlling the garbage collector of the VM.
It takes the requested operation as first argument and an optional,
operation specific second argument.

Returns `null` if the given operation is invalid or if the operation
specific argument is invalid.

Returns `false` if the requested operation would not result in any
changes.

Returns `true` if the requested oepration succeeded (e.g. starting
the GC when it was previously stopped).

Returns an object count if the given operation is `count`.

-- Testcase --
{%
	printf("Count #1: %d\n", gc("count"));

	// create an unreachable cyclic structure
	let o = {};
	o.cycle = o;
	o = null;

	printf("Count #2: %d\n", gc("count"));

	// invoking gc without any argument defaults to "collect"
	gc();

	printf("Count #3: %d\n", gc("count"));


	// create another unreachable cyclic structure
	o = {};
	o.cycle = o;
	o = null;

	printf("Count #4: %d\n", gc("count"));

	// invoking gc with explicit collect argument
	gc("collect");

	printf("Count #5: %d\n", gc("count"));
%}
-- End --

-- Expect stdout --
Count #1: 6
Count #2: 7
Count #3: 6
Count #4: 7
Count #5: 6
-- End --


Testing enabling the automatic collector.

-- Testcase --
{%
	// start GC, trigger every 10 object allocations
	gc("start", 10);

	for (let i = 0; i < 100; i++) {
		let o = {};
		o.cyle = o;
		o = null;

		if ((i % 10) == 0)
			printf("Count #%d: %d\n", (i / 10) + 1, gc("count"));
	}

	// stop GC
	gc("stop");

	for (let i = 100; i < 200; i++) {
		let o = {};
		o.cyle = o;
		o = null;

		if ((i % 10) == 0)
			printf("Count #%d: %d\n", (i / 10) + 1, gc("count"));
	}
%}
-- End --

-- Expect stdout --
Count #1: 7
Count #2: 14
Count #3: 14
Count #4: 14
Count #5: 14
Count #6: 14
Count #7: 14
Count #8: 14
Count #9: 14
Count #10: 14
Count #11: 14
Count #12: 24
Count #13: 34
Count #14: 44
Count #15: 54
Count #16: 64
Count #17: 74
Count #18: 84
Count #19: 94
Count #20: 104
-- End --