forked from bonzini/smalltalk
/
objects.st
126 lines (105 loc) · 3.23 KB
/
objects.st
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"======================================================================
|
| Test special objects
|
|
======================================================================"
"======================================================================
|
| Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation.
| Written by Paolo Bonzini
|
| This file is part of GNU Smalltalk.
|
| GNU Smalltalk is free software; you can redistribute it and/or modify it
| under the terms of the GNU General Public License as published by the Free
| Software Foundation; either version 2, or (at your option) any later version.
|
| GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
| FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
| details.
|
| You should have received a copy of the GNU General Public License along with
| GNU Smalltalk; see the file COPYING. If not, write to the Free Software
| Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
======================================================================"
Object subclass: #ObjectsTest
instanceVariableNames: 'name survive'
classVariableNames: 'Messages'
poolDictionaries: ''
category: nil
!
!ObjectsTest class methodsFor: 'test'!
testCompaction
Messages := SortedCollection new.
ObjectMemory compact.
Messages do: [ :each | each displayNl ]
!
testFinalize
| test |
self new name: 'a' survive: false.
self new name: 'b' survive: true.
self new name: 'c' survive: false.
self testCompaction.
self testCompaction.
self testCompaction.
!
testWeak
| a |
a := Array new: 1.
a makeWeak.
a at: 1 put: Object new.
ObjectMemory compact.
^(a at: 1) isNil
! !
!ObjectsTest methodsFor: 'finalization'!
name: aString survive: aBoolean
name := aString.
survive := aBoolean.
self addToBeFinalized
!
finalize
survive ifTrue: [
Messages add: name, ' finalized, surviving'.
survive := false.
self addToBeFinalized.
^self
].
Messages add: name, ' finalized'.
! !
ObjectsTest testFinalize!
^ObjectsTest testWeak!
| a b |
a := WeakArray new: 5.
a at: 1 put: 'abc'.
a at: 2 put: Object new.
a at: 4 put: nil.
a at: 5 put: 1.
b := a copy.
a printNl.
b printNl.
ObjectMemory compact.
a printNl.
b printNl.
((1 to: 5) collect: [ :each | a isAlive: each ]) printNl.
1 to: 5 do: [ :index | a clearGCFlag: index ].
((1 to: 5) collect: [ :each | a isAlive: each ]) printNl.
((1 to: 5) collect: [ :each | b isAlive: each ]) printNl!
"Test lightweight class"
| Test methodString t |
Test := Behavior new.
Transcript nextPutAll: 'New instance of Behavior created'; nl.
Test superclass: Object.
Transcript nextPutAll: 'Superclass assigned'; nl.
Test compile: 'new ^super new'.
Transcript nextPutAll: 'First method compiled'; nl.
Test compile: 'printTestMessage ''test message'' printNl.'.
Transcript nextPutAll: 'Second method compiled'; nl.
t := Test new.
Transcript nextPutAll: 'Instance created'; nl.
t printTestMessage.
t printNl.
Transcript nextPutAll: 'Well it seems to work fine'; nl.
!