author | Patrik Svestka <patrik.svestka@gmail.com> |
Tue, 09 Apr 2019 11:34:04 +0200 | |
branch | jv |
changeset 24093 | 0f94f6c8c9d4 |
parent 23547 | c69c97cec351 |
child 25420 | a18d0d048b1f |
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 |
|
17374 | 80 |
!ImmutableByteArray class methodsFor:'queries'! |
81 |
||
82 |
mutableClass |
|
83 |
"answer an equivalent mustable class" |
|
84 |
||
85 |
^ ByteArray |
|
86 |
! ! |
|
87 |
||
88 |
!ImmutableByteArray class methodsFor:'testing'! |
|
89 |
||
90 |
hasImmutableInstances |
|
91 |
^ true |
|
92 |
! ! |
|
93 |
||
12456 | 94 |
!ImmutableByteArray methodsFor:'accessing'! |
95 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
96 |
at:index put:value |
12456 | 97 |
"Trigger an error if an immutable bytearray is stored into. |
98 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 99 |
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
|
100 |
|
12493 | 101 |
self noModificationError. |
12456 | 102 |
^ super at:index put:value |
103 |
! |
|
104 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
105 |
basicAt:index put:value |
12456 | 106 |
"Trigger an error if an immutable bytearray is stored into. |
107 |
The store will be performed (for compatibility reasons) if you continue |
|
19421 | 108 |
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
|
109 |
|
12493 | 110 |
self noModificationError. |
12456 | 111 |
^ super basicAt:index put:value |
19421 | 112 |
! |
113 |
||
114 |
byteAt:index put:value |
|
115 |
"Trigger an error if an immutable bytearray is stored into. |
|
116 |
The store will be performed (for compatibility reasons) if you continue |
|
117 |
in the debugger (or proceed in an exception handler)." |
|
118 |
||
119 |
self noModificationError. |
|
120 |
^ super byteAt:index put:value |
|
121 |
! |
|
122 |
||
123 |
unsignedInt16At:index put:anInteger |
|
124 |
"Trigger an error if an immutable bytearray is stored into. |
|
125 |
The store will be performed (for compatibility reasons) if you continue |
|
126 |
in the debugger (or proceed in an exception handler)." |
|
127 |
||
128 |
self noModificationError. |
|
129 |
^ super unsignedInt16At:index put:anInteger |
|
130 |
! |
|
131 |
||
132 |
unsignedInt16At:index put:anInteger MSB:msb |
|
133 |
"Trigger an error if an immutable bytearray is stored into. |
|
134 |
The store will be performed (for compatibility reasons) if you continue |
|
135 |
in the debugger (or proceed in an exception handler)." |
|
136 |
||
137 |
self noModificationError. |
|
138 |
^ super unsignedInt16At:index put:anInteger MSB:msb |
|
139 |
! |
|
140 |
||
141 |
unsignedInt32At:index put:anInteger |
|
142 |
"Trigger an error if an immutable bytearray is stored into. |
|
143 |
The store will be performed (for compatibility reasons) if you continue |
|
144 |
in the debugger (or proceed in an exception handler)." |
|
145 |
||
146 |
self noModificationError. |
|
147 |
^ super unsignedInt32At:index put:anInteger |
|
148 |
! |
|
149 |
||
150 |
unsignedInt32At:index put:anInteger MSB:msb |
|
151 |
"Trigger an error if an immutable bytearray is stored into. |
|
152 |
The store will be performed (for compatibility reasons) if you continue |
|
153 |
in the debugger (or proceed in an exception handler)." |
|
154 |
||
155 |
self noModificationError. |
|
156 |
^ super unsignedInt32At:index put:anInteger MSB:msb |
|
157 |
! |
|
158 |
||
159 |
unsignedInt64At:index put:anInteger |
|
160 |
"Trigger an error if an immutable bytearray is stored into. |
|
161 |
The store will be performed (for compatibility reasons) if you continue |
|
162 |
in the debugger (or proceed in an exception handler)." |
|
163 |
||
164 |
self noModificationError. |
|
165 |
^ super unsignedInt64At:index put:anInteger |
|
166 |
! |
|
167 |
||
168 |
unsignedInt64At:index put:anInteger MSB:msb |
|
169 |
"Trigger an error if an immutable bytearray is stored into. |
|
170 |
The store will be performed (for compatibility reasons) if you continue |
|
171 |
in the debugger (or proceed in an exception handler)." |
|
172 |
||
173 |
self noModificationError. |
|
174 |
^ super unsignedInt64At:index put:anInteger MSB:msb |
|
12456 | 175 |
! ! |
176 |
||
177 |
!ImmutableByteArray methodsFor:'converting'! |
|
178 |
||
12488 | 179 |
asByteArray |
180 |
"return the receiver as a (mutable) byteArray" |
|
181 |
||
19421 | 182 |
^ self shallowCopy |
12488 | 183 |
|
184 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
185 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 186 |
" |
187 |
! |
|
188 |
||
12456 | 189 |
asImmutableByteArray |
19421 | 190 |
"that's what I am-" |
191 |
||
12456 | 192 |
^ self |
16823 | 193 |
! |
194 |
||
195 |
asMutableCollection |
|
196 |
"return a writable copy of myself" |
|
197 |
||
19421 | 198 |
^ self shallowCopy |
19175 | 199 |
! |
200 |
||
201 |
beImmutable |
|
202 |
"that's what I am" |
|
203 |
||
204 |
^ self |
|
205 |
! |
|
206 |
||
20524 | 207 |
beMutable |
208 |
"you never go back" |
|
209 |
||
210 |
^ self shouldNotImplement |
|
211 |
! |
|
212 |
||
19175 | 213 |
beUnsigned |
214 |
"that's what I am-" |
|
215 |
||
216 |
^ self. |
|
217 |
||
218 |
" |
|
219 |
#[1 2 3 128 255] asImmutableByteArray beUnsigned |
|
220 |
" |
|
12456 | 221 |
! ! |
222 |
||
223 |
!ImmutableByteArray methodsFor:'copying'! |
|
224 |
||
225 |
postCopy |
|
226 |
"when copied, make me a real (mutable) ByteArray" |
|
227 |
||
228 |
self changeClassTo:ByteArray. |
|
229 |
! |
|
230 |
||
231 |
postDeepCopy |
|
232 |
"when copied, make me a real (mutable) ByteArray" |
|
233 |
||
234 |
self changeClassTo:ByteArray. |
|
235 |
||
236 |
" |
|
237 |
#[1 2 3 4] asImmutableByteArray copy class |
|
238 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
239 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
240 |
" |
|
241 |
! |
|
242 |
||
243 |
shallowCopy |
|
244 |
"when copying, return a real (mutable) ByteArray" |
|
245 |
||
246 |
|sz| |
|
247 |
||
248 |
sz := self size. |
|
16399 | 249 |
^ (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
|
250 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
251 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
252 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
253 |
" |
12456 | 254 |
! ! |
255 |
||
16749 | 256 |
!ImmutableByteArray methodsFor:'printing & storing'! |
257 |
||
258 |
displayOn:aGCOrStream |
|
17536 | 259 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
260 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
16749 | 261 |
(aGCOrStream isStream) ifTrue:[ |
262 |
self storeOn:aGCOrStream. |
|
263 |
aGCOrStream nextPutAll:'"immutable"'. |
|
264 |
^ self. |
|
265 |
]. |
|
266 |
super displayOn:aGCOrStream. |
|
267 |
! ! |
|
268 |
||
12456 | 269 |
!ImmutableByteArray methodsFor:'private'! |
270 |
||
271 |
species |
|
272 |
"Copies should be mutable" |
|
273 |
||
274 |
^ ByteArray |
|
16399 | 275 |
! |
276 |
||
277 |
speciesForCopy |
|
278 |
"Copies should be mutable" |
|
279 |
||
280 |
^ ByteArray |
|
12456 | 281 |
! ! |
282 |
||
283 |
!ImmutableByteArray methodsFor:'queries'! |
|
284 |
||
14388 | 285 |
isImmutable |
286 |
^ true |
|
287 |
! |
|
288 |
||
12456 | 289 |
isLiteral |
290 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
291 |
(i.e. can be used in constant arrays)" |
|
292 |
||
293 |
"yes, I must be" |
|
294 |
^ true |
|
295 |
||
296 |
! ! |
|
297 |
||
298 |
!ImmutableByteArray methodsFor:'specials'! |
|
299 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
300 |
become:anotherObject |
12456 | 301 |
"trigger an error if I should become something else |
302 |
(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
|
303 |
|
12493 | 304 |
self noModificationError. |
12456 | 305 |
^ super become:anotherObject |
306 |
! |
|
307 |
||
308 |
becomeNil |
|
309 |
"trigger an error if I should become nil |
|
310 |
(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
|
311 |
|
12493 | 312 |
self noModificationError. |
12456 | 313 |
^ super becomeNil |
18703 | 314 |
! |
315 |
||
316 |
becomeSameAs:anotherObject |
|
317 |
"trigger an error if I should become something else |
|
318 |
(this would be an even more tricky manipulation)" |
|
319 |
||
320 |
self noModificationError. |
|
321 |
^ super becomeSameAs:anotherObject |
|
12456 | 322 |
! ! |
323 |
||
324 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
325 |
||
326 |
version_CVS |
|
18545 | 327 |
^ '$Header$' |
12456 | 328 |
! ! |
16399 | 329 |