2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
## TODO: is it better to inherit from Object???
|
|
|
|
## or treat Exception specially like UndefinedObject or Class???
|
|
|
|
##
|
|
|
|
#class Exception(Apex)
|
|
|
|
{
|
|
|
|
#dcl signalContext handlerContext messageText.
|
|
|
|
|
|
|
|
#method(#class) signal
|
|
|
|
{
|
|
|
|
^self new signal
|
|
|
|
}
|
|
|
|
|
|
|
|
#method(#class) signal: text
|
|
|
|
{
|
|
|
|
^self new signal: text
|
|
|
|
}
|
|
|
|
|
|
|
|
#method handlerContext: context
|
|
|
|
{
|
|
|
|
self.handlerContext := context.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method messageText
|
|
|
|
{
|
|
|
|
^self.messageText
|
|
|
|
}
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
#method asString
|
|
|
|
{
|
|
|
|
^(self class name) & ' - ' & self.messageText.
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:03:12 +00:00
|
|
|
#method __signal
|
|
|
|
{
|
|
|
|
self.signalContext := thisContext.
|
|
|
|
((thisContext sender) findExceptionContext) handleException: self.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method signal
|
|
|
|
{
|
2017-01-05 10:16:04 +00:00
|
|
|
| exctx exblk retval actpos |
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
self.signalContext := thisContext.
|
2017-01-05 10:16:04 +00:00
|
|
|
exctx := (thisContext sender) findExceptionContext.
|
|
|
|
[exctx notNil] whileTrue: [
|
|
|
|
exblk := exctx findExceptionHandlerFor: (self class).
|
|
|
|
(exblk notNil and:
|
|
|
|
[actpos := exctx basicSize - 1. exctx basicAt: actpos]) ifTrue: [
|
|
|
|
self.handlerContext := exctx.
|
|
|
|
exctx basicAt: actpos put: false.
|
|
|
|
[ retval := exblk value: self ] ensure: [
|
|
|
|
exctx basicAt: actpos put: true
|
2016-06-26 15:03:12 +00:00
|
|
|
].
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
thisContext unwindTo: (exctx sender) return: nil.
|
|
|
|
Processor return: retval to: (exctx sender).
|
2016-06-26 15:03:12 +00:00
|
|
|
].
|
2017-01-05 10:16:04 +00:00
|
|
|
exctx := (exctx sender) findExceptionContext.
|
2016-06-26 15:03:12 +00:00
|
|
|
].
|
|
|
|
|
|
|
|
## -----------------------------------------------------------------
|
|
|
|
## FATAL ERROR - no exception handler.
|
|
|
|
## -----------------------------------------------------------------
|
|
|
|
##thisContext unwindTo: nil return: nil.
|
2016-07-05 15:22:29 +00:00
|
|
|
##thisContext unwindTo: (Processor activeProcess initialContext) return: nil.
|
|
|
|
thisContext unwindTo: (thisProcess initialContext) return: nil.
|
2016-12-13 15:18:19 +00:00
|
|
|
('### EXCEPTION NOT HANDLED #### ' & self class name & ' - ' & self messageText) dump.
|
2016-06-26 15:03:12 +00:00
|
|
|
## TODO: debug the current process???? "
|
2016-07-05 15:22:29 +00:00
|
|
|
|
|
|
|
##Processor activeProcess terminate.
|
|
|
|
thisProcess terminate.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method signal: text
|
|
|
|
{
|
|
|
|
self.messageText := text.
|
|
|
|
^self signal.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pass
|
|
|
|
{
|
|
|
|
## pass the exception to the outer context
|
|
|
|
((self.handlerContext sender) findExceptionContext) handleException: self.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method return: value
|
|
|
|
{
|
|
|
|
(self.handlerContext notNil) ifTrue: [
|
|
|
|
Processor return: value to: self.handlerContext.
|
|
|
|
].
|
|
|
|
}
|
|
|
|
|
|
|
|
#method retry
|
|
|
|
{
|
|
|
|
## TODO: verify if return:to: causes unnecessary stack growth.
|
|
|
|
(self.handlerContext notNil) ifTrue: [
|
|
|
|
self.handlerContext pc: 0.
|
|
|
|
Processor return: self to: self.handlerContext.
|
|
|
|
].
|
|
|
|
}
|
|
|
|
|
|
|
|
#method resume: value
|
|
|
|
{
|
|
|
|
## TODO: verify if return:to: causes unnecessary stack growth.
|
|
|
|
## is this correct???
|
|
|
|
(self.signalContext notNil and: [self.handlerContext notNil]) ifTrue: [
|
|
|
|
| ctx |
|
|
|
|
ctx := self.signalContext sender.
|
|
|
|
self.signalContext := nil.
|
|
|
|
self.handlerContext := nil.
|
|
|
|
Processor return: value to: ctx.
|
|
|
|
].
|
|
|
|
}
|
|
|
|
|
|
|
|
#method resume
|
|
|
|
{
|
|
|
|
^self resume: nil.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
##============================================================================
|
|
|
|
#extend Context
|
|
|
|
{
|
|
|
|
#method isExceptionContext
|
|
|
|
{
|
|
|
|
^false
|
|
|
|
}
|
|
|
|
|
|
|
|
#method isEnsureContext
|
|
|
|
{
|
|
|
|
^false
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ensureBlock
|
|
|
|
{
|
|
|
|
^nil
|
|
|
|
}
|
|
|
|
|
|
|
|
#method findExceptionContext
|
|
|
|
{
|
|
|
|
| ctx |
|
|
|
|
ctx := self.
|
|
|
|
[ ctx notNil ] whileTrue: [
|
|
|
|
(ctx isExceptionContext) ifTrue: [^ctx].
|
|
|
|
ctx := ctx sender.
|
|
|
|
].
|
|
|
|
^nil
|
|
|
|
}
|
|
|
|
|
|
|
|
#method unwindTo: context return: retval
|
|
|
|
{
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
## <<private>>
|
|
|
|
## private: called by VM upon unwinding
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
|
|
|
|
| ctx stop |
|
2016-07-04 15:36:10 +00:00
|
|
|
|
2016-06-26 15:03:12 +00:00
|
|
|
ctx := self.
|
|
|
|
stop := false.
|
|
|
|
[stop] whileFalse: [
|
|
|
|
| eb |
|
|
|
|
eb := ctx ensureBlock.
|
|
|
|
(eb notNil) ifTrue: [
|
|
|
|
| donepos |
|
|
|
|
donepos := ctx basicSize - 1.
|
|
|
|
(ctx basicAt: donepos) ifFalse: [
|
|
|
|
ctx basicAt: donepos put: true.
|
|
|
|
eb value.
|
|
|
|
].
|
|
|
|
].
|
|
|
|
stop := (ctx == context).
|
|
|
|
ctx := ctx sender.
|
2016-07-01 16:31:47 +00:00
|
|
|
|
|
|
|
## stop ifFalse: [ stop := ctx isNil ].
|
2016-06-26 15:03:12 +00:00
|
|
|
].
|
|
|
|
|
|
|
|
^retval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
##============================================================================
|
|
|
|
#extend MethodContext
|
|
|
|
{
|
|
|
|
#method isExceptionContext
|
|
|
|
{
|
|
|
|
## 10 - STIX_METHOD_PREAMBLE_EXCEPTION in VM.
|
|
|
|
^self.method preambleCode == 10.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method isEnsureContext
|
|
|
|
{
|
|
|
|
## 10 - STIX_METHOD_PREAMBLE_ENSURE in VM.
|
|
|
|
^self.method preambleCode == 11
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ensureBlock
|
|
|
|
{
|
|
|
|
## TODO: change 8 to a constant when stix is enhanced to support constant definition
|
2017-01-05 10:16:04 +00:00
|
|
|
|
|
|
|
(* [ value-block ] ensure: [ ensure-block ]
|
|
|
|
* assuming ensure block is a parameter the ensure: method to a
|
|
|
|
* block context, the first parameter is placed after the fixed
|
|
|
|
* instance variables of the method context. As MethodContex has
|
|
|
|
* 8 instance variables, the ensure block must be at the 9th position
|
|
|
|
* which translates to index 8 *)
|
|
|
|
|
2016-06-26 15:03:12 +00:00
|
|
|
(self.method preambleCode == 11) ifFalse: [^nil].
|
|
|
|
^self basicAt: 8.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#method findExceptionHandlerFor: exception_class
|
|
|
|
{
|
2017-01-05 10:16:04 +00:00
|
|
|
(* find an exception handler block for a given exception class.
|
|
|
|
*
|
|
|
|
* for this to work, self must be an exception handler context.
|
|
|
|
* For a single on:do: call,
|
|
|
|
* self class specNumInstVars must return 8.(i.e.MethodContext has 8 instance variables.)
|
|
|
|
* basicAt: 8 must be the on: argument.
|
|
|
|
* basicAt: 9 must be the do: argument *)
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
## TODO: change 8 to a constant when stix is enhanced to support constant definition
|
|
|
|
## or calcuate the minimum size using the class information.
|
2017-01-05 10:16:04 +00:00
|
|
|
|
|
|
|
(self isExceptionContext) ifTrue: [
|
|
|
|
| size exc |
|
|
|
|
|
|
|
|
(* NOTE: the following loop scans all parameters to the on:do: method.
|
|
|
|
* if the on:do: method contains local temporary variables,
|
|
|
|
* those must be skipped from scanning. *)
|
|
|
|
|
|
|
|
size := self basicSize.
|
|
|
|
8 priorTo: size by: 2 do: [ :i |
|
2016-06-26 15:03:12 +00:00
|
|
|
exc := self basicAt: i.
|
|
|
|
((exception_class == exc) or: [exception_class inheritsFrom: exc]) ifTrue: [^self basicAt: (i + 1)].
|
|
|
|
]
|
|
|
|
].
|
|
|
|
^nil.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method handleException: exception
|
|
|
|
{
|
2017-01-05 10:16:04 +00:00
|
|
|
(* -----------------------------------------------------------------
|
|
|
|
* <<private>>
|
|
|
|
* called by Exception>>signal.
|
|
|
|
* this method only exists in the MethodContext and UndefinedObject.
|
|
|
|
* the caller must make sure that the receiver object is
|
|
|
|
* a method context or nil. Exception>>signal invokes this method
|
|
|
|
* only for an exception context which is a method context. it
|
|
|
|
* invokes it for nil when no exception context is found.
|
|
|
|
* ---------------------------------------------------------------- *)
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
| excblk retval actpos |
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
(* position of the temporary variable 'active' in MethodContext>>on:do.
|
|
|
|
* for this code to work, it must be the last temporary variable in the method. *)
|
2016-06-26 15:03:12 +00:00
|
|
|
actpos := (self basicSize) - 1.
|
|
|
|
|
|
|
|
excblk := self findExceptionHandlerFor: (exception class).
|
|
|
|
(excblk isNil or: [(self basicAt: actpos) not]) ifTrue: [
|
|
|
|
## self is an exception context but doesn't have a matching
|
|
|
|
## exception handler or the exception context is already
|
|
|
|
## in the middle of evaluation.
|
|
|
|
^(self.sender findExceptionContext) handleException: exception.
|
|
|
|
].
|
|
|
|
|
|
|
|
exception handlerContext: self.
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
(* -----------------------------------------------------------------
|
|
|
|
* if an exception occurs within an exception handler block,
|
|
|
|
* the search will reach this context again as the exception block
|
|
|
|
* is evaluated before actual unwinding. set the temporary variable
|
|
|
|
* in the exception context to mask out this context from the search
|
|
|
|
* list.
|
|
|
|
* ---------------------------------------------------------------- *)
|
2016-06-26 15:03:12 +00:00
|
|
|
self basicAt: actpos put: false.
|
|
|
|
[ retval := excblk value: exception ] ensure: [
|
|
|
|
self basicAt: actpos put: true
|
|
|
|
].
|
|
|
|
|
|
|
|
##(self.sender isNil) ifTrue: [ "TODO: CANNOT RETURN" ].
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
(* -----------------------------------------------------------------
|
|
|
|
* return to self.sender which is a caller of the exception context (on:do:)
|
|
|
|
* pass the first ensure context between thisContext and self.sender.
|
|
|
|
* [ [Exception signal: 'xxx'] ensure: [20] ] on: Exception do: [:ex | ...]
|
|
|
|
* ---------------------------------------------------------------- *)
|
2016-06-26 15:03:12 +00:00
|
|
|
thisContext unwindTo: self.sender return: nil.
|
|
|
|
Processor return: retval to: self.sender.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
##============================================================================
|
|
|
|
#extend BlockContext
|
|
|
|
{
|
|
|
|
#method on: anException do: anExceptionBlock
|
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
(* -------------------------------
|
|
|
|
thisContext isExceptionContext dump.
|
2016-06-26 15:03:12 +00:00
|
|
|
(thisContext basicSize) dump.
|
|
|
|
(thisContext basicAt: 8) dump. ## this should be anException
|
|
|
|
(thisContext basicAt: 9) dump. ## this should be anExceptionBlock
|
|
|
|
(thisContext basicAt: 10) dump. ## this should be handlerActive
|
2017-01-05 10:16:04 +00:00
|
|
|
'on:do: ABOUT TO EVALUE THE RECEIVER BLOCK' dump.
|
|
|
|
---------------------------------- *)
|
2016-06-26 15:03:12 +00:00
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method on: exc1 do: blk1 on: exc2 do: blk2
|
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ensure: aBlock
|
|
|
|
{
|
|
|
|
| retval done |
|
|
|
|
<ensure>
|
|
|
|
|
|
|
|
done := false.
|
|
|
|
retval := self value.
|
|
|
|
|
|
|
|
## the temporary variable 'done' may get changed
|
|
|
|
## during evaluation for exception handling.
|
|
|
|
done ifFalse: [
|
|
|
|
done := true.
|
|
|
|
aBlock value.
|
|
|
|
].
|
|
|
|
^retval
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ifCurtailed: aBlock
|
|
|
|
{
|
|
|
|
| v ok |
|
|
|
|
|
|
|
|
ok := false.
|
|
|
|
[ v := self value. ok := true. ] ensure: [ ok ifFalse: [aBlock value] ].
|
|
|
|
^v.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##============================================================================
|
2017-01-05 10:16:04 +00:00
|
|
|
#class InstantiationFailureException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:03:12 +00:00
|
|
|
#class NoSuchMessageException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#class PrimitiveFailureException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
#class IndexOutOfRangeException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#class SubclassResponsibilityException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#class ErrorExceptionizationFailureException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:03:12 +00:00
|
|
|
#extend Apex
|
|
|
|
{
|
|
|
|
#method(#class) primitiveFailed
|
|
|
|
{
|
|
|
|
## TODO: implement this
|
|
|
|
## experimental backtrace...
|
|
|
|
| ctx |
|
|
|
|
ctx := thisContext.
|
|
|
|
[ctx notNil] whileTrue: [
|
|
|
|
(ctx class == MethodContext)
|
2016-12-13 15:18:19 +00:00
|
|
|
ifTrue: [ (ctx method owner name & '>>' & ctx method name) dump ].
|
2016-06-26 15:03:12 +00:00
|
|
|
## TODO: include blockcontext???
|
|
|
|
ctx := ctx sender.
|
|
|
|
].
|
|
|
|
'------ END OF BACKTRACE -----------' dump.
|
|
|
|
PrimitiveFailureException signal: 'PRIMITIVE FAILED'.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method(#class) cannotInstantiate
|
|
|
|
{
|
2017-01-05 10:16:04 +00:00
|
|
|
## TOOD: accept a class
|
|
|
|
InstantiationFailureException signal: 'Cannot instantiate'.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
#method(#class) doesNotUnderstand: message_name
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
## TODO: implement this properly
|
2017-01-05 10:16:04 +00:00
|
|
|
NoSuchMessageException signal: (message_name & ' not understood by ' & (self name)).
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
#method(#class) index: index outOfRange: ubound
|
|
|
|
{
|
|
|
|
IndexOutOfRangeException signal: 'Out of range'.
|
|
|
|
}
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2017-01-05 10:16:04 +00:00
|
|
|
#method(#class) subclassResponsibility: message_name
|
|
|
|
{
|
|
|
|
SubclassResponsibilityException signal: ('Subclass must implment ' & message_name).
|
|
|
|
}
|
|
|
|
|
|
|
|
#method(#class) cannotExceptionizeError
|
|
|
|
{
|
|
|
|
## todo: accept the object
|
|
|
|
ErrorExceptionizationFailureException signal: 'Cannot exceptionize an error'
|
|
|
|
}
|
|
|
|
}
|