Fix Any.toString().

This commit is contained in:
Nikolay Igotti
2017-02-01 18:15:11 +03:00
parent 6192dc254a
commit bb43b6ab09
2 changed files with 16 additions and 13 deletions
+8 -13
View File
@@ -22,11 +22,6 @@ KInt Kotlin_Any_hashCode(KConstRef thiz) {
return reinterpret_cast<uintptr_t>(thiz); return reinterpret_cast<uintptr_t>(thiz);
} }
OBJ_GETTER(Kotlin_Any_toString, KConstRef thiz) {
// TODO: make it more sensible, such as address and type info.
return nullptr;
}
// io/Console.kt // io/Console.kt
void Kotlin_io_Console_print(KString message) { void Kotlin_io_Console_print(KString message) {
RuntimeAssert(message->type_info() == theStringTypeInfo, "Must use a string"); RuntimeAssert(message->type_info() == theStringTypeInfo, "Must use a string");
@@ -61,8 +56,8 @@ OBJ_GETTER0(Kotlin_io_Console_readLine) {
// String.kt // String.kt
KInt Kotlin_String_compareTo(KString thiz, KString other) { KInt Kotlin_String_compareTo(KString thiz, KString other) {
return memcmp(ByteArrayAddressOfElementAt(thiz, 0), return memcmp(ByteArrayAddressOfElementAt(thiz, 0),
ByteArrayAddressOfElementAt(other, 0), ByteArrayAddressOfElementAt(other, 0),
thiz->count_ < other->count_ ? thiz->count_ : other->count_); thiz->count_ < other->count_ ? thiz->count_ : other->count_);
} }
KChar Kotlin_String_get(KString thiz, KInt index) { KChar Kotlin_String_get(KString thiz, KInt index) {
@@ -112,7 +107,7 @@ OBJ_GETTER(Kotlin_String_fromCharArray, KConstRef thiz, KInt start, KInt size) {
ArrayHeader* result = ArrayContainer(theStringTypeInfo, size).GetPlace(); ArrayHeader* result = ArrayContainer(theStringTypeInfo, size).GetPlace();
for (KInt index = 0; index < size; ++index) { for (KInt index = 0; index < size; ++index) {
*ByteArrayAddressOfElementAt(result, index) = *ByteArrayAddressOfElementAt(result, index) =
*PrimitiveArrayAddressOfElementAt<KChar>(array, start + index); *PrimitiveArrayAddressOfElementAt<KChar>(array, start + index);
} }
RETURN_OBJ(result->obj()); RETURN_OBJ(result->obj());
} }
@@ -123,7 +118,7 @@ OBJ_GETTER(Kotlin_String_toCharArray, KString string) {
theCharArrayTypeInfo, string->count_).GetPlace(); theCharArrayTypeInfo, string->count_).GetPlace();
for (int index = 0; index < string->count_; ++index) { for (int index = 0; index < string->count_; ++index) {
*PrimitiveArrayAddressOfElementAt<KChar>(result, index) = *PrimitiveArrayAddressOfElementAt<KChar>(result, index) =
*ByteArrayAddressOfElementAt(string, index); *ByteArrayAddressOfElementAt(string, index);
} }
RETURN_OBJ(result->obj()); RETURN_OBJ(result->obj());
} }
@@ -156,8 +151,8 @@ KBoolean Kotlin_String_equals(KString thiz, KConstRef other) {
if (thiz == otherString) return true; if (thiz == otherString) return true;
return thiz->count_ == otherString->count_ && return thiz->count_ == otherString->count_ &&
memcmp(ByteArrayAddressOfElementAt(thiz, 0), memcmp(ByteArrayAddressOfElementAt(thiz, 0),
ByteArrayAddressOfElementAt(otherString, 0), ByteArrayAddressOfElementAt(otherString, 0),
thiz->count_) == 0; thiz->count_) == 0;
} }
KInt Kotlin_String_hashCode(KString thiz) { KInt Kotlin_String_hashCode(KString thiz) {
@@ -178,8 +173,8 @@ OBJ_GETTER(Kotlin_String_subSequence, KString thiz, KInt startIndex, KInt endInd
KInt length = endIndex - startIndex; KInt length = endIndex - startIndex;
ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace(); ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace();
memcpy(ByteArrayAddressOfElementAt(result, 0), memcpy(ByteArrayAddressOfElementAt(result, 0),
ByteArrayAddressOfElementAt(thiz, startIndex), ByteArrayAddressOfElementAt(thiz, startIndex),
length); length);
RETURN_OBJ(result->obj()); RETURN_OBJ(result->obj());
} }
+8
View File
@@ -24,6 +24,14 @@ OBJ_GETTER(makeString, const char* cstring) {
extern "C" { extern "C" {
OBJ_GETTER(Kotlin_Any_toString, KConstRef thiz) {
char cstring[80];
snprintf(cstring, sizeof(cstring), "%s %p type %p",
IsArray(thiz) ? "array" : "object",
thiz, thiz->type_info_);
RETURN_RESULT_OF(makeString, cstring);
}
OBJ_GETTER(Kotlin_Byte_toString, KByte value) { OBJ_GETTER(Kotlin_Byte_toString, KByte value) {
char cstring[8]; char cstring[8];
snprintf(cstring, sizeof(cstring), "%d", value); snprintf(cstring, sizeof(cstring), "%d", value);