author | Merge Script |
Sun, 07 Jun 2015 06:38:49 +0200 | |
branch | jv |
changeset 18457 | 214d760f8247 |
parent 18120 | e3a375d5f6a8 |
child 18555 | 7cc0ec139c8d |
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 |
||
12472 | 60 |
ATTENTION: |
14223 | 61 |
there may be still code around which checks for explicit class being ByteArray |
62 |
(both in Smalltalk and in primitive code). All code like foo 'class == ByteArray' |
|
63 |
or '__isByteArray()' will not work with ImmutableByteArrays - consider using '__isByteArrayLike()'. |
|
64 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
65 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
66 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12472 | 67 |
|
12465 | 68 |
[see also:] |
14223 | 69 |
ImmutableString |
70 |
ImmutableArray |
|
71 |
Parser Scanner |
|
12465 | 72 |
|
73 |
[author:] |
|
14223 | 74 |
Claus Gittinger |
12465 | 75 |
" |
76 |
! ! |
|
12456 | 77 |
|
17374 | 78 |
!ImmutableByteArray class methodsFor:'queries'! |
79 |
||
80 |
mutableClass |
|
81 |
"answer an equivalent mustable class" |
|
82 |
||
83 |
^ ByteArray |
|
84 |
! ! |
|
85 |
||
86 |
!ImmutableByteArray class methodsFor:'testing'! |
|
87 |
||
88 |
hasImmutableInstances |
|
89 |
^ true |
|
90 |
! ! |
|
91 |
||
12456 | 92 |
!ImmutableByteArray methodsFor:'accessing'! |
93 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
94 |
at:index put:value |
12456 | 95 |
"Trigger an error if an immutable bytearray is stored into. |
96 |
The store will be performed (for compatibility reasons) if you continue |
|
97 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
98 |
|
12493 | 99 |
self noModificationError. |
12456 | 100 |
^ super at:index put:value |
101 |
! |
|
102 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
103 |
basicAt:index put:value |
12456 | 104 |
"Trigger an error if an immutable bytearray is stored into. |
105 |
The store will be performed (for compatibility reasons) if you continue |
|
106 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
107 |
|
12493 | 108 |
self noModificationError. |
12456 | 109 |
^ super basicAt:index put:value |
110 |
! ! |
|
111 |
||
112 |
!ImmutableByteArray methodsFor:'converting'! |
|
113 |
||
12488 | 114 |
asByteArray |
115 |
"return the receiver as a (mutable) byteArray" |
|
116 |
||
117 |
^ self copy changeClassTo: ByteArray |
|
118 |
||
119 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
120 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 121 |
" |
122 |
! |
|
123 |
||
12456 | 124 |
asImmutableByteArray |
125 |
^ self |
|
16823 | 126 |
! |
127 |
||
128 |
asMutableCollection |
|
129 |
"return a writable copy of myself" |
|
130 |
||
131 |
^ self copy changeClassTo:ByteArray |
|
12456 | 132 |
! ! |
133 |
||
134 |
!ImmutableByteArray methodsFor:'copying'! |
|
135 |
||
136 |
postCopy |
|
137 |
"when copied, make me a real (mutable) ByteArray" |
|
138 |
||
139 |
self changeClassTo:ByteArray. |
|
140 |
! |
|
141 |
||
142 |
postDeepCopy |
|
143 |
"when copied, make me a real (mutable) ByteArray" |
|
144 |
||
145 |
self changeClassTo:ByteArray. |
|
146 |
||
147 |
" |
|
148 |
#[1 2 3 4] asImmutableByteArray copy class |
|
149 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
150 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
151 |
" |
|
152 |
! |
|
153 |
||
154 |
shallowCopy |
|
155 |
"when copying, return a real (mutable) ByteArray" |
|
156 |
||
157 |
|sz| |
|
158 |
||
159 |
sz := self size. |
|
16399 | 160 |
^ (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
|
161 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
162 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
163 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
164 |
" |
12456 | 165 |
! ! |
166 |
||
16749 | 167 |
!ImmutableByteArray methodsFor:'printing & storing'! |
168 |
||
169 |
displayOn:aGCOrStream |
|
17536 | 170 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
171 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
|
16749 | 172 |
(aGCOrStream isStream) ifTrue:[ |
173 |
self storeOn:aGCOrStream. |
|
174 |
aGCOrStream nextPutAll:'"immutable"'. |
|
175 |
^ self. |
|
176 |
]. |
|
177 |
super displayOn:aGCOrStream. |
|
178 |
! ! |
|
179 |
||
12456 | 180 |
!ImmutableByteArray methodsFor:'private'! |
181 |
||
182 |
species |
|
183 |
"Copies should be mutable" |
|
184 |
||
185 |
^ ByteArray |
|
16399 | 186 |
! |
187 |
||
188 |
speciesForCopy |
|
189 |
"Copies should be mutable" |
|
190 |
||
191 |
^ ByteArray |
|
12456 | 192 |
! ! |
193 |
||
194 |
!ImmutableByteArray methodsFor:'queries'! |
|
195 |
||
14388 | 196 |
isImmutable |
197 |
^ true |
|
198 |
! |
|
199 |
||
12456 | 200 |
isLiteral |
201 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
202 |
(i.e. can be used in constant arrays)" |
|
203 |
||
204 |
"yes, I must be" |
|
205 |
^ true |
|
206 |
||
12488 | 207 |
! |
208 |
||
209 |
referencesAny:aCollection |
|
210 |
"redefined to speed up searching when many byteArray instances are present" |
|
211 |
||
212 |
^ false |
|
12456 | 213 |
! ! |
214 |
||
215 |
!ImmutableByteArray methodsFor:'specials'! |
|
216 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
217 |
become:anotherObject |
12456 | 218 |
"trigger an error if I should become something else |
219 |
(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
|
220 |
|
12493 | 221 |
self noModificationError. |
12456 | 222 |
^ super become:anotherObject |
223 |
! |
|
224 |
||
225 |
becomeNil |
|
226 |
"trigger an error if I should become nil |
|
227 |
(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
|
228 |
|
12493 | 229 |
self noModificationError. |
12456 | 230 |
^ super becomeNil |
231 |
! ! |
|
232 |
||
233 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
234 |
||
235 |
version_CVS |
|
17536 | 236 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.16 2015-02-20 23:56:40 cg Exp $' |
12456 | 237 |
! ! |
16399 | 238 |