author | Claus Gittinger <cg@exept.de> |
Thu, 20 Nov 2014 02:51:05 +0100 | |
changeset 17102 | b3ff7be6e1f8 |
parent 16823 | 48a3b8612594 |
child 17374 | a58160e9e232 |
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 |
|
14 |
ByteArray variableByteSubclass:#ImmutableByteArray |
|
15 |
instanceVariableNames:'' |
|
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'System-Compiler-Support' |
|
19 |
! |
|
20 |
||
12465 | 21 |
!ImmutableByteArray class methodsFor:'documentation'! |
22 |
||
12469 | 23 |
copyright |
24 |
" |
|
25 |
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
|
26 |
All Rights Reserved |
12469 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
12465 | 37 |
documentation |
38 |
" |
|
39 |
By default, byte array literals in smalltalk are mutable objects. That |
|
40 |
may lead to some subtle (and hard to find errors), if some method passes |
|
41 |
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
|
42 |
bytearray using at:put: like messages. Since the bytearray object is kept in |
12465 | 43 |
the first methods literals, the bytearray constant has now been changed without |
44 |
having the method's sourcecode reflect this. Thus, the method will |
|
45 |
behave differently from what its source may make you think. |
|
46 |
||
47 |
To help finding this kind of 'feature/bug', the compiler can be |
|
48 |
configured to create instances of this ImmutableByteArray instead of ByteArrays |
|
49 |
for literals. Instances of ImmutableByteArray catch storing accesses and |
|
50 |
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
|
51 |
for compatibility to other smalltalk implementations. |
12465 | 52 |
(Also, if turned on, this makes inspecting bytearray literals entered in |
53 |
a workspace somewhat strange: you cannot modify it any longer). |
|
54 |
||
55 |
Turn the ImmutableArray feature on, by setting the Parser's class variable |
|
56 |
'ArraysAreImmutable' to true or use the new launchers settings menu. |
|
57 |
||
12472 | 58 |
ATTENTION: |
14223 | 59 |
there may be still code around which checks for explicit class being ByteArray |
60 |
(both in Smalltalk and in primitive code). All code like foo 'class == ByteArray' |
|
61 |
or '__isByteArray()' will not work with ImmutableByteArrays - consider using '__isByteArrayLike()'. |
|
62 |
A somewhat better approach would be to either add a flag to the object (mutability) |
|
63 |
and check this dynamically (expensive) or to place immutable objects into a readonly |
|
64 |
memory segment (the good solution). We will eventually implement the second in the future... |
|
12472 | 65 |
|
12465 | 66 |
[see also:] |
14223 | 67 |
ImmutableString |
68 |
ImmutableArray |
|
69 |
Parser Scanner |
|
12465 | 70 |
|
71 |
[author:] |
|
14223 | 72 |
Claus Gittinger |
12465 | 73 |
" |
74 |
! ! |
|
12456 | 75 |
|
76 |
!ImmutableByteArray methodsFor:'accessing'! |
|
77 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
78 |
at:index put:value |
12456 | 79 |
"Trigger an error if an immutable bytearray is stored into. |
80 |
The store will be performed (for compatibility reasons) if you continue |
|
81 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
82 |
|
12493 | 83 |
self noModificationError. |
12456 | 84 |
^ super at:index put:value |
85 |
! |
|
86 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
87 |
basicAt:index put:value |
12456 | 88 |
"Trigger an error if an immutable bytearray is stored into. |
89 |
The store will be performed (for compatibility reasons) if you continue |
|
90 |
in the debugger." |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
91 |
|
12493 | 92 |
self noModificationError. |
12456 | 93 |
^ super basicAt:index put:value |
94 |
! ! |
|
95 |
||
96 |
!ImmutableByteArray methodsFor:'converting'! |
|
97 |
||
12488 | 98 |
asByteArray |
99 |
"return the receiver as a (mutable) byteArray" |
|
100 |
||
101 |
^ self copy changeClassTo: ByteArray |
|
102 |
||
103 |
" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
104 |
#[1 2 3 4] asImmutableByteArray asByteArray |
12488 | 105 |
" |
106 |
! |
|
107 |
||
12456 | 108 |
asImmutableByteArray |
109 |
^ self |
|
16823 | 110 |
! |
111 |
||
112 |
asMutableCollection |
|
113 |
"return a writable copy of myself" |
|
114 |
||
115 |
^ self copy changeClassTo:ByteArray |
|
12456 | 116 |
! ! |
117 |
||
118 |
!ImmutableByteArray methodsFor:'copying'! |
|
119 |
||
120 |
postCopy |
|
121 |
"when copied, make me a real (mutable) ByteArray" |
|
122 |
||
123 |
self changeClassTo:ByteArray. |
|
124 |
! |
|
125 |
||
126 |
postDeepCopy |
|
127 |
"when copied, make me a real (mutable) ByteArray" |
|
128 |
||
129 |
self changeClassTo:ByteArray. |
|
130 |
||
131 |
" |
|
132 |
#[1 2 3 4] asImmutableByteArray copy class |
|
133 |
#[1 2 3 4] asImmutableByteArray shallowCopy class |
|
134 |
#[1 2 3 4] asImmutableByteArray deepCopy class |
|
135 |
" |
|
136 |
! |
|
137 |
||
138 |
shallowCopy |
|
139 |
"when copying, return a real (mutable) ByteArray" |
|
140 |
||
141 |
|sz| |
|
142 |
||
143 |
sz := self size. |
|
16399 | 144 |
^ (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
|
145 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
146 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
147 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
148 |
" |
12456 | 149 |
! ! |
150 |
||
16749 | 151 |
!ImmutableByteArray methodsFor:'printing & storing'! |
152 |
||
153 |
displayOn:aGCOrStream |
|
154 |
(aGCOrStream isStream) ifTrue:[ |
|
155 |
self storeOn:aGCOrStream. |
|
156 |
aGCOrStream nextPutAll:'"immutable"'. |
|
157 |
^ self. |
|
158 |
]. |
|
159 |
super displayOn:aGCOrStream. |
|
160 |
! ! |
|
161 |
||
12456 | 162 |
!ImmutableByteArray methodsFor:'private'! |
163 |
||
164 |
species |
|
165 |
"Copies should be mutable" |
|
166 |
||
167 |
^ ByteArray |
|
16399 | 168 |
! |
169 |
||
170 |
speciesForCopy |
|
171 |
"Copies should be mutable" |
|
172 |
||
173 |
^ ByteArray |
|
12456 | 174 |
! ! |
175 |
||
176 |
!ImmutableByteArray methodsFor:'queries'! |
|
177 |
||
14388 | 178 |
isImmutable |
179 |
^ true |
|
180 |
! |
|
181 |
||
12456 | 182 |
isLiteral |
183 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
184 |
(i.e. can be used in constant arrays)" |
|
185 |
||
186 |
"yes, I must be" |
|
187 |
^ true |
|
188 |
||
12488 | 189 |
! |
190 |
||
191 |
referencesAny:aCollection |
|
192 |
"redefined to speed up searching when many byteArray instances are present" |
|
193 |
||
194 |
^ false |
|
12456 | 195 |
! ! |
196 |
||
197 |
!ImmutableByteArray methodsFor:'specials'! |
|
198 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
199 |
become:anotherObject |
12456 | 200 |
"trigger an error if I should become something else |
201 |
(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
|
202 |
|
12493 | 203 |
self noModificationError. |
12456 | 204 |
^ super become:anotherObject |
205 |
! |
|
206 |
||
207 |
becomeNil |
|
208 |
"trigger an error if I should become nil |
|
209 |
(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
|
210 |
|
12493 | 211 |
self noModificationError. |
12456 | 212 |
^ super becomeNil |
213 |
! ! |
|
214 |
||
215 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
216 |
||
217 |
version_CVS |
|
16823 | 218 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.14 2014-08-23 17:43:58 cg Exp $' |
12456 | 219 |
! ! |
16399 | 220 |