12469
|
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 |
"
|
12456
|
12 |
"{ Package: 'stx:libcomp' }"
|
|
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
|
|
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 |
|
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
|
|
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 |
|
12472
|
58 |
ATTENTION:
|
|
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.
|
|
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...
|
|
65 |
|
12465
|
66 |
[see also:]
|
|
67 |
ImmutableString
|
|
68 |
ImmutableArray
|
|
69 |
Parser Scanner
|
|
70 |
|
|
71 |
[author:]
|
|
72 |
Claus Gittinger
|
|
73 |
"
|
|
74 |
! !
|
12456
|
75 |
|
|
76 |
!ImmutableByteArray methodsFor:'accessing'!
|
|
77 |
|
12493
|
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."
|
12493
|
82 |
|
|
83 |
self noModificationError.
|
12456
|
84 |
^ super at:index put:value
|
|
85 |
!
|
|
86 |
|
12493
|
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."
|
12493
|
91 |
|
|
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 |
"
|
|
104 |
#[1 2 3 4] asImmutableByteArray asByteArray
|
|
105 |
"
|
|
106 |
!
|
|
107 |
|
12456
|
108 |
asImmutableByteArray
|
|
109 |
^ self
|
|
110 |
! !
|
|
111 |
|
|
112 |
!ImmutableByteArray methodsFor:'copying'!
|
|
113 |
|
|
114 |
copyEmpty
|
|
115 |
"when copying, return a real (mutable) ByteArray"
|
|
116 |
|
|
117 |
^ ByteArray new:self size
|
|
118 |
!
|
|
119 |
|
|
120 |
copyEmptyAndGrow:size
|
|
121 |
"when copying, return a real (mutable) ByteArray"
|
|
122 |
|
|
123 |
^ ByteArray new:size
|
|
124 |
!
|
|
125 |
|
|
126 |
postCopy
|
|
127 |
"when copied, make me a real (mutable) ByteArray"
|
|
128 |
|
|
129 |
self changeClassTo:ByteArray.
|
|
130 |
!
|
|
131 |
|
|
132 |
postDeepCopy
|
|
133 |
"when copied, make me a real (mutable) ByteArray"
|
|
134 |
|
|
135 |
self changeClassTo:ByteArray.
|
|
136 |
|
|
137 |
"
|
|
138 |
#[1 2 3 4] asImmutableByteArray copy class
|
|
139 |
#[1 2 3 4] asImmutableByteArray shallowCopy class
|
|
140 |
#[1 2 3 4] asImmutableByteArray deepCopy class
|
|
141 |
"
|
|
142 |
!
|
|
143 |
|
|
144 |
shallowCopy
|
|
145 |
"when copying, return a real (mutable) ByteArray"
|
|
146 |
|
|
147 |
|sz|
|
|
148 |
|
|
149 |
sz := self size.
|
|
150 |
^ (ByteArray new:sz)
|
|
151 |
replaceFrom:1 to:sz with:self startingAt:1
|
|
152 |
! !
|
|
153 |
|
|
154 |
!ImmutableByteArray methodsFor:'error handling'!
|
|
155 |
|
|
156 |
creator
|
|
157 |
"find the method that contains me"
|
|
158 |
|
|
159 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil.
|
|
160 |
|
|
161 |
"
|
|
162 |
#(1 2 3) creator
|
|
163 |
"
|
|
164 |
|
|
165 |
"Modified: 24.6.1996 / 15:36:28 / stefan"
|
|
166 |
!
|
|
167 |
|
12493
|
168 |
noModificationError
|
12456
|
169 |
"a store is attempted - for our convenience, find the method that
|
|
170 |
contains me, for a nicer error message"
|
|
171 |
|
12493
|
172 |
|creator msg context|
|
12456
|
173 |
|
|
174 |
creator := self creator.
|
|
175 |
creator notNil ifTrue:[
|
12493
|
176 |
msg := ' (' , creator whoString , ')'
|
12456
|
177 |
].
|
12493
|
178 |
context := thisContext sender.
|
|
179 |
"
|
12456
|
180 |
this error is reported on an attempt to store into a literal
|
12493
|
181 |
byte array. The literal was created in creator.
|
12456
|
182 |
If you press continue in the debugger, the store will be performed.
|
|
183 |
If you don't want this, press abort and check your code.
|
|
184 |
Storing into literals is VERY VERY bad coding style.
|
|
185 |
"
|
12493
|
186 |
NoModificationError
|
|
187 |
raiseRequestWith:self
|
|
188 |
errorString:msg
|
|
189 |
in:context.
|
|
190 |
|
|
191 |
"Created: / 3.8.1998 / 14:47:45 / cg"
|
12456
|
192 |
! !
|
|
193 |
|
|
194 |
!ImmutableByteArray methodsFor:'private'!
|
|
195 |
|
|
196 |
species
|
|
197 |
"Copies should be mutable"
|
|
198 |
|
|
199 |
^ ByteArray
|
|
200 |
! !
|
|
201 |
|
|
202 |
!ImmutableByteArray methodsFor:'queries'!
|
|
203 |
|
|
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 |
|
12488
|
211 |
!
|
|
212 |
|
|
213 |
referencesAny:aCollection
|
|
214 |
"redefined to speed up searching when many byteArray instances are present"
|
|
215 |
|
|
216 |
^ false
|
12456
|
217 |
! !
|
|
218 |
|
|
219 |
!ImmutableByteArray methodsFor:'specials'!
|
|
220 |
|
12493
|
221 |
become:anotherObject
|
12456
|
222 |
"trigger an error if I should become something else
|
|
223 |
(this would be an even more tricky manipulation)"
|
12493
|
224 |
|
|
225 |
self noModificationError.
|
12456
|
226 |
^ super become:anotherObject
|
|
227 |
!
|
|
228 |
|
|
229 |
becomeNil
|
|
230 |
"trigger an error if I should become nil
|
|
231 |
(this would be an even more tricky manipulation)"
|
12493
|
232 |
|
|
233 |
self noModificationError.
|
12456
|
234 |
^ super becomeNil
|
|
235 |
! !
|
|
236 |
|
|
237 |
!ImmutableByteArray class methodsFor:'documentation'!
|
|
238 |
|
|
239 |
version_CVS
|
12493
|
240 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.6 2009-11-05 17:37:55 stefan Exp $'
|
12456
|
241 |
! !
|