]> granicus.if.org Git - php/commitdiff
Complete the work mapping arrays and hashtables
authorSam Ruby <rubys@php.net>
Sat, 22 Jul 2000 20:36:11 +0000 (20:36 +0000)
committerSam Ruby <rubys@php.net>
Sat, 22 Jul 2000 20:36:11 +0000 (20:36 +0000)
ext/java/README
ext/java/java.c
ext/java/reflect.java
ext/rpc/java/README
ext/rpc/java/java.c
ext/rpc/java/reflect.java

index 097bfabf1a304573e548c5e53e469135951e6a51..52dadd0a899f6ea24b7048c1f9d049769f8931a7 100644 (file)
@@ -40,6 +40,12 @@ What is PHP4 ext/java?
        possibly with a loss of data (example: double precision floating point
        numbers will be converted to boolean).
 
+     6) In the tradition of PHP, arrays and hashtables may pretty much
+        be used interchangably.  Note that hashtables in PHP may only be
+        indexed by integers or strings; and that arrays of primitive types
+        in Java can not be sparse.  Also note that these constructs are
+        passed by value, so may be expensive in terms of memory and time.
+
 Build and execution instructions:
 
    Given the number of platforms and providers of JVMs, no single set of
index c56bff5bdcbc0e9a87ee697c7c404812c8a522ef..ab9dcf172e3789114a909572eb449655873bccf8 100644 (file)
@@ -730,6 +730,17 @@ JNIEXPORT jlong JNICALL Java_net_php_reflect_nextElement
   return (jlong)(long)result;
 }
 
+JNIEXPORT jlong JNICALL Java_net_php_reflect_hashIndexUpdate
+  (JNIEnv *jenv, jclass self, jlong array, jlong key)
+{
+  pval *result;
+  pval *handle = (pval*)(long)array;
+  ALLOC_ZVAL(result);
+  zend_hash_index_update(handle->value.ht, (unsigned long)key, 
+    &result, sizeof(zval *), NULL);
+  return (jlong)(long)result;
+}
+
 JNIEXPORT jlong JNICALL Java_net_php_reflect_hashUpdate
   (JNIEnv *jenv, jclass self, jlong array, jbyteArray key)
 {
index d5e9393dc2131d9f99ea95f6a73dee50419718c0..3520b2aba80f5619decdeaabc15933cb4288242d 100644 (file)
@@ -46,6 +46,7 @@ public class reflect {
   private static native void setResultFromArray(long result);
   private static native long nextElement(long array);
   private static native long hashUpdate(long array, byte key[]);
+  private static native long hashIndexUpdate(long array, long key);
   private static native void setException(long result, byte value[]);
   public  static native void setEnv();
 
@@ -88,7 +89,13 @@ public class reflect {
       setResultFromArray(result);
       for (Enumeration e = ht.keys(); e.hasMoreElements(); ) {
         Object key = e.nextElement();
-        setResult(hashUpdate(result, key.toString().getBytes()), ht.get(key));
+        long slot;
+        if (key instanceof Number && 
+            !(key instanceof Double || key instanceof Float))
+          slot = hashIndexUpdate(result, ((Number)key).longValue());
+        else
+          slot = hashUpdate(result, key.toString().getBytes());
+        setResult(slot, ht.get(key));
       }
 
     } else {
@@ -176,8 +183,13 @@ public class reflect {
             if (!c.isInstance(args[i])) break;
             weight++;
           }
-        } else if (parms[i].isInstance("")) {
-         if (!(args[i] instanceof byte[]))
+        } else if (parms[i].isAssignableFrom(java.lang.String.class)) {
+         if (!(args[i] instanceof byte[]) && !(args[i] instanceof String))
+           weight+=9999;
+        } else if (parms[i].isArray()) {
+         if (args[i] instanceof java.util.Hashtable)
+           weight+=256;
+          else
            weight+=9999;
         } else if (parms[i].isPrimitive()) {
           Class c=parms[i];
@@ -235,6 +247,42 @@ public class reflect {
         if (c == Float.TYPE)   result[i]=new Float(n.floatValue());
         if (c == Long.TYPE && !(n instanceof Long)) 
           result[i]=new Long(n.longValue());
+      } else if (args[i] instanceof Hashtable && parms[i].isArray()) {
+        try {
+          Hashtable ht = (Hashtable)args[i];
+          int size = ht.size();
+
+          // Verify that the keys are Long, and determine maximum
+          for (Enumeration e = ht.keys(); e.hasMoreElements(); ) {
+            int index = ((Long)e.nextElement()).intValue();
+            if (index >= size) size = index+1;
+          }
+
+          Object tempArray[] = new Object[size];
+          Class tempTarget[] = new Class[size];
+          Class targetType = parms[i].getComponentType();
+
+          // flatten the hash table into an array
+          for (int j=0; j<size; j++) {
+            tempArray[j] = ht.get(new Long(j));
+            if (tempArray[j] == null && targetType.isPrimitive()) 
+              throw new Exception("bail");
+            tempTarget[j] = targetType;
+          }
+
+          // coerce individual elements into the target type
+          Object coercedArray[] = coerce(tempTarget, tempArray);
+        
+          // copy the results into the desired array type
+          Object array = Array.newInstance(targetType,size);
+          for (int j=0; j<size; j++) {
+            Array.set(array, j, coercedArray[j]);
+          }
+
+          result[i]=array;
+        } catch (Exception e) {
+          // leave result[i] alone...
+        }
       }
     }
     return result;
index 097bfabf1a304573e548c5e53e469135951e6a51..52dadd0a899f6ea24b7048c1f9d049769f8931a7 100644 (file)
@@ -40,6 +40,12 @@ What is PHP4 ext/java?
        possibly with a loss of data (example: double precision floating point
        numbers will be converted to boolean).
 
+     6) In the tradition of PHP, arrays and hashtables may pretty much
+        be used interchangably.  Note that hashtables in PHP may only be
+        indexed by integers or strings; and that arrays of primitive types
+        in Java can not be sparse.  Also note that these constructs are
+        passed by value, so may be expensive in terms of memory and time.
+
 Build and execution instructions:
 
    Given the number of platforms and providers of JVMs, no single set of
index c56bff5bdcbc0e9a87ee697c7c404812c8a522ef..ab9dcf172e3789114a909572eb449655873bccf8 100644 (file)
@@ -730,6 +730,17 @@ JNIEXPORT jlong JNICALL Java_net_php_reflect_nextElement
   return (jlong)(long)result;
 }
 
+JNIEXPORT jlong JNICALL Java_net_php_reflect_hashIndexUpdate
+  (JNIEnv *jenv, jclass self, jlong array, jlong key)
+{
+  pval *result;
+  pval *handle = (pval*)(long)array;
+  ALLOC_ZVAL(result);
+  zend_hash_index_update(handle->value.ht, (unsigned long)key, 
+    &result, sizeof(zval *), NULL);
+  return (jlong)(long)result;
+}
+
 JNIEXPORT jlong JNICALL Java_net_php_reflect_hashUpdate
   (JNIEnv *jenv, jclass self, jlong array, jbyteArray key)
 {
index d5e9393dc2131d9f99ea95f6a73dee50419718c0..3520b2aba80f5619decdeaabc15933cb4288242d 100644 (file)
@@ -46,6 +46,7 @@ public class reflect {
   private static native void setResultFromArray(long result);
   private static native long nextElement(long array);
   private static native long hashUpdate(long array, byte key[]);
+  private static native long hashIndexUpdate(long array, long key);
   private static native void setException(long result, byte value[]);
   public  static native void setEnv();
 
@@ -88,7 +89,13 @@ public class reflect {
       setResultFromArray(result);
       for (Enumeration e = ht.keys(); e.hasMoreElements(); ) {
         Object key = e.nextElement();
-        setResult(hashUpdate(result, key.toString().getBytes()), ht.get(key));
+        long slot;
+        if (key instanceof Number && 
+            !(key instanceof Double || key instanceof Float))
+          slot = hashIndexUpdate(result, ((Number)key).longValue());
+        else
+          slot = hashUpdate(result, key.toString().getBytes());
+        setResult(slot, ht.get(key));
       }
 
     } else {
@@ -176,8 +183,13 @@ public class reflect {
             if (!c.isInstance(args[i])) break;
             weight++;
           }
-        } else if (parms[i].isInstance("")) {
-         if (!(args[i] instanceof byte[]))
+        } else if (parms[i].isAssignableFrom(java.lang.String.class)) {
+         if (!(args[i] instanceof byte[]) && !(args[i] instanceof String))
+           weight+=9999;
+        } else if (parms[i].isArray()) {
+         if (args[i] instanceof java.util.Hashtable)
+           weight+=256;
+          else
            weight+=9999;
         } else if (parms[i].isPrimitive()) {
           Class c=parms[i];
@@ -235,6 +247,42 @@ public class reflect {
         if (c == Float.TYPE)   result[i]=new Float(n.floatValue());
         if (c == Long.TYPE && !(n instanceof Long)) 
           result[i]=new Long(n.longValue());
+      } else if (args[i] instanceof Hashtable && parms[i].isArray()) {
+        try {
+          Hashtable ht = (Hashtable)args[i];
+          int size = ht.size();
+
+          // Verify that the keys are Long, and determine maximum
+          for (Enumeration e = ht.keys(); e.hasMoreElements(); ) {
+            int index = ((Long)e.nextElement()).intValue();
+            if (index >= size) size = index+1;
+          }
+
+          Object tempArray[] = new Object[size];
+          Class tempTarget[] = new Class[size];
+          Class targetType = parms[i].getComponentType();
+
+          // flatten the hash table into an array
+          for (int j=0; j<size; j++) {
+            tempArray[j] = ht.get(new Long(j));
+            if (tempArray[j] == null && targetType.isPrimitive()) 
+              throw new Exception("bail");
+            tempTarget[j] = targetType;
+          }
+
+          // coerce individual elements into the target type
+          Object coercedArray[] = coerce(tempTarget, tempArray);
+        
+          // copy the results into the desired array type
+          Object array = Array.newInstance(targetType,size);
+          for (int j=0; j<size; j++) {
+            Array.set(array, j, coercedArray[j]);
+          }
+
+          result[i]=array;
+        } catch (Exception e) {
+          // leave result[i] alone...
+        }
       }
     }
     return result;