Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.22 KB | None | 0 0
  1. /*
  2.  * Created on 10 nov 2009
  3.  */
  4.  
  5. package interpreter.api;
  6.  
  7. import java.io.PrintStream;
  8. import java.util.NoSuchElementException;
  9.  
  10. import interpreter.api.util.InterpreterStackOverflow;
  11. import interpreter.api.util.InterpreterYieldTimeout;
  12.  
  13. public class ExecutionThread
  14. {
  15.    public final InterpreterProcess process;
  16.    public final String             name;
  17.    private final ExecutionFrame[]  frames;
  18.    private int                     frameIndex;
  19.  
  20.    public ExecutionThread(InterpreterProcess process, String name, int maxDepth)
  21.    {
  22.       this.process = process;
  23.       this.name = name;
  24.       this.frames = new ExecutionFrame[maxDepth];
  25.    }
  26.  
  27.    public void dump(PrintStream out)
  28.    {
  29.       out.println("Thread-dump in java-thread \"" + Thread.currentThread().getName() + "\" / step-thread \"" + this.name + "\"");
  30.       out.println("   at " + this.current);
  31.       for (int i = this.frameIndex - 1; i >= 0; i--)
  32.          out.println("   at " + this.frames[i]);
  33.    }
  34.  
  35.    public boolean reachedFrame(ExecutionFrame find)
  36.    {
  37.       return this.frameIndex == 0 || this.current == find;
  38.    }
  39.  
  40.    public ExecutionFrame findCallsite(ExecutionFrame frame)
  41.    {
  42.       if (this.current == frame && this.frameIndex != 0)
  43.          return this.frames[this.frameIndex - 1];
  44.       for (int i = 0; i < this.frameIndex; i++)
  45.       {
  46.          if (this.frames[i] == frame)
  47.          {
  48.             if (i == 0)
  49.                return null;
  50.             return this.frames[i - 1];
  51.          }
  52.       }
  53.       throw new NoSuchElementException();
  54.    }
  55.  
  56.    //
  57.  
  58.    private boolean yielded = false;
  59.  
  60.    public void yield()
  61.    {
  62.       if (this.yielded)
  63.          throw new IllegalStateException();
  64.       this.yielded = true;
  65.    }
  66.  
  67.    //
  68.  
  69.    public boolean stepUntilYield(ExecutionFrame callsite, int maxSteps) throws Throwable
  70.    {
  71.       for (int i = 0; i < maxSteps; i++)
  72.       {
  73.          if (this.yielded)
  74.          {
  75.             this.yielded = false;
  76.             return true; // continue later
  77.          }
  78.  
  79.          if (!this.step())
  80.             return false; // end!
  81.          if (this.reachedFrame(callsite))
  82.             return false;
  83.       }
  84.  
  85.       throw new InterpreterYieldTimeout();
  86.    }
  87.  
  88.    public boolean stepUntilYield(ExecutionFrame callsite) throws Throwable
  89.    {
  90.       while (true)
  91.       {
  92.          if (this.yielded)
  93.          {
  94.             this.yielded = false;
  95.             return true; // continue later
  96.          }
  97.  
  98.          if (!this.step())
  99.             return false; // end!
  100.          if (this.reachedFrame(callsite))
  101.             return false;
  102.       }
  103.    }
  104.  
  105.    public boolean step() throws Throwable
  106.    {
  107.       ExecutionFrame frame = this.currentFrame();
  108.  
  109.       if (frame.method == null)
  110.       {
  111.          this.popFrame();
  112.          return true;
  113.       }
  114.  
  115.       if (frame.method.impl != null)
  116.       {
  117.          frame.method.impl.call(frame); // pops frame
  118.          return true;
  119.       }
  120.  
  121.       return frame.method.stream.step(frame);
  122.    }
  123.  
  124.    //
  125.  
  126.    public void vreturn()
  127.    {
  128.       this.popFrame();
  129.    }
  130.  
  131.    public void ireturn()
  132.    {
  133.       int value = this.currentFrame().popInt();
  134.       this.popFrame().pushInt(value);
  135.    }
  136.  
  137.    public void lreturn()
  138.    {
  139.       long value = this.currentFrame().popLong();
  140.       this.popFrame().pushLong(value);
  141.    }
  142.  
  143.    public void freturn()
  144.    {
  145.       float value = this.currentFrame().popFloat();
  146.       this.popFrame().pushFloat(value);
  147.    }
  148.  
  149.    public void dreturn()
  150.    {
  151.       double value = this.currentFrame().popDouble();
  152.       this.popFrame().pushDouble(value);
  153.    }
  154.  
  155.    public void areturn()
  156.    {
  157.       int value = this.currentFrame().popReference();
  158.       this.popFrame().pushInt(value);
  159.    }
  160.  
  161.    //
  162.  
  163.    private ExecutionFrame current;
  164.  
  165.    public void pushFrame(ExecutionFrame frame)
  166.    {
  167.       if (this.current != null)
  168.          if (this.frameIndex < this.frames.length)
  169.             this.frames[this.frameIndex++] = this.current;
  170.          else
  171.             throw new InterpreterStackOverflow();
  172.       this.current = frame;
  173.    }
  174.  
  175.    public ExecutionFrame currentFrame()
  176.    {
  177.       return this.current;
  178.    }
  179.  
  180.    private ExecutionFrame popFrame()
  181.    {
  182.       return this.current = this.frames[--this.frameIndex];
  183.    }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement