author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 29 Oct 2012 22:07:56 +0000 | |
branch | jv |
changeset 17976 | 50c2416f962a |
parent 17955 | f5ee690b1a27 |
child 18011 | deb0c3355881 |
permissions | -rw-r--r-- |
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 |
||
17976
50c2416f962a
Merged with /trunk (CVS)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17955
diff
changeset
|
156 |
isImmutable |
50c2416f962a
Merged with /trunk (CVS)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17955
diff
changeset
|
157 |
^ true |
50c2416f962a
Merged with /trunk (CVS)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17955
diff
changeset
|
158 |
! |
50c2416f962a
Merged with /trunk (CVS)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17955
diff
changeset
|
159 |
|
17735 | 160 |
isLiteral |
161 |
"return true, if the receiver can be used as a literal constant in ST syntax |
|
162 |
(i.e. can be used in constant arrays)" |
|
163 |
||
164 |
"yes, I must be" |
|
165 |
^ true |
|
166 |
||
167 |
! |
|
168 |
||
169 |
referencesAny:aCollection |
|
170 |
"redefined to speed up searching when many byteArray instances are present" |
|
171 |
||
172 |
^ false |
|
173 |
! ! |
|
174 |
||
175 |
!ImmutableByteArray methodsFor:'specials'! |
|
176 |
||
177 |
become:anotherObject |
|
178 |
"trigger an error if I should become something else |
|
179 |
(this would be an even more tricky manipulation)" |
|
180 |
||
181 |
self noModificationError. |
|
182 |
^ super become:anotherObject |
|
183 |
! |
|
184 |
||
185 |
becomeNil |
|
186 |
"trigger an error if I should become nil |
|
187 |
(this would be an even more tricky manipulation)" |
|
188 |
||
189 |
self noModificationError. |
|
190 |
^ super becomeNil |
|
191 |
! ! |
|
192 |
||
193 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
194 |
||
195 |
version_CVS |
|
17976
50c2416f962a
Merged with /trunk (CVS)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17955
diff
changeset
|
196 |
^ '§Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.11 2012/10/16 15:11:45 cg Exp §' |
17865 | 197 |
! |
198 |
||
199 |
version_SVN |
|
17976
50c2416f962a
Merged with /trunk (CVS)
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
17955
diff
changeset
|
200 |
^ '$Id: ImmutableByteArray.st 10858 2012-10-29 22:07:56Z vranyj1 $' |
17846 | 201 |
! ! |