author | Stefan Vogel <sv@exept.de> |
Tue, 04 Oct 2016 12:23:58 +0200 | |
changeset 20521 | 1af1367996fd |
parent 20349 | 46e7949e6cf9 |
child 20523 | 76a818c608b3 |
permissions | -rw-r--r-- |
18544 | 1 |
"{ Encoding: utf8 }" |
2 |
||
274 | 3 |
" |
4 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
5798 | 15 |
|
17375 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
4661 | 18 |
Array variableSubclass:#ImmutableArray |
1284 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'System-Compiler-Support' |
|
274 | 23 |
! |
24 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
25 |
!ImmutableArray class methodsFor:'documentation'! |
274 | 26 |
|
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
41 |
documentation |
|
42 |
" |
|
43 |
By default, array literals in smalltalk are mutable objects. That |
|
364 | 44 |
may lead to some subtle (and hard to find errors), if some method passes |
274 | 45 |
a literal array constant as argument to someone else, who changes the |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
46 |
array using at:put: like messages. Since the array object is kept in |
274 | 47 |
the first methods literals, the array constant has now been changed without |
12463 | 48 |
having the method's sourcecode reflect this. Thus, the method will |
274 | 49 |
behave differently from what its source may make you think. |
50 |
||
364 | 51 |
To help finding this kind of 'feature/bug', the compiler can be |
274 | 52 |
configured to create instances of this ImmutableArray instead of Arrays |
53 |
for array literals. Instances of ImmutableArray catch storing accesses and |
|
54 |
enter the debugger. Although useful, this feature is disabled by default |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
55 |
for compatibility to other smalltalk implementations. |
274 | 56 |
(Also, if turned on, this makes inspecting array literals entered in |
57 |
a workspace somewhat strange: you cannot modify it any longer). |
|
58 |
||
59 |
Turn the ImmutableArray feature on by setting the Parsers class variable |
|
364 | 60 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
1296 | 61 |
|
18544 | 62 |
This class should be used only by the compiler. |
12470 | 63 |
|
64 |
ATTENTION: |
|
18544 | 65 |
there may be still code around which checks for explicit class being Array |
66 |
(both in Smalltalk and in primitive code). All code like foo 'class == Array' |
|
67 |
or '__isArray' will not work with ImmutableArrays; consider using '__isArrayLike'. |
|
68 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
69 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
70 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12470 | 71 |
|
3715 | 72 |
[see also:] |
18544 | 73 |
ImmutableString |
74 |
ImmutableByteArray |
|
75 |
Parser Scanner |
|
3715 | 76 |
|
1296 | 77 |
[author:] |
18544 | 78 |
Claus Gittinger |
274 | 79 |
" |
619 | 80 |
! ! |
81 |
||
17375 | 82 |
!ImmutableArray class methodsFor:'queries'! |
83 |
||
84 |
mutableClass |
|
85 |
"answer an equivalent mustable class" |
|
86 |
||
87 |
^ Array |
|
88 |
! ! |
|
89 |
||
90 |
!ImmutableArray class methodsFor:'testing'! |
|
91 |
||
92 |
hasImmutableInstances |
|
93 |
^ true |
|
94 |
! ! |
|
95 |
||
619 | 96 |
!ImmutableArray methodsFor:'accessing'! |
97 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
98 |
at:index put:value |
619 | 99 |
"Trigger an error if an immutable array is stored into. |
100 |
The store will be performed (for compatibility reasons) if you continue |
|
101 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
102 |
|
12492 | 103 |
self noModificationError. |
3715 | 104 |
^ super at:index put:value |
105 |
||
106 |
"Modified: / 3.8.1998 / 14:45:23 / cg" |
|
619 | 107 |
! |
108 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
109 |
basicAt:index put:value |
619 | 110 |
"Trigger an error if an immutable array is stored into. |
111 |
The store will be performed (for compatibility reasons) if you continue |
|
112 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
113 |
|
12492 | 114 |
self noModificationError. |
3715 | 115 |
^ super basicAt:index put:value |
116 |
||
117 |
"Modified: / 3.8.1998 / 14:45:30 / cg" |
|
619 | 118 |
! ! |
119 |
||
12455 | 120 |
!ImmutableArray methodsFor:'converting'! |
121 |
||
122 |
asImmutableArray |
|
123 |
^ self |
|
16822 | 124 |
! |
125 |
||
126 |
asMutableCollection |
|
127 |
"return a writable copy of myself" |
|
128 |
||
129 |
^ self copy changeClassTo:Array |
|
20521 | 130 |
! |
131 |
||
132 |
beImmutable |
|
133 |
"that's what I am" |
|
134 |
||
135 |
^ self |
|
12455 | 136 |
! ! |
137 |
||
619 | 138 |
!ImmutableArray methodsFor:'copying'! |
139 |
||
140 |
postCopy |
|
141 |
"when copied, make it me a real (mutable) Array" |
|
142 |
||
143 |
self changeClassTo:Array. |
|
144 |
! |
|
145 |
||
8931 | 146 |
postDeepCopy |
147 |
"when copied, make it me a real (mutable) Array" |
|
148 |
||
149 |
self changeClassTo:Array. |
|
150 |
! |
|
151 |
||
619 | 152 |
shallowCopy |
153 |
"when copying, return a real (mutable) Array" |
|
154 |
||
155 |
|sz| |
|
156 |
||
157 |
sz := self size. |
|
16398 | 158 |
^ (Array new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
14113
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
159 |
|
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
160 |
" |
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
161 |
#(1 2 3 4) asImmutableArray shallowCopy |
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
162 |
" |
274 | 163 |
! ! |
164 |
||
619 | 165 |
!ImmutableArray methodsFor:'private'! |
274 | 166 |
|
619 | 167 |
species |
168 |
"Copies should be mutable" |
|
169 |
||
6550 | 170 |
^ Array |
16398 | 171 |
! |
172 |
||
173 |
speciesForCopy |
|
174 |
"Copies should be mutable" |
|
175 |
||
176 |
^ Array |
|
619 | 177 |
! ! |
274 | 178 |
|
619 | 179 |
!ImmutableArray methodsFor:'queries'! |
274 | 180 |
|
14387 | 181 |
isImmutable |
182 |
^ true |
|
183 |
! |
|
184 |
||
619 | 185 |
isLiteral |
4661 | 186 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 187 |
(i.e. can be used in constant arrays)" |
274 | 188 |
|
619 | 189 |
"yes, I must be" |
190 |
^ true |
|
191 |
||
274 | 192 |
! ! |
193 |
||
361 | 194 |
!ImmutableArray methodsFor:'specials'! |
195 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
196 |
become:anotherObject |
371 | 197 |
"trigger an error if I should become something else |
198 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
199 |
|
12492 | 200 |
self noModificationError. |
361 | 201 |
^ super become:anotherObject |
202 |
! |
|
203 |
||
204 |
becomeNil |
|
371 | 205 |
"trigger an error if I should become nil |
206 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
207 |
|
12492 | 208 |
self noModificationError. |
361 | 209 |
^ super becomeNil |
18702 | 210 |
! |
211 |
||
212 |
becomeSameAs:anotherObject |
|
213 |
"trigger an error if I should become something else |
|
214 |
(this would be an even more tricky manipulation)" |
|
215 |
||
216 |
self noModificationError. |
|
217 |
^ super becomeSameAs:anotherObject |
|
20349 | 218 |
! |
219 |
||
220 |
grow:newSize |
|
221 |
"trigger an error if I should change my size" |
|
222 |
||
223 |
self noModificationError. |
|
224 |
^ super grow:newSize |
|
361 | 225 |
! ! |
226 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
227 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 228 |
|
229 |
version |
|
18544 | 230 |
^ '$Header$' |
12455 | 231 |
! |
232 |
||
233 |
version_CVS |
|
18544 | 234 |
^ '$Header$' |
660 | 235 |
! ! |
16398 | 236 |