author | Claus Gittinger <cg@exept.de> |
Thu, 05 Nov 2009 15:34:07 +0100 | |
changeset 12455 | d34abbe4c841 |
parent 9237 | 86e0ef73d524 |
child 12463 | 6b3a71aad451 |
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 |
" |
|
5798 | 12 |
"{ Package: 'stx:libcomp' }" |
13 |
||
4661 | 14 |
Array variableSubclass:#ImmutableArray |
1284 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
274 | 19 |
! |
20 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
21 |
!ImmutableArray class methodsFor:'documentation'! |
274 | 22 |
|
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
By default, array literals in smalltalk are mutable objects. That |
|
364 | 40 |
may lead to some subtle (and hard to find errors), if some method passes |
274 | 41 |
a literal array constant as argument to someone else, who changes the |
42 |
array using at:put: like messages. Since the array object is kept in |
|
43 |
the first methods literals, the array constant has now been changed without |
|
364 | 44 |
having the methods sourcecode reflect this. Thus, the method will |
274 | 45 |
behave differently from what its source may make you think. |
46 |
||
364 | 47 |
To help finding this kind of 'feature/bug', the compiler can be |
274 | 48 |
configured to create instances of this ImmutableArray instead of Arrays |
49 |
for array literals. Instances of ImmutableArray catch storing accesses and |
|
50 |
enter the debugger. Although useful, this feature is disabled by default |
|
51 |
for compatibility to other smalltalk implementations. |
|
52 |
(Also, if turned on, this makes inspecting array literals entered in |
|
53 |
a workspace somewhat strange: you cannot modify it any longer). |
|
54 |
||
55 |
Turn the ImmutableArray feature on by setting the Parsers class variable |
|
364 | 56 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
1296 | 57 |
|
3715 | 58 |
[see also:] |
59 |
ImmutableString |
|
60 |
Parser Scanner |
|
61 |
||
1296 | 62 |
[author:] |
63 |
Claus Gittinger |
|
274 | 64 |
" |
619 | 65 |
! ! |
66 |
||
67 |
!ImmutableArray methodsFor:'accessing'! |
|
68 |
||
3715 | 69 |
at:index put:value |
619 | 70 |
"Trigger an error if an immutable array is stored into. |
71 |
The store will be performed (for compatibility reasons) if you continue |
|
72 |
in the debugger." |
|
73 |
||
74 |
self notifyStoreError. |
|
3715 | 75 |
^ super at:index put:value |
76 |
||
77 |
"Modified: / 3.8.1998 / 14:45:23 / cg" |
|
619 | 78 |
! |
79 |
||
3715 | 80 |
basicAt:index put:value |
619 | 81 |
"Trigger an error if an immutable array is stored into. |
82 |
The store will be performed (for compatibility reasons) if you continue |
|
83 |
in the debugger." |
|
84 |
||
85 |
self notifyStoreError. |
|
3715 | 86 |
^ super basicAt:index put:value |
87 |
||
88 |
"Modified: / 3.8.1998 / 14:45:30 / cg" |
|
619 | 89 |
! ! |
90 |
||
12455 | 91 |
!ImmutableArray methodsFor:'converting'! |
92 |
||
93 |
asImmutableArray |
|
94 |
^ self |
|
95 |
! ! |
|
96 |
||
619 | 97 |
!ImmutableArray methodsFor:'copying'! |
98 |
||
99 |
copyEmpty |
|
100 |
"when copying, return a real (mutable) Array" |
|
101 |
||
102 |
^ Array new:self size |
|
103 |
! |
|
104 |
||
105 |
copyEmptyAndGrow:size |
|
106 |
"when copying, return a real (mutable) Array" |
|
107 |
||
108 |
^ Array new:size |
|
109 |
! |
|
110 |
||
111 |
postCopy |
|
112 |
"when copied, make it me a real (mutable) Array" |
|
113 |
||
114 |
self changeClassTo:Array. |
|
115 |
! |
|
116 |
||
8931 | 117 |
postDeepCopy |
118 |
"when copied, make it me a real (mutable) Array" |
|
119 |
||
120 |
self changeClassTo:Array. |
|
121 |
! |
|
122 |
||
619 | 123 |
shallowCopy |
124 |
"when copying, return a real (mutable) Array" |
|
125 |
||
126 |
|sz| |
|
127 |
||
128 |
sz := self size. |
|
129 |
^ (Array new:sz) |
|
130 |
replaceFrom:1 to:sz with:self startingAt:1 |
|
274 | 131 |
! ! |
132 |
||
133 |
!ImmutableArray methodsFor:'error handling'! |
|
134 |
||
135 |
creator |
|
136 |
"find the method that contains me" |
|
137 |
||
9237 | 138 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
139 |
|
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
140 |
" |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
141 |
#(1 2 3) creator |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
142 |
" |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
143 |
|
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
144 |
"Modified: 24.6.1996 / 15:36:28 / stefan" |
274 | 145 |
! |
146 |
||
147 |
notifyStoreError |
|
148 |
"a store is attempted - for our convenience, find the method that |
|
149 |
contains me, for a nicer error message" |
|
150 |
||
151 |
|creator msg| |
|
152 |
||
153 |
creator := self creator. |
|
361 | 154 |
msg := 'store into/change of literal'. |
274 | 155 |
creator notNil ifTrue:[ |
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
6550
diff
changeset
|
156 |
msg := msg , ' (' , creator whoString , ')' |
274 | 157 |
]. |
158 |
" |
|
159 |
this error is reported on an attempt to store into a literal |
|
160 |
array. The literal was created in creator. |
|
161 |
If you press continue in the debugger, the store will be performed. |
|
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
6550
diff
changeset
|
162 |
If you don't want this, press abort and check your code. |
274 | 163 |
Storing into literals is VERY VERY bad coding style. |
164 |
" |
|
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
6550
diff
changeset
|
165 |
self error:msg mayProceed:true |
274 | 166 |
! ! |
167 |
||
619 | 168 |
!ImmutableArray methodsFor:'private'! |
274 | 169 |
|
619 | 170 |
species |
171 |
"Copies should be mutable" |
|
172 |
||
6550 | 173 |
^ Array |
619 | 174 |
! ! |
274 | 175 |
|
619 | 176 |
!ImmutableArray methodsFor:'queries'! |
274 | 177 |
|
619 | 178 |
isLiteral |
4661 | 179 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 180 |
(i.e. can be used in constant arrays)" |
274 | 181 |
|
619 | 182 |
"yes, I must be" |
183 |
^ true |
|
184 |
||
274 | 185 |
! ! |
186 |
||
361 | 187 |
!ImmutableArray methodsFor:'specials'! |
188 |
||
189 |
become:anotherObject |
|
371 | 190 |
"trigger an error if I should become something else |
191 |
(this would be an even more tricky manipulation)" |
|
192 |
||
361 | 193 |
self notifyStoreError. |
194 |
^ super become:anotherObject |
|
195 |
! |
|
196 |
||
197 |
becomeNil |
|
371 | 198 |
"trigger an error if I should become nil |
199 |
(this would be an even more tricky manipulation)" |
|
200 |
||
361 | 201 |
self notifyStoreError. |
202 |
^ super becomeNil |
|
203 |
! ! |
|
204 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
205 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 206 |
|
207 |
version |
|
12455 | 208 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.24 2009-11-05 14:34:07 cg Exp $' |
209 |
! |
|
210 |
||
211 |
version_CVS |
|
212 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.24 2009-11-05 14:34:07 cg Exp $' |
|
660 | 213 |
! ! |