author | Claus Gittinger <cg@exept.de> |
Mon, 06 Mar 2006 10:00:22 +0100 | |
changeset 9236 | bf61cc0116ab |
parent 8807 | 606a4ec5b693 |
child 9412 | ddccdf467c12 |
permissions | -rw-r--r-- |
3714 | 1 |
" |
2 |
COPYRIGHT (c) 1998 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 |
||
13 |
||
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
14 |
"{ Package: 'stx:libcomp' }" |
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
15 |
|
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
16 |
String variableByteSubclass:#ImmutableString |
3714 | 17 |
instanceVariableNames:'' |
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Compiler-Support' |
|
21 |
! |
|
22 |
||
23 |
!ImmutableString class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 1998 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
||
38 |
! |
|
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
By default, string literals in smalltalk are mutable objects. That |
|
43 |
may lead to some subtle (and hard to find errors), if some method passes |
|
44 |
a string constant as argument to someone else, who changes the |
|
45 |
string using at:put: like messages. Since the string object is kept in |
|
46 |
the first methods literals, the string constant has now been changed without |
|
47 |
having the methods sourcecode reflect this. Thus, the method will |
|
48 |
behave differently from what its source may make you think. |
|
49 |
||
50 |
To help finding this kind of 'feature/bug', the compiler can be |
|
51 |
configured to create instances of this ImmutableString instead of Strings |
|
52 |
for string literals. Instances of ImmutableString catch storing accesses and |
|
53 |
enter the debugger. Although useful, this feature is disabled by default |
|
54 |
for compatibility to other smalltalk implementations. |
|
55 |
(Also, if turned on, this makes inspecting string literals entered in |
|
56 |
a workspace somewhat strange: you cannot modify it any longer). |
|
57 |
||
58 |
Turn the ImmutableString feature on by setting the Parsers class variable |
|
59 |
'StringsAreImmutable' to true or use the new launchers settings menu. |
|
60 |
||
61 |
[see also:] |
|
62 |
ImmutableArray |
|
63 |
Parser Scanner |
|
64 |
||
65 |
[author:] |
|
66 |
Claus Gittinger |
|
67 |
" |
|
68 |
||
69 |
! ! |
|
70 |
||
71 |
!ImmutableString methodsFor:'accessing'! |
|
72 |
||
73 |
at:index put:value |
|
74 |
"Trigger an error if an immutable string is stored into. |
|
75 |
The store will be performed (for compatibility reasons) if you continue |
|
76 |
in the debugger." |
|
77 |
||
78 |
self notifyStoreError. |
|
79 |
^ super at:index put:value |
|
80 |
||
81 |
"Created: / 3.8.1998 / 14:45:14 / cg" |
|
82 |
! ! |
|
83 |
||
84 |
!ImmutableString methodsFor:'copying'! |
|
85 |
||
86 |
copyEmpty |
|
87 |
"when copying, return a real (mutable) String" |
|
88 |
||
89 |
^ String new:self size |
|
90 |
||
91 |
"Created: / 3.8.1998 / 14:46:22 / cg" |
|
92 |
! |
|
93 |
||
94 |
copyEmptyAndGrow:size |
|
95 |
"when copying, return a real (mutable) String" |
|
96 |
||
97 |
^ String new:size |
|
98 |
||
99 |
"Created: / 3.8.1998 / 14:46:34 / cg" |
|
100 |
! |
|
101 |
||
102 |
postCopy |
|
103 |
"when copied, make it me a real (mutable) String" |
|
104 |
||
105 |
self changeClassTo:String. |
|
106 |
||
107 |
"Created: / 3.8.1998 / 14:46:45 / cg" |
|
108 |
! |
|
109 |
||
110 |
shallowCopy |
|
111 |
"when copying, return a real (mutable) String" |
|
112 |
||
113 |
|sz| |
|
114 |
||
115 |
sz := self size. |
|
116 |
^ (String new:sz) replaceFrom:1 to:sz with:self startingAt:1 |
|
117 |
||
118 |
"Created: / 3.8.1998 / 14:47:00 / cg" |
|
119 |
! ! |
|
120 |
||
121 |
!ImmutableString methodsFor:'error handling'! |
|
122 |
||
123 |
creator |
|
124 |
"find the method that contains me" |
|
125 |
||
9236 | 126 |
^ Method allSubInstances detect:[:aMethod | (aMethod referencesGlobal:self)] ifNone:nil. |
3714 | 127 |
|
128 |
" |
|
129 |
'hello' creator |
|
130 |
" |
|
131 |
||
132 |
"Created: / 3.8.1998 / 14:47:19 / cg" |
|
133 |
! |
|
134 |
||
135 |
notifyStoreError |
|
136 |
"a store is attempted - for our convenience, find the method that |
|
137 |
contains me, for a nicer error message" |
|
138 |
||
139 |
|creator msg| |
|
140 |
||
141 |
creator := self creator. |
|
142 |
msg := 'store into/change of literal'. |
|
143 |
creator notNil ifTrue:[ |
|
144 |
msg := msg , ' (' , creator whoString , ')' |
|
145 |
]. |
|
146 |
" |
|
147 |
this error is reported on an attempt to store into a literal |
|
148 |
string. The literal was created in creator. |
|
149 |
If you press continue in the debugger, the store will be performed. |
|
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
150 |
If you don't want this, press abort and check your code. |
3714 | 151 |
Storing into literals is VERY VERY bad coding style. |
152 |
" |
|
8807
606a4ec5b693
#notifyStoreError raise error proceedable.
Stefan Vogel <sv@exept.de>
parents:
3714
diff
changeset
|
153 |
self error:msg mayProceed:true |
3714 | 154 |
|
155 |
"Created: / 3.8.1998 / 14:47:45 / cg" |
|
156 |
! ! |
|
157 |
||
158 |
!ImmutableString methodsFor:'private'! |
|
159 |
||
160 |
species |
|
161 |
"Copies should be mutable" |
|
162 |
||
163 |
^ String |
|
164 |
||
165 |
"Created: / 3.8.1998 / 14:47:58 / cg" |
|
166 |
! ! |
|
167 |
||
168 |
!ImmutableString methodsFor:'queries'! |
|
169 |
||
170 |
isLiteral |
|
171 |
"return true, if the receiver can be used as a literal |
|
172 |
(i.e. can be used in constant strings)" |
|
173 |
||
174 |
"yes, I must be" |
|
175 |
^ true |
|
176 |
||
177 |
"Created: / 3.8.1998 / 14:48:13 / cg" |
|
178 |
! ! |
|
179 |
||
180 |
!ImmutableString methodsFor:'specials'! |
|
181 |
||
182 |
become:anotherObject |
|
183 |
"trigger an error if I should become something else |
|
184 |
(this would be an even more tricky manipulation)" |
|
185 |
||
186 |
self notifyStoreError. |
|
187 |
^ super become:anotherObject |
|
188 |
||
189 |
"Created: / 3.8.1998 / 14:48:29 / cg" |
|
190 |
! |
|
191 |
||
192 |
becomeNil |
|
193 |
"trigger an error if I should become nil |
|
194 |
(this would be an even more tricky manipulation)" |
|
195 |
||
196 |
self notifyStoreError. |
|
197 |
^ super becomeNil |
|
198 |
||
199 |
"Created: / 3.8.1998 / 14:48:37 / cg" |
|
200 |
! ! |
|
201 |
||
202 |
!ImmutableString class methodsFor:'documentation'! |
|
203 |
||
204 |
version |
|
9236 | 205 |
^ '$Header: /cvs/stx/stx/libbasic/ImmutableString.st,v 1.3 2006-03-06 09:00:22 cg Exp $' |
3714 | 206 |
! ! |