+/******************* Function: class_new_array *********************************
+
+ This function is called by class_new to setup an array class.
+
+*******************************************************************************/
+
+void class_new_array(classinfo *c)
+{
+ classinfo *comp = NULL;
+ methodinfo *clone;
+ int namelen;
+
+ /* Check array class name */
+ namelen = c->name->blength;
+ if (namelen < 2 || c->name->text[0] != '[')
+ panic("Invalid array class name");
+
+ /* Check the component type */
+ switch (c->name->text[1]) {
+ case '[':
+ /* c is an array of arrays. We have to create the component class. */
+ if (opt_eager) {
+ comp = class_new_intern(utf_new_intern(c->name->text + 1,
+ namelen - 1));
+ class_load(comp);
+ list_addfirst(&unlinkedclasses, comp);
+
+ } else {
+ comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
+ }
+ break;
+
+ case 'L':
+ /* c is an array of objects. */
+ if (namelen < 4 || c->name->text[namelen - 1] != ';')
+ panic("Invalid array class name");
+
+ if (opt_eager) {
+ comp = class_new_intern(utf_new_intern(c->name->text + 2,
+ namelen - 3));
+ class_load(comp);
+ list_addfirst(&unlinkedclasses, comp);
+
+ } else {
+ comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
+ }
+ break;
+ }
+
+ /* Setup the array class */
+ c->super = class_java_lang_Object;
+ c->flags = ACC_PUBLIC | ACC_FINAL | ACC_ABSTRACT;
+
+ c->interfacescount = 2;
+ c->interfaces = MNEW(classinfo*, 2);
+
+ if (opt_eager) {
+ classinfo *tc;
+
+ tc = class_new_intern(utf_new_char("java/lang/Cloneable"));
+ class_load(tc);
+ list_addfirst(&unlinkedclasses, tc);
+ c->interfaces[0] = tc;
+
+ tc = class_new_intern(utf_new_char("java/io/Serializable"));
+ class_load(tc);
+ list_addfirst(&unlinkedclasses, tc);
+ c->interfaces[1] = tc;
+
+ } else {
+ c->interfaces[0] = class_new(utf_new_char("java/lang/Cloneable"));
+ c->interfaces[1] = class_new(utf_new_char("java/io/Serializable"));
+ }
+
+ c->methodscount = 1;
+ c->methods = MNEW(methodinfo, c->methodscount);
+
+ clone = c->methods;
+ memset(clone, 0, sizeof(methodinfo));
+ clone->flags = ACC_PUBLIC;
+ clone->name = utf_new_char("clone");
+ clone->descriptor = utf_new_char("()Ljava/lang/Object;");
+ clone->class = c;
+ clone->stubroutine = createnativestub((functionptr) &builtin_clone_array, clone);
+ clone->monoPoly = MONO;
+
+ /* XXX: field: length? */
+
+ /* array classes are not loaded from class files */
+ c->loaded = true;
+}
+
+
+/****************** Function: class_link_array *********************************
+
+ This function is called by class_link to create the
+ arraydescriptor for an array class.
+
+ This function returns NULL if the array cannot be linked because
+ the component type has not been linked yet.
+
+*******************************************************************************/
+
+static arraydescriptor *class_link_array(classinfo *c)
+{
+ classinfo *comp = NULL;
+ s4 namelen = c->name->blength;
+ arraydescriptor *desc;
+ vftbl_t *compvftbl;
+
+ /* Check the component type */
+ switch (c->name->text[1]) {
+ case '[':
+ /* c is an array of arrays. */
+ comp = class_new(utf_new_intern(c->name->text + 1, namelen - 1));
+ if (!comp)
+ panic("Could not find component array class.");
+ break;
+
+ case 'L':
+ /* c is an array of objects. */
+ comp = class_new(utf_new_intern(c->name->text + 2, namelen - 3));
+ if (!comp)
+ panic("Could not find component class.");
+ break;
+ }
+
+ /* If the component type has not been linked, link it now */
+ if (comp && !comp->linked) {
+ if (!comp->loaded)
+ if (!class_load(comp))
+ return NULL;
+
+ if (!class_link(comp))
+ return NULL;
+ }
+
+ /* Allocate the arraydescriptor */
+ desc = NEW(arraydescriptor);
+
+ if (comp) {
+ /* c is an array of references */
+ desc->arraytype = ARRAYTYPE_OBJECT;
+ desc->componentsize = sizeof(void*);
+ desc->dataoffset = OFFSET(java_objectarray, data);
+
+ compvftbl = comp->vftbl;
+ if (!compvftbl)
+ panic("Component class has no vftbl");
+ desc->componentvftbl = compvftbl;
+
+ if (compvftbl->arraydesc) {
+ desc->elementvftbl = compvftbl->arraydesc->elementvftbl;
+ if (compvftbl->arraydesc->dimension >= 255)
+ panic("Creating array of dimension >255");
+ desc->dimension = compvftbl->arraydesc->dimension + 1;
+ desc->elementtype = compvftbl->arraydesc->elementtype;
+
+ } else {
+ desc->elementvftbl = compvftbl;
+ desc->dimension = 1;
+ desc->elementtype = ARRAYTYPE_OBJECT;
+ }
+
+ } else {
+ /* c is an array of a primitive type */
+ switch (c->name->text[1]) {
+ case 'Z':
+ desc->arraytype = ARRAYTYPE_BOOLEAN;
+ desc->dataoffset = OFFSET(java_booleanarray,data);
+ desc->componentsize = sizeof(u1);
+ break;
+
+ case 'B':
+ desc->arraytype = ARRAYTYPE_BYTE;
+ desc->dataoffset = OFFSET(java_bytearray,data);
+ desc->componentsize = sizeof(u1);
+ break;
+
+ case 'C':
+ desc->arraytype = ARRAYTYPE_CHAR;
+ desc->dataoffset = OFFSET(java_chararray,data);
+ desc->componentsize = sizeof(u2);
+ break;
+
+ case 'D':
+ desc->arraytype = ARRAYTYPE_DOUBLE;
+ desc->dataoffset = OFFSET(java_doublearray,data);
+ desc->componentsize = sizeof(double);
+ break;
+
+ case 'F':
+ desc->arraytype = ARRAYTYPE_FLOAT;
+ desc->dataoffset = OFFSET(java_floatarray,data);
+ desc->componentsize = sizeof(float);
+ break;
+
+ case 'I':
+ desc->arraytype = ARRAYTYPE_INT;
+ desc->dataoffset = OFFSET(java_intarray,data);
+ desc->componentsize = sizeof(s4);
+ break;
+
+ case 'J':
+ desc->arraytype = ARRAYTYPE_LONG;
+ desc->dataoffset = OFFSET(java_longarray,data);
+ desc->componentsize = sizeof(s8);
+ break;
+
+ case 'S':
+ desc->arraytype = ARRAYTYPE_SHORT;
+ desc->dataoffset = OFFSET(java_shortarray,data);
+ desc->componentsize = sizeof(s2);
+ break;
+
+ default:
+ panic("Invalid array class name");
+ }
+
+ desc->componentvftbl = NULL;
+ desc->elementvftbl = NULL;
+ desc->dimension = 1;
+ desc->elementtype = desc->arraytype;
+ }
+
+ return desc;
+}
+
+