author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 01 Apr 2015 10:20:10 +0100 | |
branch | jv |
changeset 18120 | e3a375d5f6a8 |
parent 18011 | deb0c3355881 |
parent 17375 | b1ab13a1778f |
child 18555 | 7cc0ec139c8d |
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 |
|
17375 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
4661 | 16 |
Array variableSubclass:#ImmutableArray |
1284 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compiler-Support' |
|
274 | 21 |
! |
22 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
23 |
!ImmutableArray class methodsFor:'documentation'! |
274 | 24 |
|
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
By default, array literals in smalltalk are mutable objects. That |
|
364 | 42 |
may lead to some subtle (and hard to find errors), if some method passes |
274 | 43 |
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
|
44 |
array using at:put: like messages. Since the array object is kept in |
274 | 45 |
the first methods literals, the array constant has now been changed without |
12463 | 46 |
having the method's sourcecode reflect this. Thus, the method will |
274 | 47 |
behave differently from what its source may make you think. |
48 |
||
364 | 49 |
To help finding this kind of 'feature/bug', the compiler can be |
274 | 50 |
configured to create instances of this ImmutableArray instead of Arrays |
51 |
for array literals. Instances of ImmutableArray catch storing accesses and |
|
52 |
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
|
53 |
for compatibility to other smalltalk implementations. |
274 | 54 |
(Also, if turned on, this makes inspecting array literals entered in |
55 |
a workspace somewhat strange: you cannot modify it any longer). |
|
56 |
||
57 |
Turn the ImmutableArray feature on by setting the Parsers class variable |
|
364 | 58 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
1296 | 59 |
|
12470 | 60 |
|
61 |
ATTENTION: |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
62 |
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
|
63 |
(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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
memory segment (the good solution). We will eventually implement the second in the future... |
12470 | 68 |
|
3715 | 69 |
[see also:] |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
70 |
ImmutableString |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
71 |
Parser Scanner |
3715 | 72 |
|
1296 | 73 |
[author:] |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
74 |
Claus Gittinger |
274 | 75 |
" |
619 | 76 |
! ! |
77 |
||
17375 | 78 |
!ImmutableArray class methodsFor:'queries'! |
79 |
||
80 |
mutableClass |
|
81 |
"answer an equivalent mustable class" |
|
82 |
||
83 |
^ Array |
|
84 |
! ! |
|
85 |
||
86 |
!ImmutableArray class methodsFor:'testing'! |
|
87 |
||
88 |
hasImmutableInstances |
|
89 |
^ true |
|
90 |
! ! |
|
91 |
||
619 | 92 |
!ImmutableArray methodsFor:'accessing'! |
93 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
94 |
at:index put:value |
619 | 95 |
"Trigger an error if an immutable array is stored into. |
96 |
The store will be performed (for compatibility reasons) if you continue |
|
97 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
98 |
|
12492 | 99 |
self noModificationError. |
3715 | 100 |
^ super at:index put:value |
101 |
||
102 |
"Modified: / 3.8.1998 / 14:45:23 / cg" |
|
619 | 103 |
! |
104 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
105 |
basicAt:index put:value |
619 | 106 |
"Trigger an error if an immutable array is stored into. |
107 |
The store will be performed (for compatibility reasons) if you continue |
|
108 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
109 |
|
12492 | 110 |
self noModificationError. |
3715 | 111 |
^ super basicAt:index put:value |
112 |
||
113 |
"Modified: / 3.8.1998 / 14:45:30 / cg" |
|
619 | 114 |
! ! |
115 |
||
12455 | 116 |
!ImmutableArray methodsFor:'converting'! |
117 |
||
118 |
asImmutableArray |
|
119 |
^ self |
|
16822 | 120 |
! |
121 |
||
122 |
asMutableCollection |
|
123 |
"return a writable copy of myself" |
|
124 |
||
125 |
^ self copy changeClassTo:Array |
|
12455 | 126 |
! ! |
127 |
||
619 | 128 |
!ImmutableArray methodsFor:'copying'! |
129 |
||
130 |
postCopy |
|
131 |
"when copied, make it me a real (mutable) Array" |
|
132 |
||
133 |
self changeClassTo:Array. |
|
134 |
! |
|
135 |
||
8931 | 136 |
postDeepCopy |
137 |
"when copied, make it me a real (mutable) Array" |
|
138 |
||
139 |
self changeClassTo:Array. |
|
140 |
! |
|
141 |
||
619 | 142 |
shallowCopy |
143 |
"when copying, return a real (mutable) Array" |
|
144 |
||
145 |
|sz| |
|
146 |
||
147 |
sz := self size. |
|
16398 | 148 |
^ (Array new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
14113
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
149 |
|
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
150 |
" |
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
151 |
#(1 2 3 4) asImmutableArray shallowCopy |
367fab89c469
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14023
diff
changeset
|
152 |
" |
274 | 153 |
! ! |
154 |
||
619 | 155 |
!ImmutableArray methodsFor:'private'! |
274 | 156 |
|
619 | 157 |
species |
158 |
"Copies should be mutable" |
|
159 |
||
6550 | 160 |
^ Array |
16398 | 161 |
! |
162 |
||
163 |
speciesForCopy |
|
164 |
"Copies should be mutable" |
|
165 |
||
166 |
^ Array |
|
619 | 167 |
! ! |
274 | 168 |
|
619 | 169 |
!ImmutableArray methodsFor:'queries'! |
274 | 170 |
|
14387 | 171 |
isImmutable |
172 |
^ true |
|
173 |
! |
|
174 |
||
619 | 175 |
isLiteral |
4661 | 176 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 177 |
(i.e. can be used in constant arrays)" |
274 | 178 |
|
619 | 179 |
"yes, I must be" |
180 |
^ true |
|
181 |
||
274 | 182 |
! ! |
183 |
||
361 | 184 |
!ImmutableArray methodsFor:'specials'! |
185 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
186 |
become:anotherObject |
371 | 187 |
"trigger an error if I should become something else |
188 |
(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
|
189 |
|
12492 | 190 |
self noModificationError. |
361 | 191 |
^ super become:anotherObject |
192 |
! |
|
193 |
||
194 |
becomeNil |
|
371 | 195 |
"trigger an error if I should become nil |
196 |
(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
|
197 |
|
12492 | 198 |
self noModificationError. |
361 | 199 |
^ super becomeNil |
200 |
! ! |
|
201 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
202 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 203 |
|
204 |
version |
|
17375 | 205 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.34 2015-02-03 13:54:33 stefan Exp $' |
12455 | 206 |
! |
207 |
||
208 |
version_CVS |
|
17375 | 209 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.34 2015-02-03 13:54:33 stefan Exp $' |
660 | 210 |
! ! |
16398 | 211 |