2015-10-15 14:40:08 +00:00
|
|
|
#class(#pointer) Context(Apex)
|
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
#dcl sender ip sp ntmprs.
|
|
|
|
|
|
|
|
#method sender
|
|
|
|
{
|
|
|
|
^self.sender
|
|
|
|
}
|
2016-04-29 16:40:25 +00:00
|
|
|
|
|
|
|
" #method isHandlerContext
|
|
|
|
{
|
|
|
|
return ^false
|
|
|
|
}"
|
|
|
|
|
|
|
|
#method handles: anException
|
|
|
|
{
|
|
|
|
^false.
|
|
|
|
}
|
|
|
|
|
|
|
|
## #method parent
|
|
|
|
## {
|
|
|
|
## ^self.sender
|
|
|
|
## }
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) MethodContext(Context)
|
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
#dcl method receiver home origin.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
#method pc
|
|
|
|
{
|
|
|
|
^ip
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: anInteger
|
|
|
|
{
|
|
|
|
ip := anInteger.
|
|
|
|
"sp := sp - 1." "whould this always work??? "
|
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
|
|
|
^sp.
|
|
|
|
|
|
|
|
}
|
|
|
|
#method sp: anInteger
|
|
|
|
{
|
|
|
|
sp := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: aPC sp: aSP
|
|
|
|
{
|
|
|
|
ip := aPC.
|
|
|
|
sp := aSP.
|
|
|
|
##sp := sp - 1.
|
|
|
|
}
|
2016-03-28 13:25:36 +00:00
|
|
|
|
2016-04-29 16:40:25 +00:00
|
|
|
## #method methodName
|
|
|
|
## {
|
|
|
|
## ^self.method basicAt: 0.
|
|
|
|
## }
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method isHandlerContext
|
|
|
|
{
|
2016-04-29 16:40:25 +00:00
|
|
|
## 10 - STIX_METHOD_PREAMBLE_EXCEPTION in VM.
|
|
|
|
^self.method preambleCode == 10.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method handles: anException
|
|
|
|
{
|
|
|
|
(self isHandlerContext) ifTrue: [^true]. ## TODO: check if xxxx
|
|
|
|
^false
|
|
|
|
}
|
|
|
|
|
|
|
|
#method handlerBlock
|
|
|
|
{
|
|
|
|
## for this to work, self must be a handler context.
|
|
|
|
^self basicAt: 9
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) BlockContext(Context)
|
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
#dcl nargs source home origin.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
#method fork
|
|
|
|
{
|
|
|
|
"crate a new process in the runnable state"
|
2015-10-18 15:06:17 +00:00
|
|
|
^self newProcess resume.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method newProcess
|
|
|
|
{
|
|
|
|
"create a new process in the suspended state"
|
2015-10-18 15:06:17 +00:00
|
|
|
<primitive: #_block_new_process>
|
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method newProcessWith: anArray
|
|
|
|
{
|
|
|
|
"create a new process in the suspended state passing the elements
|
|
|
|
of anArray as block arguments"
|
2015-10-22 02:47:25 +00:00
|
|
|
<primitive: #_block_new_process>
|
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value: a
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value: a value: b
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value: a value: b value: c
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
2016-01-29 04:04:39 +00:00
|
|
|
self primitiveFailed.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method ifTrue: aBlock
|
|
|
|
{
|
|
|
|
^(self value) ifTrue: aBlock.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ifFalse: aBlock
|
|
|
|
{
|
|
|
|
^(self value) ifFalse: aBlock.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ifTrue: trueBlock ifFalse: falseBlock
|
|
|
|
{
|
|
|
|
^(self value) ifTrue: trueBlock ifFalse: falseBlock
|
|
|
|
}
|
|
|
|
|
|
|
|
#method whileTrue: aBlock
|
|
|
|
{
|
|
|
|
## http://stackoverflow.com/questions/2500483/is-there-a-way-in-a-message-only-language-to-define-a-whiletrue-message-without
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## ^(self value) ifTrue: [aBlock value. self whileTrue: aBlock].
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## less block context before whileTrue: is recursively sent.
|
|
|
|
## whileTrue: is sent in a method context.
|
2016-03-16 02:27:18 +00:00
|
|
|
(self value) ifFalse: [^nil].
|
|
|
|
aBlock value.
|
|
|
|
self whileTrue: aBlock.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
2016-03-16 02:27:18 +00:00
|
|
|
" | pc sp xsp |
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
sp := thisContext sp.
|
2016-03-16 02:27:18 +00:00
|
|
|
sp := sp - 1. ## decrement sp by 1 becuase thisContext pushed above affects the sp method
|
2015-10-15 14:40:08 +00:00
|
|
|
pc := thisContext pc.
|
|
|
|
self value ifFalse: [ ^nil "^self" ].
|
|
|
|
aBlock value.
|
|
|
|
##thisContext pc: pc - 3 sp: sp.
|
|
|
|
##thisContext pc: pc + 2 sp: sp.
|
|
|
|
thisContext pc: pc + 1 sp: sp.
|
|
|
|
## this +2 or - 3 above is dependent on the byte code instruction size used for 'store'
|
|
|
|
## +2 to skip STORE_INTO_TEMP(pc) and POP_STACKTOP.
|
|
|
|
## TODO: make it independent of the byte code size
|
2016-03-16 02:27:18 +00:00
|
|
|
"
|
2015-10-15 14:40:08 +00:00
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## #<label>:
|
|
|
|
## thisContext pc: #<label> sp: sp.
|
|
|
|
##
|
|
|
|
## | pc |
|
|
|
|
## pc := thisContext pc.
|
|
|
|
## ^self value ifTrue: [aBlock value. thisContext pc: pc]
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## self value ifTrue: [ aBlock value. thisContext restart. ].
|
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method whileTrue
|
|
|
|
{
|
|
|
|
(self value) ifFalse: [^nil].
|
|
|
|
self whileTrue.
|
|
|
|
}
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
#method whileFalse: aBlock
|
|
|
|
{
|
|
|
|
(self value) ifTrue: [^nil].
|
|
|
|
aBlock value.
|
|
|
|
self whileFalse: aBlock.
|
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method whileFalse
|
|
|
|
{
|
|
|
|
(self value) ifTrue: [^nil].
|
|
|
|
self whileFalse.
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method pc
|
|
|
|
{
|
|
|
|
^ip
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: anInteger
|
|
|
|
{
|
|
|
|
ip := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
|
|
|
^sp
|
|
|
|
}
|
|
|
|
|
|
|
|
#method sp: anInteger
|
|
|
|
{
|
|
|
|
sp := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method restart
|
|
|
|
{
|
|
|
|
ip := source pc.
|
|
|
|
}
|
|
|
|
|
|
|
|
"------ TODO: -------------------------------------"
|
2016-03-26 06:39:56 +00:00
|
|
|
#method on: anException do: anExceptionBlock
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
| handlerActive |
|
|
|
|
<exception>
|
|
|
|
handlerActive := true.
|
|
|
|
|
2016-05-02 13:50:42 +00:00
|
|
|
"thisContext isHandlerContext dump.
|
2016-04-29 16:40:25 +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
|
2016-05-02 13:50:42 +00:00
|
|
|
'on:do: ABOUT TO EVALUE THE RECEIVER BLOCK' dump."
|
2016-03-28 13:25:36 +00:00
|
|
|
|
|
|
|
^self value.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 16:40:25 +00:00
|
|
|
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method ensure: aBlock
|
|
|
|
{
|
2016-03-28 13:25:36 +00:00
|
|
|
"##
|
|
|
|
| complete returnValue |
|
|
|
|
<ensure>
|
|
|
|
|
|
|
|
returnValue := self valueNoContextSwitch.
|
|
|
|
complete ifNil: [
|
|
|
|
complete := true.
|
|
|
|
aBlock value.
|
|
|
|
].
|
|
|
|
^returnValue. ##"
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method ifCurtailed: aBlock
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
"------ TODO: -------------------------------------"
|
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#class Exception(Object)
|
|
|
|
{
|
|
|
|
#dcl signalContext handlerContext messageText.
|
|
|
|
|
|
|
|
#method(#class) signal
|
|
|
|
{
|
|
|
|
self new signal
|
|
|
|
}
|
|
|
|
|
|
|
|
#method(#class) signal: text
|
|
|
|
{
|
|
|
|
self new signal: text
|
|
|
|
}
|
|
|
|
|
2016-05-02 13:50:42 +00:00
|
|
|
#method messageText
|
|
|
|
{
|
|
|
|
^self.messageText
|
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method signal
|
|
|
|
{
|
|
|
|
self.signalContext := thisContext.
|
2016-05-01 13:49:36 +00:00
|
|
|
|
|
|
|
self.handlerContext isNil ifTrue: [
|
|
|
|
self.handlerContext := self findHandlerContextStartingFrom: self.signalContext
|
|
|
|
].
|
|
|
|
|
|
|
|
self.handlerContext isNil
|
|
|
|
ifTrue: [ self notHandled ]
|
|
|
|
ifFalse: [ self handle ].
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method signal: text
|
|
|
|
{
|
|
|
|
self.messageText := text.
|
|
|
|
self signal.
|
|
|
|
}
|
|
|
|
|
2016-04-30 05:56:35 +00:00
|
|
|
#method pass
|
|
|
|
{
|
|
|
|
## pass the exception to the outer context
|
2016-05-02 13:50:42 +00:00
|
|
|
|
|
|
|
## TODO: Should i change the signalContex to thisContext???
|
2016-05-01 13:49:36 +00:00
|
|
|
self.handlerContext := self findHandlerContextStartingFrom: (self.handlerContext sender).
|
|
|
|
self.handlerContext isNil
|
|
|
|
ifTrue: [ self notHandled ]
|
|
|
|
ifFalse: [ self handle ].
|
|
|
|
}
|
|
|
|
|
|
|
|
#method return: value
|
|
|
|
{
|
|
|
|
self.handlerContext isNil ifFalse: [
|
|
|
|
Processor return: value to: (self.handlerContext sender)
|
|
|
|
]
|
2016-04-30 05:56:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 13:49:36 +00:00
|
|
|
#method retry
|
|
|
|
{
|
2016-05-02 13:50:42 +00:00
|
|
|
## TODO: verify if return:to: causes unnecessary stack growth.
|
|
|
|
|
|
|
|
self.handlerContext pc: 0.
|
|
|
|
Processor return: self to: self.handlerContext.
|
|
|
|
##Processor forceContext: self.handlerContext.
|
2016-05-01 13:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method resume
|
|
|
|
{
|
2016-05-02 13:50:42 +00:00
|
|
|
## TODO: verify if return:to: causes unnecessary stack growth.
|
2016-05-01 13:49:36 +00:00
|
|
|
## is this correct???
|
2016-05-02 13:50:42 +00:00
|
|
|
Processor return: self to: (self.signalContext sender).
|
2016-05-01 13:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
## ####################################################################
|
|
|
|
## ####################################################################
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method handle
|
|
|
|
{
|
2016-04-30 05:56:35 +00:00
|
|
|
Processor return: (self.handlerContext handlerBlock value: self) to: (self.handlerContext sender)
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 16:40:25 +00:00
|
|
|
#method notHandled
|
2016-03-28 13:25:36 +00:00
|
|
|
{
|
2016-04-29 16:40:25 +00:00
|
|
|
'####################### EXCEPTION NOT HANDLED ###############################' dump.
|
2016-03-28 13:25:36 +00:00
|
|
|
## TODO: debug the current process???? "
|
|
|
|
Processor activeProcess terminate.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method handlerContext
|
|
|
|
{
|
|
|
|
(self.handlerContext notNil) ifTrue: [ ^self.handlerContext ].
|
2016-04-30 05:56:35 +00:00
|
|
|
self.handlerContext := self findHandlerContextStartingFrom: self.signalContext.
|
|
|
|
^self.handlerContext.
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 05:56:35 +00:00
|
|
|
#method findHandlerContextStartingFrom: aContext
|
2016-03-28 13:25:36 +00:00
|
|
|
{
|
|
|
|
## Find exception handling context starting from a given context
|
|
|
|
| ctx |
|
|
|
|
|
|
|
|
ctx := aContext.
|
|
|
|
[ ctx notNil ]
|
|
|
|
whileTrue: [
|
2016-04-30 05:56:35 +00:00
|
|
|
(ctx handles: self) ifTrue: [ ^ ctx ].
|
2016-03-28 13:25:36 +00:00
|
|
|
ctx := ctx sender
|
|
|
|
].
|
|
|
|
^nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#class NoSuchMessageException(Exception)
|
|
|
|
{
|
2016-05-02 13:50:42 +00:00
|
|
|
#method signal
|
|
|
|
{
|
|
|
|
self signal: 'no such message'.
|
|
|
|
}
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
2016-05-02 13:59:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
#extend Apex
|
|
|
|
{
|
|
|
|
#method(#class) primitiveFailed
|
|
|
|
{
|
|
|
|
## TODO: implement this
|
|
|
|
## PrimitiveFailureError signal.
|
|
|
|
self dump.
|
|
|
|
##'primitive failed' dump.
|
|
|
|
# TODO: define a specialized exception class for primitive failure and use it.
|
|
|
|
Exception signal: 'PRIMITIVE FAILED...'.
|
|
|
|
}
|
|
|
|
}
|