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