X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSReflection.java;h=4712d0829616ac93292b68dda0de78710cf42994;hb=aaf060ff38871512d80f1bbf04a6596648b284c7;hp=69fb9b37b55846468f80bf47a87c48432ed61bc8;hpb=b1fa73c17b31f268fca5695d0876d7314fbacce3;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSReflection.java b/src/org/ibex/js/JSReflection.java index 69fb9b3..4712d08 100644 --- a/src/org/ibex/js/JSReflection.java +++ b/src/org/ibex/js/JSReflection.java @@ -1,80 +1,85 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.js; -import org.ibex.util.*; -import java.io.*; -import java.util.*; import java.lang.reflect.*; /** Automatic JS-ification via Reflection (not for use in the core) */ -public class JSReflection extends JS { +public class JSReflection extends JS.Immutable { + private static final JS.Method METHOD = new JS.Method(); - public static Object wrap(Object o) throws JSExn { + public static JS wrap(Object o) throws JSExn { 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 JSU.S((String)o); + if (o instanceof Boolean) return JSU.B(((Boolean)o).booleanValue()); + if (o instanceof Number) return JSU.N((Number)o); + if (o instanceof JS) return (JS)o; if (o instanceof Object[]) { // FIXME: get element type here } throw new JSExn("Reflection object tried to return a " + o.getClass().getName()); } - public static class Array extends JS { + public static class Array extends JS.Immutable { 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 _hasNext() { return n < arr.length; } + public JS _next() { return JSU.N(n++); } + }; + } + public JS get(JS key) throws JSExn { return wrap(arr[JSU.toInt(key)]); } } // FIXME public static class Hash { } // FIXME public Enumeration keys() throws JSExn { } - public Object get(Object key) throws JSExn { - String k = toString(key); - try { - Field f = this.getClass().getField(k); - return wrap(f.get(this)); - } catch (NoSuchFieldException nfe) { - } catch (IllegalAccessException nfe) { - } catch (SecurityException nfe) { } + public JS get(JS key) throws JSExn { + String k = JSU.toString(key); + Class c = this.getClass(); + while(c != null) { + try { + Field f = c.getField(k); + if (f != null) return wrap(f.get(this)); + } catch (NoSuchFieldException nfe) { + } catch (IllegalAccessException nfe) { + } catch (SecurityException nfe) { } + c = c.getSuperclass(); + } try { - Method[] methods = this.getClass().getMethods(); + java.lang.reflect.Method[] methods = this.getClass().getMethods(); for(int i=0; i