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