author | Claus Gittinger <cg@exept.de> |
Wed, 07 Sep 2011 09:29:58 +0200 | |
changeset 13639 | 01a74c51c29a |
parent 12538 | e6bf8c42e1d4 |
child 14023 | a61f74fd04c9 |
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 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
5798 | 13 |
|
4661 | 14 |
Array variableSubclass:#ImmutableArray |
1284 | 15 |
instanceVariableNames:'' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
274 | 19 |
! |
20 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
21 |
!ImmutableArray class methodsFor:'documentation'! |
274 | 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 |
|
364 | 40 |
may lead to some subtle (and hard to find errors), if some method passes |
274 | 41 |
a literal array constant as argument to someone else, who changes the |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
42 |
array using at:put: like messages. Since the array object is kept in |
274 | 43 |
the first methods literals, the array constant has now been changed without |
12463 | 44 |
having the method's sourcecode reflect this. Thus, the method will |
274 | 45 |
behave differently from what its source may make you think. |
46 |
||
364 | 47 |
To help finding this kind of 'feature/bug', the compiler can be |
274 | 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 |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
51 |
for compatibility to other smalltalk implementations. |
274 | 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 |
|
364 | 56 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
1296 | 57 |
|
12470 | 58 |
|
59 |
ATTENTION: |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
60 |
there may be still code around which checks for explicit class being Array |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
61 |
(both in Smalltalk and in primitive code). All code like foo 'class == Array' |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
62 |
or '__isArray' will not work with ImmutableArrays. |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
63 |
A somewhat better approach would be to either add a flag to the object (mutability) |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
64 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
65 |
memory segment (the good solution). We will eventually implement the second in the future... |
12470 | 66 |
|
3715 | 67 |
[see also:] |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
68 |
ImmutableString |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
69 |
Parser Scanner |
3715 | 70 |
|
1296 | 71 |
[author:] |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
72 |
Claus Gittinger |
274 | 73 |
" |
619 | 74 |
! ! |
75 |
||
76 |
!ImmutableArray methodsFor:'accessing'! |
|
77 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
78 |
at:index put:value |
619 | 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." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
82 |
|
12492 | 83 |
self noModificationError. |
3715 | 84 |
^ super at:index put:value |
85 |
||
86 |
"Modified: / 3.8.1998 / 14:45:23 / cg" |
|
619 | 87 |
! |
88 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
89 |
basicAt:index put:value |
619 | 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." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
93 |
|
12492 | 94 |
self noModificationError. |
3715 | 95 |
^ super basicAt:index put:value |
96 |
||
97 |
"Modified: / 3.8.1998 / 14:45:30 / cg" |
|
619 | 98 |
! ! |
99 |
||
12455 | 100 |
!ImmutableArray methodsFor:'converting'! |
101 |
||
102 |
asImmutableArray |
|
103 |
^ self |
|
104 |
! ! |
|
105 |
||
619 | 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 |
||
8931 | 126 |
postDeepCopy |
127 |
"when copied, make it me a real (mutable) Array" |
|
128 |
||
129 |
self changeClassTo:Array. |
|
130 |
! |
|
131 |
||
619 | 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 |
|
274 | 140 |
! ! |
141 |
||
142 |
!ImmutableArray methodsFor:'error handling'! |
|
143 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
144 |
creator |
274 | 145 |
"find the method that contains me" |
146 |
||
9237 | 147 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
148 |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
149 |
" |
1493
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
150 |
#(1 2 3) creator |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
151 |
" |
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
152 |
|
33e226c7d187
Move method's literals form literalArray to indexed instvars.
Stefan Vogel <sv@exept.de>
parents:
1296
diff
changeset
|
153 |
"Modified: 24.6.1996 / 15:36:28 / stefan" |
274 | 154 |
! |
155 |
||
12492 | 156 |
noModificationError |
274 | 157 |
"a store is attempted - for our convenience, find the method that |
158 |
contains me, for a nicer error message" |
|
159 |
||
12492 | 160 |
|creator msg context| |
274 | 161 |
|
162 |
creator := self creator. |
|
163 |
creator notNil ifTrue:[ |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
164 |
msg := ' (' , creator whoString , ')' |
274 | 165 |
]. |
12492 | 166 |
context := thisContext sender. |
167 |
" |
|
274 | 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. |
|
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
6550
diff
changeset
|
171 |
If you don't want this, press abort and check your code. |
274 | 172 |
Storing into literals is VERY VERY bad coding style. |
173 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
174 |
NoModificationError |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
175 |
raiseRequestWith:self |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
176 |
errorString:msg |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
177 |
in:context. |
12492 | 178 |
|
179 |
"Created: / 3.8.1998 / 14:47:45 / cg" |
|
274 | 180 |
! ! |
181 |
||
619 | 182 |
!ImmutableArray methodsFor:'private'! |
274 | 183 |
|
619 | 184 |
species |
185 |
"Copies should be mutable" |
|
186 |
||
6550 | 187 |
^ Array |
619 | 188 |
! ! |
274 | 189 |
|
619 | 190 |
!ImmutableArray methodsFor:'queries'! |
274 | 191 |
|
619 | 192 |
isLiteral |
4661 | 193 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 194 |
(i.e. can be used in constant arrays)" |
274 | 195 |
|
619 | 196 |
"yes, I must be" |
197 |
^ true |
|
198 |
||
274 | 199 |
! ! |
200 |
||
361 | 201 |
!ImmutableArray methodsFor:'specials'! |
202 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
203 |
become:anotherObject |
371 | 204 |
"trigger an error if I should become something else |
205 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
206 |
|
12492 | 207 |
self noModificationError. |
361 | 208 |
^ super become:anotherObject |
209 |
! |
|
210 |
||
211 |
becomeNil |
|
371 | 212 |
"trigger an error if I should become nil |
213 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
214 |
|
12492 | 215 |
self noModificationError. |
361 | 216 |
^ super becomeNil |
217 |
! ! |
|
218 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
219 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 220 |
|
221 |
version |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
222 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.28 2009-11-16 08:59:40 stefan Exp $' |
12455 | 223 |
! |
224 |
||
225 |
version_CVS |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
226 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.28 2009-11-16 08:59:40 stefan Exp $' |
660 | 227 |
! ! |