author | Claus Gittinger <cg@exept.de> |
Fri, 28 Jun 2019 08:49:56 +0200 | |
changeset 24385 | dd84e4dfb5cc |
parent 24311 | 196e4a37ae7a |
child 24446 | 9ae04c2b6e6d |
permissions | -rw-r--r-- |
24311 | 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 |
||
20540 | 122 |
asArray |
123 |
"return the receiver as an array. I am already. |
|
20552 | 124 |
Use asNewArray, or asMutableCollection if you need a copy for modification" |
20540 | 125 |
|
126 |
^ self |
|
127 |
! |
|
128 |
||
12455 | 129 |
asImmutableArray |
130 |
^ self |
|
16822 | 131 |
! |
132 |
||
23924 | 133 |
asImmutableCollection |
134 |
^ self |
|
135 |
||
136 |
"Created: / 15-03-2019 / 13:53:03 / Stefan Vogel" |
|
137 |
! |
|
138 |
||
16822 | 139 |
asMutableCollection |
140 |
"return a writable copy of myself" |
|
141 |
||
142 |
^ self copy changeClassTo:Array |
|
20521 | 143 |
! |
144 |
||
20539 | 145 |
asNewArray |
21794 | 146 |
"return the receiver as a unique new array." |
20539 | 147 |
|
148 |
^ self copy |
|
21794 | 149 |
|
150 |
"Modified (comment): / 12-06-2017 / 13:40:44 / mawalch" |
|
20539 | 151 |
! |
152 |
||
20521 | 153 |
beImmutable |
154 |
"that's what I am" |
|
155 |
||
156 |
^ self |
|
20523 | 157 |
! |
158 |
||
159 |
beMutable |
|
160 |
"you never go back" |
|
161 |
||
162 |
^ self shouldNotImplement |
|
12455 | 163 |
! ! |
164 |
||
619 | 165 |
!ImmutableArray methodsFor:'copying'! |
166 |
||
167 |
shallowCopy |
|
168 |
"when copying, return a real (mutable) Array" |
|
169 |
||
170 |
|sz| |
|
171 |
||
172 |
sz := self size. |
|
16398 | 173 |
^ (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
|
174 |
|
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
175 |
" |
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
176 |
#(1 2 3 4) asImmutableArray shallowCopy |
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
177 |
" |
274 | 178 |
! ! |
179 |
||
24174 | 180 |
!ImmutableArray methodsFor:'copying-private'! |
181 |
||
182 |
postCopy |
|
183 |
"when copied, make it me a real (mutable) Array" |
|
184 |
||
185 |
self changeClassTo:Array. |
|
186 |
! |
|
187 |
||
188 |
postDeepCopy |
|
189 |
"when copied, make it me a real (mutable) Array" |
|
190 |
||
191 |
self changeClassTo:Array. |
|
192 |
! ! |
|
193 |
||
21794 | 194 |
|
619 | 195 |
!ImmutableArray methodsFor:'private'! |
274 | 196 |
|
619 | 197 |
species |
198 |
"Copies should be mutable" |
|
199 |
||
6550 | 200 |
^ Array |
16398 | 201 |
! |
202 |
||
203 |
speciesForCopy |
|
204 |
"Copies should be mutable" |
|
205 |
||
206 |
^ Array |
|
619 | 207 |
! ! |
274 | 208 |
|
619 | 209 |
!ImmutableArray methodsFor:'queries'! |
274 | 210 |
|
14387 | 211 |
isImmutable |
24311 | 212 |
"return true, if the receiver is immutable. |
213 |
Since I am an immutable array, return always true here" |
|
214 |
||
14387 | 215 |
^ true |
24311 | 216 |
|
217 |
"Modified (comment): / 09-06-2019 / 14:54:41 / Claus Gittinger" |
|
14387 | 218 |
! |
219 |
||
619 | 220 |
isLiteral |
4661 | 221 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 222 |
(i.e. can be used in constant arrays)" |
274 | 223 |
|
619 | 224 |
"yes, I must be" |
225 |
^ true |
|
226 |
||
274 | 227 |
! ! |
228 |
||
361 | 229 |
!ImmutableArray methodsFor:'specials'! |
230 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
231 |
become:anotherObject |
371 | 232 |
"trigger an error if I should become something else |
233 |
(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
|
234 |
|
12492 | 235 |
self noModificationError. |
361 | 236 |
^ super become:anotherObject |
237 |
! |
|
238 |
||
239 |
becomeNil |
|
371 | 240 |
"trigger an error if I should become nil |
241 |
(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
|
242 |
|
12492 | 243 |
self noModificationError. |
361 | 244 |
^ super becomeNil |
18702 | 245 |
! |
246 |
||
247 |
becomeSameAs:anotherObject |
|
248 |
"trigger an error if I should become something else |
|
249 |
(this would be an even more tricky manipulation)" |
|
250 |
||
251 |
self noModificationError. |
|
252 |
^ super becomeSameAs:anotherObject |
|
20349 | 253 |
! |
254 |
||
255 |
grow:newSize |
|
256 |
"trigger an error if I should change my size" |
|
257 |
||
258 |
self noModificationError. |
|
259 |
^ super grow:newSize |
|
361 | 260 |
! ! |
261 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
262 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 263 |
|
264 |
version |
|
18544 | 265 |
^ '$Header$' |
12455 | 266 |
! |
267 |
||
268 |
version_CVS |
|
18544 | 269 |
^ '$Header$' |
660 | 270 |
! ! |
16398 | 271 |