@@ -309,7 +309,6 @@ struct class_entry * class_resolver_lookup_class_from_class_index(int class_hash
309
309
struct parse_type_ret parse_type_ret = parse_type (class_name_constant -> utf8 .bytes ,
310
310
class_name_constant -> utf8 .length );
311
311
312
-
313
312
struct class_entry * _class_entry = class_resolver_lookup_class (class_hash_table_length ,
314
313
class_hash_table ,
315
314
parse_type_ret .bytes ,
@@ -586,10 +585,10 @@ struct method_entry class_resolver_lookup_method_from_method_name_method_descrip
586
585
}
587
586
}
588
587
589
- int32_t * class_resolver_lookup_string (int class_hash_table_length ,
590
- struct hash_table_entry * class_hash_table ,
591
- struct class_entry * class_entry ,
592
- const int string_index )
588
+ struct objectref * class_resolver_lookup_string (int class_hash_table_length ,
589
+ struct hash_table_entry * class_hash_table ,
590
+ struct class_entry * class_entry ,
591
+ const int string_index )
593
592
{
594
593
debugf ("class_resolver_lookup_string: %d\n" , string_index );
595
594
if (class_entry -> attribute_entry [string_index - 1 ].string_objectref != nullptr ) {
@@ -605,22 +604,98 @@ int32_t * class_resolver_lookup_string(int class_hash_table_length,
605
604
(const uint8_t * )"java/lang/String" ,
606
605
16 );
607
606
608
- int32_t size = utf8_constant -> utf8 .length + 4 ;
609
- int32_t * arrayref = memory_allocate (size );
607
+ int32_t size = utf8_constant -> utf8 .length + ( sizeof ( struct arrayref )) ;
608
+ struct arrayref * arrayref = memory_allocate (size );
610
609
assert (arrayref != nullptr );
611
- arrayref [0 ] = utf8_constant -> utf8 .length ;
612
- uint8_t * bytearray = (uint8_t * )& arrayref [1 ];
613
- for (int i = 0 ; i < utf8_constant -> utf8 .length ; i ++ )
614
- bytearray [i ] = utf8_constant -> utf8 .bytes [i ];
610
+ arrayref -> class_entry = nullptr ; // byte[]
611
+ arrayref -> length = utf8_constant -> utf8 .length ;
612
+ for (int i = 0 ; i < utf8_constant -> utf8 .length ; i ++ ) {
613
+ arrayref -> u8 [i ] = utf8_constant -> utf8 .bytes [i ];
614
+ }
615
615
616
616
assert (string_class_entry != nullptr );
617
- int32_t * objectref = memory_allocate (4 + 4 );
617
+ struct objectref * objectref = memory_allocate (( sizeof ( struct objectref )) + ( sizeof ( void * )) );
618
618
assert (objectref != nullptr );
619
- objectref [ 0 ] = ( int32_t ) string_class_entry ;
620
- objectref [ 1 ] = ( int32_t ) arrayref ;
619
+ objectref -> class_entry = string_class_entry ;
620
+ objectref -> aref [ 0 ] = arrayref ;
621
621
622
622
// cache the result
623
623
class_entry -> attribute_entry [string_index - 1 ].string_objectref = objectref ;
624
624
625
625
return objectref ;
626
626
}
627
+
628
+ bool class_resolver_instanceof (int class_hash_table_length ,
629
+ struct hash_table_entry * class_hash_table ,
630
+ struct class_entry * origin_class_entry ,
631
+ const int class_index ,
632
+ struct objectref * objectref )
633
+ {
634
+ struct class_entry * index_class_entry =
635
+ class_resolver_lookup_class_from_class_index (class_hash_table_length ,
636
+ class_hash_table ,
637
+ origin_class_entry ,
638
+ class_index );
639
+ assert (index_class_entry != nullptr );
640
+
641
+ assert (objectref != nullptr );
642
+ struct class_entry * class_entry = objectref -> class_entry ;
643
+ while (true) {
644
+ debugf ("class_entry: %p\n" , class_entry );
645
+
646
+ assert (class_entry != nullptr );
647
+ if (class_entry == index_class_entry ) {
648
+ return true;
649
+ }
650
+ if (class_entry -> class_file -> super_class == 0 ) {
651
+ return false;
652
+ }
653
+
654
+ struct constant * class_constant = & class_entry -> class_file -> constant_pool [class_entry -> class_file -> super_class - 1 ];
655
+ assert (class_constant -> tag == CONSTANT_Class );
656
+ struct constant * class_name_constant = & class_entry -> class_file -> constant_pool [class_constant -> class .name_index - 1 ];
657
+ assert (class_name_constant -> tag == CONSTANT_Utf8 );
658
+ debug_print__constant__utf8_string (class_name_constant );
659
+ debugf ("\n" );
660
+
661
+ // superclass lookup
662
+ class_entry = class_resolver_lookup_class_from_class_index (class_hash_table_length ,
663
+ class_hash_table ,
664
+ class_entry ,
665
+ class_entry -> class_file -> super_class );
666
+ }
667
+ }
668
+
669
+ struct hash_table_entry * class_resolver_init_string_hash_table (int length )
670
+ {
671
+ assert ((length & (length - 1 )) == 0 ); // length must be a power of two
672
+ int hash_table_length = length ;
673
+ uint32_t hash_table_size = (sizeof (struct hash_table_entry )) * hash_table_length ;
674
+ struct hash_table_entry * hash_table = malloc_class_arena (hash_table_size );
675
+ hash_table_init (hash_table_length , hash_table );
676
+
677
+ return hash_table ;
678
+ }
679
+
680
+ void * class_resolver_memoize_string_type (int string_hash_table_length ,
681
+ struct hash_table_entry * string_hash_table ,
682
+ const uint8_t * type ,
683
+ int length )
684
+ {
685
+ struct hash_table_entry * e = hash_table_find (string_hash_table_length ,
686
+ string_hash_table ,
687
+ type ,
688
+ length );
689
+ if (e != nullptr ) {
690
+ assert (e -> value == (void * )0x12345678 );
691
+ return e ;
692
+ } else {
693
+ struct hash_table_entry * e = hash_table_add (string_hash_table_length ,
694
+ string_hash_table ,
695
+ type ,
696
+ length ,
697
+ (void * )0x12345678 );
698
+ assert (e != nullptr );
699
+ return e ;
700
+ }
701
+ }
0 commit comments