author | convert-repo |
Thu, 07 Feb 2013 04:25:37 +0000 | |
changeset 14762 | dbcf4ab2af2e |
parent 14388 | 44305237837b |
child 16399 | b8933f4d84cc |
child 18011 | deb0c3355881 |
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 |
|
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) |
|
14114
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
139 |
replaceBytesFrom:1 to:sz with:self startingAt:1 |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
140 |
|
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
141 |
" |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
142 |
#[1 2 3 4] asImmutableByteArray shallowCopy |
e18145c4ab7f
comment/format in: #shallowCopy
Stefan Vogel <sv@exept.de>
parents:
14024
diff
changeset
|
143 |
" |
12456 | 144 |
! ! |
145 |
||
146 |
!ImmutableByteArray methodsFor:'private'! |
|
147 |
||
148 |
species |
|
149 |
"Copies should be mutable" |
|
150 |
||
151 |
^ ByteArray |
|
152 |
! ! |
|
153 |
||
154 |
!ImmutableByteArray methodsFor:'queries'! |
|
155 |
||
14388 | 156 |
isImmutable |
157 |
^ true |
|
158 |
! |
|
159 |
||
12456 | 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 |
||
12488 | 167 |
! |
168 |
||
169 |
referencesAny:aCollection |
|
170 |
"redefined to speed up searching when many byteArray instances are present" |
|
171 |
||
172 |
^ false |
|
12456 | 173 |
! ! |
174 |
||
175 |
!ImmutableByteArray methodsFor:'specials'! |
|
176 |
||
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
177 |
become:anotherObject |
12456 | 178 |
"trigger an error if I should become something else |
179 |
(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
|
180 |
|
12493 | 181 |
self noModificationError. |
12456 | 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)" |
|
12538
e6bf8c42e1d4
Moved to libbasic from libcomp - change package name
Stefan Vogel <sv@exept.de>
parents:
12493
diff
changeset
|
188 |
|
12493 | 189 |
self noModificationError. |
12456 | 190 |
^ super becomeNil |
191 |
! ! |
|
192 |
||
193 |
!ImmutableByteArray class methodsFor:'documentation'! |
|
194 |
||
195 |
version_CVS |
|
14388 | 196 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableByteArray.st,v 1.11 2012-10-16 15:11:45 cg Exp $' |
12456 | 197 |
! ! |