LazyMethod.st
author claus
Thu, 10 Aug 1995 20:13:01 +0200
changeset 102 77e4d1119ff2
parent 89 f0c8faf27ceb
child 103 f4a69d7dd387
permissions -rw-r--r--
.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     1
"
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     2
 COPYRIGHT (c) 1994 by Claus Gittinger
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
     3
	      All Rights Reserved
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     4
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
343ca93df0e0 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    11
"
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    12
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    13
Method subclass:#LazyMethod
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    14
	 instanceVariableNames:''
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    15
	 classVariableNames:'Access CompilationFailedSignal'
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    16
	 poolDictionaries:''
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    17
	 category:'Kernel-Methods'
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    18
!
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    19
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    20
LazyMethod comment:'
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    21
COPYRIGHT (c) 1994 by Claus Gittinger
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    22
	      All Rights Reserved
27
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    23
102
claus
parents: 89
diff changeset
    24
$Header: /cvs/stx/stx/libcomp/LazyMethod.st,v 1.12 1995-08-10 18:11:30 claus Exp $
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    25
'!
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    26
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    27
!LazyMethod class methodsFor:'documentation'!
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    28
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    29
copyright
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    30
"
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    31
 COPYRIGHT (c) 1994 by Claus Gittinger
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    32
	      All Rights Reserved
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    33
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    34
 This software is furnished under a license and may be used
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    35
 only in accordance with the terms of that license and with the
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    36
 inclusion of the above copyright notice.   This software may not
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    37
 be provided or otherwise made available to, or used by, any
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    38
 other person.  No title to or ownership of the software is
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    39
 hereby transferred.
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    40
"
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    41
!
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    42
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    43
version
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    44
"
102
claus
parents: 89
diff changeset
    45
$Header: /cvs/stx/stx/libcomp/LazyMethod.st,v 1.12 1995-08-10 18:11:30 claus Exp $
claus
parents: 89
diff changeset
    46
$Revision: 1.12 $
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    47
"
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    48
!
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    49
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    50
documentation
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    51
"
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    52
    Instances of LazyMethod are created when doing a lazy autoload.
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    53
    They do not contain any code (neither byte- nor machinecode), but
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    54
    keep their sourcecode only.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    55
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    56
    When executed, these will trigger an error in the VM (noByteCode),
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    57
    which is cought here to create a real method from the receiver,
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    58
    amd re-execute the method.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    59
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    60
    This allows faster loading of code, which will be later compiled
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    61
    when first executed; for classes with a large number of methods, of
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    62
    which only a small subset is actually used, this can also save
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    63
    lots of memory (beside making autoloading faster).
20
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    64
"
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    65
! !
f8dd8ba75205 *** empty log message ***
claus
parents: 18
diff changeset
    66
40
45ecd4441edb compilation is donw in critical region - it was not reentrant
claus
parents: 27
diff changeset
    67
!LazyMethod class methodsFor:'initialization'!
45ecd4441edb compilation is donw in critical region - it was not reentrant
claus
parents: 27
diff changeset
    68
45ecd4441edb compilation is donw in critical region - it was not reentrant
claus
parents: 27
diff changeset
    69
initialize
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    70
    CompilationFailedSignal isNil ifTrue:[
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    71
	CompilationFailedSignal := ExecutionErrorSignal newSignalMayProceed:true.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    72
	CompilationFailedSignal nameClass:self message:#compilationFailedSignal.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    73
	CompilationFailedSignal notifierString:'compilation of lazy method failed'.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    74
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    75
	Access := Semaphore forMutualExclusion.
89
claus
parents: 88
diff changeset
    76
	"/ Access := RecursionLock new.
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    77
    ]
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    78
! !
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    79
88
claus
parents: 84
diff changeset
    80
!LazyMethod class methodsFor:'Signal constants'!
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    81
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    82
compilationFailedSignal
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
    83
    ^ CompilationFailedSignal
40
45ecd4441edb compilation is donw in critical region - it was not reentrant
claus
parents: 27
diff changeset
    84
! !
45ecd4441edb compilation is donw in critical region - it was not reentrant
claus
parents: 27
diff changeset
    85
27
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    86
!LazyMethod methodsFor:'queries'!
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    87
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    88
isLazyMethod
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    89
    ^ true
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    90
! !
7e2b3ab54d7c *** empty log message ***
claus
parents: 20
diff changeset
    91
54
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
    92
!LazyMethod methodsFor:'compiling'!
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    93
54
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
    94
makeRealMethod
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
    95
    "make the receiver a real method; i.e. compile the sourcecode and
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
    96
     fill in the bytecode. This must be done in order to execute the receiver."
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    97
54
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
    98
    |m|
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
    99
343ca93df0e0 Initial revision
claus
parents:
diff changeset
   100
    "compile the method"
343ca93df0e0 Initial revision
claus
parents:
diff changeset
   101
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   102
    "we have to sequentialize this using a lock-semaphore,
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   103
     to make sure only one method is compiled at a time.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   104
     Otherwise, we might get into trouble, if (due to a timeout)
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   105
     another recompile is forced while compiling this one ...
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   106
     (happened when autoloading animation demos)
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   107
    "
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
   108
    [
89
claus
parents: 88
diff changeset
   109
	Access critical:[
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   110
	    m := self asByteCodeMethod.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   111
	].
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   112
    ] valueUninterruptably.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   113
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   114
    (m isNil or:[(byteCode := m byteCode) isNil]) ifTrue:[
47
f861ad42703e *** empty log message ***
claus
parents: 45
diff changeset
   115
	"
f861ad42703e *** empty log message ***
claus
parents: 45
diff changeset
   116
	 compilation failed
f861ad42703e *** empty log message ***
claus
parents: 45
diff changeset
   117
	"
54
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   118
	^ nil
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   119
    ].
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   120
    literals := m literals.
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   121
    flags := m flags.
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   122
    self changeClassToThatOf:m.
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   123
    ^ self
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   124
! !
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   125
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   126
!LazyMethod methodsFor:'error handling'!
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   127
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   128
noByteCode 
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   129
    "this is triggered by the interpreter when a lazy method is about to 
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   130
     be executed (by sending the to-be executed  method this message).
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   131
     Hard-compile the method, install its bytecode in the receiver,
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   132
     and recall it."
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   133
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   134
    |sender spec class selector|
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   135
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   136
    "compile the method"
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   137
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   138
    self makeRealMethod isNil ifTrue:[
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   139
	"
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   140
	 compilation failed
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   141
	"
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   142
	selector := thisContext sender selector.
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   143
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   144
	class := self containingClass.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   145
	class notNil ifTrue:[
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   146
	    spec := class name , '>>' , selector
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   147
	] ifFalse:[
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   148
	    spec := 'unknown>>' , selector
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   149
	].
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   150
	"
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   151
	 this error is triggered, if the compilation of a lazy method
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   152
	 failed - this happens for example, if a lazy methods code has been
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   153
	 changed in a fileBrowser without checking the code for syntactical
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   154
	 correctnes, or if the instvars of an autoloaded classes superclass 
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   155
	 have been changed without changing the subclasses code ...
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   156
	 You should enter the SystemBrowser on this method, and try accepting 
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   157
	 to see what the problem is.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   158
	 The methods class is found in the local 'class',
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   159
	 the selector is found in the local 'selector'.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   160
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   161
	 As a general rule: never edit autoloaded classes from anything
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   162
	 except the browser - to check that they work and are compilable.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   163
	"
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   164
	^ CompilationFailedSignal raiseRequestWith:self
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   165
				  errorString:('compilation of lazy method ' , spec , ' failed')
40
45ecd4441edb compilation is donw in critical region - it was not reentrant
claus
parents: 27
diff changeset
   166
    ].
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   167
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   168
    "
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   169
     Now, the receiver method has mutated into a real (non-lazy) one.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   170
     Get the original message receiver and args, and execute the method.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   171
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   172
     ThisContext sender is the context of the original send (the failed one)
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   173
    "
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
   174
    sender := thisContext sender.
45
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   175
    ObjectMemory flushCaches.
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   176
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   177
    ^ self valueWithReceiver:(sender receiver)
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   178
		   arguments:(sender args)
e8331ba8ad5d *** empty log message ***
claus
parents: 40
diff changeset
   179
		    selector:(sender selector)
54
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   180
		      search:nil
86c5b39c2eca *** empty log message ***
claus
parents: 47
diff changeset
   181
		      sender:nil
18
343ca93df0e0 Initial revision
claus
parents:
diff changeset
   182
! !