author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 22 Sep 2015 16:28:42 +0100 | |
branch | jv |
changeset 18759 | c1217211909c |
parent 18705 | 843770030c5f |
child 19225 | 9e8abf62f932 |
permissions | -rw-r--r-- |
18703 | 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 |
|
101 |
in the debugger." |
|
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 |
|
110 |
in the debugger." |
|
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 |
114 |
! ! |
|
115 |
||
116 |
!ImmutableByteArray methodsFor:'converting'! |
|
117 |
||
12488 | 118 |
asByteArray |
119 |
"return the receiver as a (mutable) byteArray" |
|
120 |
||
121 |
^ self copy changeClassTo: ByteArray |
|
122 |
||
123 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
124 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 125 |
" |
126 |
! |
|
127 |
||
12456 | 128 |
asImmutableByteArray |
129 |
^ self |
|
16823 | 130 |
! |
131 |
||
132 |
asMutableCollection |
|
133 |
"return a writable copy of myself" |
|
134 |
||
135 |
^ self copy changeClassTo:ByteArray |
|
12456 | 136 |
! ! |
137 |
||
138 |
!ImmutableByteArray methodsFor:'copying'! |
|
139 |
||
140 |
postCopy |
|
141 |
"when copied, make me a real (mutable) ByteArray" |
|
142 |
||
143 |
self changeClassTo:ByteArray. |
|
144 |
! |
|
145 |
||
146 |
postDeepCopy |
|
147 |
"when copied, make me a real (mutable) ByteArray" |
|
148 |
||
149 |
self changeClassTo:ByteArray. |
|
150 |
||
151 |
" |
|
152 |
#[1 2 3 4] asImmutableByteArray copy class |
|
153 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
154 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
155 |
" |
|
156 |
! |
|
157 |
||
158 |
shallowCopy |
|
159 |
"when copying, return a real (mutable) ByteArray" |
|
160 |
||
161 |
|sz| |
|
162 |
||
163 |
sz := self size. |
|
16399 | 164 |
^ (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
|
165 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
166 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
167 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
168 |
" |
12456 | 169 |
! ! |
170 |
||
16749 | 171 |
!ImmutableByteArray methodsFor:'printing & storing'! |
172 |
||
173 |
displayOn:aGCOrStream |
|
17536 | 174 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
175 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
16749 | 176 |
(aGCOrStream isStream) ifTrue:[ |
177 |
self storeOn:aGCOrStream. |
|
178 |
aGCOrStream nextPutAll:'"immutable"'. |
|
179 |
^ self. |
|
180 |
]. |
|
181 |
super displayOn:aGCOrStream. |
|
182 |
! ! |
|
183 |
||
12456 | 184 |
!ImmutableByteArray methodsFor:'private'! |
185 |
||
186 |
species |
|
187 |
"Copies should be mutable" |
|
188 |
||
189 |
^ ByteArray |
|
16399 | 190 |
! |
191 |
||
192 |
speciesForCopy |
|
193 |
"Copies should be mutable" |
|
194 |
||
195 |
^ ByteArray |
|
12456 | 196 |
! ! |
197 |
||
198 |
!ImmutableByteArray methodsFor:'queries'! |
|
199 |
||
14388 | 200 |
isImmutable |
201 |
^ true |
|
202 |
! |
|
203 |
||
12456 | 204 |
isLiteral |
205 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
206 |
(i.e. can be used in constant arrays)" |
|
207 |
||
208 |
"yes, I must be" |
|
209 |
^ true |
|
210 |
||
211 |
! ! |
|
212 |
||
213 |
!ImmutableByteArray methodsFor:'specials'! |
|
214 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
215 |
become:anotherObject |
12456 | 216 |
"trigger an error if I should become something else |
217 |
(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
|
218 |
|
12493 | 219 |
self noModificationError. |
12456 | 220 |
^ super become:anotherObject |
221 |
! |
|
222 |
||
223 |
becomeNil |
|
224 |
"trigger an error if I should become nil |
|
225 |
(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
|
226 |
|
12493 | 227 |
self noModificationError. |
12456 | 228 |
^ super becomeNil |
18703 | 229 |
! |
230 |
||
231 |
becomeSameAs:anotherObject |
|
232 |
"trigger an error if I should become something else |
|
233 |
(this would be an even more tricky manipulation)" |
|
234 |
||
235 |
self noModificationError. |
|
236 |
^ super becomeSameAs:anotherObject |
|
12456 | 237 |
! ! |
238 |
||
239 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
240 |
||
241 |
version_CVS |
|
18545 | 242 |
^ '$Header$' |
12456 | 243 |
! ! |
16399 | 244 |