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