17735
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2009 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
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 |
"
|
|
12 |
"{ Package: 'stx:libbasic' }"
|
|
13 |
|
|
14 |
ByteArray variableByteSubclass:#ImmutableByteArray
|
|
15 |
instanceVariableNames:''
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'System-Compiler-Support'
|
|
19 |
!
|
|
20 |
|
|
21 |
!ImmutableByteArray class methodsFor:'documentation'!
|
|
22 |
|
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 2009 by eXept Software AG
|
|
26 |
All Rights Reserved
|
|
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 |
|
|
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
|
|
42 |
bytearray using at:put: like messages. Since the bytearray object is kept in
|
|
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
|
|
51 |
for compatibility to other smalltalk implementations.
|
|
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 |
|
|
58 |
ATTENTION:
|
17955
|
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...
|
17735
|
65 |
|
|
66 |
[see also:]
|
17955
|
67 |
ImmutableString
|
|
68 |
ImmutableArray
|
|
69 |
Parser Scanner
|
17735
|
70 |
|
|
71 |
[author:]
|
17955
|
72 |
Claus Gittinger
|
17735
|
73 |
"
|
|
74 |
! !
|
|
75 |
|
|
76 |
!ImmutableByteArray methodsFor:'accessing'!
|
|
77 |
|
|
78 |
at:index put:value
|
|
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."
|
|
82 |
|
|
83 |
self noModificationError.
|
|
84 |
^ super at:index put:value
|
|
85 |
!
|
|
86 |
|
|
87 |
basicAt:index put:value
|
|
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."
|
|
91 |
|
|
92 |
self noModificationError.
|
|
93 |
^ super basicAt:index put:value
|
|
94 |
! !
|
|
95 |
|
|
96 |
!ImmutableByteArray methodsFor:'converting'!
|
|
97 |
|
|
98 |
asByteArray
|
|
99 |
"return the receiver as a (mutable) byteArray"
|
|
100 |
|
|
101 |
^ self copy changeClassTo: ByteArray
|
|
102 |
|
|
103 |
"
|
|
104 |
#[1 2 3 4] asImmutableByteArray asByteArray
|
|
105 |
"
|
|
106 |
!
|
|
107 |
|
|
108 |
asImmutableByteArray
|
|
109 |
^ self
|
|
110 |
! !
|
|
111 |
|
|
112 |
!ImmutableByteArray methodsFor:'copying'!
|
|
113 |
|
|
114 |
postCopy
|
|
115 |
"when copied, make me a real (mutable) ByteArray"
|
|
116 |
|
|
117 |
self changeClassTo:ByteArray.
|
|
118 |
!
|
|
119 |
|
|
120 |
postDeepCopy
|
|
121 |
"when copied, make me a real (mutable) ByteArray"
|
|
122 |
|
|
123 |
self changeClassTo:ByteArray.
|
|
124 |
|
|
125 |
"
|
|
126 |
#[1 2 3 4] asImmutableByteArray copy class
|
|
127 |
#[1 2 3 4] asImmutableByteArray shallowCopy class
|
|
128 |
#[1 2 3 4] asImmutableByteArray deepCopy class
|
|
129 |
"
|
|
130 |
!
|
|
131 |
|
|
132 |
shallowCopy
|
|
133 |
"when copying, return a real (mutable) ByteArray"
|
|
134 |
|
|
135 |
|sz|
|
|
136 |
|
|
137 |
sz := self size.
|
|
138 |
^ (ByteArray new:sz)
|
17940
|
139 |
replaceBytesFrom:1 to:sz with:self startingAt:1
|
|
140 |
|
|
141 |
"
|
|
142 |
#[1 2 3 4] asImmutableByteArray shallowCopy
|
|
143 |
"
|
17735
|
144 |
! !
|
|
145 |
|
|
146 |
!ImmutableByteArray methodsFor:'private'!
|
|
147 |
|
|
148 |
species
|
|
149 |
"Copies should be mutable"
|
|
150 |
|
|
151 |
^ ByteArray
|
|
152 |
! !
|
|
153 |
|
|
154 |
!ImmutableByteArray methodsFor:'queries'!
|
|
155 |
|
|
156 |
isLiteral
|
|
157 |
"return true, if the receiver can be used as a literal constant in ST syntax
|
|
158 |
(i.e. can be used in constant arrays)"
|
|
159 |
|
|
160 |
"yes, I must be"
|
|
161 |
^ true
|
|
162 |
|
|
163 |
!
|
|
164 |
|
|
165 |
referencesAny:aCollection
|
|
166 |
"redefined to speed up searching when many byteArray instances are present"
|
|
167 |
|
|
168 |
^ false
|
|
169 |
! !
|
|
170 |
|
|
171 |
!ImmutableByteArray methodsFor:'specials'!
|
|
172 |
|
|
173 |
become:anotherObject
|
|
174 |
"trigger an error if I should become something else
|
|
175 |
(this would be an even more tricky manipulation)"
|
|
176 |
|
|
177 |
self noModificationError.
|
|
178 |
^ super become:anotherObject
|
|
179 |
!
|
|
180 |
|
|
181 |
becomeNil
|
|
182 |
"trigger an error if I should become nil
|
|
183 |
(this would be an even more tricky manipulation)"
|
|
184 |
|
|
185 |
self noModificationError.
|
|
186 |
^ super becomeNil
|
|
187 |
! !
|
|
188 |
|
|
189 |
!ImmutableByteArray class methodsFor:'documentation'!
|
|
190 |
|
|
191 |
version_CVS
|
17955
|
192 |
^ '§Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.10 2012/07/19 17:34:51 stefan Exp §'
|
17865
|
193 |
!
|
|
194 |
|
|
195 |
version_SVN
|
17955
|
196 |
^ '$Id: ImmutableByteArray.st 10829 2012-07-25 08:45:15Z vranyj1 $'
|
17846
|
197 |
! !
|