/interpreter/tags/at2-build270707/src/edu/vub/at/eval/InvocationStack.java

http://ambienttalk.googlecode.com/ · Java · 145 lines · 79 code · 19 blank · 47 comment · 4 complexity · 9f6ea1af94bca03aa140c8a82f061aba MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * InvocationStack.java created on 10-okt-2006 at 14:15:49
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.eval;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.objects.ATClosure;
  31. import edu.vub.at.objects.ATMessage;
  32. import edu.vub.at.objects.ATObject;
  33. import edu.vub.at.objects.ATTable;
  34. import edu.vub.at.objects.grammar.ATExpression;
  35. import java.io.PrintStream;
  36. import java.io.PrintWriter;
  37. import java.io.Serializable;
  38. import java.util.ListIterator;
  39. import java.util.Stack;
  40. /**
  41. * An InvocationStack instance represents the stack of method invocations and function applications
  42. * that are currently activated in an actor's thread. It is mainly used for debugging purposes
  43. * (e.g. generating stack trace information)
  44. *
  45. * @author tvc
  46. */
  47. public final class InvocationStack implements Cloneable, Serializable {
  48. /**
  49. * A thread-local variable is used to assign a unique invocation stack to
  50. * each separate actor. Each actor that invokes the getInvocationStack()
  51. * method receives its own separate copy of the invocation stack
  52. */
  53. private static final ThreadLocal _INVOCATION_STACK_ = new ThreadLocal() {
  54. protected synchronized Object initialValue() {
  55. return new InvocationStack();
  56. }
  57. };
  58. public static final InvocationStack getInvocationStack() {
  59. return (InvocationStack) _INVOCATION_STACK_.get();
  60. }
  61. public static final InvocationStack captureInvocationStack() {
  62. return (InvocationStack) getInvocationStack().clone();
  63. }
  64. private static class InvocationFrame implements Serializable {
  65. public final ATExpression invocation;
  66. public final ATObject receiver;
  67. public final ATTable arguments;
  68. public InvocationFrame(ATExpression inv, ATObject rcvr, ATTable args) {
  69. invocation = inv;
  70. receiver = rcvr;
  71. arguments = args;
  72. }
  73. }
  74. private final Stack invocationStack_;
  75. private InvocationStack() {
  76. invocationStack_ = new Stack();
  77. }
  78. private InvocationStack(Stack initstack) {
  79. invocationStack_ = initstack;
  80. }
  81. public void methodInvoked(ATExpression methodInvocation, ATObject receiver, ATTable args) throws InterpreterException {
  82. invocationStack_.push(new InvocationFrame(methodInvocation, receiver, args));
  83. }
  84. public void functionCalled(ATExpression funCall, ATClosure fun, ATTable evaluatedArgs) {
  85. invocationStack_.push(new InvocationFrame(funCall, fun, evaluatedArgs));
  86. }
  87. /**
  88. * @param result if null, the method invocation was aborted via an exception
  89. */
  90. public void methodReturned(ATObject result) {
  91. invocationStack_.pop();
  92. }
  93. /**
  94. * @param result if null, the function call was aborted via an exception
  95. */
  96. public void funcallReturned(ATObject result) {
  97. invocationStack_.pop();
  98. }
  99. public void printStackTrace(PrintStream s) {
  100. if(!invocationStack_.isEmpty()) {
  101. s.println("origin:");
  102. // iterator loops from bottom to top by default
  103. ListIterator i = invocationStack_.listIterator();
  104. while (i.hasNext()) { i.next(); } // skip to last element
  105. while(i.hasPrevious()) { // traverse stack top to bottom
  106. InvocationFrame frame = (InvocationFrame) i.previous();
  107. s.println("at "+Evaluator.toString(frame.invocation));
  108. }
  109. }
  110. }
  111. public void printStackTrace(PrintWriter s) {
  112. if(!invocationStack_.isEmpty()) {
  113. s.println("origin:");
  114. // iterator loops from bottom to top by default
  115. ListIterator i = invocationStack_.listIterator();
  116. while (i.hasNext()) { i.next(); } // skip to last element
  117. while(i.hasPrevious()) { // traverse stack top to bottom
  118. InvocationFrame frame = (InvocationFrame) i.previous();
  119. s.println("at "+Evaluator.toString(frame.invocation));
  120. }
  121. }
  122. }
  123. public Object clone() {
  124. return new InvocationStack((Stack) invocationStack_.clone());
  125. }
  126. }