Skip to content

Commit

Permalink
[K/N][runtime] Used internal isInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
homuroll authored and qodana-bot committed May 22, 2023
1 parent d1a3752 commit 55632f5
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ internal class CAdapterApiExporter(
|KObjHeader* DerefStablePointer(void*, KObjHeader**) RUNTIME_NOTHROW;
|void* CreateStablePointer(KObjHeader*) RUNTIME_NOTHROW;
|void DisposeStablePointer(void*) RUNTIME_NOTHROW;
|${prefix}_KBoolean IsInstance(const KObjHeader*, const KTypeInfo*) RUNTIME_NOTHROW;
|${prefix}_KBoolean IsInstanceInternal(const KObjHeader*, const KTypeInfo*) RUNTIME_NOTHROW;
|void EnterFrame(KObjHeader** start, int parameters, int count) RUNTIME_NOTHROW;
|void LeaveFrame(KObjHeader** start, int parameters, int count) RUNTIME_NOTHROW;
|void SetCurrentFrame(KObjHeader** start) RUNTIME_NOTHROW;
Expand Down Expand Up @@ -338,7 +338,7 @@ internal class CAdapterApiExporter(
| Kotlin_initRuntimeIfNeeded();
| ScopedRunnableState stateGuard;
| KObjHolder holder;
| return IsInstance(DerefStablePointer(ref, holder.slot()), (const KTypeInfo*)type);
| return IsInstanceInternal(DerefStablePointer(ref, holder.slot()), (const KTypeInfo*)type);
|}
""".trimMargin())
predefinedTypes.forEach {
Expand Down
4 changes: 2 additions & 2 deletions kotlin-native/llvmDebugInfoC/src/scripts/konan_lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def _type_info_by_address(address, debugger = lldb.debugger):
return candidates

def is_instance_of(addr, typeinfo):
return evaluate("(bool)IsInstance({:#x}, {:#x})".format(addr, typeinfo)).GetValue() == "true"
return evaluate("(bool)Konan_DebugIsInstance({:#x}, {:#x})".format(addr, typeinfo)).GetValue() == "true"

def is_string_or_array(value):
start = time.monotonic()
soa = evaluate("(int)IsInstance({0:#x}, {1:#x}) ? 1 : ((int)Konan_DebugIsArray({0:#x})) ? 2 : 0)"
soa = evaluate("(int)Konan_DebugIsInstance({0:#x}, {1:#x}) ? 1 : ((int)Konan_DebugIsArray({0:#x})) ? 2 : 0)"
.format(value.unsigned, _symbol_loaded_address('kclass:kotlin.String'))).unsigned
log(lambda: "is_string_or_array:{:#x}:{}".format(value.unsigned, soa))
bench(start, lambda: "is_string_or_array({:#x}) = {}".format(value.unsigned, soa))
Expand Down
4 changes: 4 additions & 0 deletions kotlin-native/runtime/src/debug/cpp/KDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugIsArray(KRef obj) {
return impl(obj);
}

RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugIsInstance(KRef obj, const TypeInfo* typeInfo) {
return IsInstanceInternal(obj, typeInfo);
}

RUNTIME_USED RUNTIME_WEAK int32_t Konan_DebugGetFieldCount(KRef obj) {
auto* impl = getImpl<int32_t (*)(KRef)>(obj, DO_DebugGetFieldCount);
if (impl == nullptr) return 0;
Expand Down
2 changes: 1 addition & 1 deletion kotlin-native/runtime/src/legacymm/cpp/Weak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ OBJ_GETTER(Konan_getWeakReferenceImpl, ObjHeader* referred) {
}

#if KONAN_OBJC_INTEROP
if (IsInstance(referred, theObjCObjectWrapperTypeInfo)) {
if (IsInstanceInternal(referred, theObjCObjectWrapperTypeInfo)) {
RETURN_RESULT_OF(makeObjCWeakReferenceImpl, referred->GetAssociatedObject());
}
#endif // KONAN_OBJC_INTEROP
Expand Down
2 changes: 1 addition & 1 deletion kotlin-native/runtime/src/main/cpp/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern "C" void Kotlin_runUnhandledExceptionHook(KRef exception);
extern "C" void ReportUnhandledException(KRef exception);

void ThrowException(KRef exception) {
RuntimeAssert(exception != nullptr && IsInstance(exception, theThrowableTypeInfo),
RuntimeAssert(exception != nullptr && IsInstanceInternal(exception, theThrowableTypeInfo),
"Throwing something non-throwable");
#if KONAN_NO_EXCEPTIONS
PrintThrowable(exception);
Expand Down
3 changes: 3 additions & 0 deletions kotlin-native/runtime/src/main/cpp/KDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ int32_t Konan_DebugPrint(KRef obj);
RUNTIME_USED RUNTIME_WEAK
int32_t Konan_DebugIsArray(KRef obj);

RUNTIME_USED RUNTIME_WEAK
int32_t Konan_DebugIsInstance(KRef obj, const TypeInfo* typeInfo);

// Returns number of fields in an objects, or elements in an array.
RUNTIME_USED RUNTIME_WEAK
int32_t Konan_DebugGetFieldCount(KRef obj);
Expand Down
9 changes: 6 additions & 3 deletions kotlin-native/runtime/src/main/cpp/ObjCExportErrors.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ static void printlnMessage(const char* message) {
static char kotlinExceptionOriginChar;

static bool isExceptionOfType(KRef exception, const TypeInfo** types) {
if (types) for (int i = 0; types[i] != nullptr; ++i) {
// TODO: use fast instance check when possible.
if (IsInstance(exception, types[i])) return true;
if (types) {
const TypeInfo* type = exception->type_info();
for (int i = 0; types[i] != nullptr; ++i) {
// TODO: use fast instance check when possible.
if (IsSubtype(type, types[i])) return true;
}
}

return false;
Expand Down
9 changes: 7 additions & 2 deletions kotlin-native/runtime/src/main/cpp/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

extern "C" {

KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) {
// Note: keeping it for compatibility with external tools only, will be deprecated and removed in the future.
RUNTIME_PURE RUNTIME_USED RUNTIME_WEAK KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) {
return IsInstanceInternal(obj, type_info);
}

KBoolean IsInstanceInternal(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();
Expand Down Expand Up @@ -42,7 +47,7 @@ KBoolean IsArray(KConstRef obj) {
}

KBoolean Kotlin_TypeInfo_isInstance(KConstRef obj, KNativePtr typeInfo) {
return IsInstance(obj, reinterpret_cast<const TypeInfo*>(typeInfo));
return IsInstanceInternal(obj, reinterpret_cast<const TypeInfo*>(typeInfo));
}

OBJ_GETTER(Kotlin_TypeInfo_getPackageName, KNativePtr typeInfo, KBoolean checkFlags) {
Expand Down
3 changes: 2 additions & 1 deletion kotlin-native/runtime/src/main/cpp/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ extern const TypeInfo* theWorkerBoundReferenceTypeInfo;
extern const TypeInfo* theCleanerImplTypeInfo;
extern const TypeInfo* theRegularWeakReferenceImplTypeInfo;

KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) RUNTIME_PURE RUNTIME_USED; // Used in konan_lldb.py
KBoolean IsInstance(const ObjHeader* obj, const TypeInfo* type_info) RUNTIME_PURE RUNTIME_USED RUNTIME_WEAK;
KBoolean IsInstanceInternal(const ObjHeader* obj, const TypeInfo* type_info) RUNTIME_PURE;
KBoolean IsSubtype(const TypeInfo* obj_type_info, const TypeInfo* type_info) RUNTIME_PURE;
KBoolean IsSubclassFast(const TypeInfo* obj_type_info, int32_t lo, int32_t hi) RUNTIME_PURE;
void CheckCast(const ObjHeader* obj, const TypeInfo* type_info);
Expand Down
2 changes: 1 addition & 1 deletion kotlin-native/runtime/src/mm/cpp/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ RUNTIME_NOTHROW extern "C" OBJ_GETTER(Konan_getWeakReferenceImpl, ObjHeader* ref
RETURN_RESULT_OF(makePermanentWeakReferenceImpl, referred);
}
#if KONAN_OBJC_INTEROP
if (IsInstance(referred, theObjCObjectWrapperTypeInfo)) {
if (IsInstanceInternal(referred, theObjCObjectWrapperTypeInfo)) {
RETURN_RESULT_OF(makeObjCWeakReferenceImpl, referred->GetAssociatedObject());
}
#endif // KONAN_OBJC_INTEROP
Expand Down

0 comments on commit 55632f5

Please sign in to comment.