View Javadoc

1   /*
2    * Copyright (c) 2001, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   */
18  
19  package net.sf.zel.vm;
20  
21  
22  import java.io.InputStream;
23  import java.io.PrintStream;
24  import java.util.Map;
25  import java.util.concurrent.Future;
26  import java.util.concurrent.ThreadPoolExecutor;
27  
28  
29  /**
30   * Virtual Machine Execution Context
31   * 
32   * @author zoly
33   */
34  public class ExecutionContext
35          implements Cloneable
36  {
37  
38      public ThreadPoolExecutor execService;
39  
40      public ResultCache resultCache;
41  
42      /**
43       * The virtual machine heap
44       */
45      public Map heap = null;
46  
47      /**
48       * current memory context
49       */
50      public Map memContext = null;
51  
52      /**
53       * the program
54       */
55      public Program code = null;
56      
57      /**
58       * The Instruction pointer
59       */
60      public int ip = 0;
61      
62      /**
63       * The halt register
64       */
65      public boolean terminated = false;
66      
67      /**
68       * The main stack
69       */
70      public SimpleStack stack;
71      
72      /**
73       * The memory context stack
74       */
75      public SimpleStack memContextStack = new SimpleStack(32);
76      
77      /**
78       * Standard Input
79       */
80      public InputStream in = null;
81      
82      /**
83       * Standard Output
84       */
85      public PrintStream out = null;
86      
87      /**
88       * Standard Error Output
89       */
90      public PrintStream err = null;
91  
92  
93      /**
94       * The parent Execution Context
95       */
96      public ExecutionContext parent = null;
97  
98  
99      /**
100      * Default constructor - private, does some init
101      */
102     private ExecutionContext()
103     {
104         this.in = System.in;
105         this.out = System.out;
106         this.err = System.err;
107     }
108 
109     /**
110      * Initializes execution context with MAP based heap memory
111      *
112      * @param program Program
113      * @param heap Map
114      */
115     public ExecutionContext(Program program, java.util.Map heap, ThreadPoolExecutor execService)
116     {
117         this();
118         code = program;
119         this.heap = heap;
120         this.memContext = heap;
121         this.execService= execService;
122         init();
123     }
124 
125 
126     private void init()
127     {
128         this.stack = new SimpleStack(32);
129         if (execService == null)
130         {
131             this.stack = new SimpleStack(32);
132             this.resultCache = new SimpleResultCache();
133         }
134         else
135         {
136             this.resultCache = new SynchronizedResultCache();
137         }
138     }
139 
140 
141     /**
142      * aditional constructor that allows you to set the standard Input/Output streams
143      * @param program
144      * @param heap
145      * @param in
146      * @param out
147      * @param err 
148      */
149     public ExecutionContext(Program program, java.util.Map heap, InputStream in, PrintStream out, PrintStream err, ThreadPoolExecutor execService)
150     {
151         code = program;
152         this.heap = heap;
153         this.memContext = heap;
154         this.in = in;
155         this.out = out;
156         this.err = err;
157         this.execService= execService;
158         init();
159     }
160 
161     /**
162      * pops object out of stack
163      * @return Object
164      */
165 
166     public Object popSyncStackVal()
167     {
168         Object result = this.stack.pop();
169         if (result instanceof Future<?>)
170             try
171             {
172                 return ((Future<Object>) result).get();
173             } catch (Exception ex)
174             {
175                 throw new RuntimeException(ex);
176             }
177         else return result;
178     }
179 
180 
181 
182     public ExecutionContext getSubProgramContext(Program program) 
183     {
184         ExecutionContext ec;
185         try
186         {
187             ec = (ExecutionContext) this.clone();
188         } catch (CloneNotSupportedException ex)
189         {
190             throw new RuntimeException("Execution Context must be clonable", ex);
191         }
192         ec.code = program;
193         ec.ip =0;        
194         ec.stack = new SimpleStack(32);        
195         ec.parent = this;
196         return ec;
197     }
198 
199 
200     public Map newMem()
201     {
202         try
203         {
204             return this.memContext.getClass().newInstance();
205         } catch (InstantiationException ex)
206         {
207             throw new RuntimeException(ex);
208         } catch (IllegalAccessException ex)
209         {
210             throw new RuntimeException(ex);
211         }
212     }
213 
214     /**
215      * Constructs string with the execution context
216      * @return String
217      */
218     @Override
219     public String toString()
220     {
221         StringBuilder result = new StringBuilder();
222         result.append("\nStack:");
223         result.append(stack.toString());
224         result.append("\nMemCtx:");
225         result.append(memContext); 
226         result.append("\nHeap:");
227         result.append(heap);  
228         result.append("\nProgram:");
229         result.append(code);
230         result.append("\nInstruction pointer: " + ip);
231         if (parent != null)
232             result.append("\n Called from: ").append(parent);
233         return result.toString();
234     }
235 }