2003/10/31 10:57:24
[org.ibex.core.git] / src / org / xwt / js / JS.java
index 885d840..ebd2e36 100644 (file)
@@ -219,38 +219,37 @@ public abstract class JS {
 
         CompiledFunction currentCompiledFunction = null;
         Vec stack = new Vec();
-        int pc;
+        public Scope scope = null;
+        int pc = 0;
+        boolean paused = false;
 
-        /** binds this thread to the current Java Thread */
-        public void bindToCurrentJavaThread() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
+        CompiledFunction tailCallFunction = null;
+        JS.Array tailCallArgs = null;
 
-        /** returns the line of code that is currently executing */
-        public int getLine() { return currentCompiledFunction == null ? -1 : currentCompiledFunction.getLine(pc); }
+        public Thread(CompiledFunction function) {
+            this(function, new CompiledFunctionImpl.FunctionScope("unknown", function.parentScope));
+        }
+        public Thread(CompiledFunction function, Scope scope) {
+            stack.push(new CompiledFunctionImpl.CallMarker(this));
+            stack.push(new JS.Array());
+            this.currentCompiledFunction = function;
+            this.scope = scope;
+        }
+
+        public void setTailCall(JS.CompiledFunction f, JS.Array args) { tailCallFunction = f; tailCallArgs = args; }
 
-        /** returns the name of the source code file which declared the currently executing function */
+        public static JS.Thread current() { return (JS.Thread)javaThreadToJSThread.get(java.lang.Thread.currentThread()); }
+
+        public Object resume() { bind(); paused = false; Object ret = CompiledFunctionImpl.eval(this); unbind(); return ret; }
+        public void pause() { paused = true; unbind(); }
+        public void bind() { javaThreadToJSThread.put(java.lang.Thread.currentThread(), this); }
+        public void unbind() { if (current() == this) javaThreadToJSThread.remove(java.lang.Thread.currentThread()); }
+        public int getLine() { return currentCompiledFunction == null ? -1 : currentCompiledFunction.getLine(this); }
         public String getSourceName() { return currentCompiledFunction == null ? null : currentCompiledFunction.getSourceName();  }
 
         /** fetches the currently-executing javascript function */
         public JS.CompiledFunction getCurrentCompiledFunction() { return currentCompiledFunction; }
-
-
-        // Statics ///////////////////////////////////////////////////////////////////////
-
         private static Hashtable javaThreadToJSThread = new Hashtable();
-
-        /** returns the JS thread for a given Java thread, creating one if necessary */
-        public static JS.Thread fromJavaThread(java.lang.Thread t) {
-            JS.Thread ret = (JS.Thread)javaThreadToJSThread.get(t);
-            if (ret == null) {
-                ret = new JS.Thread();
-                ret.bindToCurrentJavaThread();
-            }
-            return ret;
-        }
-        
-        public static JS.Thread currentJSThread() {
-            return fromJavaThread(java.lang.Thread.currentThread());
-        }
     }
 }