added some more code to support exception handling

This commit is contained in:
hyunghwan.chung
2016-03-28 13:25:36 +00:00
parent 2a88f5d503
commit 77f0bfad5d
7 changed files with 257 additions and 99 deletions

View File

@ -1,10 +1,16 @@
#class(#pointer) Context(Apex)
{
#dcl sender ip sp ntmprs.
#method sender
{
^self.sender
}
}
#class(#pointer) MethodContext(Context)
{
#dcl sender ip sp ntmprs method receiver home origin.
#dcl method receiver home origin.
#method pc
{
@ -33,11 +39,16 @@
sp := aSP.
##sp := sp - 1.
}
#method isHandlerContext
{
^self.method primitive == 512
}
}
#class(#pointer) BlockContext(Context)
{
#dcl caller ip sp ntmprs nargs source home origin.
#dcl nargs source home origin.
#method fork
{
@ -146,6 +157,12 @@
## self value ifTrue: [ aBlock value. thisContext restart. ].
}
#method whileTrue
{
(self value) ifFalse: [^nil].
self whileTrue.
}
#method whileFalse: aBlock
{
(self value) ifTrue: [^nil].
@ -153,6 +170,12 @@
self whileFalse: aBlock.
}
#method whileFalse
{
(self value) ifTrue: [^nil].
self whileFalse.
}
#method pc
{
^ip
@ -179,16 +202,30 @@
}
"------ TODO: -------------------------------------"
#method on: anException do: anExceptionBlock
{
<primitive: #block_on_do>
self primitiveFailed.
| handlerActive |
<exception>
handlerActive := true.
(thisContext basicAt: 9) dump.
^self value.
}
#method ensure: aBlock
{
"##
| complete returnValue |
<ensure>
returnValue := self valueNoContextSwitch.
complete ifNil: [
complete := true.
aBlock value.
].
^returnValue. ##"
}
#method ifCurtailed: aBlock
@ -198,3 +235,78 @@
"------ TODO: -------------------------------------"
}
#class Exception(Object)
{
#dcl signalContext handlerContext messageText.
#method(#class) signal
{
self new signal
}
#method(#class) signal: text
{
self new signal: text
}
#method signal
{
self.signalContext := thisContext.
self isHandled
ifTrue: [ self handle ]
ifFalse: [ self notHandled ].
}
#method signal: text
{
self.messageText := text.
self signal.
}
#method isHandled
{
^self handlerContext notNil
}
#method handle
{
self return: (self.handlerContext handlerBlock value: self)
}
#method notHandle
{
'EXCEPTION NOT HANDLED' dump.
## TODO: debug the current process???? "
Processor activeProcess terminate.
}
#method handlerContext
{
(self.handlerContext notNil) ifTrue: [ ^self.handlerContext ].
^self handlerContextStartingFrom: self.signalContext sender.
}
#method handlerContextStartingFrom: aContext
{
## Find exception handling context starting from a given context
| ctx |
ctx := aContext.
[ ctx notNil ]
whileTrue: [
(ctx handles: self) ifTrue: [ ^self.handlerContext := ctx ].
ctx := ctx sender
].
^nil
}
#method return: anObject
{
Processor return: anObject to: (self.handlerContext parent)
}
}
#class NoSuchMessageException(Exception)
{
}

View File

@ -380,4 +380,10 @@
"#method signal: aSemaphore onOutput: file
{
}"
#method return: anObject to: aContext
{
<primitive: #_processor_return_to>
self primitiveFailed.
}
}

View File

@ -254,6 +254,11 @@
#class(#pointer) CompiledMethod(Object)
{
#dcl owner preamble preamble_data_1 preamble_data_2 ntmprs nargs code source.
#method preamble
{
^self.preamble
}
}
#include 'Context.st'.