author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 02:21:27 +0100 | |
changeset 607 | a9a526c51233 |
parent 601 | 338404aae5ec |
child 625 | 5f45ffc4cdd4 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 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 |
||
253 | 13 |
CompiledCode subclass:#Method |
93 | 14 |
instanceVariableNames:'source sourcePosition category package' |
306 | 15 |
classVariableNames:'PrivateMethodSignal |
16 |
LastFileReference LastSourceFileName |
|
17 |
LastWhoClass' |
|
1 | 18 |
poolDictionaries:'' |
19 |
category:'Kernel-Methods' |
|
20 |
! |
|
21 |
||
42 | 22 |
!Method class methodsFor:'documentation'! |
23 |
||
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
159 | 27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
version |
|
601
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libbasic/Method.st,v 1.59 1995-11-23 00:27:24 cg Exp $' |
88 | 40 |
! |
41 |
||
42 | 42 |
documentation |
43 |
" |
|
68 | 44 |
this class defines protocol for executable methods; |
45 |
both compiled and interpreted methods are represented by this class. |
|
46 |
Compiled methods have a non-nil code field, while interpreted methods have |
|
47 |
a nil code field and non-nil byteCode field. |
|
88 | 48 |
If there are both non-nil code and bytecode fields, the VM will execute |
227 | 49 |
the machine-code of a method. If both are nil when executed, a noByteCode |
50 |
message is sent by the VM to the method where a signal is raised. |
|
1 | 51 |
|
227 | 52 |
The methods sourcecode is represented by source and sourcePosition: |
53 |
- if sourcePosition is a Number, the source-field is the fileName and |
|
54 |
sourcePosition is the character offset of the source-chunk in this source file. |
|
55 |
- If sourcePosition is nil, the source is the string in the source field. |
|
56 |
(an old version used ExternalString instances here, but that lead to |
|
57 |
10000 additional little objects ...) |
|
1 | 58 |
|
68 | 59 |
The flags field defines things like the number of method-locals, |
227 | 60 |
method arguments and stack requirements (for interpreted methods). |
68 | 61 |
Do not depend on any value in the flags field - it may change without |
62 |
notice. |
|
1 | 63 |
|
438 | 64 |
Notice, that in ST/X, method can be subclassed; executable code is |
65 |
identified not by being a subclass of Block or Method, but instead by |
|
66 |
having the executable flag bit set in the class. The VM can execute anything |
|
67 |
which is identified as executable (assuming that the first instance variable |
|
68 |
is the machine-code address) - this allows for easy future extension. |
|
69 |
||
93 | 70 |
Instance variables: |
71 |
||
159 | 72 |
source <String> the source itself (if sourcePosition isNil) |
73 |
or the fileName where the source is found |
|
93 | 74 |
|
159 | 75 |
sourcePosition <Integer> the position of the methods chunk in the file |
93 | 76 |
|
159 | 77 |
category <Symbol> the methods category |
78 |
package <Symbol> the package, in which the methods was defined |
|
227 | 79 |
|
80 |
||
81 |
Class variables: |
|
82 |
||
83 |
PrivateMethodSignal raised on privacy violation (see docu) |
|
84 |
||
85 |
LastFileReference weak reference to the last sourceFile |
|
86 |
LastSourceFileName to speedup source access via NFS |
|
93 | 87 |
|
68 | 88 |
WARNING: layout known by compiler and runtime system - dont change |
42 | 89 |
" |
176 | 90 |
! |
91 |
||
92 |
privacy |
|
93 |
" |
|
94 |
ST/X includes an EXPERIMENTAL implementation of method privacy. |
|
95 |
Individual methods may be set to private or protected via the |
|
96 |
#setPrivate and #setProtected messages. Also, categories may be |
|
202 | 97 |
filedIn as a whole as private using #privateMethodsFor: or as |
98 |
protected using #protectedMethodsFor: instead of the well known #methodsFor:. |
|
99 |
||
176 | 100 |
The additional #publicMethodsFor: is for documentation purposes, and |
227 | 101 |
is equivalent to #methodsFor: (also to support fileIn of ENVY methods). |
176 | 102 |
|
103 |
Private methods may be executed only when called via a self or super-send |
|
104 |
from the superclass, the class itself or subclasses. |
|
415 | 105 |
Protected methods may not be called from subclasses, i.e. they may only |
106 |
be called via self sends from within the current class. |
|
107 |
(i.e. private methods are less private than protected ones) |
|
176 | 108 |
|
109 |
When such a situation arises, the VM (runtime system) will raise the |
|
110 |
PrivateMethodSignal exception (if nonNil), which usually brings you into the |
|
111 |
debugger. |
|
112 |
||
227 | 113 |
If PrivateMethodSignal is nil, the VM will not check for this, and |
176 | 114 |
execution is as usual. (you may want to nil-it for production code, |
115 |
and leave it non nil during development). |
|
116 |
||
117 |
NOTICE: there is no (not yet ?) standard defined for method privacy, |
|
118 |
however, the interface was designed to be somewhat ENVY compatible (from |
|
119 |
what can be deduced by reading PD code). |
|
120 |
Also, the usability of privacy is still to be tested. |
|
415 | 121 |
This interface, the implementation and the rules for when a privacy violation |
122 |
may change (in case of some ANSI standard being defined). |
|
227 | 123 |
Be warned and send me suggestions & critics (constructive ;-) |
176 | 124 |
" |
438 | 125 |
! |
126 |
||
127 |
dynamicMethods |
|
128 |
" |
|
129 |
On systems which support dynamic loading of machine code (SYS5.4, Linux), |
|
130 |
methods may now be compiled to machine code from within the browser, |
|
131 |
and the resulting machine code object be loaded in. |
|
132 |
The ObjectFileLoader keeps (weak) handles to the resulting methods and |
|
133 |
invalidates the corresponding method objects, if the underlying methods |
|
134 |
object code is unloaded. |
|
135 |
Invalid methods will trap into the debugger when executed; |
|
136 |
also, the browser marks them as '(* not executable *)' in its method list. |
|
137 |
" |
|
42 | 138 |
! ! |
1 | 139 |
|
109 | 140 |
!Method class methodsFor:'initialization'! |
141 |
||
142 |
initialize |
|
143 |
PrivateMethodSignal isNil ifTrue:[ |
|
159 | 144 |
"EXPERIMENTAL" |
145 |
PrivateMethodSignal := ExecutionErrorSignal newSignalMayProceed:true. |
|
146 |
PrivateMethodSignal nameClass:self message:#privateMethodSignal. |
|
176 | 147 |
PrivateMethodSignal notifierString:'attempt to execute private/protected method'. |
109 | 148 |
] |
149 |
! ! |
|
150 |
||
345 | 151 |
!Method class methodsFor:'Signal constants'! |
159 | 152 |
|
153 |
privateMethodSignal |
|
176 | 154 |
"return the signal raised when a private/protected method is called |
155 |
by some other object (i.e. not a self- or super send)" |
|
156 |
||
159 | 157 |
^ PrivateMethodSignal |
158 |
! ! |
|
159 |
||
3 | 160 |
!Method class methodsFor:'queries'! |
161 |
||
162 |
isBuiltInClass |
|
163 |
"this class is known by the run-time-system" |
|
164 |
||
165 |
^ self == Method |
|
44 | 166 |
! |
167 |
||
168 |
maxNumberOfArguments |
|
169 |
"this limit will be removed in one of the next versions ..." |
|
170 |
||
423 | 171 |
^ 15 "cannot be easily changed; should ask the VM about this number" |
3 | 172 |
! ! |
173 |
||
571 | 174 |
!Method methodsFor:'copying'! |
175 |
||
176 |
copy |
|
572 | 177 |
"redefined to change source ref into a real string" |
178 |
||
571 | 179 |
|aCopy| |
180 |
||
181 |
aCopy := super copy. |
|
182 |
sourcePosition notNil ifTrue:[ |
|
183 |
aCopy source:(self source) |
|
184 |
]. |
|
185 |
^ aCopy |
|
186 |
! ! |
|
187 |
||
259 | 188 |
!Method methodsFor:'queries'! |
189 |
||
190 |
isMethod |
|
191 |
"return true, if the receiver is some kind of method; |
|
192 |
true returned here - the method is redefined from Object." |
|
193 |
||
194 |
^ true |
|
423 | 195 |
! |
196 |
||
197 |
modificationTime |
|
198 |
"try to extract the modificationTime as a timeStamp from |
|
199 |
the receivers source. If there is no source or no history line, |
|
200 |
we do not know the modification time, and nil is returned." |
|
201 |
||
202 |
|s list histLine| |
|
203 |
||
204 |
HistoryManager isNil ifTrue:[^ nil]. |
|
205 |
s := self source. |
|
206 |
s isNil ifTrue:[^ nil]. |
|
207 |
list := HistoryManager getAllHistoriesFrom:s. |
|
208 |
list size == 0 ifTrue:[^ nil]. |
|
209 |
histLine := list last. |
|
210 |
^ AbsoluteTime date:histLine date time:histLine time |
|
211 |
||
212 |
" |
|
213 |
(Method compiledMethodAt:#modificationTime) modificationTime |
|
214 |
(Method compiledMethodAt:#isMethod) modificationTime |
|
215 |
" |
|
216 |
||
217 |
"Modified: 8.9.1995 / 15:08:22 / claus" |
|
259 | 218 |
! ! |
219 |
||
1 | 220 |
!Method methodsFor:'accessing'! |
221 |
||
222 |
source |
|
223 |
"return the sourcestring for the receiver" |
|
224 |
||
443 | 225 |
|aStream fileName junk w myClass| |
1 | 226 |
|
227 |
source notNil ifTrue:[ |
|
176 | 228 |
" |
229 |
if sourcePosition is nonNil, its the fileName and |
|
230 |
sourcePosition is the offset. |
|
231 |
Otherwise, source is the real source |
|
232 |
" |
|
159 | 233 |
sourcePosition isNil ifTrue:[^ source]. |
176 | 234 |
"/ |
235 |
"/ original code: |
|
236 |
"/ |
|
237 |
"/ aStream := Smalltalk systemFileStreamFor:('source/' , source). |
|
238 |
"/ aStream notNil ifTrue:[ |
|
239 |
"/ aStream position:sourcePosition. |
|
240 |
"/ junk := aStream nextChunk. |
|
241 |
"/ aStream close |
|
242 |
"/ ] |
|
243 |
||
244 |
"/ |
|
245 |
"/ we keep the last source file open, because open/close |
|
246 |
"/ operations maybe somewhat slow on NFS-mounted file systems |
|
247 |
"/ Since the reference to the file is weak, it will be closed |
|
248 |
"/ automatically if the file is not referenced for a while. Neat trick. |
|
249 |
||
250 |
LastSourceFileName = source ifTrue:[ |
|
251 |
aStream := LastFileReference at:1. |
|
252 |
]. |
|
253 |
||
254 |
aStream isNil ifTrue:[ |
|
328 | 255 |
fileName := Smalltalk getSourceFileName:source. |
601
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
256 |
"/ fileName isNil ifTrue:[ |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
257 |
"/ fileName := source |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
258 |
"/ ]. |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
259 |
fileName notNil ifTrue:[ |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
260 |
aStream := fileName asFilename readStream. |
192 | 261 |
]. |
443 | 262 |
aStream isNil ifTrue:[ |
486
1e19564b2b5e
prepare for CVS SourceCodeManager
Claus Gittinger <cg@exept.de>
parents:
463
diff
changeset
|
263 |
w := self who. |
1e19564b2b5e
prepare for CVS SourceCodeManager
Claus Gittinger <cg@exept.de>
parents:
463
diff
changeset
|
264 |
w notNil ifTrue:[ |
1e19564b2b5e
prepare for CVS SourceCodeManager
Claus Gittinger <cg@exept.de>
parents:
463
diff
changeset
|
265 |
myClass := w at:1. |
522
198bf04a3aa4
moved source-file access code into class
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
266 |
aStream := myClass sourceStream. |
486
1e19564b2b5e
prepare for CVS SourceCodeManager
Claus Gittinger <cg@exept.de>
parents:
463
diff
changeset
|
267 |
]. |
601
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
268 |
"/ |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
269 |
"/ final chance: try current directory |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
270 |
"/ |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
271 |
aStream isNil ifTrue:[ |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
272 |
aStream := source asFilename readStream. |
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
273 |
]. |
443 | 274 |
] |
176 | 275 |
]. |
276 |
||
159 | 277 |
aStream notNil ifTrue:[ |
278 |
aStream position:sourcePosition. |
|
279 |
junk := aStream nextChunk. |
|
176 | 280 |
|
281 |
" |
|
282 |
keep a weak reference - maybe its needed again soon ... |
|
283 |
" |
|
284 |
LastFileReference isNil ifTrue:[ |
|
285 |
LastFileReference := WeakArray new:1 |
|
286 |
]. |
|
287 |
LastFileReference at:1 put:aStream. |
|
288 |
LastSourceFileName := source |
|
159 | 289 |
] |
1 | 290 |
]. |
291 |
^ junk |
|
486
1e19564b2b5e
prepare for CVS SourceCodeManager
Claus Gittinger <cg@exept.de>
parents:
463
diff
changeset
|
292 |
|
601
338404aae5ec
take local source file as last chance only (in case of a fileOut,
Claus Gittinger <cg@exept.de>
parents:
572
diff
changeset
|
293 |
"Modified: 23.11.1995 / 01:23:48 / cg" |
1 | 294 |
! |
295 |
||
296 |
source:aString |
|
297 |
"set the methods sourcestring" |
|
298 |
||
299 |
source := aString. |
|
300 |
sourcePosition := nil |
|
301 |
! |
|
302 |
||
192 | 303 |
sourceFilename |
1 | 304 |
"return the sourcefilename if source is extern; nil otherwise" |
305 |
||
306 |
sourcePosition notNil ifTrue:[^ source]. |
|
307 |
^ nil |
|
308 |
! |
|
309 |
||
310 |
sourcePosition |
|
311 |
"return the sourceposition if source is extern; nil otherwise" |
|
312 |
||
313 |
^ sourcePosition |
|
314 |
! |
|
315 |
||
192 | 316 |
sourceFilename:aFileName position:aNumber |
1 | 317 |
"set the methods sourcefile/position" |
318 |
||
319 |
source := aFileName. |
|
320 |
sourcePosition := aNumber |
|
321 |
! |
|
322 |
||
39 | 323 |
comment |
324 |
"return the methods comment. |
|
68 | 325 |
This is done by searching for and returning the first comment |
159 | 326 |
from the methods source (excluding any double-quotes). |
68 | 327 |
Returns nil if there is no comment (or source is not available)." |
39 | 328 |
|
159 | 329 |
|src stream| |
39 | 330 |
|
159 | 331 |
src := self source. |
332 |
src isNil ifTrue:[^ nil]. |
|
93 | 333 |
|
159 | 334 |
stream := ReadStream on:src. |
335 |
(stream skipThrough:Character doubleQuote) isNil ifTrue:[^ nil]. |
|
336 |
^ stream upTo:Character doubleQuote. |
|
93 | 337 |
|
338 |
" |
|
159 | 339 |
(Method compiledMethodAt:#comment) comment |
93 | 340 |
" |
39 | 341 |
! |
342 |
||
1 | 343 |
category |
344 |
"return the methods category or nil" |
|
345 |
||
346 |
^ category |
|
347 |
! |
|
348 |
||
349 |
category:aStringOrSymbol |
|
350 |
"set the methods category" |
|
351 |
||
352 |
category := aStringOrSymbol asSymbol |
|
353 |
! |
|
354 |
||
93 | 355 |
package |
356 |
"return the package-symbol" |
|
357 |
||
358 |
^ package |
|
359 |
! |
|
360 |
||
227 | 361 |
package:aSymbol |
362 |
"set the package-symbol" |
|
363 |
||
364 |
package := aSymbol |
|
365 |
! |
|
366 |
||
1 | 367 |
flags |
44 | 368 |
"return the flags (number of method variables, stacksize etc.). |
369 |
Dont depend on the values in the flag field - it may change |
|
370 |
without notice." |
|
1 | 371 |
|
372 |
^ flags |
|
373 |
! |
|
374 |
||
375 |
flags:newFlags |
|
68 | 376 |
"set the flags (number of method variables, stacksize). |
377 |
WARNING: for internal use by the compiler only." |
|
1 | 378 |
|
56 | 379 |
"protect myself a bit - putting in an object would crash me ..." |
380 |
(newFlags isMemberOf:SmallInteger) ifTrue:[ |
|
159 | 381 |
flags := newFlags |
56 | 382 |
] |
1 | 383 |
! |
384 |
||
414 | 385 |
privacy |
386 |
"return a symbol describing the methods access rights (privacy); |
|
387 |
Currently, this is one of #private, #protected and #public. |
|
388 |
Notice: method privacy is a nonstandard feature, not supported |
|
389 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
390 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
391 |
It may change or even vanish (if it shows to be not useful)." |
|
392 |
||
393 |
self isPrivate ifTrue:[^ #private]. |
|
394 |
self isProtected ifTrue:[^ #protected]. |
|
395 |
^ #public |
|
396 |
||
397 |
"Modified: 27.8.1995 / 22:53:31 / claus" |
|
398 |
! |
|
399 |
||
400 |
privacy:aSymbol |
|
401 |
"set the methods access rights (privacy) from a symbol; |
|
402 |
Currently, this must be one of #private, #protected and #public. |
|
403 |
Notice: method privacy is a nonstandard feature, not supported |
|
404 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
405 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
406 |
It may change or even vanish (if it shows to be not useful)." |
|
407 |
||
415 | 408 |
aSymbol == #public ifTrue:[^ self setToPublic]. |
409 |
aSymbol == #private ifTrue:[ |
|
410 |
self setToPrivate |
|
411 |
] ifFalse:[ |
|
412 |
aSymbol == #protected ifTrue:[ |
|
413 |
self setToProtected |
|
414 |
]. |
|
415 |
]. |
|
416 |
ObjectMemory flushCaches. |
|
414 | 417 |
|
418 |
"Modified: 27.8.1995 / 22:58:08 / claus" |
|
419 |
! |
|
420 |
||
176 | 421 |
setToPrivate |
422 |
"set the flag bit stating that this method is private. |
|
423 |
Execution of the receiver will only be allowed for self/super-sends from |
|
424 |
the class, superclasses or subclasses (or via #perform). |
|
425 |
If a private method is called by some other class, a runtime |
|
426 |
error (PrivateMethodSignal) is raised. |
|
427 |
Notice: method privacy is a nonstandard feature, not supported |
|
428 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
429 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
430 |
It may change or even vanish (if it shows to be not useful)." |
|
68 | 431 |
|
432 |
%{ /* NOCONTEXT */ |
|
176 | 433 |
/* I made this a primitive to get the define constant from stc.h */ |
434 |
||
435 |
#ifdef F_PRIVATE |
|
68 | 436 |
int f = _intVal(_INST(flags)); |
437 |
||
360 | 438 |
f = (f & ~F_CLASSPRIVATE) | F_PRIVATE; |
68 | 439 |
_INST(flags) = _MKSMALLINT(f); |
440 |
#endif |
|
441 |
%} |
|
442 |
! |
|
443 |
||
176 | 444 |
setToProtected |
445 |
"set the flag bit stating that this method is protected. |
|
446 |
Execution of the receiver will only be allowed for self sends from |
|
447 |
the class or superclasses. (or via #perform). |
|
448 |
If a private method is called by some other class, a runtime |
|
449 |
error (PrivateMethodSignal) is raised. |
|
450 |
Notice: method privacy is a nonstandard feature, not supported |
|
451 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
452 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
453 |
It may change or even vanish (if it shows to be not useful)." |
|
454 |
||
455 |
%{ /* NOCONTEXT */ |
|
456 |
/* I made this a primitive to get the define constant from stc.h */ |
|
457 |
||
458 |
#ifdef F_CLASSPRIVATE |
|
459 |
int f = _intVal(_INST(flags)); |
|
460 |
||
360 | 461 |
f = (f & ~F_PRIVATE) | F_CLASSPRIVATE; |
176 | 462 |
_INST(flags) = _MKSMALLINT(f); |
463 |
#endif |
|
464 |
%} |
|
465 |
! |
|
466 |
||
467 |
setToPublic |
|
213 | 468 |
"clear any privacy of the receiver. The receiver may be executed by |
360 | 469 |
any send. This is the default. |
470 |
Notice: method privacy is a nonstandard feature, not supported |
|
471 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
472 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
473 |
It may change or even vanish (if it shows to be not useful)." |
|
68 | 474 |
|
475 |
%{ /* NOCONTEXT */ |
|
176 | 476 |
/* I made this a primitive to get the define constant from stc.h */ |
477 |
||
68 | 478 |
int f = _intVal(_INST(flags)); |
479 |
||
176 | 480 |
#if F_PRIVATE |
481 |
f = f & ~F_PRIVATE; |
|
482 |
#endif |
|
483 |
#if F_CLASSPRIVATE |
|
484 |
f = f & ~F_CLASSPRIVATE; |
|
485 |
#endif |
|
486 |
_INST(flags) = _MKSMALLINT(f); |
|
487 |
%} |
|
488 |
! |
|
489 |
||
490 |
isPrivate |
|
491 |
"return true, if this is a private method. |
|
492 |
Execution of private methods is only allowed via self/super sends |
|
493 |
from superclasses, the class itself or subclasses. |
|
494 |
If a private method is called by some other class, a runtime |
|
495 |
error (PrivateMethodSignal) is raised. |
|
496 |
Notice: method privacy is a nonstandard feature, not supported |
|
497 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
498 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
499 |
It may change or even vanish (if it shows to be not useful)." |
|
500 |
||
501 |
%{ /* NOCONTEXT */ |
|
502 |
/* I made this a primitive to get the define constant from stc.h */ |
|
503 |
||
68 | 504 |
#ifdef F_PRIVATE |
176 | 505 |
int f = _intVal(_INST(flags)); |
506 |
||
68 | 507 |
if (f & F_PRIVATE) { |
159 | 508 |
RETURN (true); |
68 | 509 |
} |
510 |
#endif |
|
511 |
%}. |
|
512 |
^ false |
|
513 |
! |
|
514 |
||
176 | 515 |
isProtected |
516 |
"return true, if this is a protected method. |
|
517 |
Execution of protected methods is only allowed via self sends |
|
518 |
from superclasses or the class itself. |
|
519 |
If a protected method is called by some other class, a runtime |
|
520 |
error (PrivateMethodSignal) is raised. |
|
521 |
Notice: method privacy is a nonstandard feature, not supported |
|
522 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
523 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
524 |
It may change or even vanish (if it shows to be not useful)." |
|
525 |
||
526 |
%{ /* NOCONTEXT */ |
|
527 |
/* I made this a primitive to get the define constant from stc.h */ |
|
528 |
||
529 |
#ifdef F_CLASSPRIVATE |
|
530 |
int f = _intVal(_INST(flags)); |
|
531 |
||
532 |
if (f & F_CLASSPRIVATE) { |
|
533 |
RETURN (true); |
|
534 |
} |
|
535 |
#endif |
|
536 |
%}. |
|
537 |
^ false |
|
538 |
! |
|
539 |
||
540 |
isPublic |
|
541 |
"return true, if this is a public method - I.e. can be executed via any send. |
|
360 | 542 |
This is the default. |
543 |
Notice: method privacy is a nonstandard feature, not supported |
|
544 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
545 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
546 |
It may change or even vanish (if it shows to be not useful)." |
|
176 | 547 |
|
548 |
%{ /* NOCONTEXT */ |
|
549 |
/* I made this a primitive to get the define constant from stc.h */ |
|
550 |
||
551 |
int f = _intVal(_INST(flags)); |
|
552 |
#ifdef F_PRIVATE |
|
553 |
if (f & F_PRIVATE) { |
|
554 |
RETURN (false); |
|
555 |
} |
|
556 |
#endif |
|
557 |
#ifdef F_CLASSPRIVATE |
|
558 |
if (f & F_CLASSPRIVATE) { |
|
559 |
RETURN (false); |
|
560 |
} |
|
561 |
#endif |
|
562 |
%}. |
|
563 |
^ true |
|
564 |
! |
|
565 |
||
532
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
566 |
restricted:aBoolean |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
567 |
"set or clear the flag bit stating that this method is restricted. |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
568 |
Execution of the receiver will only be allowed if the system is not in |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
569 |
'trap restricted mode' (-->ObjectMemory) otherise a runtime |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
570 |
error (PrivateMethodSignal) is raised. |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
571 |
Notice: method restriction is a nonstandard feature, not supported |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
572 |
by other smalltalk implementations and not specified in the ANSI spec. |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
573 |
This is EXPERIMENTAL - and being evaluated for usability. |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
574 |
It may change or even vanish (if it shows to be not useful)." |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
575 |
|
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
576 |
%{ /* NOCONTEXT */ |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
577 |
/* I made this a primitive to get the define constant from stc.h */ |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
578 |
|
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
579 |
#ifdef F_RESTRICTED |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
580 |
int f = _intVal(_INST(flags)); |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
581 |
int old = f; |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
582 |
|
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
583 |
if (aBoolean == true) |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
584 |
f |= F_RESTRICTED; |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
585 |
else |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
586 |
f &= ~F_RESTRICTED; |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
587 |
_INST(flags) = _MKSMALLINT(f); |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
588 |
if (old & F_RESTRICTED) |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
589 |
RETURN(true); |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
590 |
#endif |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
591 |
%}. |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
592 |
^ false |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
593 |
|
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
594 |
"(ObjectMemory class compiledMethodAt:#compressingGarbageCollect) restricted:true" |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
595 |
|
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
596 |
"Created: 7.11.1995 / 20:36:19 / stefan" |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
597 |
! |
2511c99de912
New code to trap restricted (RT) methods.
Stefan Vogel <sv@exept.de>
parents:
530
diff
changeset
|
598 |
|
44 | 599 |
numberOfMethodArgs:aNumber |
600 |
"currently, the number of arguments is NOT remembered in |
|
49 | 601 |
methods, but this will be added soon to allow for more checking |
602 |
in #perform:. |
|
603 |
||
44 | 604 |
The limitation in the max. number of arguments is due to the |
109 | 605 |
missing SENDxx functions in the VM and cases in #perform. This too |
606 |
will be removed in a later release, allowing any number of arguments. |
|
44 | 607 |
- for use by compiler only." |
608 |
||
56 | 609 |
(aNumber between:0 and:self class maxNumberOfArguments) ifFalse:[ |
159 | 610 |
self error:('ST/X only supports up to a maximum of ' , |
611 |
self class maxNumberOfArguments printString , |
|
612 |
' method arguments'). |
|
613 |
^ self |
|
56 | 614 |
]. |
615 |
%{ |
|
616 |
/* made this a primitive to get define in stc.h */ |
|
617 |
#ifdef F_NARGS |
|
618 |
_INST(flags) = _MKSMALLINT( (_intVal(_INST(flags)) & ~F_NARGS) | (_intVal(aNumber) << F_NARGSHIFT) ); |
|
619 |
#endif |
|
620 |
%} |
|
44 | 621 |
! |
622 |
||
49 | 623 |
numberOfMethodArgs |
624 |
"return the number of arguments, the method expects." |
|
625 |
||
56 | 626 |
%{ /* NOCONTEXT */ |
627 |
/* made this a primitive to get define in stc.h */ |
|
628 |
||
629 |
#ifdef F_NARGS |
|
630 |
RETURN (_MKSMALLINT((_intVal(_INST(flags)) & F_NARGS) >> F_NARGSHIFT)); |
|
631 |
#endif |
|
632 |
%} |
|
633 |
. |
|
49 | 634 |
" |
56 | 635 |
The old implementation simply counted the arguments from |
636 |
the methods source - new versions include this information |
|
109 | 637 |
in the flag instVar, for more security in #perform: |
68 | 638 |
" |
49 | 639 |
|
640 |
^ self methodArgNames size |
|
641 |
! |
|
642 |
||
1 | 643 |
numberOfMethodVars:aNumber |
56 | 644 |
"set the number of method variables - for use by compiler only. |
68 | 645 |
WARNING: playing around here with incorrect values |
159 | 646 |
may crash smalltalk badly." |
1 | 647 |
|
56 | 648 |
%{ /* NOCONTEXT */ |
649 |
int f = _intVal(_INST(flags)); |
|
1 | 650 |
|
651 |
/* made this a primitive to get define in stc.h */ |
|
253 | 652 |
if (__isSmallInteger(aNumber)) { |
159 | 653 |
f = (f & ~F_NVARS) | (_intVal(aNumber) << F_NVARSHIFT); |
654 |
_INST(flags) = _MKSMALLINT(f); |
|
56 | 655 |
} |
1 | 656 |
%} |
657 |
! |
|
658 |
||
49 | 659 |
numberOfMethodVars |
660 |
"return the number of method local variables. |
|
661 |
Do not depend on the returned value - future optimizations |
|
68 | 662 |
may change things here (i.e. when moving block-locals into |
663 |
surrounding method for inlining). |
|
49 | 664 |
- for debugging only." |
665 |
||
666 |
%{ /* NOCONTEXT */ |
|
667 |
/* made this a primitive to get define in stc.h */ |
|
668 |
||
56 | 669 |
RETURN (_MKSMALLINT((_intVal(_INST(flags)) & F_NVARS) >> F_NVARSHIFT)); |
49 | 670 |
%} |
671 |
! |
|
672 |
||
1 | 673 |
stackSize:aNumber |
56 | 674 |
"set the depth of the local stack - for use by compiler only. |
68 | 675 |
WARNING: playing around here with incorrect values |
159 | 676 |
may crash smalltalk badly. |
677 |
(if the runtime library was compiled with DEBUG, |
|
678 |
a bad stack will be detected and triggers an error)" |
|
1 | 679 |
|
56 | 680 |
%{ /* NOCONTEXT */ |
681 |
int f = _intVal(_INST(flags)); |
|
682 |
||
1 | 683 |
/* made this a primitive to get define in stc.h */ |
253 | 684 |
if (__isSmallInteger(aNumber)) { |
159 | 685 |
f = (f & ~F_NSTACK) | (_intVal(aNumber) << F_NSTACKSHIFT); |
686 |
_INST(flags) = _MKSMALLINT(f); |
|
56 | 687 |
} |
1 | 688 |
%} |
49 | 689 |
! |
690 |
||
691 |
stackSize |
|
692 |
"return the number of temporaries needed as stack in the context. |
|
693 |
Do not depend on the returned value - future optimizations |
|
694 |
may change things here. |
|
695 |
- for debugging only." |
|
696 |
||
697 |
%{ /* NOCONTEXT */ |
|
698 |
/* made this a primitive to get define in stc.h */ |
|
699 |
||
56 | 700 |
RETURN (_MKSMALLINT((_intVal(_INST(flags)) & F_NSTACK) >> F_NSTACKSHIFT)); |
49 | 701 |
%} |
1 | 702 |
! ! |
703 |
||
704 |
!Method methodsFor:'queries'! |
|
705 |
||
159 | 706 |
who |
707 |
"return the class and selector of where I am defined in. |
|
56 | 708 |
Since there is no information of the containing class |
159 | 709 |
in the method, we have to do a search here. |
68 | 710 |
|
711 |
Normally, this is not a problem, except when a method is |
|
159 | 712 |
accepted in the debugger or redefined from within a method |
713 |
(maybe done indirectly, if doIt is done recursively) |
|
714 |
- the information about which class the original method was |
|
715 |
defined in is lost in this case. |
|
68 | 716 |
|
306 | 717 |
Problem: this is heavily called for in the debugger to create |
718 |
a readable context walkback. For unbound methods, it is |
|
719 |
slow, since the search (over all classes) will always fail. |
|
720 |
Q: should we add a backref from the method to the class |
|
721 |
and/or add a subclass of Method for unbound ones ?" |
|
722 |
||
723 |
|classes cls sel| |
|
724 |
||
725 |
" |
|
726 |
very first, look in the class we found something the last time |
|
727 |
this may often give a hit, when asking who repeatingly for |
|
728 |
a context chain. (keep last by its name, to not keep classes from |
|
729 |
being garbage collected) |
|
730 |
" |
|
731 |
LastWhoClass notNil ifTrue:[ |
|
732 |
cls := Smalltalk at:LastWhoClass. |
|
308 | 733 |
cls notNil ifTrue:[ |
328 | 734 |
sel := cls selectorAtMethod:self. |
308 | 735 |
sel notNil ifTrue:[^ Array with:cls with:sel]. |
736 |
] |
|
306 | 737 |
]. |
1 | 738 |
|
159 | 739 |
" |
740 |
first, limit the search to global classes only - |
|
741 |
since probability is high, that the receiver is found in there ... |
|
742 |
" |
|
307 | 743 |
classes := Smalltalk allClasses. |
306 | 744 |
" |
745 |
instance methods are usually more common - search those first |
|
746 |
" |
|
747 |
classes do:[:aClass | |
|
159 | 748 |
|sel| |
749 |
||
328 | 750 |
sel := aClass selectorAtMethod:self. |
306 | 751 |
sel notNil ifTrue:[LastWhoClass := aClass name. |
752 |
^ Array with:aClass with:sel]. |
|
753 |
]. |
|
754 |
||
755 |
LastWhoClass := nil. |
|
756 |
classes do:[:aClass | |
|
757 |
|sel| |
|
758 |
||
328 | 759 |
sel := aClass class selectorAtMethod:self. |
159 | 760 |
sel notNil ifTrue:[^ Array with:aClass class with:sel]. |
1 | 761 |
]. |
77 | 762 |
" |
763 |
mhmh - must be a method of some anonymous class (i.e. one not |
|
227 | 764 |
in the Smalltalk dictionary). Search all instances of Behavior |
77 | 765 |
" |
328 | 766 |
Behavior allSubInstancesDo:[:someClass | |
159 | 767 |
|sel| |
768 |
||
328 | 769 |
sel := someClass selectorAtMethod:self. |
159 | 770 |
sel notNil ifTrue:[^ Array with:someClass with:sel] |
77 | 771 |
]. |
772 |
" |
|
773 |
none found - sorry |
|
774 |
" |
|
1 | 775 |
^ nil |
159 | 776 |
|
777 |
"typical situation: some well-known class" |
|
778 |
" |
|
779 |
|m| |
|
780 |
m := Object compiledMethodAt:#copy. |
|
781 |
m who |
|
782 |
" |
|
783 |
||
784 |
"untypical situation: an anonymous class" |
|
785 |
" |
|
786 |
|m cls| |
|
787 |
||
788 |
Object |
|
789 |
subclass:#FunnyClass |
|
790 |
instanceVariableNames:'foo' |
|
791 |
classVariableNames:'' |
|
792 |
poolDictionaries:'' |
|
793 |
category:'testing'. |
|
794 |
cls := Smalltalk at:#FunnyClass. |
|
795 |
Smalltalk removeClass:cls. |
|
796 |
||
797 |
cls compile:'testMethod1:arg foo:=arg'. |
|
798 |
cls compile:'testMethod2 ^ foo'. |
|
799 |
m := cls compiledMethodAt:#testMethod1:. |
|
800 |
||
801 |
m who |
|
802 |
" |
|
803 |
! |
|
804 |
||
805 |
containingClass |
|
806 |
"return the class I am defined in. |
|
807 |
See comment in who." |
|
808 |
||
809 |
"based on who, which has been added for ST-80 compatibility" |
|
810 |
||
811 |
|pair| |
|
812 |
||
813 |
pair := self who. |
|
814 |
pair notNil ifTrue:[^ pair at:1]. |
|
815 |
" |
|
816 |
none found - sorry |
|
817 |
" |
|
818 |
^ nil |
|
176 | 819 |
|
820 |
" |
|
821 |
|m| |
|
822 |
m := Object compiledMethodAt:#at:. |
|
823 |
m containingClass |
|
824 |
" |
|
825 |
! |
|
826 |
||
827 |
selector |
|
828 |
"return the selector under which I am found in my containingClasses |
|
829 |
method-table. |
|
830 |
See comment in who." |
|
831 |
||
832 |
"based on who, which has been added for ST-80 compatibility" |
|
833 |
||
834 |
|pair| |
|
835 |
||
836 |
pair := self who. |
|
837 |
pair notNil ifTrue:[^ pair at:2]. |
|
838 |
" |
|
839 |
none found - sorry |
|
840 |
" |
|
841 |
^ nil |
|
842 |
||
843 |
" |
|
844 |
|m| |
|
845 |
m := Object compiledMethodAt:#at:. |
|
846 |
m selector |
|
847 |
" |
|
1 | 848 |
! |
849 |
||
227 | 850 |
parse:parseSelector return:accessSelector or:valueIfNoSource |
851 |
"helper for methodArgNames, methodVarNames etc. |
|
852 |
Get the source, let parser parse it using parseSelector, |
|
853 |
return parser info using accessSelector" |
|
1 | 854 |
|
855 |
|parser sourceString| |
|
856 |
||
857 |
sourceString := self source. |
|
858 |
sourceString notNil ifTrue:[ |
|
227 | 859 |
parser := Parser perform:parseSelector with:sourceString. |
159 | 860 |
(parser isNil or:[parser == #Error]) ifTrue:[^ nil]. |
227 | 861 |
^ parser perform:accessSelector |
1 | 862 |
]. |
227 | 863 |
^ valueIfNoSource |
864 |
! |
|
865 |
||
866 |
methodArgNames |
|
867 |
"return a collection with the methods argument names. |
|
868 |
Uses Parser to parse methods source and extract the names." |
|
869 |
||
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
870 |
^ self parse:#'parseMethodSpecificationSilent:' return:#methodArgs or:nil |
1 | 871 |
|
109 | 872 |
" |
873 |
(Method compiledMethodAt:#printOn:) methodArgNames |
|
874 |
" |
|
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
875 |
|
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
876 |
"Modified: 31.10.1995 / 14:36:46 / cg" |
1 | 877 |
! |
878 |
||
879 |
methodVarNames |
|
880 |
"return a collection with the methods local-variable names. |
|
44 | 881 |
Uses Parser to parse methods source and extract the names." |
1 | 882 |
|
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
883 |
^ self parse:#'parseMethodArgAndVarSpecificationSilent:' return:#methodVars or:nil |
1 | 884 |
|
109 | 885 |
" |
886 |
(Method compiledMethodAt:#printOn:) methodVarNames |
|
887 |
" |
|
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
888 |
|
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
889 |
"Modified: 31.10.1995 / 14:36:49 / cg" |
1 | 890 |
! |
891 |
||
227 | 892 |
hasPrimitiveCode |
893 |
"return true, if the method contains primitive code; false if not. |
|
894 |
Uses Parser to parse methods source and get the information." |
|
895 |
||
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
896 |
|src| |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
897 |
|
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
898 |
src := self source. |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
899 |
src notNil ifTrue:[ |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
900 |
(src includesString:'%{' ) ifFalse:[ |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
901 |
"/ cannot contain primitive code. |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
902 |
^ false |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
903 |
] |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
904 |
]. |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
905 |
^ self parse:#'parseMethodSilent:' return:#hasPrimitiveCode or:false |
227 | 906 |
|
907 |
" |
|
908 |
(Method compiledMethodAt:#hasPrimitiveCode) hasPrimitiveCode |
|
909 |
(Object compiledMethodAt:#at:) hasPrimitiveCode |
|
910 |
(Object compiledMethodAt:#basicAt:) hasPrimitiveCode |
|
911 |
" |
|
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
912 |
|
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
913 |
"Modified: 31.10.1995 / 14:43:37 / cg" |
227 | 914 |
! |
915 |
||
1 | 916 |
methodArgAndVarNames |
917 |
"return a collection with the methods argument and variable names. |
|
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
918 |
Uses Parser to parse methods source and extract the names. |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
919 |
Returns nil if the source is not available, or some other |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
920 |
syntax/parse error occurred. For methods with no args and no vars, |
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
921 |
an empty collection is returned." |
1 | 922 |
|
923 |
|parser sourceString argNames varNames| |
|
924 |
||
925 |
sourceString := self source. |
|
926 |
sourceString notNil ifTrue:[ |
|
463
447ead9f870c
be silent on transcript when parsing for args, vars and primitiveCode
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
927 |
parser := Parser parseMethodArgAndVarSpecificationSilent:sourceString. |
159 | 928 |
(parser isNil or:[parser == #Error]) ifTrue:[^ nil]. |
929 |
argNames := parser methodArgs. |
|
930 |
varNames := parser methodVars. |
|
931 |
argNames isNil ifTrue:[^ varNames]. |
|
932 |
varNames isNil ifTrue:[^ argNames]. |
|
933 |
^ (argNames , varNames) |
|
1 | 934 |
]. |
935 |
^ nil |
|
936 |
||
109 | 937 |
" |
938 |
(Method compiledMethodAt:#printOn:) methodArgAndVarNames |
|
939 |
" |
|
1 | 940 |
! |
941 |
||
942 |
methodComment |
|
44 | 943 |
"return the methods first comment, nil if there is none. |
944 |
This is a somewhat stupid implementation." |
|
1 | 945 |
|
946 |
|text line nQuote index qIndex qIndex2 comment| |
|
947 |
||
142 | 948 |
text := self source asCollectionOfLines. |
1 | 949 |
(text size < 2) ifTrue:[^nil]. |
950 |
||
951 |
line := (text at:2). |
|
952 |
nQuote := line occurrencesOf:(Character doubleQuote). |
|
953 |
(nQuote == 2) ifTrue:[ |
|
159 | 954 |
qIndex := line indexOf:(Character doubleQuote). |
955 |
qIndex2 := line indexOf:(Character doubleQuote) startingAt:(qIndex + 1). |
|
956 |
^ line copyFrom:(qIndex + 1) to:(qIndex2 - 1) |
|
1 | 957 |
]. |
958 |
(nQuote == 1) ifTrue:[ |
|
159 | 959 |
qIndex := line indexOf:(Character doubleQuote). |
960 |
comment := line copyFrom:(qIndex + 1). |
|
1 | 961 |
|
159 | 962 |
index := 3. |
963 |
line := text at:index. |
|
964 |
nQuote := line occurrencesOf:(Character doubleQuote). |
|
965 |
[nQuote ~~ 1] whileTrue:[ |
|
966 |
comment := comment , Character cr asString , line withoutSpaces. |
|
967 |
index := index + 1. |
|
968 |
line := text at:index. |
|
969 |
nQuote := line occurrencesOf:(Character doubleQuote) |
|
970 |
]. |
|
971 |
qIndex := line indexOf:(Character doubleQuote). |
|
972 |
^ comment , Character cr asString , (line copyTo:(qIndex - 1)) withoutSpaces |
|
1 | 973 |
]. |
974 |
^ nil |
|
975 |
||
109 | 976 |
" |
977 |
(Method compiledMethodAt:#methodComment) methodComment |
|
978 |
" |
|
1 | 979 |
! |
980 |
||
981 |
referencesGlobal:aGlobalSymbol |
|
982 |
"return true, if this method references the global |
|
983 |
bound to aGlobalSymbol." |
|
984 |
||
985 |
literals isNil ifTrue:[^ false]. |
|
986 |
^ (literals identityIndexOf:aGlobalSymbol startingAt:1) ~~ 0 |
|
987 |
! |
|
988 |
||
989 |
sends:aSelectorSymbol |
|
990 |
"return true, if this method contains a message-send |
|
991 |
with aSelectorSymbol as selector. |
|
992 |
- due to the simple check in the literal array, also simple uses |
|
44 | 993 |
of aSelectorSymbol as symbol will return true. |
994 |
Should ask compiler, if there is really a send." |
|
1 | 995 |
|
996 |
^ self referencesGlobal:aSelectorSymbol |
|
77 | 997 |
! |
998 |
||
93 | 999 |
isLazyMethod |
1000 |
"return true, if this is a lazy method. |
|
1001 |
False is returned here - this method is redefined in LazyMethod" |
|
1002 |
||
1003 |
^ false |
|
1004 |
! |
|
1005 |
||
77 | 1006 |
isWrapped |
93 | 1007 |
"return true, if this is a wrapper method. |
1008 |
False is returned here - this method is redefined in WrappedMethod" |
|
1009 |
||
77 | 1010 |
^ false |
93 | 1011 |
! |
1012 |
||
227 | 1013 |
wrapper |
1014 |
"only for wrapped methods: return the wrapper. |
|
1015 |
Thats the WrapperMethod which contains myself." |
|
1016 |
||
1017 |
WrappedMethod allInstancesDo:[:m | |
|
1018 |
m originalMethod == self ifTrue:[^ m]. |
|
1019 |
]. |
|
1020 |
^ nil |
|
1021 |
! |
|
1022 |
||
93 | 1023 |
isInvalid |
1024 |
"return true, if this method is not executable due to |
|
328 | 1025 |
a (re)-compilation error. (see comment in Method>>invalidCodeObject)" |
93 | 1026 |
|
1027 |
|m| |
|
1028 |
||
328 | 1029 |
m := Method compiledMethodAt:#invalidCodeObject. |
391 | 1030 |
self ~~ m ifTrue:[ |
1031 |
(self code notNil and:[self code = m code]) ifTrue:[^ true]. |
|
1032 |
(byteCode notNil and:[byteCode == m byteCode]) ifTrue:[^ true]. |
|
1033 |
]. |
|
362 | 1034 |
|
375 | 1035 |
m := Method compiledMethodAt:#uncompiledCodeObject. |
391 | 1036 |
self ~~ m ifTrue:[ |
1037 |
(self code notNil and:[self code = m code]) ifTrue:[^ true]. |
|
1038 |
(byteCode notNil and:[byteCode == m byteCode]) ifTrue:[^ true]. |
|
1039 |
]. |
|
362 | 1040 |
m := Metaclass compiledMethodAt:#invalidCodeObject. |
391 | 1041 |
self ~~ m ifTrue:[ |
1042 |
(self code notNil and:[self code = m code]) ifTrue:[^ true]. |
|
1043 |
(byteCode notNil and:[byteCode == m byteCode]) ifTrue:[^ true]. |
|
1044 |
]. |
|
93 | 1045 |
^ false |
1 | 1046 |
! ! |
1047 |
||
438 | 1048 |
!Method methodsFor:'trap methods'! |
1049 |
||
1050 |
makeInvalid |
|
1051 |
"make the receiver an invalid method, which raises an invalidCodeObject |
|
1052 |
signal when executed. This is not for public use - it is required for |
|
1053 |
the objectFileLoader to invalidate methods whose code is unloaded." |
|
1054 |
||
1055 |
|invldMethod| |
|
1056 |
||
1057 |
invldMethod := self class compiledMethodAt:#invalidCodeObject. |
|
1058 |
self code:invldMethod code. |
|
1059 |
self byteCode:invldMethod byteCode. |
|
1060 |
||
1061 |
"Created: 17.9.1995 / 15:00:52 / claus" |
|
1062 |
! |
|
1063 |
||
1064 |
makeUncompiled |
|
1065 |
"make the receiver an uncompiled method, which raises an invalidCodeObject |
|
1066 |
signal when executed. This is not for public use - it is required for |
|
1067 |
the compiler to invalidate methods which cannot be compiled due to errors |
|
1068 |
after a class definition change (for example: instvars are no longer there)." |
|
1069 |
||
1070 |
|invldMethod| |
|
1071 |
||
1072 |
invldMethod := self class compiledMethodAt:#uncompiledCodeObject. |
|
1073 |
self code:invldMethod code. |
|
1074 |
self byteCode:invldMethod byteCode. |
|
1075 |
||
1076 |
"Created: 17.9.1995 / 15:01:14 / claus" |
|
1077 |
! ! |
|
1078 |
||
1 | 1079 |
!Method methodsFor:'error handling'! |
1080 |
||
328 | 1081 |
invalidCodeObject |
93 | 1082 |
"this method is triggered by the interpreter when a nil or non method |
1083 |
is about to be executed. |
|
1084 |
In this case, the VM sends this to the bad method (the receiver). |
|
1085 |
||
1086 |
Also, the Compiler creates methods with their code/bytecode set to |
|
1087 |
this method if - after a class change - a method cannot be compiled |
|
1088 |
and is therefore no longer executable (for example, after an instvar |
|
68 | 1089 |
has been removed, and a method still tries to access this instvar) |
44 | 1090 |
|
93 | 1091 |
Thus, we arrive here, when playing around in a classes methodArray, |
1092 |
or compiler/runtime system is broken :-(, |
|
1093 |
or you ignore the error messages during some recompile." |
|
1 | 1094 |
|
142 | 1095 |
^ InvalidCodeSignal |
159 | 1096 |
raiseRequestWith:self |
1097 |
errorString:'invalid method - not executable'. |
|
1 | 1098 |
! |
1099 |
||
375 | 1100 |
uncompiledCodeObject |
1101 |
"this method is invoked by methods which contain primitive code, |
|
1102 |
but have not been compiled to machine code (either due to an error |
|
1103 |
when compiling, or simply because no stc is available. |
|
1104 |
For those methods, the compiler generated a method object consisting |
|
1105 |
of the original source code, but with this methods machine/byte code. |
|
1106 |
Therefore, we patch (kludge) the lineNumber information, to show the |
|
1107 |
first line (instead of the real line below)" |
|
1108 |
||
1109 |
thisContext setLineNumber:1. |
|
1110 |
^ InvalidCodeSignal |
|
1111 |
raiseRequestWith:self |
|
1112 |
errorString:'invalid method - not compiled'. |
|
1113 |
! |
|
1114 |
||
109 | 1115 |
wrongNumberOfArguments:numberGiven |
1116 |
"this error is triggered, if a method is called with a wrong number |
|
1117 |
of arguments. This only applies to #valueWithReceiverXXX - sends. |
|
1118 |
With a normal send, this error cannot happen." |
|
68 | 1119 |
|
142 | 1120 |
^ ArgumentSignal |
159 | 1121 |
raiseRequestWith:self |
1122 |
errorString:('method got ' , numberGiven printString , |
|
1123 |
' args while ' , self numberOfMethodArgs printString , ' where expected') |
|
68 | 1124 |
! |
1125 |
||
109 | 1126 |
privateMethodCalled |
176 | 1127 |
"this error is triggered, if a private or protected method is called from |
360 | 1128 |
outside. |
1129 |
If you continue in the debugger, the method will be called, |
|
415 | 1130 |
and further privacy exceptions will NOT be reported at this call location, |
360 | 1131 |
until any new method is compiled, or the privacy of any method changes, |
1132 |
or the caches are flushed. |
|
1133 |
(the reason is that after the continue, the method is enterred into the |
|
1134 |
calling cache, for which method privacy is not checked. |
|
1135 |
Any of the above actions flushes this cache and a privacy check |
|
1136 |
is performed again.) |
|
1137 |
Future versions may not enter private methods into the cache, to fix this |
|
1138 |
(unobvious) behavior. However, then you will get an exception for EVERY |
|
1139 |
call to a private method ... |
|
1140 |
||
1141 |
Notice: method privacy is a nonstandard feature, not supported |
|
1142 |
by other smalltalk implementations and not specified in the ANSI spec. |
|
1143 |
This is EXPERIMENTAL - and being evaluated for usability. |
|
1144 |
It may change or even vanish (if it shows to be not useful)." |
|
68 | 1145 |
|
142 | 1146 |
^ PrivateMethodSignal raise |
1 | 1147 |
! ! |
1148 |
||
1149 |
!Method methodsFor:'executing'! |
|
1150 |
||
1151 |
valueWithReceiver:anObject arguments:argArray |
|
2 | 1152 |
"low level call of a methods code - BIG DANGER ALERT. |
1153 |
Perform the receiver-method on anObject as receiver and argArray as |
|
1154 |
arguments. This does NO message lookup at all and mimics a |
|
1155 |
traditional function call. |
|
44 | 1156 |
This method is provided for debugging- and breakpoint-support |
68 | 1157 |
(replacing a method by a stub and recalling the original), or to implement |
1158 |
experimental MI implementations - it is not for general use. |
|
1159 |
||
1160 |
The receiver must be a method compiled in anObjects class or one of its |
|
1161 |
superclasses and also, the number of arguments given must match the methods |
|
1162 |
expectations - |
|
1163 |
- otherwise strange things (and also strange crashes) can occur. |
|
1164 |
The system is NOT always detecting a wrong method/receiver combination. |
|
77 | 1165 |
YOU HAVE BEEN WARNED." |
68 | 1166 |
|
77 | 1167 |
^ self valueWithReceiver:anObject arguments:argArray selector:nil search:nil |
68 | 1168 |
! |
1169 |
||
1170 |
valueWithReceiver:anObject arguments:argArray selector:aSymbol |
|
1171 |
"low level call of a methods code - BIG DANGER ALERT. |
|
1172 |
Perform the receiver-method on anObject as receiver and argArray as |
|
1173 |
arguments. This does NO message lookup at all and mimics a |
|
1174 |
traditional function call. |
|
1175 |
This method is provided for debugging- and breakpoint-support |
|
1176 |
(replacing a method by a stub and recalling the original), or to implement |
|
1177 |
experimental MI implementations - it is not for general use. |
|
1178 |
||
44 | 1179 |
The receiver must be a method compiled in anObjects class or one of its |
56 | 1180 |
superclasses and also, the number of arguments given must match the methods |
1181 |
expectations - |
|
44 | 1182 |
- otherwise strange things (and also strange crashes) can occur. |
1183 |
The system is NOT always detecting a wrong method/receiver combination. |
|
77 | 1184 |
YOU HAVE BEEN WARNED." |
1185 |
||
227 | 1186 |
^ self valueWithReceiver:anObject |
1187 |
arguments:argArray |
|
1188 |
selector:aSymbol |
|
1189 |
search:nil |
|
1190 |
sender:nil |
|
77 | 1191 |
! |
1192 |
||
1193 |
valueWithReceiver:anObject arguments:argArray selector:aSymbol search:aClass |
|
1194 |
"low level call of a methods code - BIG DANGER ALERT. |
|
1195 |
Perform the receiver-method on anObject as receiver and argArray as |
|
1196 |
arguments. This does NO message lookup at all and mimics a |
|
1197 |
traditional function call. |
|
1198 |
This method is provided for debugging- and breakpoint-support |
|
1199 |
(replacing a method by a stub and recalling the original), or to implement |
|
1200 |
experimental MI implementations - it is not for general use. |
|
1201 |
||
1202 |
The receiver must be a method compiled in anObjects class or one of its |
|
1203 |
superclasses and also, the number of arguments given must match the methods |
|
1204 |
expectations - |
|
1205 |
- otherwise strange things (and also strange crashes) can occur. |
|
1206 |
The system is NOT always detecting a wrong method/receiver combination. |
|
1207 |
YOU HAVE BEEN WARNED." |
|
1 | 1208 |
|
227 | 1209 |
^ self valueWithReceiver:anObject |
1210 |
arguments:argArray |
|
1211 |
selector:aSymbol |
|
1212 |
search:nil |
|
1213 |
sender:nil |
|
1214 |
! |
|
1215 |
||
1216 |
valueWithReceiver:anObject arguments:argArray selector:aSymbol search:aClass sender:virtualSender |
|
1217 |
"low level call of a methods code - BIG DANGER ALERT. |
|
1218 |
Perform the receiver-method on anObject as receiver and argArray as |
|
1219 |
arguments. This does NO message lookup at all and mimics a |
|
1220 |
traditional function call. |
|
1221 |
This method is provided for debugging- and breakpoint-support |
|
1222 |
(replacing a method by a stub and recalling the original), or to implement |
|
1223 |
experimental MI implementations - it is not for general use. |
|
1224 |
||
1225 |
The receiver must be a method compiled in anObjects class or one of its |
|
1226 |
superclasses and also, the number of arguments given must match the methods |
|
1227 |
expectations - |
|
1228 |
- otherwise strange things (and also strange crashes) can occur. |
|
1229 |
The system is NOT always detecting a wrong method/receiver combination. |
|
1230 |
YOU HAVE BEEN WARNED." |
|
1231 |
||
1 | 1232 |
%{ |
1233 |
OBJFUNC code; |
|
1234 |
OBJ searchClass; |
|
1235 |
static struct inlineCache dummy = _DUMMYILC0; |
|
2 | 1236 |
int nargs; |
1237 |
OBJ *ap; |
|
328 | 1238 |
extern OBJ __interpret(), interpret(); |
2 | 1239 |
|
56 | 1240 |
if (__isArray(argArray)) { |
159 | 1241 |
nargs = _arraySize(argArray); |
1242 |
ap = _ArrayInstPtr(argArray)->a_element; |
|
2 | 1243 |
} else { |
159 | 1244 |
if (argArray == nil) { |
1245 |
nargs = 0; |
|
1246 |
} else |
|
1247 |
nargs = -1; |
|
2 | 1248 |
} |
1 | 1249 |
|
109 | 1250 |
#ifdef F_NARGS |
1251 |
if (((_intVal(_INST(flags)) & F_NARGS) >> F_NARGSHIFT) == nargs) |
|
1252 |
#endif |
|
1253 |
{ |
|
159 | 1254 |
code = _MethodInstPtr(self)->m_code; |
1255 |
if (aClass == nil) { |
|
328 | 1256 |
searchClass = dummy.ilc_class = __Class(anObject); |
159 | 1257 |
} else { |
1258 |
searchClass = dummy.ilc_class = aClass; |
|
1259 |
} |
|
77 | 1260 |
|
227 | 1261 |
if (nargs <= 15) { |
1262 |
/* |
|
1263 |
* add virtual sender (unwinding) here later, |
|
1264 |
* to allow hiding contexts in lazy methods. |
|
328 | 1265 |
* (this is cosmetics only; therefore its done later) |
227 | 1266 |
*/ |
1267 |
if (code) { |
|
159 | 1268 |
/* compiled code */ |
1269 |
switch (nargs) { |
|
1270 |
case 0: |
|
1271 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy) ); |
|
68 | 1272 |
|
159 | 1273 |
case 1: |
1274 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, ap[0]) ); |
|
1 | 1275 |
|
159 | 1276 |
case 2: |
1277 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, ap[0], ap[1]) ); |
|
68 | 1278 |
|
159 | 1279 |
case 3: |
1280 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, ap[0], ap[1], ap[2]) ); |
|
1 | 1281 |
|
159 | 1282 |
case 4: |
1283 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1284 |
ap[0], ap[1], ap[2], ap[3]) ); |
|
68 | 1285 |
|
159 | 1286 |
case 5: |
1287 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1288 |
ap[0], ap[1], ap[2], ap[3], ap[4]) ); |
|
1 | 1289 |
|
159 | 1290 |
case 6: |
1291 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1292 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]) ); |
|
1 | 1293 |
|
159 | 1294 |
case 7: |
1295 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1296 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6]) ); |
|
68 | 1297 |
|
159 | 1298 |
case 8: |
1299 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1300 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7]) ); |
|
1 | 1301 |
|
159 | 1302 |
case 9: |
1303 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1304 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8]) ); |
|
1 | 1305 |
|
159 | 1306 |
case 10: |
1307 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1308 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8], |
|
1309 |
ap[9]) ); |
|
68 | 1310 |
|
159 | 1311 |
case 11: |
1312 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1313 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8], |
|
1314 |
ap[9], ap[10]) ); |
|
1 | 1315 |
|
159 | 1316 |
case 12: |
1317 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1318 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8], |
|
1319 |
ap[9], ap[10], ap[11]) ); |
|
77 | 1320 |
|
159 | 1321 |
case 13: |
1322 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1323 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8], |
|
1324 |
ap[9], ap[10], ap[11], ap[12]) ); |
|
77 | 1325 |
|
159 | 1326 |
case 14: |
1327 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1328 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8], |
|
1329 |
ap[9], ap[10], ap[11], ap[12], ap[13]) ); |
|
77 | 1330 |
|
159 | 1331 |
case 15: |
1332 |
RETURN ( (*code)(anObject, aSymbol, SND_COMMA searchClass, &dummy, |
|
1333 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], ap[7], ap[8], |
|
1334 |
ap[9], ap[10], ap[11], ap[12], ap[13], ap[14]) ); |
|
1335 |
} |
|
227 | 1336 |
} else { |
159 | 1337 |
/* interpreted code */ |
328 | 1338 |
#ifdef PASS_ARG_POINTER |
1339 |
RETURN ( __interpret(self, nargs, anObject, aSymbol, SND_COMMA searchClass, ap) ); |
|
1340 |
#else |
|
159 | 1341 |
switch (nargs) { |
1342 |
case 0: |
|
1343 |
RETURN ( interpret(self, 0, anObject, aSymbol, SND_COMMA searchClass) ); |
|
68 | 1344 |
|
159 | 1345 |
case 1: |
1346 |
RETURN ( interpret(self, 1, anObject, aSymbol, SND_COMMA searchClass, |
|
1347 |
ap[0]) ); |
|
1 | 1348 |
|
159 | 1349 |
case 2: |
1350 |
RETURN ( interpret(self, 2, anObject, aSymbol, SND_COMMA searchClass, |
|
1351 |
ap[0], ap[1]) ); |
|
68 | 1352 |
|
159 | 1353 |
case 3: |
1354 |
RETURN ( interpret(self, 3, anObject, aSymbol, SND_COMMA searchClass, |
|
1355 |
ap[0], ap[1], ap[2]) ); |
|
1 | 1356 |
|
159 | 1357 |
case 4: |
1358 |
RETURN ( interpret(self, 4, anObject, aSymbol, SND_COMMA searchClass, |
|
1359 |
ap[0], ap[1], ap[2], ap[3]) ); |
|
68 | 1360 |
|
159 | 1361 |
case 5: |
1362 |
RETURN ( interpret(self, 5, anObject, aSymbol, SND_COMMA searchClass, |
|
1363 |
ap[0], ap[1], ap[2], ap[3], ap[4]) ); |
|
1 | 1364 |
|
159 | 1365 |
case 6: |
1366 |
RETURN ( interpret(self, 6, anObject, aSymbol, SND_COMMA searchClass, |
|
1367 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5]) ); |
|
68 | 1368 |
|
159 | 1369 |
case 7: |
1370 |
RETURN ( interpret(self, 7, anObject, aSymbol, SND_COMMA searchClass, |
|
1371 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6]) ); |
|
68 | 1372 |
|
159 | 1373 |
case 8: |
1374 |
RETURN ( interpret(self, 8, anObject, aSymbol, SND_COMMA searchClass, |
|
1375 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1376 |
ap[7]) ); |
|
1 | 1377 |
|
159 | 1378 |
case 9: |
1379 |
RETURN ( interpret(self, 9, anObject, aSymbol, SND_COMMA searchClass, |
|
1380 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1381 |
ap[7], ap[8]) ); |
|
68 | 1382 |
|
159 | 1383 |
case 10: |
1384 |
RETURN ( interpret(self, 10, anObject, aSymbol, SND_COMMA searchClass, |
|
1385 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1386 |
ap[7], ap[8], ap[9]) ); |
|
1 | 1387 |
|
159 | 1388 |
case 11: |
1389 |
RETURN ( interpret(self, 11, anObject, aSymbol, SND_COMMA searchClass, |
|
1390 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1391 |
ap[7], ap[8], ap[9], ap[10]) ); |
|
68 | 1392 |
|
159 | 1393 |
case 12: |
1394 |
RETURN ( interpret(self, 12, anObject, aSymbol, SND_COMMA searchClass, |
|
1395 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1396 |
ap[7], ap[8], ap[9], ap[11]) ); |
|
77 | 1397 |
|
159 | 1398 |
case 13: |
1399 |
RETURN ( interpret(self, 13, anObject, aSymbol, SND_COMMA searchClass, |
|
1400 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1401 |
ap[7], ap[8], ap[9], ap[11], ap[12]) ); |
|
77 | 1402 |
|
159 | 1403 |
case 14: |
1404 |
RETURN ( interpret(self, 14, anObject, aSymbol, SND_COMMA searchClass, |
|
1405 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1406 |
ap[7], ap[8], ap[9], ap[11], ap[12], ap[13]) ); |
|
77 | 1407 |
|
159 | 1408 |
case 15: |
1409 |
RETURN ( interpret(self, 15, anObject, aSymbol, SND_COMMA searchClass, |
|
1410 |
ap[0], ap[1], ap[2], ap[3], ap[4], ap[5], ap[6], |
|
1411 |
ap[7], ap[8], ap[9], ap[11], ap[12], ap[13], ap[14]) ); |
|
1412 |
} |
|
328 | 1413 |
#endif |
227 | 1414 |
} |
159 | 1415 |
} |
1 | 1416 |
} |
1417 |
%} |
|
2 | 1418 |
. |
1419 |
(argArray isMemberOf:Array) ifFalse:[ |
|
159 | 1420 |
" |
1421 |
arguments must be either nil or an array |
|
1422 |
" |
|
1423 |
^ self badArgumentArray |
|
2 | 1424 |
]. |
109 | 1425 |
(argArray size ~~ self numberOfMethodArgs) ifTrue:[ |
159 | 1426 |
" |
1427 |
the method expects a different number of arguments |
|
1428 |
" |
|
1429 |
^ self wrongNumberOfArguments:argArray size |
|
109 | 1430 |
]. |
1431 |
" |
|
1432 |
the VM only supports a limited number of arguments in sends |
|
1433 |
" |
|
1434 |
^ self tooManyArguments |
|
1435 |
||
1436 |
" |
|
1437 |
(Float compiledMethodAt:#+) |
|
159 | 1438 |
valueWithReceiver:1.0 arguments:#(2.0) |
2 | 1439 |
|
109 | 1440 |
'the next example is a wrong one - which is detected by True's method ...'. |
1441 |
(True compiledMethodAt:#printString) |
|
159 | 1442 |
valueWithReceiver:false arguments:nil |
109 | 1443 |
|
1444 |
'the next example is a wrong one - it is nowhere detected |
|
1445 |
and a wrong value returned ...'. |
|
1446 |
(Point compiledMethodAt:#x) |
|
159 | 1447 |
valueWithReceiver:(1->2) arguments:nil |
109 | 1448 |
|
1449 |
'the next example is VERY bad one - it is nowhere detected |
|
1450 |
and may crash the system WARNING: save your work before doing this ...'. |
|
1451 |
(Point compiledMethodAt:#x) |
|
159 | 1452 |
valueWithReceiver:(Object new) arguments:nil |
109 | 1453 |
|
1454 |
'the next example is a wrong one - which is detected here ...'. |
|
1455 |
(Object compiledMethodAt:#printOn:) |
|
159 | 1456 |
valueWithReceiver:false arguments:nil |
109 | 1457 |
|
1458 |
'the next example is a wrong one - which is detected here ...'. |
|
1459 |
(Object compiledMethodAt:#printOn:) |
|
159 | 1460 |
valueWithReceiver:false arguments:#() |
109 | 1461 |
" |
1 | 1462 |
! ! |
1463 |
||
93 | 1464 |
!Method methodsFor:'printing & storing'! |
1 | 1465 |
|
275 | 1466 |
whoString |
1467 |
"return a string as className>>selector, if this is not an unbound |
|
1468 |
method. Otherwise return 'unbound'. Used with debugging." |
|
1469 |
||
1470 |
|w| |
|
1471 |
||
1472 |
w := self who. |
|
1473 |
w notNil ifTrue:[ |
|
1474 |
^ (w at:1) name , '>>' , (w at:2) |
|
1475 |
]. |
|
1476 |
^ 'unbound' |
|
1477 |
||
1478 |
" |
|
1479 |
Method new whoString |
|
1480 |
(Method compiledMethodAt:#whoString) whoString |
|
1481 |
" |
|
1482 |
! |
|
1483 |
||
1 | 1484 |
printOn:aStream |
56 | 1485 |
"put a printed representation of the receiver onto aStream. |
1486 |
Since methods do not store their class/selector, we have to search |
|
1487 |
for it here." |
|
1 | 1488 |
|
227 | 1489 |
|classAndSelector m wrapped| |
1 | 1490 |
|
253 | 1491 |
wrapped := false. |
1492 |
||
159 | 1493 |
aStream nextPutAll:(self classNameWithArticle). |
1494 |
aStream nextPut:$(. |
|
227 | 1495 |
|
176 | 1496 |
classAndSelector := self who. |
227 | 1497 |
classAndSelector isNil ifTrue:[ |
1498 |
" |
|
1499 |
not anchored in any class. |
|
1500 |
check if wrapped (to be more informative in inspectors) |
|
1501 |
" |
|
1502 |
m := self wrapper. |
|
1503 |
m notNil ifTrue:[ |
|
1504 |
classAndSelector := m who. |
|
1505 |
wrapped := true. |
|
1506 |
] |
|
1507 |
]. |
|
176 | 1508 |
classAndSelector notNil ifTrue:[ |
1509 |
(classAndSelector at:1) name printOn:aStream. |
|
159 | 1510 |
aStream nextPutAll:' '. |
227 | 1511 |
(classAndSelector at:2) printOn:aStream. |
1512 |
wrapped ifTrue:[ |
|
1513 |
aStream nextPutAll:'; wrapped' |
|
1514 |
]. |
|
1 | 1515 |
] ifFalse:[ |
202 | 1516 |
" |
227 | 1517 |
sorry, a method which is nowhere anchored |
202 | 1518 |
" |
253 | 1519 |
aStream nextPutAll:'unbound' |
56 | 1520 |
]. |
1521 |
aStream nextPut:$) |
|
176 | 1522 |
|
1523 |
" |
|
253 | 1524 |
(Object compiledMethodAt:#at:) printOn:Transcript. Transcript cr. |
1525 |
(Object compiledMethodAt:#at:) copy printOn:Transcript. Transcript cr. |
|
176 | 1526 |
" |
1 | 1527 |
! ! |
1528 |
||
159 | 1529 |
!Method class methodsFor:'binary storage'! |
1530 |
||
1531 |
binaryDefinitionFrom: stream manager: manager |
|
1532 |
"read my definition from stream." |
|
1533 |
||
1534 |
|cls sel| |
|
1535 |
||
1536 |
"type-byte" |
|
1537 |
stream nextByte == 0 ifTrue:[ |
|
1538 |
" |
|
1539 |
built-in method |
|
1540 |
" |
|
1541 |
cls := manager nextObject. |
|
1542 |
sel := manager nextObject. |
|
1543 |
||
1544 |
" |
|
1545 |
mhmh - on the source system, this was a machinecode |
|
1546 |
method, while here its an interpreted one ... |
|
1547 |
" |
|
1548 |
cls isLoaded ifFalse:[ |
|
1549 |
cls autoload |
|
1550 |
]. |
|
1551 |
^ cls compiledMethodAt:sel |
|
1552 |
]. |
|
1553 |
" |
|
1554 |
bytecode method |
|
1555 |
" |
|
1556 |
^ super binaryDefinitionFrom:stream manager:manager |
|
1557 |
! ! |
|
1558 |
||
13 | 1559 |
!Method methodsFor:'binary storage'! |
1560 |
||
444 | 1561 |
asExecutableMethod |
1562 |
"if the receiver has neither bytecodes nor machinecode, create & return a |
|
1563 |
method having semantics as the receivers source. This may be machine code, |
|
1564 |
if the system supports dynamic loading of object code and the source includes |
|
1565 |
primitive code. However, bytecode is preferred, since it compiles faster. |
|
68 | 1566 |
Otherwise, return the receiver. The new method is not installed in |
1567 |
the methodDictionary of any class - just returned. |
|
444 | 1568 |
Can be used to compile lazy methods down to executable ones." |
68 | 1569 |
|
444 | 1570 |
|temporaryMethod cls sourceString silent lazy| |
68 | 1571 |
|
142 | 1572 |
byteCode notNil ifTrue:[ |
159 | 1573 |
" |
1574 |
is already a bytecoded method |
|
1575 |
" |
|
1576 |
^ self |
|
142 | 1577 |
]. |
68 | 1578 |
|
1579 |
cls := self containingClass. |
|
159 | 1580 |
cls isNil ifTrue:[ |
1581 |
'cannot generate bytecode (no class for compilation)' errorPrintNL. |
|
1582 |
^ nil |
|
1583 |
]. |
|
68 | 1584 |
sourceString := self source. |
142 | 1585 |
sourceString isNil ifTrue:[ |
159 | 1586 |
'cannot generate bytecode (no source for compilation)' errorPrintNL. |
142 | 1587 |
^ nil |
68 | 1588 |
]. |
227 | 1589 |
|
142 | 1590 |
" |
159 | 1591 |
dont want this to go into the changes file, |
1592 |
dont want output on Transcript and definitely |
|
1593 |
dont want a lazy method ... |
|
142 | 1594 |
" |
421 | 1595 |
Class withoutUpdatingChangesDo:[ |
423 | 1596 |
silent := Smalltalk silentLoading:true. |
1597 |
lazy := Compiler compileLazy:false. |
|
159 | 1598 |
|
423 | 1599 |
[ |
421 | 1600 |
|compiler| |
350 | 1601 |
|
421 | 1602 |
compiler := cls compilerClass. |
350 | 1603 |
|
421 | 1604 |
"/ |
1605 |
"/ kludge - have to make ST/X's compiler protocol |
|
1606 |
"/ be compatible to ST-80's |
|
1607 |
"/ |
|
1608 |
(compiler respondsTo:#compile:forClass:inCategory:notifying:install:skipIfSame:) |
|
1609 |
ifTrue:[ |
|
423 | 1610 |
temporaryMethod := compiler |
421 | 1611 |
compile:sourceString |
1612 |
forClass:cls |
|
1613 |
inCategory:(self category) |
|
1614 |
notifying:nil |
|
1615 |
install:false. |
|
1616 |
] ifFalse:[ |
|
423 | 1617 |
temporaryMethod := compiler new |
421 | 1618 |
compile:sourceString |
1619 |
in:cls |
|
1620 |
notifying:nil |
|
1621 |
ifFail:nil |
|
1622 |
]. |
|
423 | 1623 |
] valueNowOrOnUnwindDo:[ |
421 | 1624 |
Compiler compileLazy:lazy. |
1625 |
Smalltalk silentLoading:silent. |
|
1626 |
] |
|
142 | 1627 |
]. |
1628 |
(temporaryMethod isNil or:[temporaryMethod == #Error]) ifTrue:[ |
|
159 | 1629 |
'cannot generate bytecode (contains primitive code or error)' errorPrintNL. |
142 | 1630 |
^ nil. |
1631 |
]. |
|
1632 |
" |
|
1633 |
try to save a bit of memory, by sharing the source (whatever it is) |
|
1634 |
" |
|
192 | 1635 |
temporaryMethod sourceFilename:source position:sourcePosition. |
142 | 1636 |
^ temporaryMethod |
68 | 1637 |
|
444 | 1638 |
"Created: 24.10.1995 / 14:02:30 / cg" |
1639 |
! |
|
1640 |
||
1641 |
asByteCodeMethod |
|
1642 |
"if the receiver has no bytecodes, create & return a method having |
|
1643 |
the same semantics as the receiver, but uses interpreted bytecodes. |
|
1644 |
Otherwise, return the receiver. The new method is not installed in |
|
1645 |
the methodDictionary of any class - just returned. |
|
1646 |
Can be used to obtain a bytecode version of a machine-code method, |
|
1647 |
for binary storage or dynamic recompilation (which is not yet finished) |
|
1648 |
or to compile lazy methods down to executable ones." |
|
1649 |
||
1650 |
|doMachineCode mthd| |
|
1651 |
||
1652 |
byteCode notNil ifTrue:[ |
|
1653 |
" |
|
1654 |
is already a bytecoded method |
|
1655 |
" |
|
1656 |
^ self |
|
1657 |
]. |
|
1658 |
doMachineCode := Compiler stcCompilation:#never. |
|
1659 |
[ |
|
1660 |
mthd := self asExecutableMethod. |
|
1661 |
] valueNowOrOnUnwindDo:[ |
|
1662 |
Compiler stcCompilation:doMachineCode. |
|
1663 |
]. |
|
1664 |
^ mthd |
|
1665 |
||
1666 |
"Created: 24.10.1995 / 14:02:32 / cg" |
|
1667 |
"Modified: 24.10.1995 / 14:17:21 / cg" |
|
68 | 1668 |
! |
1669 |
||
159 | 1670 |
storeBinaryDefinitionOn:stream manager:manager |
1671 |
"only store bytecode-methods - machinecode methods are stored |
|
1672 |
as class/selector pair and a lookup is done when restored. |
|
1673 |
||
56 | 1674 |
If the receiver method is a built-in (i.e. machine coded) |
1675 |
method, a temporary interpreted byte code method is created, |
|
68 | 1676 |
and its bytecode stored. |
1677 |
This works only, if the source of the method is available and the |
|
1678 |
method does not contain primitive code." |
|
13 | 1679 |
|
159 | 1680 |
|storedMethod who| |
13 | 1681 |
|
1682 |
byteCode isNil ifTrue:[ |
|
159 | 1683 |
self code notNil ifTrue:[ |
1684 |
(who := self who) notNil ifTrue:[ |
|
1685 |
" |
|
1686 |
machine code only - assume its a built-in method, |
|
1687 |
and store the class/selector information. |
|
1688 |
The restored method may not be exactly the same ... |
|
1689 |
" |
|
343 | 1690 |
manager putIdOfClass:(self class) on:stream. |
1691 |
stream nextPutByte:0. "means: built-in method" |
|
159 | 1692 |
manager putIdOf:(who at:1) on:stream. |
1693 |
manager putIdOf:(who at:2) on:stream. |
|
1694 |
^ self |
|
1695 |
] |
|
1696 |
]. |
|
1 | 1697 |
|
159 | 1698 |
storedMethod := self asByteCodeMethod. |
1699 |
storedMethod isNil ifTrue:[ |
|
1700 |
self error:'store of built-in method failed'. |
|
1701 |
^ nil |
|
1702 |
]. |
|
1703 |
^ storedMethod storeBinaryDefinitionOn:stream manager:manager |
|
1704 |
]. |
|
1705 |
||
343 | 1706 |
manager putIdOfClass:(self class) on:stream. |
1707 |
stream nextPutByte:1. "means: byte-coded method" |
|
159 | 1708 |
self storeBinaryDefinitionBodyOn:stream manager:manager |
1 | 1709 |
! |
1710 |
||
159 | 1711 |
readBinaryContentsFrom: stream manager: manager |
1712 |
"tell the newly restored Font about restoration" |
|
1 | 1713 |
|
159 | 1714 |
self code notNil ifTrue:[ |
1715 |
"built-in method - already complete" |
|
1716 |
^ self |
|
1717 |
]. |
|
1 | 1718 |
|
159 | 1719 |
^ super readBinaryContentsFrom: stream manager: manager |
1 | 1720 |
! ! |