author | Claus Gittinger <cg@exept.de> |
Mon, 27 Feb 2012 20:44:21 +0100 | |
changeset 14023 | a61f74fd04c9 |
parent 12538 | e6bf8c42e1d4 |
child 14113 | 367fab89c469 |
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 |
||
619 | 142 |
!ImmutableArray methodsFor:'private'! |
274 | 143 |
|
619 | 144 |
species |
145 |
"Copies should be mutable" |
|
146 |
||
6550 | 147 |
^ Array |
619 | 148 |
! ! |
274 | 149 |
|
619 | 150 |
!ImmutableArray methodsFor:'queries'! |
274 | 151 |
|
619 | 152 |
isLiteral |
4661 | 153 |
"return true, if the receiver can be used as a literal constant in ST syntax |
619 | 154 |
(i.e. can be used in constant arrays)" |
274 | 155 |
|
619 | 156 |
"yes, I must be" |
157 |
^ true |
|
158 |
||
274 | 159 |
! ! |
160 |
||
361 | 161 |
!ImmutableArray methodsFor:'specials'! |
162 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12492
diff
changeset
|
163 |
become:anotherObject |
371 | 164 |
"trigger an error if I should become something else |
165 |
(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
|
166 |
|
12492 | 167 |
self noModificationError. |
361 | 168 |
^ super become:anotherObject |
169 |
! |
|
170 |
||
171 |
becomeNil |
|
371 | 172 |
"trigger an error if I should become nil |
173 |
(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
|
174 |
|
12492 | 175 |
self noModificationError. |
361 | 176 |
^ super becomeNil |
177 |
! ! |
|
178 |
||
2697
fc8552cd6ebf
added node-walk facility (ST80 compatibility)
Claus Gittinger <cg@exept.de>
parents:
1493
diff
changeset
|
179 |
!ImmutableArray class methodsFor:'documentation'! |
660 | 180 |
|
181 |
version |
|
14023
a61f74fd04c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
12538
diff
changeset
|
182 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.29 2012-02-27 19:44:21 cg Exp $' |
12455 | 183 |
! |
184 |
||
185 |
version_CVS |
|
14023
a61f74fd04c9
moved: #noModificationError
Claus Gittinger <cg@exept.de>
parents:
12538
diff
changeset
|
186 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableArray.st,v 1.29 2012-02-27 19:44:21 cg Exp $' |
660 | 187 |
! ! |