/interpreter/tags/at_build150307/src/edu/vub/at/eval/InvocationStack.java

http://ambienttalk.googlecode.com/ · Java · 131 lines · 67 code · 18 blank · 46 comment · 2 complexity · cb326acfb882ef1781d495e54609a32f 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.Serializable;
  37. import java.util.ListIterator;
  38. import java.util.Stack;
  39. /**
  40. * An InvocationStack instance represents the stack of method invocations and function applications
  41. * that are currently activated in an actor's thread. It is mainly used for debugging purposes
  42. * (e.g. generating stack trace information)
  43. *
  44. * @author tvc
  45. */
  46. public final class InvocationStack implements Cloneable, Serializable {
  47. /**
  48. * A thread-local variable is used to assign a unique invocation stack to
  49. * each separate actor. Each actor that invokes the getInvocationStack()
  50. * method receives its own separate copy of the invocation stack
  51. */
  52. private static final ThreadLocal _INVOCATION_STACK_ = new ThreadLocal() {
  53. protected synchronized Object initialValue() {
  54. return new InvocationStack();
  55. }
  56. };
  57. public static final InvocationStack getInvocationStack() {
  58. return (InvocationStack) _INVOCATION_STACK_.get();
  59. }
  60. public static final InvocationStack captureInvocationStack() {
  61. return (InvocationStack) getInvocationStack().clone();
  62. }
  63. private static class InvocationFrame implements Serializable {
  64. public final ATExpression invocation;
  65. public final ATObject receiver;
  66. public final ATTable arguments;
  67. public InvocationFrame(ATExpression inv, ATObject rcvr, ATTable args) {
  68. invocation = inv;
  69. receiver = rcvr;
  70. arguments = args;
  71. }
  72. }
  73. private final Stack invocationStack_;
  74. private InvocationStack() {
  75. invocationStack_ = new Stack();
  76. }
  77. private InvocationStack(Stack initstack) {
  78. invocationStack_ = initstack;
  79. }
  80. public void methodInvoked(ATExpression methodInvocation, ATObject receiver, ATMessage message) throws InterpreterException {
  81. invocationStack_.push(new InvocationFrame(methodInvocation, receiver, message.base_getArguments()));
  82. }
  83. public void functionCalled(ATExpression funCall, ATClosure fun, ATTable evaluatedArgs) {
  84. invocationStack_.push(new InvocationFrame(funCall, fun, evaluatedArgs));
  85. }
  86. /**
  87. * @param result if null, the method invocation was aborted via an exception
  88. */
  89. public void methodReturned(ATObject result) {
  90. invocationStack_.pop();
  91. }
  92. /**
  93. * @param result if null, the function call was aborted via an exception
  94. */
  95. public void funcallReturned(ATObject result) {
  96. invocationStack_.pop();
  97. }
  98. public void printStackTrace(PrintStream s) {
  99. if(!invocationStack_.isEmpty()) {
  100. s.println("origin:");
  101. // iterator loops from bottom to top by default
  102. ListIterator i = invocationStack_.listIterator();
  103. while (i.hasNext()) { i.next(); } // skip to last element
  104. while(i.hasPrevious()) { // traverse stack top to bottom
  105. InvocationFrame frame = (InvocationFrame) i.previous();
  106. s.println("at "+Evaluator.toString(frame.invocation));
  107. }
  108. }
  109. }
  110. public Object clone() {
  111. return new InvocationStack((Stack) invocationStack_.clone());
  112. }
  113. }