author | Claus Gittinger <cg@exept.de> |
Thu, 03 Aug 2017 16:41:13 +0200 | |
changeset 22159 | c8f19ee3f888 |
parent 21648 | bde0d970dcf3 |
child 22649 | ad0f62637e91 |
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 |
||
131 |
unsignedInt16At: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 unsignedInt16At:index put:anInteger |
|
138 |
! |
|
139 |
||
140 |
unsignedInt16At: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 unsignedInt16At:index put:anInteger MSB:msb |
|
147 |
! |
|
148 |
||
149 |
unsignedInt32At: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 unsignedInt32At:index put:anInteger |
|
156 |
! |
|
157 |
||
158 |
unsignedInt32At: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 unsignedInt32At:index put:anInteger MSB:msb |
|
165 |
! |
|
166 |
||
167 |
unsignedInt64At:index put:anInteger |
|
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 unsignedInt64At:index put:anInteger |
|
174 |
! |
|
175 |
||
176 |
unsignedInt64At: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 unsignedInt64At:index put:anInteger MSB:msb |
|
12456 | 183 |
! ! |
184 |
||
185 |
!ImmutableByteArray methodsFor:'converting'! |
|
186 |
||
12488 | 187 |
asByteArray |
188 |
"return the receiver as a (mutable) byteArray" |
|
189 |
||
19421 | 190 |
^ self shallowCopy |
12488 | 191 |
|
192 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
193 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 194 |
" |
195 |
! |
|
196 |
||
12456 | 197 |
asImmutableByteArray |
19421 | 198 |
"that's what I am-" |
199 |
||
12456 | 200 |
^ self |
16823 | 201 |
! |
202 |
||
203 |
asMutableCollection |
|
204 |
"return a writable copy of myself" |
|
205 |
||
19421 | 206 |
^ self shallowCopy |
19175 | 207 |
! |
208 |
||
209 |
beImmutable |
|
210 |
"that's what I am" |
|
211 |
||
212 |
^ self |
|
213 |
! |
|
214 |
||
20524 | 215 |
beMutable |
216 |
"you never go back" |
|
217 |
||
218 |
^ self shouldNotImplement |
|
219 |
! |
|
220 |
||
19175 | 221 |
beUnsigned |
222 |
"that's what I am-" |
|
223 |
||
224 |
^ self. |
|
225 |
||
226 |
" |
|
227 |
#[1 2 3 128 255] asImmutableByteArray beUnsigned |
|
228 |
" |
|
12456 | 229 |
! ! |
230 |
||
231 |
!ImmutableByteArray methodsFor:'copying'! |
|
232 |
||
233 |
postCopy |
|
234 |
"when copied, make me a real (mutable) ByteArray" |
|
235 |
||
236 |
self changeClassTo:ByteArray. |
|
237 |
! |
|
238 |
||
239 |
postDeepCopy |
|
240 |
"when copied, make me a real (mutable) ByteArray" |
|
241 |
||
242 |
self changeClassTo:ByteArray. |
|
243 |
||
244 |
" |
|
245 |
#[1 2 3 4] asImmutableByteArray copy class |
|
246 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
247 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
248 |
" |
|
249 |
! |
|
250 |
||
251 |
shallowCopy |
|
252 |
"when copying, return a real (mutable) ByteArray" |
|
253 |
||
254 |
|sz| |
|
255 |
||
256 |
sz := self size. |
|
16399 | 257 |
^ (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
|
258 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
259 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
260 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
261 |
" |
12456 | 262 |
! ! |
263 |
||
16749 | 264 |
!ImmutableByteArray methodsFor:'printing & storing'! |
265 |
||
266 |
displayOn:aGCOrStream |
|
17536 | 267 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
21547 | 268 |
"/ old ST80 means: draw-yourself on a GC. |
269 |
aGCOrStream isStream ifFalse:[ |
|
270 |
^ super displayOn:aGCOrStream. |
|
16749 | 271 |
]. |
21547 | 272 |
|
273 |
self storeOn:aGCOrStream. |
|
274 |
aGCOrStream nextPutAll:'"immutable"'. |
|
275 |
||
276 |
"Modified (format): / 22-02-2017 / 16:58:38 / cg" |
|
16749 | 277 |
! ! |
278 |
||
12456 | 279 |
!ImmutableByteArray methodsFor:'private'! |
280 |
||
281 |
species |
|
282 |
"Copies should be mutable" |
|
283 |
||
284 |
^ ByteArray |
|
16399 | 285 |
! |
286 |
||
287 |
speciesForCopy |
|
288 |
"Copies should be mutable" |
|
289 |
||
290 |
^ ByteArray |
|
12456 | 291 |
! ! |
292 |
||
293 |
!ImmutableByteArray methodsFor:'queries'! |
|
294 |
||
14388 | 295 |
isImmutable |
296 |
^ true |
|
297 |
! |
|
298 |
||
12456 | 299 |
isLiteral |
300 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
301 |
(i.e. can be used in constant arrays)" |
|
302 |
||
303 |
"yes, I must be" |
|
304 |
^ true |
|
305 |
||
306 |
! ! |
|
307 |
||
308 |
!ImmutableByteArray methodsFor:'specials'! |
|
309 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
310 |
become:anotherObject |
12456 | 311 |
"trigger an error if I should become something else |
312 |
(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
|
313 |
|
12493 | 314 |
self noModificationError. |
12456 | 315 |
^ super become:anotherObject |
316 |
! |
|
317 |
||
318 |
becomeNil |
|
319 |
"trigger an error if I should become nil |
|
320 |
(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
|
321 |
|
12493 | 322 |
self noModificationError. |
12456 | 323 |
^ super becomeNil |
18703 | 324 |
! |
325 |
||
326 |
becomeSameAs:anotherObject |
|
327 |
"trigger an error if I should become something else |
|
328 |
(this would be an even more tricky manipulation)" |
|
329 |
||
330 |
self noModificationError. |
|
331 |
^ super becomeSameAs:anotherObject |
|
12456 | 332 |
! ! |
333 |
||
334 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
335 |
||
336 |
version_CVS |
|
18545 | 337 |
^ '$Header$' |
12456 | 338 |
! ! |
16399 | 339 |