Implement JDWP ArrayType.NewInstance.

With this, you can go into the Eclipse "Display" view and evaluate
"new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".

Change-Id: Id110c5701a9de12222760be6623585b5f7e28023
diff --git a/vm/Debugger.c b/vm/Debugger.c
index dc54d76..9402911 100644
--- a/vm/Debugger.c
+++ b/vm/Debugger.c
@@ -1169,6 +1169,20 @@
 }
 
 /*
+ * Allocate a new array object of the specified type and length.  The
+ * type is the array type, not the element type.
+ *
+ * Add it to the registry to prevent it from being GCed.
+ */
+ObjectId dvmDbgCreateArrayObject(RefTypeId arrayTypeId, u4 length)
+{
+    ClassObject* clazz = refTypeIdToClassObject(arrayTypeId);
+    Object* newObj = (Object*) dvmAllocArrayByClass(clazz, length, ALLOC_DEFAULT);
+    dvmReleaseTrackedAlloc(newObj, NULL);
+    return objectToObjectId(newObj);
+}
+
+/*
  * Determine if "instClassId" is an instance of "classId".
  */
 bool dvmDbgMatchType(RefTypeId instClassId, RefTypeId classId)
diff --git a/vm/Debugger.h b/vm/Debugger.h
index 04477fb..d722160 100644
--- a/vm/Debugger.h
+++ b/vm/Debugger.h
@@ -191,6 +191,7 @@
 
 ObjectId dvmDbgCreateString(const char* str);
 ObjectId dvmDbgCreateObject(RefTypeId classId);
+ObjectId dvmDbgCreateArrayObject(RefTypeId arrayTypeId, u4 length);
 
 bool dvmDbgMatchType(RefTypeId instClassId, RefTypeId classId);
 
diff --git a/vm/jdwp/JdwpHandler.c b/vm/jdwp/JdwpHandler.c
index 69fe510..d2a657d 100644
--- a/vm/jdwp/JdwpHandler.c
+++ b/vm/jdwp/JdwpHandler.c
@@ -896,6 +896,30 @@
 }
 
 /*
+ * Create a new array object of the requested type and length.
+ */
+static JdwpError handleAT_newInstance(JdwpState* state,
+    const u1* buf, int dataLen, ExpandBuf* pReply)
+{
+    RefTypeId arrayTypeId;
+    u4 length;
+    ObjectId objectId;
+
+    arrayTypeId = dvmReadRefTypeId(&buf);
+    length = read4BE(&buf);
+
+    LOGV("Creating array %s[%u]\n",
+        dvmDbgGetClassDescriptor(arrayTypeId), length);
+    objectId = dvmDbgCreateArrayObject(arrayTypeId, length);
+    if (objectId == 0)
+        return ERR_OUT_OF_MEMORY;
+
+    expandBufAdd1(pReply, JT_ARRAY);
+    expandBufAddObjectId(pReply, objectId);
+    return ERR_NONE;
+}
+
+/*
  * Return line number information for the method, if present.
  */
 static JdwpError handleM_LineTable(JdwpState* state,
@@ -2012,7 +2036,7 @@
     { 3,    4,  handleCT_NewInstance,   "ClassType.NewInstance" },
 
     /* ArrayType command set (4) */
-    //4,    1,  NewInstance
+    { 4,    1,  handleAT_newInstance,   "ArrayType.NewInstance" },
 
     /* InterfaceType command set (5) */