blib/lib/SPVM/Document/Language/GarbageCollection.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1 =head1 Name
2  
3 SPVM::Document::Language::GarbageCollection - Garbage Collection in the SPVM Language
4  
5 =head1 Description
6  
7 This document describes garbage collection in the SPVM language.
8  
9 =head1 Garbage Collection
10  
11 =head2 Reference Count GC
12  
13 Garbage collection in SPVM is a reference counted GC.
14  
15 The object is destroyed when its reference count reaches 0.
16  
17 The reference count of an object assigned by L<assignment operator|SPVM::Document::Language::Operators/"Assignment Operator"> is incremented by 1.
18
19 # The reference count is incremented by 1
20 my $object = Point->new;
21  
22 The reference count of an object whose assignment is removed by an assignment operator is decreased by 1.
23  
24 # The reference count is decremented by 1
25 $object = undef;
26  
27 Reference counts incremented by assignments to local variables are decremented at the end of its scope.
28  
29 {
30 # The reference count is incremented by 1
31 my $object = Point->new;
32
33 # The reference count of $object is decremented by 1 at the end of this scope
34 }
35  
36 =head2 Assignment
37  
38 An assignment operator changes the reference count of the left operand and the right operand.
39  
40 LEFT_OPERAND = RIGHT_OPERAND
41  
42 If the type of the value owned by I<RIGHT_OPERAND> is an object type and defined, the reference count of the object is incremented by 1.
43  
44 And if the type of the value owned by I<LEFT_OPERAND> is an object type and defined, the reference count of the object is decremented by 1.
45  
46 =head2 Scope
47  
48 A scope is the part surrounded by a L<scope block|SPVM::Document::Language::Class/"Scope Block">.
49  
50 # Scope block
51 {
52 # Beginning of scope
53
54 my $point = Point->new;
55
56 # End of scope
57 }
58  
59 =head3 Entering Scope
60  
61 The operation of entering scope is executed at the start of a L<scope block|SPVM::Document::Language::Class/"Scope Block">.
62  
63 This operation memorizes the top position of L<mortal stack|/"Mortal Stack"> of a method.
64  
65 =head3 Pushing a Local Variable on the Mortal Stack
66  
67 The operation of a local variable on the mortal stack pushes a local variable on the mortal stack of a method.
68  
69 =head3 Leaving Scope
70  
71 The operation of leaving scope is executed at the end of a L<scope block|SPVM::Document::Language::Class/"Scope Block">.
72  
73 This operation assigns objects from the top position memorized by L<entering scope|/"Entering Scope"> to the current top position of L<mortal stack|/"Mortal Stack"> to C<undef>.
74  
75 =head3 Mortal Stack
76  
77 A mortal stack is a stack that is used by L<entering scope|/"Entering Scope">, L<pushing a local variable on the mortal stack|/"Pushing a Local Variable on the Mortal Stack"> and L<leaving scope|/"Leaving Scope">.
78  
79 A method has one mortal stack.
80  
81 =head2 Weak Reference
82  
83 SPVM supports weak references. Weak references are used to avoid circular references.
84  
85 This is an example that objects have circular references.
86  
87 {
88 my $foo = new Foo;
89 my $bar = new Bar;
90
91 $foo->{bar} = $bar;
92 $bar->{foo} = $foo;
93 }
94  
95 The L<weaken operator|SPVM::Document::Language::Operators/"weaken Operator"> converts a reference to a weak reference.
96  
97 {
98 my $foo = new Foo;
99 my $bar = new Bar;
100
101 $foo->{bar} = $bar;
102 $bar->{foo} = $foo;
103
104 weaken $foo->{bar};
105 }
106  
107 If a reference is convertd to a weak reference, the reference count of the referenced object is decremented by 1.
108  
109 And the weaken flag of the field trun on, and the back reference from the field is added to the referenced object.
110  
111 The L<isweak operator|SPVM::Document::Language::Operators/"isweak Operator"> checks if the weaken flag of the field turn on.
112  
113 my $isweak = isweaken $foo->{bar};
114  
115 The L<unweaken operator|SPVM::Document::Language::Operators/"unweaken Operator"> converts a weak reference to a reference.
116  
117 unweaken $foo->{bar};
118  
119 If a weak reference is convertd to a reference, the reference count of the referenced object is incremented by 1.
120  
121 And the weaken flag of the field trun off, and the back reference from the field is removed from the referenced object.
122  
123 =head1 See Also
124  
125 =over 2
126  
127 =item * L<SPVM::Document::Language::Operators>
128  
129 =item * L<SPVM::Document::Language>
130  
131 =item * L<SPVM::Document>
132  
133 =back
134  
135 =head1 Copyright & License
136  
137 Copyright (c) 2023 Yuki Kimoto
138  
139 MIT License