2003/06/09 06:38:36
[org.ibex.core.git] / src / org / xwt / js / JS.java
index b0d54e5..d3da1f1 100644 (file)
@@ -104,7 +104,7 @@ public abstract class JS {
            try {
                return vec.elementAt(i);
            } catch (ArrayIndexOutOfBoundsException e) {
-               throw new JS.Exn(e.getMessage());
+               return null;
            }
        }
        public void put(Object key, Object val) {
@@ -137,24 +137,28 @@ public abstract class JS {
        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 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");
            Vec stack = new Vec();
            stack.push(args);
-           return bytecodes.eval(new FunctionScope(sourceName, parentScope), stack);
+           return bytecodes.eval(args == null ? parentScope : 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);
+           if (!getSourceName().equals("java")) currentFunction.put(Thread.currentThread(), this);
            try {
                return _call(args);
            } catch (ByteCodeBlock.ReturnException e) {  // ignore
@@ -169,29 +173,19 @@ public abstract class JS {
        }
     }
 
-    public static class Script extends Function {
-       Vector e = null;
-       private Script(Vector e) { this.e = e; }
-       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, new Vec());
-           return null;
-       }
-       public static Script parse(Reader r, String sourceName, int line) throws IOException {
-           Parser p = new Parser(r, sourceName, line);
-           try {
-               Vector exprs = new Vector();
-               while(true) {
-                   ByteCodeBlock ret = p.parseStatement();
-                   if (ret == null) break;
-                   exprs.addElement(ret);
-               }
-               return new Script(exprs);
-           } catch (Exception e) {
-               if (Log.on) Log.log(Parser.class, e);
-               return null;
+    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;
            }
+           return new Function(line, sourceName, b, null);
+       } catch (Exception e) {
+           if (Log.on) Log.log(Parser.class, e);
+           return null;
        }
     }