2003/06/04 23:35:36
[org.ibex.core.git] / src / org / xwt / js / JS.java
index 0628891..c053881 100644 (file)
@@ -26,6 +26,23 @@ public abstract class JS {
        return "unknown:??";
     }
 
+    public static boolean toBoolean(Object o) {
+       if (o == null) return false;
+       if (o instanceof Boolean) return ((Boolean)o).booleanValue();
+       if (o instanceof Number) return o.equals(new Integer(0));
+       return true;
+    }
+    public static long toLong(Object o) { return toNumber(o).longValue(); }
+    public static double toDouble(Object o) { return toNumber(o).doubleValue(); }
+    public static Number toNumber(Object o) {
+       if (o == null) return new Long(0);
+       if (o instanceof Number) return ((Number)o);
+       if (o instanceof String) try { return new Double((String)o); } catch (NumberFormatException e) { return new Double(0); }
+       if (o instanceof Boolean) return ((Boolean)o).booleanValue() ? new Long(1) : new Long(0);
+       if (o instanceof JS) return ((JS)o).coerceToNumber();
+       // FIXME
+       throw new Error("toNumber() got object of type " + o.getClass().getName());
+    }
 
     // Instance Methods ////////////////////////////////////////////////////////////////////
  
@@ -88,7 +105,7 @@ public abstract class JS {
            }
        }
        public void put(Object key, Object val) {
-           if (key.equals("length")) vec.setSize(Parser.toNumber(val).intValue());
+           if (key.equals("length")) vec.setSize(toNumber(val).intValue());
            int i = intVal(key);
            if (i == Integer.MIN_VALUE) super.put(key, val);
            else {
@@ -121,17 +138,17 @@ 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 ((Parser.Expr)e.elementAt(0)).sourceName; }
+       public String getSourceName() throws JS.Exn { return ((ForthBlock)e.elementAt(0)).sourceName; }
        public Object _call(JS.Array args) throws JS.Exn {
            Scope rootScope = (Scope)args.elementAt(0);
            Function saved = (Function)currentFunction.get(Thread.currentThread());
            currentFunction.put(Thread.currentThread(), this);
            try {
                for(int i=0; i<e.size(); i++)
-                   ((Parser.Expr)e.elementAt(i)).eval(rootScope);
-           } catch (Parser.ReturnException e) {
+                   ((ForthBlock)e.elementAt(i)).eval(rootScope);
+           } catch (ForthBlock.ReturnException e) {
                // ignore
-           } catch (Parser.ControlTransferException e) {
+           } catch (ForthBlock.ControlTransferException e) {
                throw new JS.Exn("block threw a ControlTransferException: " + e);
            } finally {
                if (saved == null) currentFunction.remove(Thread.currentThread());
@@ -144,7 +161,7 @@ public abstract class JS {
            try {
                Vector exprs = new Vector();
                while(true) {
-                   Parser.Expr ret = p.parseBlock(false);
+                   ForthBlock ret = p.parseStatement(false);
                    if (ret == null) break;
                    exprs.addElement(ret);
                }