J_Class.c
J_Error_t jGetClass(J_Class** c,J_UtfString name,J_Thread* currentThread){
  J_Object  obj;
  J_ClassMethods* method = NULL;
  uint8_t*  p;
  uint32_t  idx,len;
  J_Error_t r;

 /* Lookup in the hash table */
  idx = jUtfHash(name) % MAX_CLASS_HASH;
  (*c) = globalHash[idx];
  while(*c){
	J_UtfString tmp = jGetClassName((*c),(*c)->thisClassIdx);
	if(tmp.len==name.len && !strncmp(name.str,tmp.str,name.len)) break;
	(*c) = (*c)->nextClass;
  }
  if(*c) return EOK;
  
 /* try to allocate memory */
  (*c) = (J_Class*) calloc(1,sizeof(J_Class));
  if(!(*c)){
	dbg(DBG_MEMORY,"error allocating memory for class");
	return ENOMEM;
  }

 /* try allocate object */
  if((obj=allocateObject((*c),1))<0){
	dbg(DBG_MEMORY,"error allocate object");
	return ENOMEM;
  }
  
 /* backward link */
  (*c)->obj = obj;

 /* load class */
  p = (uint8_t*) (loadFile(name,&len)).pc;
  if(p==NULL || len==0)
	return ECLASSNOTFOUND;

 /* clear superclass pointer */
  (*c)->superClass = 0;

 /* where is allocated into memory */
  (*c)->classmem = p;

 /* parse constants */
  if(jParseConstants((*c),p,&len)!=EOK)
	return ECONSTSFAIL;
  p += len;

 /* get access_flags section */
  (*c)->accessFlags = (uint16_t*) p;
  p += 2;

 /* get this class index */
  (*c)->thisClassIdx = getUint16(p);
  p += 2;

 /* add to hash table */
  (*c)->nextClass = globalHash[idx];
  globalHash[idx] = (*c);

 /* get super_class index */
  (*c)->numberSuperClasses = 0;
  (*c)->superClassIdx = getUint16(p);
  p += 2;
  
 /* get interfaces count and parse */
  (*c)->interfacesCount = getUint16(p); 
  p += 2; 
  len = 0;
  if((*c)->interfacesCount>0)
	if(jParseInterfaces((*c),p,&len)!=EOK)
	  return EINTERFACEFAIL;
  if(len) p += len;

 /* get fields count and parse */
  (*c)->fieldsCount = getUint16(p); 
  p += 2; 
  len = 0;
  if((*c)->fieldsCount>0)
	if(jParseFields((*c),p,&len)!=EOK)
	  return EFIELDSFAIL;
  if(len) p += len;

 /* get methods count and parse */
  (*c)->methodsCount = getUint16(p); 
  p += 2;
  len = 0;
  if(jParseMethods((*c),p,&len)!=EOK)
	return EMETHODSFAIL;
  p += len;

 /* try to load superclasses */
  if((*c)->superClassIdx>0){
	r = jGetClassByIndex((*c),&((*c)->superClass),(*c)->superClassIdx,currentThread);
	if(r!=EOK) return r;
	else {
	  (*c)->numberSuperClasses++;
	}
  }	

 /* find class init <clinit> method : static{...} */
  r = jGetMethod((*c),jUtfCreate("<clinit>"),jUtfCreate("()V"),NULL,&method);
 	
 /* and try to execute it */
  if(r==EOK && method!=NULL){
	jExecMethod((*c),method,NULL,0,currentThread);
  }
  
  return EOK;
}
/**
 *  Function for loading class file by given name
 */
Function for loading class file by given name
Function for loading class file by given name
J_Error_t jGetClass(J_Class** c,J_UtfString name,J_Thread* currentThread){
  J_Object  obj;
  J_ClassMethods* method = NULL;
  uint8_t*  p;
  uint32_t  idx,len;
  J_Error_t r;
  idx = jUtfHash(name) % MAX_CLASS_HASH;
  (*c) = globalHash[idx];
  while(*c){
	J_UtfString tmp = jGetClassName((*c),(*c)->thisClassIdx);
	if(tmp.len==name.len && !strncmp(name.str,tmp.str,name.len)) break;
	(*c) = (*c)->nextClass;
  }
  if(*c) return EOK;
  (*c) = (J_Class*) calloc(1,sizeof(J_Class));
  if(!(*c)){
	dbg(DBG_MEMORY,"error allocating memory for class");
	return ENOMEM;
  }
  if((obj=allocateObject((*c),1))<0){
	dbg(DBG_MEMORY,"error allocate object");
	return ENOMEM;
  }
  (*c)->obj = obj;
  p = (uint8_t*) (loadFile(name,&len)).pc;
  if(p==NULL || len==0)
	return ECLASSNOTFOUND;
  (*c)->superClass = 0;
  (*c)->classmem = p;
  if(jParseConstants((*c),p,&len)!=EOK)
	return ECONSTSFAIL;
  p += len;
  (*c)->accessFlags = (uint16_t*) p;
  p += 2;
  (*c)->thisClassIdx = getUint16(p);
  p += 2;
  (*c)->nextClass = globalHash[idx];
  globalHash[idx] = (*c);
  (*c)->numberSuperClasses = 0;
  (*c)->superClassIdx = getUint16(p);
  p += 2;
  (*c)->interfacesCount = getUint16(p); 
  p += 2; 
  len = 0;
  if((*c)->interfacesCount>0)
	if(jParseInterfaces((*c),p,&len)!=EOK)
	  return EINTERFACEFAIL;
  if(len) p += len;
  (*c)->fieldsCount = getUint16(p); 
  p += 2; 
  len = 0;
  if((*c)->fieldsCount>0)
	if(jParseFields((*c),p,&len)!=EOK)
	  return EFIELDSFAIL;
  if(len) p += len;
  (*c)->methodsCount = getUint16(p); 
  p += 2;
  len = 0;
  if(jParseMethods((*c),p,&len)!=EOK)
	return EMETHODSFAIL;
  p += len;
  if((*c)->superClassIdx>0){
	r = jGetClassByIndex((*c),&((*c)->superClass),(*c)->superClassIdx,currentThread);
	if(r!=EOK) return r;
	else {
	  (*c)->numberSuperClasses++;
	}
  }	
  r = jGetMethod((*c),jUtfCreate("<clinit>"),jUtfCreate("()V"),NULL,&method);
  if(r==EOK && method!=NULL){
	jExecMethod((*c),method,NULL,0,currentThread);
  }
  return EOK;
}
J_Error_t jGetClass ( J_Class * * c , J_UtfString name , J_Thread * currentThread ) { J_Object obj ; J_ClassMethods * method = NULL ; uint8_t * p ; uint32_t idx , len ; J_Error_t r ; idx = jUtfHash ( name ) % MAX_CLASS_HASH ; ( * c ) = globalHash [ idx ] ; while ( * c ) { J_UtfString tmp = jGetClassName ( ( * c ) , ( * c ) -> thisClassIdx ) ; if ( tmp . len == name . len && ! strncmp ( name . str , tmp . str , name . len ) ) break ; ( * c ) = ( * c ) -> nextClass ; } if ( * c ) return EOK ; ( * c ) = ( J_Class * ) calloc ( 1 , sizeof ( J_Class ) ) ; if ( ! ( * c ) ) { dbg ( DBG_MEMORY , " " ) ; return ENOMEM ; } if ( ( obj = allocateObject ( ( * c ) , 1 ) ) < 0 ) { dbg ( DBG_MEMORY , " " ) ; return ENOMEM ; } ( * c ) -> obj = obj ; p = ( uint8_t * ) ( loadFile ( name , & len ) ) . pc ; if ( p == NULL || len == 0 ) return ECLASSNOTFOUND ; ( * c ) -> superClass = 0 ; ( * c ) -> classmem = p ; if ( jParseConstants ( ( * c ) , p , & len ) != EOK ) return ECONSTSFAIL ; p += len ; ( * c ) -> accessFlags = ( uint16_t * ) p ; p += 2 ; ( * c ) -> thisClassIdx = getUint16 ( p ) ; p += 2 ; ( * c ) -> nextClass = globalHash [ idx ] ; globalHash [ idx ] = ( * c ) ; ( * c ) -> numberSuperClasses = 0 ; ( * c ) -> superClassIdx = getUint16 ( p ) ; p += 2 ; ( * c ) -> interfacesCount = getUint16 ( p ) ; p += 2 ; len = 0 ; if ( ( * c ) -> interfacesCount > 0 ) if ( jParseInterfaces ( ( * c ) , p , & len ) != EOK ) return EINTERFACEFAIL ; if ( len ) p += len ; ( * c ) -> fieldsCount = getUint16 ( p ) ; p += 2 ; len = 0 ; if ( ( * c ) -> fieldsCount > 0 ) if ( jParseFields ( ( * c ) , p , & len ) != EOK ) return EFIELDSFAIL ; if ( len ) p += len ; ( * c ) -> methodsCount = getUint16 ( p ) ; p += 2 ; len = 0 ; if ( jParseMethods ( ( * c ) , p , & len ) != EOK ) return EMETHODSFAIL ; p += len ; if ( ( * c ) -> superClassIdx > 0 ) { r = jGetClassByIndex ( ( * c ) , & ( ( * c ) -> superClass ) , ( * c ) -> superClassIdx , currentThread ) ; if ( r != EOK ) return r ; else { ( * c ) -> numberSuperClasses ++ ; } } r = jGetMethod ( ( * c ) , jUtfCreate ( " " ) , jUtfCreate ( " " ) , NULL , & method ) ; if ( r == EOK && method != NULL ) { jExecMethod ( ( * c ) , method , NULL , 0 , currentThread ) ; } return EOK ; }
Function for loading class file by given name
Function for loading class file by given name
/* Lookup in the hash table */ /* try to allocate memory */ /* try allocate object */ /* backward link */ /* load class */ /* clear superclass pointer */ /* where is allocated into memory */ /* parse constants */ /* get access_flags section */ /* get this class index */ /* add to hash table */ /* get super_class index */ /* get interfaces count and parse */ /* get fields count and parse */ /* get methods count and parse */ /* try to load superclasses */ /* find class init <clinit> method : static{...} */ /* and try to execute it */