aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/ownmove.zc
blob: 76a2a17e120c264b9710b31fe5ea618a8249197e (plain) (blame)
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
namespace;
include rt;

// moves between owned locals and bounded lends of a local to a global.
word sink;
// the scratch buffer is lent to sink while helper reads it through the global.
int helper() { return sink[0] + sink[1]; }
int lend(int a, int b) {
	word buf = rt.new(4);
	buf[0] = a; buf[1] = b;
	word saved = sink;
	sink = m*buf;
	int s = helper();
	buf[0] = s;
	sink = saved;
	return buf[0];
}
// a replacement built in an inner block becomes the outer owner once the old one is released.
int grow(int n) {
	word r = rt.new(2);
	r[0] = n;
	if (n > 0) {
		word bigger = rt.new(8);
		bigger[0] = r[0] * 10;
		rt.free(r);
		r = bigger;
	}
	return r[0];
}
fn main() {
	rt.put_n(lend(3, 4)); rt.put_c(32);
	rt.put_n(grow(5)); rt.put_c(32);
	rt.put_n(grow(0)); rt.put_c(32);
	int i = 0;
	while (i < 100) { grow(i); i++; }
	rt.gc();
	rt.put_n(sink == 0); rt.put_c(10);
	return 0;
}