author | sr |
Wed, 14 Sep 2011 11:25:43 +0200 | |
changeset 13693 | 16c890248840 |
parent 12538 | e6bf8c42e1d4 |
child 14024 | f4f9f043c086 |
permissions | -rw-r--r-- |
12469 | 1 |
" |
2 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
3 |
All Rights Reserved |
12469 | 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:
12493
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
12456 | 13 |
|
14 |
ByteArray variableByteSubclass:#ImmutableByteArray |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
19 |
! |
|
20 |
||
12465 | 21 |
!ImmutableByteArray class methodsFor:'documentation'! |
22 |
||
12469 | 23 |
copyright |
24 |
" |
|
25 |
COPYRIGHT (c) 2009 by eXept Software AG |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
26 |
All Rights Reserved |
12469 | 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 |
||
12465 | 37 |
documentation |
38 |
" |
|
39 |
By default, byte 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 byte-array object as argument to someone else, who changes the |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
42 |
bytearray using at:put: like messages. Since the bytearray object is kept in |
12465 | 43 |
the first methods literals, the bytearray 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 ImmutableByteArray instead of ByteArrays |
|
49 |
for literals. Instances of ImmutableByteArray 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:
12493
diff
changeset
|
51 |
for compatibility to other smalltalk implementations. |
12465 | 52 |
(Also, if turned on, this makes inspecting bytearray literals entered in |
53 |
a workspace somewhat strange: you cannot modify it any longer). |
|
54 |
||
55 |
Turn the ImmutableArray feature on, by setting the Parser's class variable |
|
56 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
|
57 |
||
12472 | 58 |
ATTENTION: |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
59 |
there may be still code around which checks for explicit class being ByteArray |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
60 |
(both in Smalltalk and in primitive code). All code like foo 'class == ByteArray' |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
61 |
or '__isByteArray' will not work with ImmutableByteArrays. |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
62 |
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:
12493
diff
changeset
|
63 |
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:
12493
diff
changeset
|
64 |
memory segment (the good solution). We will eventually implement the second in the future... |
12472 | 65 |
|
12465 | 66 |
[see also:] |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
67 |
ImmutableString |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
68 |
ImmutableArray |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
69 |
Parser Scanner |
12465 | 70 |
|
71 |
[author:] |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
72 |
Claus Gittinger |
12465 | 73 |
" |
74 |
! ! |
|
12456 | 75 |
|
76 |
!ImmutableByteArray methodsFor:'accessing'! |
|
77 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
78 |
at:index put:value |
12456 | 79 |
"Trigger an error if an immutable bytearray 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:
12493
diff
changeset
|
82 |
|
12493 | 83 |
self noModificationError. |
12456 | 84 |
^ super at:index put:value |
85 |
! |
|
86 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
87 |
basicAt:index put:value |
12456 | 88 |
"Trigger an error if an immutable bytearray is stored into. |
89 |
The store will be performed (for compatibility reasons) if you continue |
|
90 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
91 |
|
12493 | 92 |
self noModificationError. |
12456 | 93 |
^ super basicAt:index put:value |
94 |
! ! |
|
95 |
||
96 |
!ImmutableByteArray methodsFor:'converting'! |
|
97 |
||
12488 | 98 |
asByteArray |
99 |
"return the receiver as a (mutable) byteArray" |
|
100 |
||
101 |
^ self copy changeClassTo: ByteArray |
|
102 |
||
103 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
104 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 105 |
" |
106 |
! |
|
107 |
||
12456 | 108 |
asImmutableByteArray |
109 |
^ self |
|
110 |
! ! |
|
111 |
||
112 |
!ImmutableByteArray methodsFor:'copying'! |
|
113 |
||
114 |
copyEmpty |
|
115 |
"when copying, return a real (mutable) ByteArray" |
|
116 |
||
117 |
^ ByteArray new:self size |
|
118 |
! |
|
119 |
||
120 |
copyEmptyAndGrow:size |
|
121 |
"when copying, return a real (mutable) ByteArray" |
|
122 |
||
123 |
^ ByteArray new:size |
|
124 |
! |
|
125 |
||
126 |
postCopy |
|
127 |
"when copied, make me a real (mutable) ByteArray" |
|
128 |
||
129 |
self changeClassTo:ByteArray. |
|
130 |
! |
|
131 |
||
132 |
postDeepCopy |
|
133 |
"when copied, make me a real (mutable) ByteArray" |
|
134 |
||
135 |
self changeClassTo:ByteArray. |
|
136 |
||
137 |
" |
|
138 |
#[1 2 3 4] asImmutableByteArray copy class |
|
139 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
140 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
141 |
" |
|
142 |
! |
|
143 |
||
144 |
shallowCopy |
|
145 |
"when copying, return a real (mutable) ByteArray" |
|
146 |
||
147 |
|sz| |
|
148 |
||
149 |
sz := self size. |
|
150 |
^ (ByteArray new:sz) |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
151 |
replaceFrom:1 to:sz with:self startingAt:1 |
12456 | 152 |
! ! |
153 |
||
154 |
!ImmutableByteArray methodsFor:'error handling'! |
|
155 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
156 |
creator |
12456 | 157 |
"find the method that contains me" |
158 |
||
159 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
|
160 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
161 |
" |
12456 | 162 |
#(1 2 3) creator |
163 |
" |
|
164 |
||
165 |
"Modified: 24.6.1996 / 15:36:28 / stefan" |
|
166 |
! |
|
167 |
||
12493 | 168 |
noModificationError |
12456 | 169 |
"a store is attempted - for our convenience, find the method that |
170 |
contains me, for a nicer error message" |
|
171 |
||
12493 | 172 |
|creator msg context| |
12456 | 173 |
|
174 |
creator := self creator. |
|
175 |
creator notNil ifTrue:[ |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
176 |
msg := ' (' , creator whoString , ')' |
12456 | 177 |
]. |
12493 | 178 |
context := thisContext sender. |
179 |
" |
|
12456 | 180 |
this error is reported on an attempt to store into a literal |
12493 | 181 |
byte array. The literal was created in creator. |
12456 | 182 |
If you press continue in the debugger, the store will be performed. |
183 |
If you don't want this, press abort and check your code. |
|
184 |
Storing into literals is VERY VERY bad coding style. |
|
185 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
186 |
NoModificationError |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
187 |
raiseRequestWith:self |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
188 |
errorString:msg |
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
189 |
in:context. |
12493 | 190 |
|
191 |
"Created: / 3.8.1998 / 14:47:45 / cg" |
|
12456 | 192 |
! ! |
193 |
||
194 |
!ImmutableByteArray methodsFor:'private'! |
|
195 |
||
196 |
species |
|
197 |
"Copies should be mutable" |
|
198 |
||
199 |
^ ByteArray |
|
200 |
! ! |
|
201 |
||
202 |
!ImmutableByteArray methodsFor:'queries'! |
|
203 |
||
204 |
isLiteral |
|
205 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
206 |
(i.e. can be used in constant arrays)" |
|
207 |
||
208 |
"yes, I must be" |
|
209 |
^ true |
|
210 |
||
12488 | 211 |
! |
212 |
||
213 |
referencesAny:aCollection |
|
214 |
"redefined to speed up searching when many byteArray instances are present" |
|
215 |
||
216 |
^ false |
|
12456 | 217 |
! ! |
218 |
||
219 |
!ImmutableByteArray methodsFor:'specials'! |
|
220 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
221 |
become:anotherObject |
12456 | 222 |
"trigger an error if I should become something else |
223 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
224 |
|
12493 | 225 |
self noModificationError. |
12456 | 226 |
^ super become:anotherObject |
227 |
! |
|
228 |
||
229 |
becomeNil |
|
230 |
"trigger an error if I should become nil |
|
231 |
(this would be an even more tricky manipulation)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
232 |
|
12493 | 233 |
self noModificationError. |
12456 | 234 |
^ super becomeNil |
235 |
! ! |
|
236 |
||
237 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
238 |
||
239 |
version_CVS |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
240 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.7 2009-11-16 08:59:40 stefan Exp $' |
12456 | 241 |
! ! |