JavaContext.st
changeset 2353 fa7400d022a0
parent 2304 73806b718f4e
child 2380 9195eccdcbd9
child 2396 fadc6d7a2f5b
--- a/JavaContext.st	Sat Feb 02 01:23:18 2013 +0100
+++ b/JavaContext.st	Sat Feb 16 19:08:45 2013 +0100
@@ -1,34 +1,62 @@
 "
  COPYRIGHT (c) 1996-2011 by Claus Gittinger
 
+ New code and modifications done at SWING Research Group [1]:
+
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
  inclusion of the above copyright notice.   This software may not
  be provided or otherwise made available to, or used by, any
  other person.  No title to or ownership of the software is
  hereby transferred.
+
+ [1] Code written at SWING Research Group contains a signature
+     of one of the above copright owners. For exact set of such code,
+     see the differences between this version and version stx:libjava
+     as of 1.9.2010
 "
 "{ Package: 'stx:libjava' }"
 
 Context variableSubclass:#JavaContext
-	instanceVariableNames:'exArg exPC byteCode constPool'
+	instanceVariableNames:'exArg exPC byteCode constPool acqrMonitors'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Languages-Java-Support'
 !
 
+Object subclass:#FinallyToken
+	instanceVariableNames:'context exception selector value'
+	classVariableNames:''
+	poolDictionaries:''
+	privateIn:JavaContext
+!
+
 !JavaContext class methodsFor:'documentation'!
 
 copyright
 "
  COPYRIGHT (c) 1996-2011 by Claus Gittinger
 
+ New code and modifications done at SWING Research Group [1]:
+
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
  inclusion of the above copyright notice.   This software may not
  be provided or otherwise made available to, or used by, any
  other person.  No title to or ownership of the software is
  hereby transferred.
+
+ [1] Code written at SWING Research Group contains a signature
+     of one of the above copright owners. For exact set of such code,
+     see the differences between this version and version stx:libjava
+     as of 1.9.2010
+
 "
 ! !
 
@@ -50,6 +78,21 @@
 
     "Modified: / 13-12-1995 / 19:05:22 / cg"
     "Modified: / 25-10-2010 / 17:19:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+shouldExecuteFinallyOnUnwind
+    "Return true, if a finally block should be executed
+     upon force unwind"
+
+    | m pc |
+
+    m := self method.
+    pc := self pc.
+    self assert: m notNil.
+    m hasFinally ifFalse:[ ^ false ].
+    ^(method handlerFor: nil at: pc) notNil.
+
+    "Created: / 10-04-2012 / 11:09:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !JavaContext methodsFor:'ST context mimicri'!
@@ -200,20 +243,6 @@
     "Modified: / 13.1.1998 / 17:23:27 / cg"
 !
 
-numVars
-    "return the number of locals.
-     Redefined since Java keeps the receiver of a non-static method
-     at local slot 0 and holds the args as locals."
-
-    |n|
-
-    n := super numVars.
-    ^ (n - self numArgs) max:0
-
-    "Created: / 13.1.1998 / 17:03:08 / cg"
-    "Modified: / 13.1.1998 / 17:25:16 / cg"
-!
-
 pc
     lineNr isNil ifTrue:[^ nil].
     ^ lineNr bitAnd:16rFFFF
@@ -294,11 +323,68 @@
 
 !JavaContext methodsFor:'accessing'!
 
+acquiredMonitors
+    ^acqrMonitors
+
+    "Created: / 08-11-2011 / 12:23:18 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified (format): / 27-08-2012 / 16:46:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+acquiredMonitorsDo: aBlock 
+"/    acqrMonitors isNil ifTrue: [ ^self ].
+"/    acqrMonitors copy reverseDo: aBlock.
+    aBlock value: self receiver.
+
+    "Created: / 08-11-2011 / 15:03:31 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 26-08-2012 / 19:28:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+addMonitor: mon 
+    self assert: (acqrMonitors isNil or: [ acqrMonitors isOrderedCollection ]).
+    acqrMonitors ifNil: [ acqrMonitors := Stack new ].
+    acqrMonitors push: mon.
+    self markForUnwind.
+
+    "Created: / 08-11-2011 / 14:19:21 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-11-2011 / 21:40:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-11-2011 / 19:13:15 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+!
+
 programmingLanguage
 
     ^JavaLanguage instance
 
     "Created: / 17-03-2011 / 10:17:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+removeMonitor: mon 
+    | poppedObject |
+    acqrMonitors isNil ifTrue: [
+        Logger 
+            log: ('removeMonitor: called but no monitors in acqrMonitors (%1)' 
+                    bindWith: self)
+            severity: #warn
+            facility: #JVM.
+        self breakPoint: #mh.
+        ^ self.
+    ].
+    poppedObject := acqrMonitors top.
+    self assert: (poppedObject == mon).
+    acqrMonitors remove: mon
+        ifAbsent: [
+            Logger 
+                log: ('removeMonitor: called but no such monitor in acqrMonitors (%1)' 
+                        bindWith: self)
+                severity: #warn
+                facility: #JVM.
+            self breakPoint: #mh.
+            ^ self.
+        ].
+        acqrMonitors isEmpty ifTrue: [self unmarkForUnwind].
+
+    "Created: / 08-11-2011 / 14:19:58 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
+    "Modified: / 08-11-2011 / 21:39:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-11-2011 / 19:14:29 / Marcel Hlopko <hlopkmar@fel.cvut.cz>"
 ! !
 
 !JavaContext methodsFor:'exception handler support'!
@@ -316,6 +402,50 @@
     "Created: / 7.1.1998 / 21:36:56 / cg"
 ! !
 
+!JavaContext methodsFor:'non local control flow'!
+
+unwindAndRestartForJavaException
+
+    "Called by JavaVM>>throwException: unwinds the stack
+     up to this context and restarts it so an exception handler
+     executes"
+
+    | con wasMarked |
+
+    "Each context that has a monitor acquired has
+     and unwind action that release all monitors acquired.
+     However, we DONT want my monitors to be released,
+     so we temporarily unmark this context for unwind and
+     then mark it again, eventually"
+
+    wasMarked := self isUnwindContext.
+    wasMarked ifTrue:[self unmarkForUnwind].    
+    self senderIsNil ifFalse:[
+        con := thisContext evaluateUnwindActionsUpTo:self.
+    ].
+    wasMarked ifTrue:[self markForUnwind].
+
+    "oops, if nil, I am not on the calling chain;
+     (bad bad, unwind action have already been performed.
+      should we check for this situation first and NOT evaluate
+      the unwind actions in this case ?)
+    "
+    con isNil ifTrue:[
+        "
+         tried to return to a context which is already dead
+         (i.e. the method/block has already executed a return)
+        "
+        ^ self invalidReturnOrRestartError:#'unwindAndRestart:' with:nil
+    ].
+    "
+     now, that all unwind-actions are done, I can use the
+     low-level restart ...
+    "
+    ^ self restart
+
+    "Created: / 08-11-2011 / 22:00:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !JavaContext methodsFor:'printing & storing'!
 
 receiverPrintString
@@ -403,16 +533,75 @@
     "Created: / 7.5.1998 / 01:26:19 / cg"
 ! !
 
+!JavaContext::FinallyToken methodsFor:'accessing'!
+
+context
+    ^ context
+!
+
+context:something
+    context := something.
+!
+
+exception
+    ^ exception
+!
+
+exception:something
+    exception := something.
+!
+
+selector
+    ^ selector
+!
+
+selector:something
+    selector := something.
+!
+
+value
+    ^ value
+!
+
+value:something
+    value := something.
+! !
+
+!JavaContext::FinallyToken methodsFor:'actions'!
+
+pass
+
+    "First, release all leftover acquired monitors"
+    JavaVM releaseMonitorsOfUnwindingContext: context.
+
+    selector == #return ifTrue:[
+        exception return
+    ].
+    selector == #return: ifTrue:[
+        exception return: value
+    ].
+
+    self error:'Should never be reached'.
+
+    "Created: / 04-04-2012 / 20:24:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !JavaContext class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libjava/JavaContext.st,v 1.38 2011-11-24 11:54:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libjava/JavaContext.st,v 1.39 2013-02-16 18:08:32 vrany Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libjava/JavaContext.st,v 1.38 2011-11-24 11:54:57 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libjava/JavaContext.st,v 1.39 2013-02-16 18:08:32 vrany Exp $'
+!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
 !
 
 version_SVN
-    ^ '§Id: JavaContext.st,v 1.36 2011/08/18 18:42:48 vrany Exp §'
+    ^ '§Id§'
 ! !
+