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