implement instanceof

This commit is contained in:
Zack Buhman 2024-12-27 05:31:50 -06:00
parent 168c0abbae
commit 05d3d7d2b9
2 changed files with 33 additions and 1 deletions

View File

@ -1093,7 +1093,20 @@ void op_ineg(struct vm * vm)
void op_instanceof(struct vm * vm, uint32_t index) void op_instanceof(struct vm * vm, uint32_t index)
{ {
assert(!"op_instanceof"); struct class_entry * class_entry =
class_resolver_lookup_class_from_class_index(vm->class_hash_table.length,
vm->class_hash_table.entry,
vm->current_frame->class_entry,
index);
assert(class_entry != nullptr);
int32_t * objectref = (int32_t *)operand_stack_pop_u32(vm->current_frame);
if (objectref == nullptr) {
operand_stack_push_u32(vm->current_frame, 0);
} else {
int32_t value = objectref[0] == (int32_t)class_entry;
operand_stack_push_u32(vm->current_frame, value);
}
} }
void op_invokedynamic(struct vm * vm, uint32_t index) void op_invokedynamic(struct vm * vm, uint32_t index)
@ -1785,6 +1798,7 @@ void op_putfield(struct vm * vm, uint32_t index)
{ {
uint32_t value = operand_stack_pop_u32(vm->current_frame); uint32_t value = operand_stack_pop_u32(vm->current_frame);
int32_t * objectref = (int32_t *)operand_stack_pop_u32(vm->current_frame); int32_t * objectref = (int32_t *)operand_stack_pop_u32(vm->current_frame);
assert(objectref != nullptr);
int32_t * objectfields = &objectref[1]; int32_t * objectfields = &objectref[1];
objectfields[field_entry->instance_index] = value; objectfields[field_entry->instance_index] = value;
} }
@ -1795,6 +1809,7 @@ void op_putfield(struct vm * vm, uint32_t index)
uint32_t high = operand_stack_pop_u32(vm->current_frame); uint32_t high = operand_stack_pop_u32(vm->current_frame);
uint32_t low = operand_stack_pop_u32(vm->current_frame); uint32_t low = operand_stack_pop_u32(vm->current_frame);
int32_t * objectref = (int32_t *)operand_stack_pop_u32(vm->current_frame); int32_t * objectref = (int32_t *)operand_stack_pop_u32(vm->current_frame);
assert(objectref != nullptr);
int32_t * objectfields = &objectref[1]; int32_t * objectfields = &objectref[1];
objectfields[field_entry->instance_index + 1] = high; objectfields[field_entry->instance_index + 1] = high;
objectfields[field_entry->instance_index] = low; objectfields[field_entry->instance_index] = low;

17
p/InstanceOf.java Normal file
View File

@ -0,0 +1,17 @@
package p;
class ClassA {
}
class InstanceOf {
static boolean test() {
ClassA ca = new ClassA();
Object a = ca;
return (ca instanceof ClassA);
}
public static void main() {
test();
}
}