2003/06/12 17:57:37
[org.ibex.core.git] / src / org / xwt / js / JS.java
index 11faeaa..85189b4 100644 (file)
@@ -12,18 +12,7 @@ import java.util.*;
  */
 public abstract class JS { 
 
-
-    // Static Methods //////////////////////////////////////////////////////////////////////
-
-    private static Hashtable currentFunction = new Hashtable();
-    public static Function getCurrentFunction() { return (Function)currentFunction.get(Thread.currentThread()); }
-    public static String getCurrentFunctionSourceName() { return getCurrentFunctionSourceName(Thread.currentThread()); }
-    public static String getFileAndLine() { return getCurrentFunctionSourceName() + ":" + getCurrentFunction().getLine(); }
-    public static String getCurrentFunctionSourceName(Thread t) {
-       Function f = (Function)currentFunction.get(t);
-       if (f == null) return "null";
-       return f.getSourceName();
-    }
+    // Public Helper Methods //////////////////////////////////////////////////////////////////////
 
     public static boolean toBoolean(Object o) {
        if (o == null) return false;
@@ -78,127 +67,6 @@ public abstract class JS {
        public Object getObject() { return js; } 
     } 
 
-    /** A JavaScript Array */
-    public static class Array extends Obj {
-       private Vec vec = new Vec();
-       public Array() { }
-       public Array(int size) { vec.setSize(size); }
-       private static int intVal(Object o) {
-           if (o instanceof Number) {
-               int intVal = ((Number)o).intValue();
-               if (intVal == ((Number)o).doubleValue()) return intVal;
-               return Integer.MIN_VALUE;
-           }
-           if (!(o instanceof String)) return Integer.MIN_VALUE;
-           String s = (String)o;
-           for(int i=0; i<s.length(); i++) if (s.charAt(i) < '0' || s.charAt(i) > '9') return Integer.MIN_VALUE;
-           return Integer.parseInt(s);
-       }
-       public Object get(Object key) throws JS.Exn {
-           // FIXME: HACK!
-           if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
-           if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
-           if (key.equals("length")) return new Long(vec.size());
-           int i = intVal(key);
-           if (i == Integer.MIN_VALUE) return super.get(key);
-           try {
-               return vec.elementAt(i);
-           } catch (ArrayIndexOutOfBoundsException e) {
-               return null;
-           }
-       }
-       public void put(Object key, Object val) {
-           if (key.equals("length")) vec.setSize(toNumber(val).intValue());
-           int i = intVal(key);
-           if (i == Integer.MIN_VALUE) super.put(key, val);
-           else {
-               if (i >= vec.size()) vec.setSize(i+1);
-               vec.setElementAt(val, i);
-           }
-       }
-       public Object[] keys() {
-           Object[] sup = super.keys();
-           Object[] ret = new Object[vec.size() + 1 + sup.length];
-           System.arraycopy(sup, 0, ret, vec.size(), sup.length);
-           for(int i=0; i<vec.size(); i++) ret[i] = new Integer(i);
-           ret[vec.size()] = "length";
-           return ret;
-       }
-       public void setSize(int i) { vec.setSize(i); }
-       public int length() { return vec.size(); }
-       public Object elementAt(int i) { return vec.elementAt(i); }
-       public void addElement(Object o) { vec.addElement(o); }
-       public void setElementAt(Object o, int i) { vec.setElementAt(o, i); }
-    }
-
-    /** Anything that is callable */
-    public static class Function extends Obj {
-       ByteCodeBlock bytecodes;
-       int line;
-       String sourceName;
-       Scope parentScope;
-       public Function(int line, String sourceName, ByteCodeBlock bytecodes, Scope parentScope) {
-           this.sourceName = sourceName;
-           this.line = line;
-           this.bytecodes = bytecodes;
-           this.parentScope = parentScope;
-       }
-       public Function cloneWithNewParentScope(Scope s) {
-           if (this.getClass() != Function.class)
-               throw new Error("org.xwt.js.JS.Function.cloneWithNewParentScope() is not valid for subclasses");
-           return new Function(line, sourceName, bytecodes, s);
-       }
-       public String getSourceName() throws JS.Exn { return sourceName; }
-       public int getLine() throws JS.Exn { return line; }
-       public Object _call(JS.Array args) throws JS.Exn, ByteCodeBlock.ControlTransferException {
-           if (bytecodes == null) throw new Error("tried to call() a JS.Function with bytecodes == null");
-           Context cx = Context.getContextForCurrentThread();
-           int size = cx.stack.size();
-           cx.stack.push(new Context.CallMarker());
-           cx.stack.push(args);
-           bytecodes.eval(args == null ? parentScope : new FunctionScope(sourceName, parentScope));
-           Object ret = cx.stack.pop();
-           if (cx.stack.size() > size) {
-               Log.log(this, "warning, stack grew by " + (cx.stack.size() - size) +
-                       " elements during call to " + getSourceName() + ":" + getLine());
-               Log.log(this, "top element is " + cx.stack.peek());
-           }
-           return ret;
-       }
-       public final Object call(JS.Array args) throws JS.Exn {
-           Function saved = (Function)currentFunction.get(Thread.currentThread());
-           if (!getSourceName().equals("java")) currentFunction.put(Thread.currentThread(), this);
-           try {
-               return _call(args);
-           } catch (ByteCodeBlock.ReturnException e) {  // ignore
-               return e.retval;
-           } catch (ByteCodeBlock.ControlTransferException e) {
-               throw new RuntimeException(getSourceName() + ":" + getLine() +
-                                          " error, ControlTransferException tried to leave a function");
-           } finally {
-               if (saved == null) currentFunction.remove(Thread.currentThread());
-               else currentFunction.put(Thread.currentThread(), saved);
-           }
-       }
-    }
-
-    public static Function parse(Reader r, String sourceName, int line) throws IOException {
-       ByteCodeBlock b = new ByteCodeBlock(line, sourceName);
-       Parser p = new Parser(r, sourceName, line);
-       try {
-           while(true) {
-               int size = b.size();
-               p.parseStatement(false, b);
-               if (size == b.size()) break;
-           }
-           b.add(Tokens.RETURN);
-           return new Function(line, sourceName, b, null);
-       } catch (Exception e) {
-           if (Log.on) Log.log(Parser.class, e);
-           return null;
-       }
-    }
-
     /** Any object which becomes part of the scope chain must support this interface */ 
     public static class Scope extends Obj { 
        private Scope parentScope;
@@ -225,17 +93,16 @@ public abstract class JS {
            else super.put(s, NULL);
        }
     } 
+
+    public static CompiledFunction parse(String sourceName, int firstLine, Reader sourceCode, JS.Scope parentScope) throws IOException {
+       return new CompiledFunction(sourceName, firstLine, sourceCode, parentScope);
+    }
  
-    private class FunctionScope extends JS.Scope {
-       String sourceName;
-       public FunctionScope(String sourceName, Scope parentScope) { super(parentScope); this.sourceName = sourceName; }
-       public String getSourceName() { return sourceName; }
-       public Object get(Object key) throws JS.Exn {
-           if (key.equals("trapee")) return org.xwt.Trap.currentTrapee();
-           else if (key.equals("cascade")) return org.xwt.Trap.cascadeFunction;
-           return super.get(key);
-       }
+    /** anything that is callable with the () operator */
+    public static abstract class Callable extends JS.Obj {
+       public abstract Object call(Array args) throws JS.Exn;
     }
+
 }