update JSReflection
authorbrian <brian@brianweb.net>
Tue, 13 Jul 2004 05:21:46 +0000 (05:21 +0000)
committerbrian <brian@brianweb.net>
Tue, 13 Jul 2004 05:21:46 +0000 (05:21 +0000)
darcs-hash:20040713052146-24bed-2d92939f71dfb4176ba1679922695ec7a9f28689.gz

src/org/ibex/js/JSReflection.java

index d2a9d14..52f7f9c 100644 (file)
@@ -6,16 +6,15 @@ import java.io.*;
 import java.util.*;
 import java.lang.reflect.*;
 
 import java.util.*;
 import java.lang.reflect.*;
 
-// FIXME: Update for new API
 /** Automatic JS-ification via Reflection (not for use in the core) */
 public class JSReflection extends JS {
 
 /** Automatic JS-ification via Reflection (not for use in the core) */
 public class JSReflection extends JS {
 
-    /*public static Object wrap(Object o) throws JSExn {
+    public static JS wrap(Object o) throws JSExn {
         if (o == null) return null;
         if (o == null) return null;
-        if (o instanceof String) return o;
-        if (o instanceof Boolean) return o;
-        if (o instanceof Number) return o;
-        if (o instanceof JS) return o;
+        if (o instanceof String) return JS.S((String)o);
+        if (o instanceof Boolean) return JS.B(((Boolean)o).booleanValue());
+        if (o instanceof Number) return JS.N((Number)o);
+        if (o instanceof JS) return (JS)o;
         if (o instanceof Object[]) {
             // FIXME: get element type here
         }
         if (o instanceof Object[]) {
             // FIXME: get element type here
         }
@@ -25,15 +24,24 @@ public class JSReflection extends JS {
     public static class Array extends JS {
         final Object[] arr;
         public Array(Object[] arr) { this.arr = arr; }
     public static class Array extends JS {
         final Object[] arr;
         public Array(Object[] arr) { this.arr = arr; }
-        public Enumeration keys() throws JSExn { return new CounterEnumeration(arr.length); }
-        public Object get(Object key) throws JSExn { return wrap(arr[toInt(key)]); }
-        public void put(Object key, Object val) throws JSExn { throw new JSExn("can't write to org.ibex.js.Reflection.Array's"); }
+        // FEATURE: Add a JSCounterEnumeration
+        public Enumeration keys() throws JSExn {
+            return new Enumeration(null) {
+                private int n = 0;
+                public boolean _hasMoreElements() { return n < arr.length; }
+                public JS _nextElement() {
+                    return n >= arr.length ? null : JS.N(n++);
+                }
+            };
+        }
+        public JS get(JS key) throws JSExn { return wrap(arr[toInt(key)]); }
+        public void put(JS key, JS val) throws JSExn { throw new JSExn("can't write to org.ibex.js.Reflection.Array's"); }
     }
 
     // FIXME public static class Hash { }
     // FIXME public Enumeration keys() throws JSExn {  }
 
     }
 
     // FIXME public static class Hash { }
     // FIXME public Enumeration keys() throws JSExn {  }
 
-    public Object get(Object key) throws JSExn {
+    public JS get(JS key) throws JSExn {
         String k = toString(key);
         try {
             Field f = this.getClass().getField(k);
         String k = toString(key);
         try {
             Field f = this.getClass().getField(k);
@@ -49,11 +57,11 @@ public class JSReflection extends JS {
         return null;
     }
 
         return null;
     }
 
-    public void put(Object key, Object val) throws JSExn {
+    public void put(JS key, JS val) throws JSExn {
         throw new JSExn("put() not supported yet");
     }
 
         throw new JSExn("put() not supported yet");
     }
 
-    public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
+    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
         String k = toString(method);
         try {
             Method[] methods = this.getClass().getMethods();
         String k = toString(method);
         try {
             Method[] methods = this.getClass().getMethods();
@@ -77,5 +85,5 @@ public class JSReflection extends JS {
             throw new JSExn("unhandled reflected exception: " + ite.toString());
         } catch (SecurityException nfe) { }
         throw new JSExn("called a reflection method with the wrong number of arguments");
             throw new JSExn("unhandled reflected exception: " + ite.toString());
         } catch (SecurityException nfe) { }
         throw new JSExn("called a reflection method with the wrong number of arguments");
-    }*/
+    }
 } 
 }