author | Stefan Vogel <sv@exept.de> |
Fri, 28 Jun 1996 17:32:42 +0200 | |
changeset 1493 | 33e226c7d187 |
parent 1296 | 41f533f705cd |
child 2697 | fc8552cd6ebf |
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 |
||
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
13 |
'From Smalltalk/X, Version:2.10.9 on 25-jun-1996 at 14:32:57' ! |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
14 |
|
274 | 15 |
Array subclass:#ImmutableArray |
1284 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'System-Compiler-Support' |
|
274 | 20 |
! |
21 |
||
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
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 |
|
59 |
[author:] |
|
60 |
Claus Gittinger |
|
274 | 61 |
" |
619 | 62 |
! ! |
63 |
||
64 |
!ImmutableArray methodsFor:'accessing'! |
|
65 |
||
66 |
at: index put: value |
|
67 |
"Trigger an error if an immutable array is stored into. |
|
68 |
The store will be performed (for compatibility reasons) if you continue |
|
69 |
in the debugger." |
|
70 |
||
71 |
self notifyStoreError. |
|
72 |
^ super at: index put: value |
|
73 |
! |
|
74 |
||
75 |
basicAt: index put: value |
|
76 |
"Trigger an error if an immutable array is stored into. |
|
77 |
The store will be performed (for compatibility reasons) if you continue |
|
78 |
in the debugger." |
|
79 |
||
80 |
self notifyStoreError. |
|
81 |
^ super basicAt: index put: value |
|
82 |
! ! |
|
83 |
||
84 |
!ImmutableArray methodsFor:'copying'! |
|
85 |
||
86 |
copyEmpty |
|
87 |
"when copying, return a real (mutable) Array" |
|
88 |
||
89 |
^ Array new:self size |
|
90 |
! |
|
91 |
||
92 |
copyEmptyAndGrow:size |
|
93 |
"when copying, return a real (mutable) Array" |
|
94 |
||
95 |
^ Array new:size |
|
96 |
! |
|
97 |
||
98 |
postCopy |
|
99 |
"when copied, make it me a real (mutable) Array" |
|
100 |
||
101 |
self changeClassTo:Array. |
|
102 |
! |
|
103 |
||
104 |
shallowCopy |
|
105 |
"when copying, return a real (mutable) Array" |
|
106 |
||
107 |
|sz| |
|
108 |
||
109 |
sz := self size. |
|
110 |
^ (Array new:sz) |
|
111 |
replaceFrom:1 to:sz with:self startingAt:1 |
|
274 | 112 |
! ! |
113 |
||
114 |
!ImmutableArray methodsFor:'error handling'! |
|
115 |
||
116 |
creator |
|
117 |
"find the method that contains me" |
|
118 |
||
330 | 119 |
Method allSubInstances do:[:aMethod | |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
120 |
(aMethod referencesGlobal:self) ifTrue:[ |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
121 |
^ aMethod. |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
122 |
]. |
274 | 123 |
]. |
124 |
^ nil |
|
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
125 |
|
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
126 |
" |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
127 |
#(1 2 3) creator |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
128 |
" |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
129 |
|
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
130 |
"Modified: 24.6.1996 / 15:36:28 / stefan" |
274 | 131 |
! |
132 |
||
133 |
notifyStoreError |
|
134 |
"a store is attempted - for our convenience, find the method that |
|
135 |
contains me, for a nicer error message" |
|
136 |
||
137 |
|creator msg| |
|
138 |
||
139 |
creator := self creator. |
|
361 | 140 |
msg := 'store into/change of literal'. |
274 | 141 |
creator notNil ifTrue:[ |
142 |
msg := msg , ' (' , creator whoString , ')' |
|
143 |
]. |
|
144 |
" |
|
145 |
this error is reported on an attempt to store into a literal |
|
146 |
array. The literal was created in creator. |
|
147 |
If you press continue in the debugger, the store will be performed. |
|
148 |
If you dont want this, press abort and check your code. |
|
149 |
Storing into literals is VERY VERY bad coding style. |
|
150 |
" |
|
151 |
self error:msg |
|
152 |
! ! |
|
153 |
||
619 | 154 |
!ImmutableArray methodsFor:'private'! |
274 | 155 |
|
619 | 156 |
species |
157 |
"Copies should be mutable" |
|
158 |
||
159 |
^Array |
|
160 |
! ! |
|
274 | 161 |
|
619 | 162 |
!ImmutableArray methodsFor:'queries'! |
274 | 163 |
|
619 | 164 |
isLiteral |
165 |
"return true, if the receiver can be used as a literal |
|
166 |
(i.e. can be used in constant arrays)" |
|
274 | 167 |
|
619 | 168 |
"yes, I must be" |
169 |
^ true |
|
170 |
||
274 | 171 |
! ! |
172 |
||
361 | 173 |
!ImmutableArray methodsFor:'specials'! |
174 |
||
175 |
become:anotherObject |
|
371 | 176 |
"trigger an error if I should become something else |
177 |
(this would be an even more tricky manipulation)" |
|
178 |
||
361 | 179 |
self notifyStoreError. |
180 |
^ super become:anotherObject |
|
181 |
! |
|
182 |
||
183 |
becomeNil |
|
371 | 184 |
"trigger an error if I should become nil |
185 |
(this would be an even more tricky manipulation)" |
|
186 |
||
361 | 187 |
self notifyStoreError. |
188 |
^ super becomeNil |
|
189 |
! ! |
|
190 |
||
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
191 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 192 |
|
193 |
version |
|
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
194 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.15 1996-06-28 15:32:42 stefan Exp $' |
660 | 195 |
! ! |