author | Stefan Vogel <sv@exept.de> |
Fri, 06 Mar 1998 16:38:37 +0100 | |
changeset 657 | 0ecf1ff6f6bf |
parent 539 | 69a1cd05c7d6 |
child 754 | 1363fa7dfcd1 |
permissions | -rw-r--r-- |
65 | 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 |
|
261 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'System-Compiler-Support' |
|
65 | 18 |
! |
19 |
||
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
20 |
!ImmutableArray class methodsFor:'documentation'! |
65 | 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 |
|
98 | 39 |
may lead to some subtle (and hard to find errors), if some method passes |
65 | 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 |
|
98 | 43 |
having the methods sourcecode reflect this. Thus, the method will |
65 | 44 |
behave differently from what its source may make you think. |
45 |
||
98 | 46 |
To help finding this kind of 'feature/bug', the compiler can be |
65 | 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 |
|
98 | 55 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
263 | 56 |
|
57 |
[author:] |
|
58 |
Claus Gittinger |
|
65 | 59 |
" |
140 | 60 |
! ! |
61 |
||
62 |
!ImmutableArray methodsFor:'accessing'! |
|
63 |
||
64 |
at: index put: value |
|
65 |
"Trigger an error if an immutable array is stored into. |
|
66 |
The store will be performed (for compatibility reasons) if you continue |
|
67 |
in the debugger." |
|
68 |
||
69 |
self notifyStoreError. |
|
70 |
^ super at: index put: value |
|
71 |
! |
|
72 |
||
73 |
basicAt: index put: value |
|
74 |
"Trigger an error if an immutable array is stored into. |
|
75 |
The store will be performed (for compatibility reasons) if you continue |
|
76 |
in the debugger." |
|
77 |
||
78 |
self notifyStoreError. |
|
79 |
^ super basicAt: index put: value |
|
80 |
! ! |
|
81 |
||
82 |
!ImmutableArray methodsFor:'copying'! |
|
83 |
||
84 |
copyEmpty |
|
85 |
"when copying, return a real (mutable) Array" |
|
86 |
||
87 |
^ Array new:self size |
|
88 |
! |
|
89 |
||
90 |
copyEmptyAndGrow:size |
|
91 |
"when copying, return a real (mutable) Array" |
|
92 |
||
93 |
^ Array new:size |
|
94 |
! |
|
95 |
||
96 |
postCopy |
|
97 |
"when copied, make it me a real (mutable) Array" |
|
98 |
||
99 |
self changeClassTo:Array. |
|
100 |
! |
|
101 |
||
102 |
shallowCopy |
|
103 |
"when copying, return a real (mutable) Array" |
|
104 |
||
105 |
|sz| |
|
106 |
||
107 |
sz := self size. |
|
108 |
^ (Array new:sz) |
|
109 |
replaceFrom:1 to:sz with:self startingAt:1 |
|
65 | 110 |
! ! |
111 |
||
112 |
!ImmutableArray methodsFor:'error handling'! |
|
113 |
||
114 |
creator |
|
115 |
"find the method that contains me" |
|
116 |
||
84 | 117 |
Method allSubInstances do:[:aMethod | |
292
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
118 |
(aMethod referencesGlobal:self) ifTrue:[ |
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
119 |
^ aMethod. |
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
120 |
]. |
65 | 121 |
]. |
122 |
^ nil |
|
292
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
123 |
|
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
124 |
" |
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
125 |
#(1 2 3) creator |
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
126 |
" |
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
127 |
|
163651658aee
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
263
diff
changeset
|
128 |
"Modified: 24.6.1996 / 15:36:28 / stefan" |
65 | 129 |
! |
130 |
||
131 |
notifyStoreError |
|
132 |
"a store is attempted - for our convenience, find the method that |
|
133 |
contains me, for a nicer error message" |
|
134 |
||
135 |
|creator msg| |
|
136 |
||
137 |
creator := self creator. |
|
97 | 138 |
msg := 'store into/change of literal'. |
65 | 139 |
creator notNil ifTrue:[ |
140 |
msg := msg , ' (' , creator whoString , ')' |
|
141 |
]. |
|
142 |
" |
|
143 |
this error is reported on an attempt to store into a literal |
|
144 |
array. The literal was created in creator. |
|
145 |
If you press continue in the debugger, the store will be performed. |
|
146 |
If you dont want this, press abort and check your code. |
|
147 |
Storing into literals is VERY VERY bad coding style. |
|
148 |
" |
|
149 |
self error:msg |
|
150 |
! ! |
|
151 |
||
140 | 152 |
!ImmutableArray methodsFor:'private'! |
65 | 153 |
|
140 | 154 |
species |
155 |
"Copies should be mutable" |
|
156 |
||
157 |
^Array |
|
158 |
! ! |
|
65 | 159 |
|
140 | 160 |
!ImmutableArray methodsFor:'queries'! |
65 | 161 |
|
140 | 162 |
isLiteral |
163 |
"return true, if the receiver can be used as a literal |
|
164 |
(i.e. can be used in constant arrays)" |
|
65 | 165 |
|
140 | 166 |
"yes, I must be" |
167 |
^ true |
|
168 |
||
65 | 169 |
! ! |
170 |
||
97 | 171 |
!ImmutableArray methodsFor:'specials'! |
172 |
||
173 |
become:anotherObject |
|
100 | 174 |
"trigger an error if I should become something else |
175 |
(this would be an even more tricky manipulation)" |
|
176 |
||
97 | 177 |
self notifyStoreError. |
178 |
^ super become:anotherObject |
|
179 |
! |
|
180 |
||
181 |
becomeNil |
|
100 | 182 |
"trigger an error if I should become nil |
183 |
(this would be an even more tricky manipulation)" |
|
184 |
||
97 | 185 |
self notifyStoreError. |
186 |
^ super becomeNil |
|
187 |
! ! |
|
188 |
||
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
189 |
!ImmutableArray class methodsFor:'documentation'! |
148 | 190 |
|
191 |
version |
|
539
69a1cd05c7d6
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
292
diff
changeset
|
192 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ImmArray.st,v 1.16 1997-06-19 14:57:04 cg Exp $' |
148 | 193 |
! ! |