author | Claus Gittinger <cg@exept.de> |
Fri, 13 Dec 2019 15:35:34 +0100 | |
changeset 25116 | aa3a98621b0a |
parent 24447 | 36e27d5222ef |
permissions | -rw-r--r-- |
24310 | 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 |
|
21648 | 82 |
!ImmutableByteArray class methodsFor:'instance creation'! |
83 |
||
84 |
fromPackedString:aString |
|
85 |
^ (ByteArray fromPackedString:aString) beImmutable. |
|
86 |
||
87 |
"Created: / 15-03-2017 / 17:55:57 / stefan" |
|
88 |
! ! |
|
89 |
||
17374 | 90 |
!ImmutableByteArray class methodsFor:'queries'! |
91 |
||
92 |
mutableClass |
|
93 |
"answer an equivalent mustable class" |
|
94 |
||
95 |
^ ByteArray |
|
96 |
! ! |
|
97 |
||
98 |
!ImmutableByteArray class methodsFor:'testing'! |
|
99 |
||
100 |
hasImmutableInstances |
|
101 |
^ true |
|
102 |
! ! |
|
103 |
||
12456 | 104 |
!ImmutableByteArray methodsFor:'accessing'! |
105 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
106 |
at:index put:value |
12456 | 107 |
"Trigger an error if an immutable bytearray is stored into. |
108 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 109 |
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
|
110 |
|
12493 | 111 |
self noModificationError. |
12456 | 112 |
^ super at:index put:value |
113 |
! |
|
114 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
115 |
basicAt:index put:value |
12456 | 116 |
"Trigger an error if an immutable bytearray is stored into. |
117 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 118 |
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
|
119 |
|
12493 | 120 |
self noModificationError. |
12456 | 121 |
^ super basicAt:index put:value |
19421 | 122 |
! |
123 |
||
124 |
byteAt:index put:value |
|
125 |
"Trigger an error if an immutable bytearray is stored into. |
|
126 |
The store will be performed (for compatibility reasons) if you continue |
|
127 |
in the debugger (or proceed in an exception handler)." |
|
128 |
||
129 |
self noModificationError. |
|
130 |
^ super byteAt:index put:value |
|
131 |
! |
|
132 |
||
22649 | 133 |
doubleAt:index put:anInteger |
134 |
"Trigger an error if an immutable bytearray is stored into. |
|
135 |
The store will be performed (for compatibility reasons) if you continue |
|
136 |
in the debugger (or proceed in an exception handler)." |
|
137 |
||
138 |
self noModificationError. |
|
139 |
^ super doubleAt:index put:anInteger |
|
140 |
! |
|
141 |
||
142 |
doubleAt:index put:anInteger MSB:msb |
|
143 |
"Trigger an error if an immutable bytearray is stored into. |
|
144 |
The store will be performed (for compatibility reasons) if you continue |
|
145 |
in the debugger (or proceed in an exception handler)." |
|
146 |
||
147 |
self noModificationError. |
|
148 |
^ super doubleAt:index put:anInteger MSB:msb |
|
149 |
! |
|
150 |
||
151 |
floatAt:index put:anInteger |
|
152 |
"Trigger an error if an immutable bytearray is stored into. |
|
153 |
The store will be performed (for compatibility reasons) if you continue |
|
154 |
in the debugger (or proceed in an exception handler)." |
|
155 |
||
156 |
self noModificationError. |
|
157 |
^ super floatAt:index put:anInteger |
|
158 |
! |
|
159 |
||
160 |
floatAt:index put:anInteger MSB:msb |
|
161 |
"Trigger an error if an immutable bytearray is stored into. |
|
162 |
The store will be performed (for compatibility reasons) if you continue |
|
163 |
in the debugger (or proceed in an exception handler)." |
|
164 |
||
165 |
self noModificationError. |
|
166 |
^ super floatAt:index put:anInteger MSB:msb |
|
167 |
! |
|
168 |
||
169 |
signedInt128At:index put:anInteger MSB:msb |
|
170 |
"Trigger an error if an immutable bytearray is stored into. |
|
171 |
The store will be performed (for compatibility reasons) if you continue |
|
172 |
in the debugger (or proceed in an exception handler)." |
|
173 |
||
174 |
self noModificationError. |
|
175 |
^ super signedInt128At:index put:anInteger MSB:msb |
|
176 |
! |
|
177 |
||
178 |
signedInt16At:index put:anInteger MSB:msb |
|
179 |
"Trigger an error if an immutable bytearray is stored into. |
|
180 |
The store will be performed (for compatibility reasons) if you continue |
|
181 |
in the debugger (or proceed in an exception handler)." |
|
182 |
||
183 |
self noModificationError. |
|
184 |
^ super signedInt16At:index put:anInteger MSB:msb |
|
185 |
! |
|
186 |
||
187 |
signedInt32At:index put:anInteger MSB:msb |
|
188 |
"Trigger an error if an immutable bytearray is stored into. |
|
189 |
The store will be performed (for compatibility reasons) if you continue |
|
190 |
in the debugger (or proceed in an exception handler)." |
|
191 |
||
192 |
self noModificationError. |
|
193 |
^ super signedInt32At:index put:anInteger MSB:msb |
|
194 |
! |
|
195 |
||
196 |
signedInt64At:index put:anInteger MSB:msb |
|
197 |
"Trigger an error if an immutable bytearray is stored into. |
|
198 |
The store will be performed (for compatibility reasons) if you continue |
|
199 |
in the debugger (or proceed in an exception handler)." |
|
200 |
||
201 |
self noModificationError. |
|
202 |
^ super signedInt64At:index put:anInteger MSB:msb |
|
203 |
! |
|
204 |
||
205 |
unsignedInt128At:index put:anInteger MSB:msb |
|
206 |
"Trigger an error if an immutable bytearray is stored into. |
|
207 |
The store will be performed (for compatibility reasons) if you continue |
|
208 |
in the debugger (or proceed in an exception handler)." |
|
209 |
||
210 |
self noModificationError. |
|
211 |
^ super unsignedInt128At:index put:anInteger MSB:msb |
|
212 |
! |
|
213 |
||
19421 | 214 |
unsignedInt16At:index put:anInteger |
215 |
"Trigger an error if an immutable bytearray is stored into. |
|
216 |
The store will be performed (for compatibility reasons) if you continue |
|
217 |
in the debugger (or proceed in an exception handler)." |
|
218 |
||
219 |
self noModificationError. |
|
220 |
^ super unsignedInt16At:index put:anInteger |
|
221 |
! |
|
222 |
||
223 |
unsignedInt16At:index put:anInteger MSB:msb |
|
224 |
"Trigger an error if an immutable bytearray is stored into. |
|
225 |
The store will be performed (for compatibility reasons) if you continue |
|
226 |
in the debugger (or proceed in an exception handler)." |
|
227 |
||
228 |
self noModificationError. |
|
229 |
^ super unsignedInt16At:index put:anInteger MSB:msb |
|
230 |
! |
|
231 |
||
232 |
unsignedInt32At:index put:anInteger |
|
233 |
"Trigger an error if an immutable bytearray is stored into. |
|
234 |
The store will be performed (for compatibility reasons) if you continue |
|
235 |
in the debugger (or proceed in an exception handler)." |
|
236 |
||
237 |
self noModificationError. |
|
238 |
^ super unsignedInt32At:index put:anInteger |
|
239 |
! |
|
240 |
||
241 |
unsignedInt32At:index put:anInteger MSB:msb |
|
242 |
"Trigger an error if an immutable bytearray is stored into. |
|
243 |
The store will be performed (for compatibility reasons) if you continue |
|
244 |
in the debugger (or proceed in an exception handler)." |
|
245 |
||
246 |
self noModificationError. |
|
247 |
^ super unsignedInt32At:index put:anInteger MSB:msb |
|
248 |
! |
|
249 |
||
250 |
unsignedInt64At:index put:anInteger |
|
251 |
"Trigger an error if an immutable bytearray is stored into. |
|
252 |
The store will be performed (for compatibility reasons) if you continue |
|
253 |
in the debugger (or proceed in an exception handler)." |
|
254 |
||
255 |
self noModificationError. |
|
256 |
^ super unsignedInt64At:index put:anInteger |
|
257 |
! |
|
258 |
||
259 |
unsignedInt64At:index put:anInteger MSB:msb |
|
260 |
"Trigger an error if an immutable bytearray is stored into. |
|
261 |
The store will be performed (for compatibility reasons) if you continue |
|
262 |
in the debugger (or proceed in an exception handler)." |
|
263 |
||
264 |
self noModificationError. |
|
265 |
^ super unsignedInt64At:index put:anInteger MSB:msb |
|
12456 | 266 |
! ! |
267 |
||
268 |
!ImmutableByteArray methodsFor:'converting'! |
|
269 |
||
12488 | 270 |
asByteArray |
271 |
"return the receiver as a (mutable) byteArray" |
|
272 |
||
19421 | 273 |
^ self shallowCopy |
12488 | 274 |
|
275 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
276 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 277 |
" |
278 |
! |
|
279 |
||
12456 | 280 |
asImmutableByteArray |
19421 | 281 |
"that's what I am-" |
282 |
||
12456 | 283 |
^ self |
16823 | 284 |
! |
285 |
||
23923 | 286 |
asImmutableCollection |
287 |
^ self |
|
288 |
||
289 |
"Created: / 15-03-2019 / 13:53:07 / Stefan Vogel" |
|
290 |
! |
|
291 |
||
16823 | 292 |
asMutableCollection |
293 |
"return a writable copy of myself" |
|
294 |
||
19421 | 295 |
^ self shallowCopy |
19175 | 296 |
! |
297 |
||
298 |
beImmutable |
|
299 |
"that's what I am" |
|
300 |
||
301 |
^ self |
|
302 |
! |
|
303 |
||
20524 | 304 |
beMutable |
305 |
"you never go back" |
|
306 |
||
307 |
^ self shouldNotImplement |
|
308 |
! |
|
309 |
||
19175 | 310 |
beUnsigned |
311 |
"that's what I am-" |
|
312 |
||
313 |
^ self. |
|
314 |
||
315 |
" |
|
316 |
#[1 2 3 128 255] asImmutableByteArray beUnsigned |
|
317 |
" |
|
12456 | 318 |
! ! |
319 |
||
320 |
!ImmutableByteArray methodsFor:'copying'! |
|
321 |
||
24173 | 322 |
shallowCopy |
323 |
"when copying, return a real (mutable) ByteArray" |
|
324 |
||
325 |
|sz| |
|
326 |
||
327 |
sz := self size. |
|
328 |
^ (ByteArray new:sz) replaceBytesFrom:1 to:sz with:self startingAt:1 |
|
329 |
||
330 |
" |
|
331 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
|
332 |
" |
|
333 |
! ! |
|
334 |
||
335 |
!ImmutableByteArray methodsFor:'copying-private'! |
|
336 |
||
12456 | 337 |
postCopy |
338 |
"when copied, make me a real (mutable) ByteArray" |
|
339 |
||
340 |
self changeClassTo:ByteArray. |
|
341 |
! |
|
342 |
||
343 |
postDeepCopy |
|
344 |
"when copied, make me a real (mutable) ByteArray" |
|
345 |
||
346 |
self changeClassTo:ByteArray. |
|
347 |
||
348 |
" |
|
349 |
#[1 2 3 4] asImmutableByteArray copy class |
|
350 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
351 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
352 |
" |
|
353 |
! ! |
|
354 |
||
16749 | 355 |
!ImmutableByteArray methodsFor:'printing & storing'! |
356 |
||
357 |
displayOn:aGCOrStream |
|
17536 | 358 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21547 | 359 |
"/ old ST80 means: draw-yourself on a GC. |
360 |
aGCOrStream isStream ifFalse:[ |
|
361 |
^ super displayOn:aGCOrStream. |
|
16749 | 362 |
]. |
21547 | 363 |
|
364 |
self storeOn:aGCOrStream. |
|
24447 | 365 |
self size > 0 ifTrue:[ |
366 |
aGCOrStream nextPutAll:' "immutable"'. |
|
367 |
]. |
|
21547 | 368 |
|
369 |
"Modified (format): / 22-02-2017 / 16:58:38 / cg" |
|
24447 | 370 |
"Modified: / 21-07-2019 / 08:31:13 / Claus Gittinger" |
16749 | 371 |
! ! |
372 |
||
12456 | 373 |
!ImmutableByteArray methodsFor:'private'! |
374 |
||
375 |
species |
|
376 |
"Copies should be mutable" |
|
377 |
||
378 |
^ ByteArray |
|
16399 | 379 |
! |
380 |
||
381 |
speciesForCopy |
|
382 |
"Copies should be mutable" |
|
383 |
||
384 |
^ ByteArray |
|
12456 | 385 |
! ! |
386 |
||
387 |
!ImmutableByteArray methodsFor:'queries'! |
|
388 |
||
14388 | 389 |
isImmutable |
24310 | 390 |
"return true, if the receiver is immutable. |
391 |
Since I am an immutable byte array, return always true here" |
|
392 |
||
14388 | 393 |
^ true |
24310 | 394 |
|
395 |
"Modified (comment): / 09-06-2019 / 14:54:36 / Claus Gittinger" |
|
14388 | 396 |
! |
397 |
||
12456 | 398 |
isLiteral |
399 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
400 |
(i.e. can be used in constant arrays)" |
|
401 |
||
402 |
"yes, I must be" |
|
403 |
^ true |
|
404 |
||
405 |
! ! |
|
406 |
||
407 |
!ImmutableByteArray methodsFor:'specials'! |
|
408 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
409 |
become:anotherObject |
12456 | 410 |
"trigger an error if I should become something else |
411 |
(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
|
412 |
|
12493 | 413 |
self noModificationError. |
12456 | 414 |
^ super become:anotherObject |
415 |
! |
|
416 |
||
417 |
becomeNil |
|
418 |
"trigger an error if I should become nil |
|
419 |
(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
|
420 |
|
12493 | 421 |
self noModificationError. |
12456 | 422 |
^ super becomeNil |
18703 | 423 |
! |
424 |
||
425 |
becomeSameAs:anotherObject |
|
426 |
"trigger an error if I should become something else |
|
427 |
(this would be an even more tricky manipulation)" |
|
428 |
||
429 |
self noModificationError. |
|
430 |
^ super becomeSameAs:anotherObject |
|
12456 | 431 |
! ! |
432 |
||
433 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
434 |
||
435 |
version_CVS |
|
18545 | 436 |
^ '$Header$' |
12456 | 437 |
! ! |
16399 | 438 |