2003/06/09 00:07:40
[org.ibex.core.git] / src / org / xwt / js / JS.java
index ea117c4..6c9448d 100644 (file)
@@ -132,10 +132,26 @@ public abstract class JS {
     }
 
     /** Anything that is callable */
-    public static abstract class Function extends Obj {
-       public String getSourceName() throws JS.Exn { return "unknown"; }
-       public int getLine() throws JS.Exn { return -1; }
-       public abstract Object _call(JS.Array args) throws JS.Exn, ByteCodeBlock.ControlTransferException;
+    public static class Function extends Obj {
+       ByteCodeBlock bytecodes;
+       int line;
+       String sourceName;
+       Scope parentScope;
+       public Function() { this(-1, "unknown", null, null); }
+       public Function(int line, String sourceName, ByteCodeBlock bytecodes, Scope parentScope) {
+           this.sourceName = sourceName;
+           this.line = line;
+           this.bytecodes = bytecodes;
+           this.parentScope = parentScope;
+       }
+       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");
+           Vec stack = new Vec();
+           stack.push(args);
+           return bytecodes.eval(new FunctionScope(sourceName, parentScope), stack);
+       }
        public final Object call(JS.Array args) throws JS.Exn {
            Function saved = (Function)currentFunction.get(Thread.currentThread());
            currentFunction.put(Thread.currentThread(), this);
@@ -159,7 +175,7 @@ public abstract class JS {
        public String getSourceName() throws JS.Exn { return ((ByteCodeBlock)e.elementAt(0)).getSourceName(); }
        public Object _call(JS.Array args) throws JS.Exn, ByteCodeBlock.ControlTransferException {
            Scope rootScope = (Scope)args.elementAt(0);
-           for(int i=0; i<e.size(); i++) ((ByteCodeBlock)e.elementAt(i)).eval(rootScope);
+           for(int i=0; i<e.size(); i++) ((ByteCodeBlock)e.elementAt(i)).eval(rootScope, new Vec());
            return null;
        }
        public static Script parse(Reader r, String sourceName, int line) throws IOException {
@@ -206,7 +222,16 @@ public abstract class JS {
        }
     } 
  
-
+    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);
+       }
+    }
 }