author | Claus Gittinger <cg@exept.de> |
Thu, 01 Feb 2001 12:34:25 +0100 | |
changeset 5798 | a8485d80886b |
parent 4661 | 9ebc7608738d |
child 6550 | adb966d9afc9 |
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 |
||
112 |
shallowCopy |
|
113 |
"when copying, return a real (mutable) Array" |
|
114 |
||
115 |
|sz| |
|
116 |
||
117 |
sz := self size. |
|
118 |
^ (Array new:sz) |
|
119 |
replaceFrom:1 to:sz with:self startingAt:1 |
|
274 | 120 |
! ! |
121 |
||
122 |
!ImmutableArray methodsFor:'error handling'! |
|
123 |
||
124 |
creator |
|
125 |
"find the method that contains me" |
|
126 |
||
330 | 127 |
Method allSubInstances do:[:aMethod | |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
128 |
(aMethod referencesGlobal:self) ifTrue:[ |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
129 |
^ aMethod. |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
130 |
]. |
274 | 131 |
]. |
132 |
^ nil |
|
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
133 |
|
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 |
#(1 2 3) creator |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
136 |
" |
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 |
"Modified: 24.6.1996 / 15:36:28 / stefan" |
274 | 139 |
! |
140 |
||
141 |
notifyStoreError |
|
142 |
"a store is attempted - for our convenience, find the method that |
|
143 |
contains me, for a nicer error message" |
|
144 |
||
145 |
|creator msg| |
|
146 |
||
147 |
creator := self creator. |
|
361 | 148 |
msg := 'store into/change of literal'. |
274 | 149 |
creator notNil ifTrue:[ |
150 |
msg := msg , ' (' , creator whoString , ')' |
|
151 |
]. |
|
152 |
" |
|
153 |
this error is reported on an attempt to store into a literal |
|
154 |
array. The literal was created in creator. |
|
155 |
If you press continue in the debugger, the store will be performed. |
|
156 |
If you dont want this, press abort and check your code. |
|
157 |
Storing into literals is VERY VERY bad coding style. |
|
158 |
" |
|
159 |
self error:msg |
|
160 |
! ! |
|
161 |
||
619 | 162 |
!ImmutableArray methodsFor:'private'! |
274 | 163 |
|
619 | 164 |
species |
165 |
"Copies should be mutable" |
|
166 |
||
167 |
^Array |
|
168 |
! ! |
|
274 | 169 |
|
619 | 170 |
!ImmutableArray methodsFor:'queries'! |
274 | 171 |
|
619 | 172 |
isLiteral |
4661 | 173 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 174 |
(i.e. can be used in constant arrays)" |
274 | 175 |
|
619 | 176 |
"yes, I must be" |
177 |
^ true |
|
178 |
||
274 | 179 |
! ! |
180 |
||
361 | 181 |
!ImmutableArray methodsFor:'specials'! |
182 |
||
183 |
become:anotherObject |
|
371 | 184 |
"trigger an error if I should become something else |
185 |
(this would be an even more tricky manipulation)" |
|
186 |
||
361 | 187 |
self notifyStoreError. |
188 |
^ super become:anotherObject |
|
189 |
! |
|
190 |
||
191 |
becomeNil |
|
371 | 192 |
"trigger an error if I should become nil |
193 |
(this would be an even more tricky manipulation)" |
|
194 |
||
361 | 195 |
self notifyStoreError. |
196 |
^ super becomeNil |
|
197 |
! ! |
|
198 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
199 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 200 |
|
201 |
version |
|
5798 | 202 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.19 2001-02-01 11:34:25 cg Exp $' |
660 | 203 |
! ! |