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
|
|
14 |
instanceVariableNames:''
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
69
|
17 |
category:'System-Compiler-Support'
|
65
|
18 |
!
|
|
19 |
|
|
20 |
!ImmutableArray class methodsFor:'documentation'!
|
|
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.
|
65
|
56 |
"
|
140
|
57 |
! !
|
|
58 |
|
|
59 |
!ImmutableArray methodsFor:'accessing'!
|
|
60 |
|
|
61 |
at: index put: value
|
|
62 |
"Trigger an error if an immutable array is stored into.
|
|
63 |
The store will be performed (for compatibility reasons) if you continue
|
|
64 |
in the debugger."
|
|
65 |
|
|
66 |
self notifyStoreError.
|
|
67 |
^ super at: index put: value
|
|
68 |
!
|
|
69 |
|
|
70 |
basicAt: index put: value
|
|
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.
|
|
76 |
^ super basicAt: index put: value
|
|
77 |
! !
|
|
78 |
|
|
79 |
!ImmutableArray methodsFor:'copying'!
|
|
80 |
|
|
81 |
copyEmpty
|
|
82 |
"when copying, return a real (mutable) Array"
|
|
83 |
|
|
84 |
^ Array new:self size
|
|
85 |
!
|
|
86 |
|
|
87 |
copyEmptyAndGrow:size
|
|
88 |
"when copying, return a real (mutable) Array"
|
|
89 |
|
|
90 |
^ Array new:size
|
|
91 |
!
|
|
92 |
|
|
93 |
postCopy
|
|
94 |
"when copied, make it me a real (mutable) Array"
|
|
95 |
|
|
96 |
self changeClassTo:Array.
|
|
97 |
!
|
|
98 |
|
|
99 |
shallowCopy
|
|
100 |
"when copying, return a real (mutable) Array"
|
|
101 |
|
|
102 |
|sz|
|
|
103 |
|
|
104 |
sz := self size.
|
|
105 |
^ (Array new:sz)
|
|
106 |
replaceFrom:1 to:sz with:self startingAt:1
|
65
|
107 |
! !
|
|
108 |
|
|
109 |
!ImmutableArray methodsFor:'error handling'!
|
|
110 |
|
|
111 |
creator
|
|
112 |
"find the method that contains me"
|
|
113 |
|
84
|
114 |
Method allSubInstances do:[:aMethod |
|
65
|
115 |
|lits|
|
|
116 |
|
|
117 |
lits := aMethod literals.
|
|
118 |
(lits notNil and:[(lits identityIndexOf:self) ~~ 0]) ifTrue:[
|
|
119 |
^ aMethod
|
|
120 |
]
|
|
121 |
].
|
|
122 |
^ nil
|
|
123 |
!
|
|
124 |
|
|
125 |
notifyStoreError
|
|
126 |
"a store is attempted - for our convenience, find the method that
|
|
127 |
contains me, for a nicer error message"
|
|
128 |
|
|
129 |
|creator msg|
|
|
130 |
|
|
131 |
creator := self creator.
|
97
|
132 |
msg := 'store into/change of literal'.
|
65
|
133 |
creator notNil ifTrue:[
|
|
134 |
msg := msg , ' (' , creator whoString , ')'
|
|
135 |
].
|
|
136 |
"
|
|
137 |
this error is reported on an attempt to store into a literal
|
|
138 |
array. The literal was created in creator.
|
|
139 |
If you press continue in the debugger, the store will be performed.
|
|
140 |
If you dont want this, press abort and check your code.
|
|
141 |
Storing into literals is VERY VERY bad coding style.
|
|
142 |
"
|
|
143 |
self error:msg
|
|
144 |
! !
|
|
145 |
|
140
|
146 |
!ImmutableArray methodsFor:'private'!
|
65
|
147 |
|
140
|
148 |
species
|
|
149 |
"Copies should be mutable"
|
|
150 |
|
|
151 |
^Array
|
|
152 |
! !
|
65
|
153 |
|
140
|
154 |
!ImmutableArray methodsFor:'queries'!
|
65
|
155 |
|
140
|
156 |
isLiteral
|
|
157 |
"return true, if the receiver can be used as a literal
|
|
158 |
(i.e. can be used in constant arrays)"
|
65
|
159 |
|
140
|
160 |
"yes, I must be"
|
|
161 |
^ true
|
|
162 |
|
65
|
163 |
! !
|
|
164 |
|
97
|
165 |
!ImmutableArray methodsFor:'specials'!
|
|
166 |
|
|
167 |
become:anotherObject
|
100
|
168 |
"trigger an error if I should become something else
|
|
169 |
(this would be an even more tricky manipulation)"
|
|
170 |
|
97
|
171 |
self notifyStoreError.
|
|
172 |
^ super become:anotherObject
|
|
173 |
!
|
|
174 |
|
|
175 |
becomeNil
|
100
|
176 |
"trigger an error if I should become nil
|
|
177 |
(this would be an even more tricky manipulation)"
|
|
178 |
|
97
|
179 |
self notifyStoreError.
|
|
180 |
^ super becomeNil
|
|
181 |
! !
|
|
182 |
|
148
|
183 |
!ImmutableArray class methodsFor:'documentation'!
|
|
184 |
|
|
185 |
version
|
|
186 |
^ '$Header: /cvs/stx/stx/libcomp/Attic/ImmArray.st,v 1.12 1995-12-03 12:15:31 cg Exp $'
|
|
187 |
! !
|