author | Jan Vrany <jan.vrany@labware.com> |
Wed, 22 Mar 2023 13:57:18 +0000 | |
branch | jv |
changeset 25445 | 1623217d2268 |
parent 25420 | a18d0d048b1f |
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 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
131 |
asImmutableCollection |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
132 |
^ self |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
133 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
134 |
"Created: / 15-03-2019 / 13:53:03 / Stefan Vogel" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
135 |
! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
136 |
|
16822 | 137 |
asMutableCollection |
138 |
"return a writable copy of myself" |
|
139 |
||
140 |
^ self copy changeClassTo:Array |
|
20521 | 141 |
! |
142 |
||
20539 | 143 |
asNewArray |
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
144 |
"return the receiver as a unique new array." |
20539 | 145 |
|
146 |
^ self copy |
|
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
147 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
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 |
||
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
178 |
!ImmutableArray methodsFor:'copying-private'! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
179 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
180 |
postCopy |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
181 |
"when copied, make it me a real (mutable) Array" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
182 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
183 |
self changeClassTo:Array. |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
184 |
! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
185 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
186 |
postDeepCopy |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
187 |
"when copied, make it me a real (mutable) Array" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
188 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
189 |
self changeClassTo:Array. |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
190 |
! ! |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
191 |
|
619 | 192 |
!ImmutableArray methodsFor:'private'! |
274 | 193 |
|
619 | 194 |
species |
195 |
"Copies should be mutable" |
|
196 |
||
6550 | 197 |
^ Array |
16398 | 198 |
! |
199 |
||
200 |
speciesForCopy |
|
201 |
"Copies should be mutable" |
|
202 |
||
203 |
^ Array |
|
619 | 204 |
! ! |
274 | 205 |
|
619 | 206 |
!ImmutableArray methodsFor:'queries'! |
274 | 207 |
|
14387 | 208 |
isImmutable |
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
209 |
"return true, if the receiver is immutable. |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
210 |
Since I am an immutable array, return always true here" |
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
211 |
|
14387 | 212 |
^ true |
25420
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
213 |
|
a18d0d048b1f
Cherry-picked changes from eXept, part 4
Jan Vrany <jan.vrany@labware.com>
parents:
20579
diff
changeset
|
214 |
"Modified (comment): / 09-06-2019 / 14:54:41 / Claus Gittinger" |
14387 | 215 |
! |
216 |
||
619 | 217 |
isLiteral |
4661 | 218 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 219 |
(i.e. can be used in constant arrays)" |
274 | 220 |
|
619 | 221 |
"yes, I must be" |
222 |
^ true |
|
223 |
||
274 | 224 |
! ! |
225 |
||
361 | 226 |
!ImmutableArray methodsFor:'specials'! |
227 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
228 |
become:anotherObject |
371 | 229 |
"trigger an error if I should become something else |
230 |
(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
|
231 |
|
12492 | 232 |
self noModificationError. |
361 | 233 |
^ super become:anotherObject |
234 |
! |
|
235 |
||
236 |
becomeNil |
|
371 | 237 |
"trigger an error if I should become nil |
238 |
(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
|
239 |
|
12492 | 240 |
self noModificationError. |
361 | 241 |
^ super becomeNil |
18702 | 242 |
! |
243 |
||
244 |
becomeSameAs:anotherObject |
|
245 |
"trigger an error if I should become something else |
|
246 |
(this would be an even more tricky manipulation)" |
|
247 |
||
248 |
self noModificationError. |
|
249 |
^ super becomeSameAs:anotherObject |
|
20349 | 250 |
! |
251 |
||
252 |
grow:newSize |
|
253 |
"trigger an error if I should change my size" |
|
254 |
||
255 |
self noModificationError. |
|
256 |
^ super grow:newSize |
|
361 | 257 |
! ! |
258 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
259 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 260 |
|
261 |
version |
|
18544 | 262 |
^ '$Header$' |
12455 | 263 |
! |
264 |
||
265 |
version_CVS |
|
18544 | 266 |
^ '$Header$' |
660 | 267 |
! ! |
16398 | 268 |