17735
|
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 |
"{ Package: 'stx:libbasic' }"
|
|
13 |
|
|
14 |
Array variableSubclass:#ImmutableArray
|
|
15 |
instanceVariableNames:''
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'System-Compiler-Support'
|
|
19 |
!
|
|
20 |
|
|
21 |
!ImmutableArray class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
|
39 |
By default, array literals in smalltalk are mutable objects. That
|
|
40 |
may lead to some subtle (and hard to find errors), if some method passes
|
|
41 |
a literal array constant as argument to someone else, who changes the
|
|
42 |
array using at:put: like messages. Since the array object is kept in
|
|
43 |
the first methods literals, the array constant has now been changed without
|
|
44 |
having the method's sourcecode reflect this. Thus, the method will
|
|
45 |
behave differently from what its source may make you think.
|
|
46 |
|
|
47 |
To help finding this kind of 'feature/bug', the compiler can be
|
|
48 |
configured to create instances of this ImmutableArray instead of Arrays
|
|
49 |
for array literals. Instances of ImmutableArray catch storing accesses and
|
|
50 |
enter the debugger. Although useful, this feature is disabled by default
|
|
51 |
for compatibility to other smalltalk implementations.
|
|
52 |
(Also, if turned on, this makes inspecting array literals entered in
|
|
53 |
a workspace somewhat strange: you cannot modify it any longer).
|
|
54 |
|
|
55 |
Turn the ImmutableArray feature on by setting the Parsers class variable
|
|
56 |
'ArraysAreImmutable' to true or use the new launchers settings menu.
|
|
57 |
|
|
58 |
|
|
59 |
ATTENTION:
|
|
60 |
there may be still code around which checks for explicit class being Array
|
|
61 |
(both in Smalltalk and in primitive code). All code like foo 'class == Array'
|
|
62 |
or '__isArray' will not work with ImmutableArrays.
|
|
63 |
A somewhat better approach would be to either add a flag to the object (mutability)
|
|
64 |
and check this dynamically (expensive) or to place immutable objects into a readonly
|
|
65 |
memory segment (the good solution). We will eventually implement the second in the future...
|
|
66 |
|
|
67 |
[see also:]
|
|
68 |
ImmutableString
|
|
69 |
Parser Scanner
|
|
70 |
|
|
71 |
[author:]
|
|
72 |
Claus Gittinger
|
|
73 |
"
|
|
74 |
! !
|
|
75 |
|
|
76 |
!ImmutableArray methodsFor:'accessing'!
|
|
77 |
|
|
78 |
at:index put:value
|
|
79 |
"Trigger an error if an immutable array is stored into.
|
|
80 |
The store will be performed (for compatibility reasons) if you continue
|
|
81 |
in the debugger."
|
|
82 |
|
|
83 |
self noModificationError.
|
|
84 |
^ super at:index put:value
|
|
85 |
|
|
86 |
"Modified: / 3.8.1998 / 14:45:23 / cg"
|
|
87 |
!
|
|
88 |
|
|
89 |
basicAt:index put:value
|
|
90 |
"Trigger an error if an immutable array is stored into.
|
|
91 |
The store will be performed (for compatibility reasons) if you continue
|
|
92 |
in the debugger."
|
|
93 |
|
|
94 |
self noModificationError.
|
|
95 |
^ super basicAt:index put:value
|
|
96 |
|
|
97 |
"Modified: / 3.8.1998 / 14:45:30 / cg"
|
|
98 |
! !
|
|
99 |
|
|
100 |
!ImmutableArray methodsFor:'converting'!
|
|
101 |
|
|
102 |
asImmutableArray
|
|
103 |
^ self
|
|
104 |
! !
|
|
105 |
|
|
106 |
!ImmutableArray methodsFor:'copying'!
|
|
107 |
|
|
108 |
copyEmpty
|
|
109 |
"when copying, return a real (mutable) Array"
|
|
110 |
|
|
111 |
^ Array new:self size
|
|
112 |
!
|
|
113 |
|
|
114 |
copyEmptyAndGrow:size
|
|
115 |
"when copying, return a real (mutable) Array"
|
|
116 |
|
|
117 |
^ Array new:size
|
|
118 |
!
|
|
119 |
|
|
120 |
postCopy
|
|
121 |
"when copied, make it me a real (mutable) Array"
|
|
122 |
|
|
123 |
self changeClassTo:Array.
|
|
124 |
!
|
|
125 |
|
|
126 |
postDeepCopy
|
|
127 |
"when copied, make it me a real (mutable) Array"
|
|
128 |
|
|
129 |
self changeClassTo:Array.
|
|
130 |
!
|
|
131 |
|
|
132 |
shallowCopy
|
|
133 |
"when copying, return a real (mutable) Array"
|
|
134 |
|
|
135 |
|sz|
|
|
136 |
|
|
137 |
sz := self size.
|
|
138 |
^ (Array new:sz)
|
|
139 |
replaceFrom:1 to:sz with:self startingAt:1
|
|
140 |
! !
|
|
141 |
|
|
142 |
!ImmutableArray methodsFor:'error handling'!
|
|
143 |
|
|
144 |
creator
|
|
145 |
"find the method that contains me"
|
|
146 |
|
|
147 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil.
|
|
148 |
|
|
149 |
"
|
|
150 |
#(1 2 3) creator
|
|
151 |
"
|
|
152 |
|
|
153 |
"Modified: 24.6.1996 / 15:36:28 / stefan"
|
|
154 |
!
|
|
155 |
|
|
156 |
noModificationError
|
|
157 |
"a store is attempted - for our convenience, find the method that
|
|
158 |
contains me, for a nicer error message"
|
|
159 |
|
|
160 |
|creator msg context|
|
|
161 |
|
|
162 |
creator := self creator.
|
|
163 |
creator notNil ifTrue:[
|
|
164 |
msg := ' (' , creator whoString , ')'
|
|
165 |
].
|
|
166 |
context := thisContext sender.
|
|
167 |
"
|
|
168 |
this error is reported on an attempt to store into a literal
|
|
169 |
array. The literal was created in creator.
|
|
170 |
If you press continue in the debugger, the store will be performed.
|
|
171 |
If you don't want this, press abort and check your code.
|
|
172 |
Storing into literals is VERY VERY bad coding style.
|
|
173 |
"
|
|
174 |
NoModificationError
|
|
175 |
raiseRequestWith:self
|
|
176 |
errorString:msg
|
|
177 |
in:context.
|
|
178 |
|
|
179 |
"Created: / 3.8.1998 / 14:47:45 / cg"
|
|
180 |
! !
|
|
181 |
|
|
182 |
!ImmutableArray methodsFor:'private'!
|
|
183 |
|
|
184 |
species
|
|
185 |
"Copies should be mutable"
|
|
186 |
|
|
187 |
^ Array
|
|
188 |
! !
|
|
189 |
|
|
190 |
!ImmutableArray methodsFor:'queries'!
|
|
191 |
|
|
192 |
isLiteral
|
|
193 |
"return true, if the receiver can be used as a literal constant in ST syntax
|
|
194 |
(i.e. can be used in constant arrays)"
|
|
195 |
|
|
196 |
"yes, I must be"
|
|
197 |
^ true
|
|
198 |
|
|
199 |
! !
|
|
200 |
|
|
201 |
!ImmutableArray methodsFor:'specials'!
|
|
202 |
|
|
203 |
become:anotherObject
|
|
204 |
"trigger an error if I should become something else
|
|
205 |
(this would be an even more tricky manipulation)"
|
|
206 |
|
|
207 |
self noModificationError.
|
|
208 |
^ super become:anotherObject
|
|
209 |
!
|
|
210 |
|
|
211 |
becomeNil
|
|
212 |
"trigger an error if I should become nil
|
|
213 |
(this would be an even more tricky manipulation)"
|
|
214 |
|
|
215 |
self noModificationError.
|
|
216 |
^ super becomeNil
|
|
217 |
! !
|
|
218 |
|
|
219 |
!ImmutableArray class methodsFor:'documentation'!
|
|
220 |
|
|
221 |
version
|
17845
|
222 |
^ '$Id: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.28 2009/11/16 08:59:40 stefan Exp $'
|
17735
|
223 |
!
|
|
224 |
|
|
225 |
version_CVS
|
17845
|
226 |
^ '§Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.28 2009/11/16 08:59:40 stefan Exp §'
|
17846
|
227 |
! !
|