author | Claus Gittinger <cg@exept.de> |
Wed, 23 Mar 2016 14:38:20 +0100 | |
changeset 19421 | 08358cad9dbe |
parent 19175 | 03a070a3c83f |
child 19431 | 3e697e4bcbf5 |
child 20524 | 6b3c5f464f78 |
permissions | -rw-r--r-- |
19421 | 1 |
"{ Encoding: utf8 }" |
2 |
||
12469 | 3 |
" |
4 |
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
|
5 |
All Rights Reserved |
12469 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
12456 | 15 |
|
17374 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
12456 | 18 |
ByteArray variableByteSubclass:#ImmutableByteArray |
19 |
instanceVariableNames:'' |
|
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'System-Compiler-Support' |
|
23 |
! |
|
24 |
||
12465 | 25 |
!ImmutableByteArray class methodsFor:'documentation'! |
26 |
||
12469 | 27 |
copyright |
28 |
" |
|
29 |
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
|
30 |
All Rights Reserved |
12469 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
12465 | 41 |
documentation |
42 |
" |
|
43 |
By default, byte array literals in smalltalk are mutable objects. That |
|
44 |
may lead to some subtle (and hard to find errors), if some method passes |
|
45 |
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
|
46 |
bytearray using at:put: like messages. Since the bytearray object is kept in |
12465 | 47 |
the first methods literals, the bytearray constant has now been changed without |
48 |
having the method's sourcecode reflect this. Thus, the method will |
|
49 |
behave differently from what its source may make you think. |
|
50 |
||
51 |
To help finding this kind of 'feature/bug', the compiler can be |
|
52 |
configured to create instances of this ImmutableByteArray instead of ByteArrays |
|
53 |
for literals. Instances of ImmutableByteArray catch storing accesses and |
|
54 |
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
|
55 |
for compatibility to other smalltalk implementations. |
12465 | 56 |
(Also, if turned on, this makes inspecting bytearray literals entered in |
57 |
a workspace somewhat strange: you cannot modify it any longer). |
|
58 |
||
59 |
Turn the ImmutableArray feature on, by setting the Parser's class variable |
|
60 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
|
61 |
||
18545 | 62 |
This class should be used only by the compiler. |
63 |
||
12472 | 64 |
ATTENTION: |
14223 | 65 |
there may be still code around which checks for explicit class being ByteArray |
66 |
(both in Smalltalk and in primitive code). All code like foo 'class == ByteArray' |
|
67 |
or '__isByteArray()' will not work with ImmutableByteArrays - consider using '__isByteArrayLike()'. |
|
68 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
69 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
70 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12472 | 71 |
|
12465 | 72 |
[see also:] |
14223 | 73 |
ImmutableString |
74 |
ImmutableArray |
|
75 |
Parser Scanner |
|
12465 | 76 |
|
77 |
[author:] |
|
14223 | 78 |
Claus Gittinger |
12465 | 79 |
" |
80 |
! ! |
|
12456 | 81 |
|
17374 | 82 |
!ImmutableByteArray class methodsFor:'queries'! |
83 |
||
84 |
mutableClass |
|
85 |
"answer an equivalent mustable class" |
|
86 |
||
87 |
^ ByteArray |
|
88 |
! ! |
|
89 |
||
90 |
!ImmutableByteArray class methodsFor:'testing'! |
|
91 |
||
92 |
hasImmutableInstances |
|
93 |
^ true |
|
94 |
! ! |
|
95 |
||
12456 | 96 |
!ImmutableByteArray methodsFor:'accessing'! |
97 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
98 |
at:index put:value |
12456 | 99 |
"Trigger an error if an immutable bytearray is stored into. |
100 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 101 |
in the debugger (or proceed in an exception handler)." |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
102 |
|
12493 | 103 |
self noModificationError. |
12456 | 104 |
^ super at:index put:value |
105 |
! |
|
106 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
107 |
basicAt:index put:value |
12456 | 108 |
"Trigger an error if an immutable bytearray is stored into. |
109 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 110 |
in the debugger (or proceed in an exception handler)." |
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
111 |
|
12493 | 112 |
self noModificationError. |
12456 | 113 |
^ super basicAt:index put:value |
19421 | 114 |
! |
115 |
||
116 |
byteAt:index put:value |
|
117 |
"Trigger an error if an immutable bytearray is stored into. |
|
118 |
The store will be performed (for compatibility reasons) if you continue |
|
119 |
in the debugger (or proceed in an exception handler)." |
|
120 |
||
121 |
self noModificationError. |
|
122 |
^ super byteAt:index put:value |
|
123 |
! |
|
124 |
||
125 |
unsignedInt16At:index put:anInteger |
|
126 |
"Trigger an error if an immutable bytearray is stored into. |
|
127 |
The store will be performed (for compatibility reasons) if you continue |
|
128 |
in the debugger (or proceed in an exception handler)." |
|
129 |
||
130 |
self noModificationError. |
|
131 |
^ super unsignedInt16At:index put:anInteger |
|
132 |
! |
|
133 |
||
134 |
unsignedInt16At:index put:anInteger MSB:msb |
|
135 |
"Trigger an error if an immutable bytearray is stored into. |
|
136 |
The store will be performed (for compatibility reasons) if you continue |
|
137 |
in the debugger (or proceed in an exception handler)." |
|
138 |
||
139 |
self noModificationError. |
|
140 |
^ super unsignedInt16At:index put:anInteger MSB:msb |
|
141 |
! |
|
142 |
||
143 |
unsignedInt32At:index put:anInteger |
|
144 |
"Trigger an error if an immutable bytearray is stored into. |
|
145 |
The store will be performed (for compatibility reasons) if you continue |
|
146 |
in the debugger (or proceed in an exception handler)." |
|
147 |
||
148 |
self noModificationError. |
|
149 |
^ super unsignedInt32At:index put:anInteger |
|
150 |
! |
|
151 |
||
152 |
unsignedInt32At:index put:anInteger MSB:msb |
|
153 |
"Trigger an error if an immutable bytearray is stored into. |
|
154 |
The store will be performed (for compatibility reasons) if you continue |
|
155 |
in the debugger (or proceed in an exception handler)." |
|
156 |
||
157 |
self noModificationError. |
|
158 |
^ super unsignedInt32At:index put:anInteger MSB:msb |
|
159 |
! |
|
160 |
||
161 |
unsignedInt64At:index put:anInteger |
|
162 |
"Trigger an error if an immutable bytearray is stored into. |
|
163 |
The store will be performed (for compatibility reasons) if you continue |
|
164 |
in the debugger (or proceed in an exception handler)." |
|
165 |
||
166 |
self noModificationError. |
|
167 |
^ super unsignedInt64At:index put:anInteger |
|
168 |
! |
|
169 |
||
170 |
unsignedInt64At:index put:anInteger MSB:msb |
|
171 |
"Trigger an error if an immutable bytearray is stored into. |
|
172 |
The store will be performed (for compatibility reasons) if you continue |
|
173 |
in the debugger (or proceed in an exception handler)." |
|
174 |
||
175 |
self noModificationError. |
|
176 |
^ super unsignedInt64At:index put:anInteger MSB:msb |
|
12456 | 177 |
! ! |
178 |
||
179 |
!ImmutableByteArray methodsFor:'converting'! |
|
180 |
||
12488 | 181 |
asByteArray |
182 |
"return the receiver as a (mutable) byteArray" |
|
183 |
||
19421 | 184 |
^ self shallowCopy |
12488 | 185 |
|
186 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
187 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 188 |
" |
189 |
! |
|
190 |
||
12456 | 191 |
asImmutableByteArray |
19421 | 192 |
"that's what I am-" |
193 |
||
12456 | 194 |
^ self |
16823 | 195 |
! |
196 |
||
197 |
asMutableCollection |
|
198 |
"return a writable copy of myself" |
|
199 |
||
19421 | 200 |
^ self shallowCopy |
19175 | 201 |
! |
202 |
||
203 |
beImmutable |
|
204 |
"that's what I am" |
|
205 |
||
206 |
^ self |
|
207 |
! |
|
208 |
||
209 |
beUnsigned |
|
210 |
"that's what I am-" |
|
211 |
||
212 |
^ self. |
|
213 |
||
214 |
" |
|
215 |
#[1 2 3 128 255] asImmutableByteArray beUnsigned |
|
216 |
" |
|
12456 | 217 |
! ! |
218 |
||
219 |
!ImmutableByteArray methodsFor:'copying'! |
|
220 |
||
221 |
postCopy |
|
222 |
"when copied, make me a real (mutable) ByteArray" |
|
223 |
||
224 |
self changeClassTo:ByteArray. |
|
225 |
! |
|
226 |
||
227 |
postDeepCopy |
|
228 |
"when copied, make me a real (mutable) ByteArray" |
|
229 |
||
230 |
self changeClassTo:ByteArray. |
|
231 |
||
232 |
" |
|
233 |
#[1 2 3 4] asImmutableByteArray copy class |
|
234 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
235 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
236 |
" |
|
237 |
! |
|
238 |
||
239 |
shallowCopy |
|
240 |
"when copying, return a real (mutable) ByteArray" |
|
241 |
||
242 |
|sz| |
|
243 |
||
244 |
sz := self size. |
|
16399 | 245 |
^ (ByteArray new:sz) replaceBytesFrom:1 to:sz with:self startingAt:1 |
14114
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
246 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
247 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
248 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
249 |
" |
12456 | 250 |
! ! |
251 |
||
16749 | 252 |
!ImmutableByteArray methodsFor:'printing & storing'! |
253 |
||
254 |
displayOn:aGCOrStream |
|
17536 | 255 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
256 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
16749 | 257 |
(aGCOrStream isStream) ifTrue:[ |
258 |
self storeOn:aGCOrStream. |
|
259 |
aGCOrStream nextPutAll:'"immutable"'. |
|
260 |
^ self. |
|
261 |
]. |
|
262 |
super displayOn:aGCOrStream. |
|
263 |
! ! |
|
264 |
||
12456 | 265 |
!ImmutableByteArray methodsFor:'private'! |
266 |
||
267 |
species |
|
268 |
"Copies should be mutable" |
|
269 |
||
270 |
^ ByteArray |
|
16399 | 271 |
! |
272 |
||
273 |
speciesForCopy |
|
274 |
"Copies should be mutable" |
|
275 |
||
276 |
^ ByteArray |
|
12456 | 277 |
! ! |
278 |
||
279 |
!ImmutableByteArray methodsFor:'queries'! |
|
280 |
||
14388 | 281 |
isImmutable |
282 |
^ true |
|
283 |
! |
|
284 |
||
12456 | 285 |
isLiteral |
286 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
287 |
(i.e. can be used in constant arrays)" |
|
288 |
||
289 |
"yes, I must be" |
|
290 |
^ true |
|
291 |
||
292 |
! ! |
|
293 |
||
294 |
!ImmutableByteArray methodsFor:'specials'! |
|
295 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
296 |
become:anotherObject |
12456 | 297 |
"trigger an error if I should become something else |
298 |
(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
|
299 |
|
12493 | 300 |
self noModificationError. |
12456 | 301 |
^ super become:anotherObject |
302 |
! |
|
303 |
||
304 |
becomeNil |
|
305 |
"trigger an error if I should become nil |
|
306 |
(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
|
307 |
|
12493 | 308 |
self noModificationError. |
12456 | 309 |
^ super becomeNil |
18703 | 310 |
! |
311 |
||
312 |
becomeSameAs:anotherObject |
|
313 |
"trigger an error if I should become something else |
|
314 |
(this would be an even more tricky manipulation)" |
|
315 |
||
316 |
self noModificationError. |
|
317 |
^ super becomeSameAs:anotherObject |
|
12456 | 318 |
! ! |
319 |
||
320 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
321 |
||
322 |
version_CVS |
|
18545 | 323 |
^ '$Header$' |
12456 | 324 |
! ! |
16399 | 325 |