2016-05-17 15:12:27 +00:00
|
|
|
#class Exception(Apex)
|
|
|
|
{
|
2016-06-22 13:43:49 +00:00
|
|
|
#dcl signalContext handlerContext messageText.
|
2016-05-17 15:12:27 +00:00
|
|
|
|
|
|
|
## To be extended below.
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#class(#pointer) Context(Apex)
|
|
|
|
{
|
2016-06-22 03:23:14 +00:00
|
|
|
#dcl sender ip sp ntmprs.
|
2016-03-28 13:25:36 +00:00
|
|
|
|
|
|
|
#method sender
|
|
|
|
{
|
|
|
|
^self.sender
|
|
|
|
}
|
2016-04-29 16:40:25 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method isExceptionContext
|
2016-04-29 16:40:25 +00:00
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
^false
|
2016-04-29 16:40:25 +00:00
|
|
|
}
|
2016-06-20 15:42:51 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method isEnsureContext
|
2016-06-22 03:23:14 +00:00
|
|
|
{
|
|
|
|
^false
|
|
|
|
}
|
|
|
|
|
2016-06-20 15:42:51 +00:00
|
|
|
#method ensureBlock
|
|
|
|
{
|
2016-06-22 03:23:14 +00:00
|
|
|
^nil
|
2016-06-20 15:42:51 +00:00
|
|
|
}
|
2016-06-22 13:43:49 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method findExceptionContext
|
2016-06-22 13:43:49 +00:00
|
|
|
{
|
|
|
|
| ctx |
|
|
|
|
ctx := self.
|
2016-06-23 15:35:28 +00:00
|
|
|
[ ctx notNil ] whileTrue: [
|
|
|
|
(ctx isExceptionContext) ifTrue: [^ctx].
|
|
|
|
ctx := ctx sender.
|
|
|
|
].
|
2016-06-22 13:43:49 +00:00
|
|
|
^nil
|
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method findEnsureContextAbove: sentinel
|
2016-06-22 13:43:49 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
| ctx |
|
|
|
|
ctx := self.
|
|
|
|
[ ctx notNil and: [ctx ~~ sentinel] ] whileTrue: [
|
|
|
|
(ctx isEnsureContext) ifTrue: [^ctx].
|
|
|
|
ctx := ctx sender.
|
|
|
|
].
|
|
|
|
^nil
|
2016-06-22 13:43:49 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
|
|
|
|
#method hasSender: context
|
2016-06-22 13:43:49 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
| s |
|
|
|
|
(self == context) ifTrue: [^false].
|
|
|
|
s := self.sender.
|
|
|
|
[ s isNil ] whileFalse: [
|
|
|
|
(s == context) ifTrue: [^true].
|
|
|
|
s := s sender.
|
|
|
|
].
|
|
|
|
^false
|
|
|
|
}
|
2016-06-22 13:43:49 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method isDead
|
|
|
|
{
|
|
|
|
^self.ip < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
#method terminate
|
|
|
|
{
|
|
|
|
self.sender := nil.
|
|
|
|
self.ip = -1;
|
|
|
|
}
|
2016-06-22 13:43:49 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method terminateTo: prevctx
|
|
|
|
{
|
|
|
|
| ctx sctx |
|
|
|
|
(self hasSender: prevctx) ifTrue: [
|
|
|
|
ctx := self.sender.
|
|
|
|
[ctx ~~ prevctx] whileTrue: [
|
|
|
|
sctx := ctx sender.
|
|
|
|
ctx terminate.
|
|
|
|
ctx := sctx.
|
|
|
|
]
|
2016-06-22 13:43:49 +00:00
|
|
|
].
|
2016-06-23 15:35:28 +00:00
|
|
|
self.sender := prevctx.
|
|
|
|
}
|
2016-06-22 13:43:49 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method resume: retval through: context
|
|
|
|
{
|
|
|
|
| ctx |
|
|
|
|
self isDead ifTrue: ["TODO: cannot return" 'CANNOT RETURN' dump.].
|
2016-06-22 13:43:49 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
ctx := context.
|
|
|
|
[ ctx notNil ] whileTrue: [
|
|
|
|
| donepos |
|
|
|
|
donepos := ctx basicSize - 1.
|
|
|
|
|
|
|
|
(ctx basicAt: donepos) ifFalse: [
|
|
|
|
| ensblk |
|
|
|
|
ctx basicAt: donepos put: true.
|
|
|
|
ensblk := ctx ensureBlock.
|
|
|
|
thisContext terminateTo: ctx.
|
|
|
|
ensblk value.
|
|
|
|
].
|
|
|
|
|
|
|
|
ctx := ctx findEnsureContextAbove: self.
|
|
|
|
].
|
|
|
|
|
|
|
|
thisContext terminateTo: self.
|
|
|
|
^retval
|
|
|
|
}
|
|
|
|
|
|
|
|
#method unwindTo: context return: retval
|
|
|
|
{
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
## <<private>>
|
|
|
|
## private: called by VM upon unwinding
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
|
|
|
|
| ctx stop |
|
|
|
|
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-06-22 13:43:49 +00:00
|
|
|
].
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
^retval
|
2016-06-22 13:43:49 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
^self.ip
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 05:53:35 +00:00
|
|
|
#method pcplus1
|
|
|
|
{
|
|
|
|
^self.ip + 1
|
|
|
|
}
|
|
|
|
|
2016-05-18 11:10:54 +00:00
|
|
|
#method goto: anInteger
|
|
|
|
{
|
|
|
|
<primitive: #_context_goto>
|
|
|
|
self primitiveFailed. ## TODO: need to make this a hard failure?
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method pc: anInteger
|
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
self.ip := anInteger.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
2016-05-03 10:10:28 +00:00
|
|
|
^self.sp.
|
2015-10-15 14:40:08 +00:00
|
|
|
|
|
|
|
}
|
2016-06-23 15:35:28 +00:00
|
|
|
#method sp: new_sp
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
self.sp := new_sp
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method pc: new_pc sp: new_sp
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
self.ip := new_pc.
|
|
|
|
self.sp := new_sp.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
2016-03-28 13:25:36 +00:00
|
|
|
|
2016-05-03 10:10:28 +00:00
|
|
|
#method method
|
|
|
|
{
|
|
|
|
^self.method
|
|
|
|
}
|
2016-04-29 16:40:25 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method isExceptionContext
|
2016-03-28 13:25:36 +00:00
|
|
|
{
|
2016-04-29 16:40:25 +00:00
|
|
|
## 10 - STIX_METHOD_PREAMBLE_EXCEPTION in VM.
|
2016-06-22 03:23:14 +00:00
|
|
|
^self.method preambleCode == 10.
|
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method isEnsureContext
|
2016-06-22 03:23:14 +00:00
|
|
|
{
|
|
|
|
## 10 - STIX_METHOD_PREAMBLE_ENSURE in VM.
|
|
|
|
^self.method preambleCode == 11
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ensureBlock
|
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
## TODO: change 8 to a constant when stix is enhanced to support constant definition
|
2016-06-22 03:23:14 +00:00
|
|
|
(self.method preambleCode == 11) ifFalse: [^nil].
|
|
|
|
^self basicAt: 8.
|
2016-04-29 16:40:25 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
|
|
|
|
#method findExceptionHandlerFor: exception_class
|
2016-04-29 16:40:25 +00:00
|
|
|
{
|
2016-06-18 00:12:27 +00:00
|
|
|
## find an exception handler block for a given exception class.
|
|
|
|
##
|
2016-05-03 10:10:28 +00:00
|
|
|
## for this to work, self must be an exception handler context.
|
|
|
|
## For a single on:do: call,
|
|
|
|
## self class specNumInstVars must return 8.
|
|
|
|
## basicAt: 8 must be the on: argument.
|
|
|
|
## basicAt: 9 must be the do: argument
|
2016-04-29 16:40:25 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
(self isExceptionContext) ifTrue: [
|
2016-05-03 10:10:28 +00:00
|
|
|
| bound exc |
|
|
|
|
## NOTE: if on:do: has a temporary varible, bound must be adjusted to reflect it.
|
|
|
|
bound := self basicSize - 1.
|
2016-06-22 03:23:14 +00:00
|
|
|
## TODO: change 8 to a constant when stix is enhanced to support constant definition
|
2016-06-18 00:12:27 +00:00
|
|
|
## or calcuate the minimum size using the class information.
|
2016-06-22 03:23:14 +00:00
|
|
|
8 to: bound by: 2 do: [ :i |
|
2016-05-03 10:10:28 +00:00
|
|
|
exc := self basicAt: i.
|
2016-06-23 15:35:28 +00:00
|
|
|
((exception_class == exc) or: [exception_class inheritsFrom: exc]) ifTrue: [^self basicAt: (i + 1)].
|
2016-05-03 10:10:28 +00:00
|
|
|
]
|
|
|
|
].
|
|
|
|
^nil.
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
2016-06-22 03:23:14 +00:00
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method handleException: exception
|
2016-06-22 03:23:14 +00:00
|
|
|
{
|
2016-06-23 15:35:28 +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.
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
|
|
|
|
| excblk retval actpos |
|
|
|
|
|
|
|
|
## 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.
|
|
|
|
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.
|
|
|
|
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
## 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.
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
self basicAt: actpos put: false.
|
|
|
|
[ retval := excblk value: exception ] ensure: [
|
|
|
|
self basicAt: actpos put: true
|
2016-06-22 03:23:14 +00:00
|
|
|
].
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
##(self.sender isNil) ifTrue: [ "TODO: CANNOT RETURN" ].
|
|
|
|
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
## 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 | ...]
|
|
|
|
##
|
|
|
|
## -------------------------------------------------------------------
|
|
|
|
##^self.sender resume: retval through: (thisContext findEnsureContextAbove: self.sender).
|
|
|
|
thisContext unwindTo: self.sender return: nil.
|
|
|
|
Processor return: retval to: self.sender.
|
2016-06-22 03:23:14 +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
|
|
|
|
2016-06-22 03:23:14 +00:00
|
|
|
#method fork
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
|
|
|
"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
|
|
|
}
|
2016-05-12 05:53:35 +00:00
|
|
|
#method value: a value: b value: c value: d
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
|
|
|
self primitiveFailed.
|
|
|
|
}
|
|
|
|
#method value: a value: b value: c value: d value: e
|
|
|
|
{
|
|
|
|
<primitive: #_block_value>
|
|
|
|
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
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
## Naive implementation
|
|
|
|
## --------------------------------------------------
|
|
|
|
## (self value) ifFalse: [^nil].
|
|
|
|
## aBlock value.
|
|
|
|
## self whileTrue: aBlock.
|
|
|
|
## --------------------------------------------------
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-05-18 11:10:54 +00:00
|
|
|
| pc sp |
|
|
|
|
|
2016-05-12 05:53:35 +00:00
|
|
|
pc := thisContext pcplus1.
|
2016-05-18 11:10:54 +00:00
|
|
|
(self value) ifFalse: [ ^nil ].
|
2015-10-15 14:40:08 +00:00
|
|
|
aBlock value.
|
2016-05-18 11:10:54 +00:00
|
|
|
|
|
|
|
## the pc: method leaves thisContext and pc in the stack after
|
|
|
|
## having changes the instruction poointer.
|
|
|
|
## as a result, the stack keeps growing. the goto method
|
|
|
|
## clears thisContext and pc off the stack unlike normal methods.
|
|
|
|
##thisContext pc: pc.
|
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method whileTrue
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## (self value) ifFalse: [^nil].
|
|
|
|
## self whileTrue.
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
| pc |
|
|
|
|
pc := thisContext pcplus1.
|
2016-05-18 11:10:54 +00:00
|
|
|
(self value) ifFalse: [ ^nil ].
|
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-16 10:13:03 +00:00
|
|
|
#method whileFalse: aBlock
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
## Naive implementation
|
|
|
|
## --------------------------------------------------
|
|
|
|
## (self value) ifTrue: [^nil].
|
|
|
|
## aBlock value.
|
|
|
|
## self whileFalse: aBlock.
|
|
|
|
## --------------------------------------------------
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
## The assignment to 'pc' uses the POP_INTO_TEMPVAR_1.
|
|
|
|
## It pops a value off the stack top, stores it to the second
|
|
|
|
## temporary variable(aBlock is the first temporary variable).
|
|
|
|
## It is a single byte instruction. 'pc' returned by
|
|
|
|
## 'thisContext pcplus1'' should point to the instruction after
|
|
|
|
## the POP_INTO_TEMPVAR_0 instruction.
|
|
|
|
##
|
|
|
|
## It would need the increment of 2 if the pair of
|
|
|
|
## STORE_INTO_TEMPVAR_1 and POP_STACKTOP were used.
|
|
|
|
## This implementation is subject to the instructions chosen
|
|
|
|
## by the compiler.
|
|
|
|
##
|
|
|
|
| pc |
|
|
|
|
pc := thisContext pcplus1.
|
|
|
|
(self value) ifTrue: [ ^nil "^self" ].
|
|
|
|
aBlock value.
|
2016-05-18 11:10:54 +00:00
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-03-16 10:13:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 13:25:36 +00:00
|
|
|
#method whileFalse
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
## (self value) ifTrue: [^nil].
|
|
|
|
## self whileFalse.
|
|
|
|
|
|
|
|
## --------------------------------------------------
|
2016-06-24 15:01:51 +00:00
|
|
|
## Non-recursive implementation
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
|
|
|
| pc |
|
|
|
|
pc := thisContext pcplus1.
|
|
|
|
(self value) ifTrue: [ ^nil "^self" ].
|
2016-05-18 11:10:54 +00:00
|
|
|
thisContext goto: pc.
|
2016-05-12 05:53:35 +00:00
|
|
|
## --------------------------------------------------
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method pc
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
^self.ip
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: anInteger
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
self.ip := anInteger.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
^self.sp
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method sp: anInteger
|
|
|
|
{
|
2016-05-12 05:53:35 +00:00
|
|
|
self.sp := anInteger.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method restart
|
|
|
|
{
|
|
|
|
ip := source pc.
|
|
|
|
}
|
|
|
|
|
2016-06-20 15:42:51 +00:00
|
|
|
|
2016-03-26 06:39:56 +00:00
|
|
|
#method on: anException do: anExceptionBlock
|
2015-10-15 14:40:08 +00:00
|
|
|
{
|
2016-06-22 13:43:49 +00:00
|
|
|
| exception_active |
|
2016-03-28 13:25:36 +00:00
|
|
|
<exception>
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
"thisContext isExceptionContext 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
|
|
|
|
2016-06-22 13:43:49 +00:00
|
|
|
exception_active := true.
|
2016-03-28 13:25:36 +00:00
|
|
|
^self value.
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 10:10:28 +00:00
|
|
|
#method on: exc1 do: blk1 on: exc2 do: blk2
|
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
| exception_active |
|
2016-05-03 10:10:28 +00:00
|
|
|
<exception>
|
2016-06-23 15:35:28 +00:00
|
|
|
exception_active := true.
|
2016-05-03 10:10:28 +00:00
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
#method ensure: aBlock
|
|
|
|
{
|
2016-06-23 15:35:28 +00:00
|
|
|
| retval done |
|
2016-06-22 03:23:14 +00:00
|
|
|
<ensure>
|
2016-06-23 15:35:28 +00:00
|
|
|
|
|
|
|
done := false.
|
|
|
|
retval := self value.
|
|
|
|
|
|
|
|
## the temporary variable 'done' may get changed
|
|
|
|
## during evaluation for exception handling.
|
|
|
|
done ifFalse: [
|
|
|
|
done := true.
|
|
|
|
aBlock value.
|
|
|
|
].
|
|
|
|
^retval
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method ifCurtailed: aBlock
|
|
|
|
{
|
2016-06-22 03:23:14 +00:00
|
|
|
| v ok |
|
2016-06-20 15:42:51 +00:00
|
|
|
|
2016-06-22 03:23:14 +00:00
|
|
|
ok := false.
|
|
|
|
[ v := self value. ok := true. ] ensure: [ ok ifFalse: [aBlock value] ].
|
|
|
|
^v.
|
2016-06-20 15:42:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 14:40:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 03:23:14 +00:00
|
|
|
##
|
2016-05-03 10:10:28 +00:00
|
|
|
## TODO: is it better to inherit from Object???
|
|
|
|
## or treat Exception specially like UndefinedObject or Class???
|
2016-06-22 03:23:14 +00:00
|
|
|
##
|
2016-05-17 15:12:27 +00:00
|
|
|
#extend Exception
|
2016-03-28 13:25:36 +00:00
|
|
|
{
|
|
|
|
#method(#class) signal
|
|
|
|
{
|
2016-06-24 14:29:43 +00:00
|
|
|
^self new signal
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method(#class) signal: text
|
|
|
|
{
|
2016-06-24 14:29:43 +00:00
|
|
|
^self new signal: text
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 13:43:49 +00:00
|
|
|
#method handlerContext: context
|
|
|
|
{
|
|
|
|
self.handlerContext := context.
|
|
|
|
}
|
|
|
|
|
2016-05-02 13:50:42 +00:00
|
|
|
#method messageText
|
|
|
|
{
|
|
|
|
^self.messageText
|
|
|
|
}
|
|
|
|
|
2016-06-23 15:35:28 +00:00
|
|
|
#method __signal
|
2016-03-28 13:25:36 +00:00
|
|
|
{
|
|
|
|
self.signalContext := thisContext.
|
2016-06-23 15:35:28 +00:00
|
|
|
((thisContext sender) findExceptionContext) handleException: self.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method signal
|
|
|
|
{
|
|
|
|
| excctx excblk retval actpos |
|
|
|
|
|
|
|
|
self.signalContext := thisContext.
|
|
|
|
excctx := (thisContext sender) findExceptionContext.
|
|
|
|
[excctx notNil] whileTrue: [
|
|
|
|
excblk := excctx findExceptionHandlerFor: (self class).
|
|
|
|
(excblk notNil and:
|
|
|
|
[actpos := excctx basicSize - 1. excctx basicAt: actpos]) ifTrue: [
|
|
|
|
self.handlerContext := excctx.
|
|
|
|
excctx basicAt: actpos put: false.
|
|
|
|
[ retval := excblk value: self ] ensure: [
|
|
|
|
excctx basicAt: actpos put: true
|
|
|
|
].
|
|
|
|
|
|
|
|
thisContext unwindTo: (excctx sender) return: nil.
|
|
|
|
Processor return: retval to: (excctx sender).
|
|
|
|
].
|
|
|
|
excctx := (excctx sender) findExceptionContext.
|
|
|
|
].
|
|
|
|
|
|
|
|
## -----------------------------------------------------------------
|
|
|
|
## FATAL ERROR - no exception handler.
|
|
|
|
## -----------------------------------------------------------------
|
2016-06-24 14:29:43 +00:00
|
|
|
##thisContext unwindTo: nil return: nil.
|
|
|
|
thisContext unwindTo: (Processor activeProcess initialContext) return: nil.
|
2016-06-23 15:35:28 +00:00
|
|
|
('### EXCEPTION NOT HANDLED #### ', self class name, ' - ', self messageText) dump.
|
|
|
|
## TODO: debug the current process???? "
|
|
|
|
Processor activeProcess terminate.
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method signal: text
|
|
|
|
{
|
|
|
|
self.messageText := text.
|
2016-06-24 14:29:43 +00:00
|
|
|
^self signal.
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 05:56:35 +00:00
|
|
|
#method pass
|
|
|
|
{
|
|
|
|
## pass the exception to the outer context
|
2016-06-23 15:35:28 +00:00
|
|
|
((self.handlerContext sender) findExceptionContext) handleException: self.
|
2016-05-01 13:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method return: value
|
|
|
|
{
|
2016-06-24 14:29:43 +00:00
|
|
|
(self.handlerContext notNil) ifTrue: [
|
|
|
|
Processor return: value to: self.handlerContext.
|
|
|
|
].
|
2016-04-30 05:56:35 +00:00
|
|
|
}
|
|
|
|
|
2016-05-01 13:49:36 +00:00
|
|
|
#method retry
|
|
|
|
{
|
2016-06-24 14:29:43 +00:00
|
|
|
## 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.
|
|
|
|
].
|
2016-05-01 13:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method resume
|
|
|
|
{
|
2016-06-24 14:29:43 +00:00
|
|
|
^self resume: nil.
|
2016-05-01 13:49:36 +00:00
|
|
|
}
|
2016-03-28 13:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#class NoSuchMessageException(Exception)
|
|
|
|
{
|
|
|
|
}
|
2016-05-02 13:59:28 +00:00
|
|
|
|
2016-05-03 10:10:28 +00:00
|
|
|
#class PrimitiveFailureException(Exception)
|
|
|
|
{
|
|
|
|
}
|
2016-05-02 13:59:28 +00:00
|
|
|
|
|
|
|
#extend Apex
|
|
|
|
{
|
|
|
|
#method(#class) primitiveFailed
|
|
|
|
{
|
|
|
|
## TODO: implement this
|
2016-05-03 10:10:28 +00:00
|
|
|
## experimental backtrace...
|
|
|
|
| ctx |
|
|
|
|
ctx := thisContext.
|
|
|
|
[ctx notNil] whileTrue: [
|
|
|
|
(ctx class == MethodContext)
|
2016-05-07 01:37:44 +00:00
|
|
|
ifTrue: [ (ctx method owner name, '>>', ctx method name) dump ].
|
2016-05-03 10:10:28 +00:00
|
|
|
## TODO: include blockcontext???
|
|
|
|
ctx := ctx sender.
|
|
|
|
].
|
2016-05-17 15:12:27 +00:00
|
|
|
'------ END OF BACKTRACE -----------' dump.
|
2016-05-03 10:10:28 +00:00
|
|
|
PrimitiveFailureException signal: 'PRIMITIVE FAILED'.
|
|
|
|
}
|
2016-05-07 01:37:44 +00:00
|
|
|
|
2016-05-03 10:10:28 +00:00
|
|
|
#method(#class) cannotInstantiate
|
|
|
|
{
|
|
|
|
Exception signal: 'Cannot instantiate'.
|
2016-05-02 13:59:28 +00:00
|
|
|
}
|
2016-05-17 15:12:27 +00:00
|
|
|
|
|
|
|
#method(#class) doesNotUnderstand: messageSymbol
|
|
|
|
{
|
|
|
|
## TODO: implement this properly
|
|
|
|
NoSuchMessageException signal: (messageSymbol, ' not understood by ', (self name)).
|
|
|
|
}
|
2016-05-02 13:59:28 +00:00
|
|
|
}
|