cfad47cfa3/t3compiler/tads3/test/data/finalize.t

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
#include "tads.h"
2
#include "t3.h"
3
#include "t3test.h"
4
5
function _main(args)
6
{
7
    local x;
8
    
9
    t3SetSay(_my_say_embed);
10
11
    /*
12
     *   Create a bunch of objects, but only keep a reference to the last
13
     *   of them. 
14
     */
15
    for (local i = 1 ; i <= 10 ; ++i)
16
        x = new obj1(i);
17
18
    "\nrunning garbage collection: should delete objects 1 - 9...\n";
19
    t3RunGC();
20
    "done running garbage collection!\n\n";
21
22
    "running gc again: should delete object 10...\n";
23
    x = nil;
24
    t3RunGC();
25
    "done running gc again!\n\n";
26
27
    "Make sure obj2.objptr is still valid - obj2.objptr.id_ =
28
        << obj2.objptr.id_ >>\n\n";
29
    x = t3test_get_obj_id(obj2.objptr);
30
    "- id = <<x>>, gc state = <<t3test_get_obj_gc_state(x)>>\n";
31
32
    "Creating obj3\n";
33
    new obj3;
34
35
    "\nrunning gc: should delete obj3...\n";
36
    t3RunGC();
37
    "done running gc\n\n";
38
39
    "\nrunning gc: should delete obj4...\n";
40
    t3RunGC();
41
    "done running gc\n\n";
42
43
    "\nclearing obj2.objptr; running gc: should delete object id = 5...\n";
44
    x = t3test_get_obj_id(obj2.objptr);
45
    "- id = <<x>>, gc state = <<t3test_get_obj_gc_state(x)>>\n";
46
    obj2.objptr = nil;
47
    t3RunGC();
48
    "done running gc\n";
49
    "- id = <<x>>, gc state = <<t3test_get_obj_gc_state(x)>>\n\n";
50
}
51
52
class obj3: object
53
    finalize()
54
    {
55
        "This is obj3.finalize - setting self.objptr = new obj4\n";
56
        objptr = new obj4;
57
    }
58
    objptr = nil
59
;
60
61
_my_say_embed(str) { tadsSay(str); }
62
63
class obj4: object
64
    finalize()
65
    {
66
        "This is obj4.finalize\n";
67
    }
68
;    
69
70
obj2: object
71
    objptr = nil
72
;
73
74
class obj1: object
75
    finalize()
76
    {
77
        "This is obj1.finalize - id = <<id_>>\n";
78
        if (id_ == 5)
79
        {
80
            "-- Storing a reference to 'self' in obj2.objptr\n";
81
            obj2.objptr = self;
82
        }
83
    }
84
    construct(id)
85
    {
86
        id_ = id;
87
        "This is obj1.construct - id == <<id_>>\n";
88
    }
89
    id_ = nil
90
;
91