author | Claus Gittinger <cg@exept.de> |
Mon, 27 May 2019 16:53:37 +0200 | |
changeset 24192 | 18512bf68422 |
parent 24173 | ee8d63b7e7a4 |
child 24310 | 80e6b5fa6041 |
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 |
|
17374 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
12456 | 16 |
ByteArray variableByteSubclass:#ImmutableByteArray |
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compiler-Support' |
|
21 |
! |
|
22 |
||
12465 | 23 |
!ImmutableByteArray class methodsFor:'documentation'! |
24 |
||
12469 | 25 |
copyright |
26 |
" |
|
27 |
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
|
28 |
All Rights Reserved |
12469 | 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 |
||
12465 | 39 |
documentation |
40 |
" |
|
41 |
By default, byte array literals in smalltalk are mutable objects. That |
|
42 |
may lead to some subtle (and hard to find errors), if some method passes |
|
43 |
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
|
44 |
bytearray using at:put: like messages. Since the bytearray object is kept in |
12465 | 45 |
the first methods literals, the bytearray constant has now been changed without |
46 |
having the method's sourcecode reflect this. Thus, the method will |
|
47 |
behave differently from what its source may make you think. |
|
48 |
||
49 |
To help finding this kind of 'feature/bug', the compiler can be |
|
50 |
configured to create instances of this ImmutableByteArray instead of ByteArrays |
|
51 |
for literals. Instances of ImmutableByteArray 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:
12493
diff
changeset
|
53 |
for compatibility to other smalltalk implementations. |
12465 | 54 |
(Also, if turned on, this makes inspecting bytearray literals entered in |
55 |
a workspace somewhat strange: you cannot modify it any longer). |
|
56 |
||
57 |
Turn the ImmutableArray feature on, by setting the Parser's class variable |
|
58 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
|
59 |
||
18545 | 60 |
This class should be used only by the compiler. |
61 |
||
12472 | 62 |
ATTENTION: |
14223 | 63 |
there may be still code around which checks for explicit class being ByteArray |
64 |
(both in Smalltalk and in primitive code). All code like foo 'class == ByteArray' |
|
65 |
or '__isByteArray()' will not work with ImmutableByteArrays - consider using '__isByteArrayLike()'. |
|
66 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
67 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
68 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12472 | 69 |
|
12465 | 70 |
[see also:] |
14223 | 71 |
ImmutableString |
72 |
ImmutableArray |
|
73 |
Parser Scanner |
|
12465 | 74 |
|
75 |
[author:] |
|
14223 | 76 |
Claus Gittinger |
12465 | 77 |
" |
78 |
! ! |
|
12456 | 79 |
|
21648 | 80 |
!ImmutableByteArray class methodsFor:'instance creation'! |
81 |
||
82 |
fromPackedString:aString |
|
83 |
^ (ByteArray fromPackedString:aString) beImmutable. |
|
84 |
||
85 |
"Created: / 15-03-2017 / 17:55:57 / stefan" |
|
86 |
! ! |
|
87 |
||
17374 | 88 |
!ImmutableByteArray class methodsFor:'queries'! |
89 |
||
90 |
mutableClass |
|
91 |
"answer an equivalent mustable class" |
|
92 |
||
93 |
^ ByteArray |
|
94 |
! ! |
|
95 |
||
96 |
!ImmutableByteArray class methodsFor:'testing'! |
|
97 |
||
98 |
hasImmutableInstances |
|
99 |
^ true |
|
100 |
! ! |
|
101 |
||
12456 | 102 |
!ImmutableByteArray methodsFor:'accessing'! |
103 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
104 |
at:index put:value |
12456 | 105 |
"Trigger an error if an immutable bytearray is stored into. |
106 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 107 |
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
|
108 |
|
12493 | 109 |
self noModificationError. |
12456 | 110 |
^ super at:index put:value |
111 |
! |
|
112 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
113 |
basicAt:index put:value |
12456 | 114 |
"Trigger an error if an immutable bytearray is stored into. |
115 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 116 |
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
|
117 |
|
12493 | 118 |
self noModificationError. |
12456 | 119 |
^ super basicAt:index put:value |
19421 | 120 |
! |
121 |
||
122 |
byteAt:index put:value |
|
123 |
"Trigger an error if an immutable bytearray is stored into. |
|
124 |
The store will be performed (for compatibility reasons) if you continue |
|
125 |
in the debugger (or proceed in an exception handler)." |
|
126 |
||
127 |
self noModificationError. |
|
128 |
^ super byteAt:index put:value |
|
129 |
! |
|
130 |
||
22649 | 131 |
doubleAt:index put:anInteger |
132 |
"Trigger an error if an immutable bytearray is stored into. |
|
133 |
The store will be performed (for compatibility reasons) if you continue |
|
134 |
in the debugger (or proceed in an exception handler)." |
|
135 |
||
136 |
self noModificationError. |
|
137 |
^ super doubleAt:index put:anInteger |
|
138 |
! |
|
139 |
||
140 |
doubleAt:index put:anInteger MSB:msb |
|
141 |
"Trigger an error if an immutable bytearray is stored into. |
|
142 |
The store will be performed (for compatibility reasons) if you continue |
|
143 |
in the debugger (or proceed in an exception handler)." |
|
144 |
||
145 |
self noModificationError. |
|
146 |
^ super doubleAt:index put:anInteger MSB:msb |
|
147 |
! |
|
148 |
||
149 |
floatAt:index put:anInteger |
|
150 |
"Trigger an error if an immutable bytearray is stored into. |
|
151 |
The store will be performed (for compatibility reasons) if you continue |
|
152 |
in the debugger (or proceed in an exception handler)." |
|
153 |
||
154 |
self noModificationError. |
|
155 |
^ super floatAt:index put:anInteger |
|
156 |
! |
|
157 |
||
158 |
floatAt:index put:anInteger MSB:msb |
|
159 |
"Trigger an error if an immutable bytearray is stored into. |
|
160 |
The store will be performed (for compatibility reasons) if you continue |
|
161 |
in the debugger (or proceed in an exception handler)." |
|
162 |
||
163 |
self noModificationError. |
|
164 |
^ super floatAt:index put:anInteger MSB:msb |
|
165 |
! |
|
166 |
||
167 |
signedInt128At:index put:anInteger MSB:msb |
|
168 |
"Trigger an error if an immutable bytearray is stored into. |
|
169 |
The store will be performed (for compatibility reasons) if you continue |
|
170 |
in the debugger (or proceed in an exception handler)." |
|
171 |
||
172 |
self noModificationError. |
|
173 |
^ super signedInt128At:index put:anInteger MSB:msb |
|
174 |
! |
|
175 |
||
176 |
signedInt16At:index put:anInteger MSB:msb |
|
177 |
"Trigger an error if an immutable bytearray is stored into. |
|
178 |
The store will be performed (for compatibility reasons) if you continue |
|
179 |
in the debugger (or proceed in an exception handler)." |
|
180 |
||
181 |
self noModificationError. |
|
182 |
^ super signedInt16At:index put:anInteger MSB:msb |
|
183 |
! |
|
184 |
||
185 |
signedInt32At:index put:anInteger MSB:msb |
|
186 |
"Trigger an error if an immutable bytearray is stored into. |
|
187 |
The store will be performed (for compatibility reasons) if you continue |
|
188 |
in the debugger (or proceed in an exception handler)." |
|
189 |
||
190 |
self noModificationError. |
|
191 |
^ super signedInt32At:index put:anInteger MSB:msb |
|
192 |
! |
|
193 |
||
194 |
signedInt64At:index put:anInteger MSB:msb |
|
195 |
"Trigger an error if an immutable bytearray is stored into. |
|
196 |
The store will be performed (for compatibility reasons) if you continue |
|
197 |
in the debugger (or proceed in an exception handler)." |
|
198 |
||
199 |
self noModificationError. |
|
200 |
^ super signedInt64At:index put:anInteger MSB:msb |
|
201 |
! |
|
202 |
||
203 |
unsignedInt128At:index put:anInteger MSB:msb |
|
204 |
"Trigger an error if an immutable bytearray is stored into. |
|
205 |
The store will be performed (for compatibility reasons) if you continue |
|
206 |
in the debugger (or proceed in an exception handler)." |
|
207 |
||
208 |
self noModificationError. |
|
209 |
^ super unsignedInt128At:index put:anInteger MSB:msb |
|
210 |
! |
|
211 |
||
19421 | 212 |
unsignedInt16At:index put:anInteger |
213 |
"Trigger an error if an immutable bytearray is stored into. |
|
214 |
The store will be performed (for compatibility reasons) if you continue |
|
215 |
in the debugger (or proceed in an exception handler)." |
|
216 |
||
217 |
self noModificationError. |
|
218 |
^ super unsignedInt16At:index put:anInteger |
|
219 |
! |
|
220 |
||
221 |
unsignedInt16At:index put:anInteger MSB:msb |
|
222 |
"Trigger an error if an immutable bytearray is stored into. |
|
223 |
The store will be performed (for compatibility reasons) if you continue |
|
224 |
in the debugger (or proceed in an exception handler)." |
|
225 |
||
226 |
self noModificationError. |
|
227 |
^ super unsignedInt16At:index put:anInteger MSB:msb |
|
228 |
! |
|
229 |
||
230 |
unsignedInt32At:index put:anInteger |
|
231 |
"Trigger an error if an immutable bytearray is stored into. |
|
232 |
The store will be performed (for compatibility reasons) if you continue |
|
233 |
in the debugger (or proceed in an exception handler)." |
|
234 |
||
235 |
self noModificationError. |
|
236 |
^ super unsignedInt32At:index put:anInteger |
|
237 |
! |
|
238 |
||
239 |
unsignedInt32At:index put:anInteger MSB:msb |
|
240 |
"Trigger an error if an immutable bytearray is stored into. |
|
241 |
The store will be performed (for compatibility reasons) if you continue |
|
242 |
in the debugger (or proceed in an exception handler)." |
|
243 |
||
244 |
self noModificationError. |
|
245 |
^ super unsignedInt32At:index put:anInteger MSB:msb |
|
246 |
! |
|
247 |
||
248 |
unsignedInt64At:index put:anInteger |
|
249 |
"Trigger an error if an immutable bytearray is stored into. |
|
250 |
The store will be performed (for compatibility reasons) if you continue |
|
251 |
in the debugger (or proceed in an exception handler)." |
|
252 |
||
253 |
self noModificationError. |
|
254 |
^ super unsignedInt64At:index put:anInteger |
|
255 |
! |
|
256 |
||
257 |
unsignedInt64At:index put:anInteger MSB:msb |
|
258 |
"Trigger an error if an immutable bytearray is stored into. |
|
259 |
The store will be performed (for compatibility reasons) if you continue |
|
260 |
in the debugger (or proceed in an exception handler)." |
|
261 |
||
262 |
self noModificationError. |
|
263 |
^ super unsignedInt64At:index put:anInteger MSB:msb |
|
12456 | 264 |
! ! |
265 |
||
266 |
!ImmutableByteArray methodsFor:'converting'! |
|
267 |
||
12488 | 268 |
asByteArray |
269 |
"return the receiver as a (mutable) byteArray" |
|
270 |
||
19421 | 271 |
^ self shallowCopy |
12488 | 272 |
|
273 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
274 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 275 |
" |
276 |
! |
|
277 |
||
12456 | 278 |
asImmutableByteArray |
19421 | 279 |
"that's what I am-" |
280 |
||
12456 | 281 |
^ self |
16823 | 282 |
! |
283 |
||
23923 | 284 |
asImmutableCollection |
285 |
^ self |
|
286 |
||
287 |
"Created: / 15-03-2019 / 13:53:07 / Stefan Vogel" |
|
288 |
! |
|
289 |
||
16823 | 290 |
asMutableCollection |
291 |
"return a writable copy of myself" |
|
292 |
||
19421 | 293 |
^ self shallowCopy |
19175 | 294 |
! |
295 |
||
296 |
beImmutable |
|
297 |
"that's what I am" |
|
298 |
||
299 |
^ self |
|
300 |
! |
|
301 |
||
20524 | 302 |
beMutable |
303 |
"you never go back" |
|
304 |
||
305 |
^ self shouldNotImplement |
|
306 |
! |
|
307 |
||
19175 | 308 |
beUnsigned |
309 |
"that's what I am-" |
|
310 |
||
311 |
^ self. |
|
312 |
||
313 |
" |
|
314 |
#[1 2 3 128 255] asImmutableByteArray beUnsigned |
|
315 |
" |
|
12456 | 316 |
! ! |
317 |
||
318 |
!ImmutableByteArray methodsFor:'copying'! |
|
319 |
||
24173 | 320 |
shallowCopy |
321 |
"when copying, return a real (mutable) ByteArray" |
|
322 |
||
323 |
|sz| |
|
324 |
||
325 |
sz := self size. |
|
326 |
^ (ByteArray new:sz) replaceBytesFrom:1 to:sz with:self startingAt:1 |
|
327 |
||
328 |
" |
|
329 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
|
330 |
" |
|
331 |
! ! |
|
332 |
||
333 |
!ImmutableByteArray methodsFor:'copying-private'! |
|
334 |
||
12456 | 335 |
postCopy |
336 |
"when copied, make me a real (mutable) ByteArray" |
|
337 |
||
338 |
self changeClassTo:ByteArray. |
|
339 |
! |
|
340 |
||
341 |
postDeepCopy |
|
342 |
"when copied, make me a real (mutable) ByteArray" |
|
343 |
||
344 |
self changeClassTo:ByteArray. |
|
345 |
||
346 |
" |
|
347 |
#[1 2 3 4] asImmutableByteArray copy class |
|
348 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
349 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
350 |
" |
|
351 |
! ! |
|
352 |
||
16749 | 353 |
!ImmutableByteArray methodsFor:'printing & storing'! |
354 |
||
355 |
displayOn:aGCOrStream |
|
17536 | 356 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21547 | 357 |
"/ old ST80 means: draw-yourself on a GC. |
358 |
aGCOrStream isStream ifFalse:[ |
|
359 |
^ super displayOn:aGCOrStream. |
|
16749 | 360 |
]. |
21547 | 361 |
|
362 |
self storeOn:aGCOrStream. |
|
363 |
aGCOrStream nextPutAll:'"immutable"'. |
|
364 |
||
365 |
"Modified (format): / 22-02-2017 / 16:58:38 / cg" |
|
16749 | 366 |
! ! |
367 |
||
12456 | 368 |
!ImmutableByteArray methodsFor:'private'! |
369 |
||
370 |
species |
|
371 |
"Copies should be mutable" |
|
372 |
||
373 |
^ ByteArray |
|
16399 | 374 |
! |
375 |
||
376 |
speciesForCopy |
|
377 |
"Copies should be mutable" |
|
378 |
||
379 |
^ ByteArray |
|
12456 | 380 |
! ! |
381 |
||
382 |
!ImmutableByteArray methodsFor:'queries'! |
|
383 |
||
14388 | 384 |
isImmutable |
385 |
^ true |
|
386 |
! |
|
387 |
||
12456 | 388 |
isLiteral |
389 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
390 |
(i.e. can be used in constant arrays)" |
|
391 |
||
392 |
"yes, I must be" |
|
393 |
^ true |
|
394 |
||
395 |
! ! |
|
396 |
||
397 |
!ImmutableByteArray methodsFor:'specials'! |
|
398 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
399 |
become:anotherObject |
12456 | 400 |
"trigger an error if I should become something else |
401 |
(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
|
402 |
|
12493 | 403 |
self noModificationError. |
12456 | 404 |
^ super become:anotherObject |
405 |
! |
|
406 |
||
407 |
becomeNil |
|
408 |
"trigger an error if I should become nil |
|
409 |
(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
|
410 |
|
12493 | 411 |
self noModificationError. |
12456 | 412 |
^ super becomeNil |
18703 | 413 |
! |
414 |
||
415 |
becomeSameAs:anotherObject |
|
416 |
"trigger an error if I should become something else |
|
417 |
(this would be an even more tricky manipulation)" |
|
418 |
||
419 |
self noModificationError. |
|
420 |
^ super becomeSameAs:anotherObject |
|
12456 | 421 |
! ! |
422 |
||
423 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
424 |
||
425 |
version_CVS |
|
18545 | 426 |
^ '$Header$' |
12456 | 427 |
! ! |
16399 | 428 |