Implement instanceof checks. (#23)

This commit is contained in:
Nikolay Igotti
2016-10-31 08:14:04 +03:00
committed by GitHub
parent e4d1557300
commit 8d2c0edd6b
3 changed files with 29 additions and 6 deletions
+21 -1
View File
@@ -74,11 +74,31 @@ void* AllocInstance(const TypeInfo* type_info, PlacementHint hint) {
return ObjectContainer(type_info).GetPlace();
}
void* AllocArrayInstance(const TypeInfo* type_info, PlacementHint hint, uint32_t elements) {
void* AllocArrayInstance(
const TypeInfo* type_info, PlacementHint hint, uint32_t elements) {
RuntimeAssert(type_info->instanceSize_ < 0, "must be an array");
return ArrayContainer(type_info, elements).GetPlace();
}
int IsInstance(const ObjHeader* obj, const TypeInfo* type_info) {
// We assume null check is handled by caller.
RuntimeAssert(obj != nullptr, "must not be null");
const TypeInfo* obj_type_info = obj->type_info();
// If it is an interface - check in list of implemented interfaces.
if (type_info->fieldsCount_ < 0) {
for (int i = 0; i < obj_type_info->implementedInterfacesCount_; ++i) {
if (obj_type_info->implementedInterfaces_[i] == type_info) {
return 1;
}
}
return 0;
}
while (obj_type_info != nullptr && obj_type_info != type_info) {
obj_type_info = obj_type_info->superType_;
}
return obj_type_info != nullptr;
}
#ifdef __cplusplus
}
#endif
+1
View File
@@ -260,6 +260,7 @@ extern "C" {
void InitMemory();
void* AllocInstance(const TypeInfo* type_info, PlacementHint hint);
void* AllocArrayInstance(const TypeInfo* type_info, PlacementHint hint, uint32_t elements);
int IsInstance(const ObjHeader* obj, const TypeInfo* type_info);
#ifdef __cplusplus
}
+7 -5
View File
@@ -28,15 +28,17 @@ struct TypeInfo {
// Must be pointer to Any for array classes, and null for Any.
const TypeInfo* superType_;
// All object references inside this object.
const int* objOffsets_;
int objOffsetsCount_;
const int32_t* objOffsets_;
int32_t objOffsetsCount_;
const TypeInfo* const* implementedInterfaces_;
int implementedInterfacesCount_;
void* const* vtable_; // TODO: place vtable at the end of TypeInfo to eliminate the indirection
int32_t implementedInterfacesCount_;
// TODO: place vtable at the end of TypeInfo to eliminate the indirection.
void* const* vtable_;
const MethodTableRecord* openMethods_;
uint32_t openMethodsCount_;
const FieldTableRecord* fields_;
uint32_t fieldsCount_;
// Is negative to mark an interface.
int32_t fieldsCount_;
};
#ifdef __cplusplus