More runtime work. Console read, string hashing.

This commit is contained in:
Nikolay Igotti
2016-10-27 18:57:32 +03:00
parent cbb5499313
commit b6e314f274
4 changed files with 49 additions and 20 deletions
+37 -17
View File
@@ -1,7 +1,9 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "Assert.h" #include "Assert.h"
#include "City.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "Memory.h" #include "Memory.h"
#include "Natives.h" #include "Natives.h"
@@ -21,7 +23,6 @@ KInt Kotlin_Any_hashCode(const ObjHeader* thiz) {
} }
ArrayHeader* Kotlin_Any_toString(const ObjHeader* thiz) { ArrayHeader* Kotlin_Any_toString(const ObjHeader* thiz) {
return nullptr; return nullptr;
} }
@@ -118,23 +119,36 @@ void Kotlin_io_Console_print(const ArrayHeader* array) {
write(STDOUT_FILENO, ByteArrayAddressOfElementAt(array, 0), array->count_); write(STDOUT_FILENO, ByteArrayAddressOfElementAt(array, 0), array->count_);
} }
// String.kt ArrayHeader* Kotlin_io_Console_readLine() {
KInt Kotlin_String_compareTo(const ArrayHeader* obj, const ArrayHeader* other) { char data[2048];
return memcmp(ByteArrayAddressOfElementAt(obj, 0), if (!fgets(data, sizeof(data) - 1, stdin)) {
ByteArrayAddressOfElementAt(other, 0), return nullptr;
obj->count_ < other->count_ ? obj->count_ : other->count_); }
int32_t length = strlen(data);
ArrayHeader* result = ArrayContainer(theStringTypeInfo, length).GetPlace();
memcpy(
ByteArrayAddressOfElementAt(result, 0),
data, length);
return result;
} }
KChar Kotlin_String_get(const ArrayHeader* obj, KInt index) { // String.kt
KInt Kotlin_String_compareTo(const ArrayHeader* thiz, const ArrayHeader* other) {
return memcmp(ByteArrayAddressOfElementAt(thiz, 0),
ByteArrayAddressOfElementAt(other, 0),
thiz->count_ < other->count_ ? thiz->count_ : other->count_);
}
KChar Kotlin_String_get(const ArrayHeader* thiz, KInt index) {
// TODO: support full UTF-8. // TODO: support full UTF-8.
if (static_cast<uint32_t>(index) >= obj->count_) { if (static_cast<uint32_t>(index) >= thiz->count_) {
ThrowArrayIndexOutOfBoundsException(); ThrowArrayIndexOutOfBoundsException();
} }
return *ByteArrayAddressOfElementAt(obj, index); return *ByteArrayAddressOfElementAt(thiz, index);
} }
KInt Kotlin_String_getStringLength(const ArrayHeader* array) { KInt Kotlin_String_getStringLength(const ArrayHeader* thiz) {
return array->count_; return thiz->count_;
} }
ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array) { ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array) {
@@ -150,19 +164,19 @@ ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array) {
} }
ArrayHeader* Kotlin_String_plusImpl( ArrayHeader* Kotlin_String_plusImpl(
const ArrayHeader* obj, const ArrayHeader* other) { const ArrayHeader* thiz, const ArrayHeader* other) {
// TODO: support UTF-8 // TODO: support UTF-8
RuntimeAssert(obj->type_info() == theStringTypeInfo, "Must be a string"); RuntimeAssert(thiz->type_info() == theStringTypeInfo, "Must be a string");
RuntimeAssert(other->type_info() == theStringTypeInfo, "Must be a string"); RuntimeAssert(other->type_info() == theStringTypeInfo, "Must be a string");
uint32_t result_length = obj->count_ + other->count_; uint32_t result_length = thiz->count_ + other->count_;
ArrayHeader* result = ArrayContainer( ArrayHeader* result = ArrayContainer(
theStringTypeInfo, result_length).GetPlace(); theStringTypeInfo, result_length).GetPlace();
memcpy( memcpy(
ByteArrayAddressOfElementAt(result, 0), ByteArrayAddressOfElementAt(result, 0),
ByteArrayAddressOfElementAt(obj, 0), ByteArrayAddressOfElementAt(thiz, 0),
obj->count_); thiz->count_);
memcpy( memcpy(
ByteArrayAddressOfElementAt(result, obj->count_), ByteArrayAddressOfElementAt(result, thiz->count_),
ByteArrayAddressOfElementAt(other, 0), ByteArrayAddressOfElementAt(other, 0),
other->count_); other->count_);
return result; return result;
@@ -178,4 +192,10 @@ KBool Kotlin_String_equals(
thiz->count_) == 0; thiz->count_) == 0;
} }
KInt Kotlin_String_hashCode(const ArrayHeader* thiz) {
// TODO: consider caching strings hashes.
// TODO: maybe use some simpler hashing algorithm?
return CityHash64(ByteArrayAddressOfElementAt(thiz, 0), thiz->count_);
}
} }
+5 -2
View File
@@ -65,14 +65,17 @@ KInt Kotlin_IntArray_getArrayLength(const ArrayHeader* thiz);
// io/Console.kt // io/Console.kt
void Kotlin_io_Console_print(const ArrayHeader* thiz); void Kotlin_io_Console_print(const ArrayHeader* thiz);
ArrayHeader* Kotlin_io_Console_readLine();
// String.kt // String.kt
KInt Kotlin_String_hashCode(const ArrayHeader* thiz);
KBool Kotlin_String_equals(const ArrayHeader* thiz, const ObjHeader* other);
KInt Kotlin_String_compareTo(const ArrayHeader* thiz, const ArrayHeader* other); KInt Kotlin_String_compareTo(const ArrayHeader* thiz, const ArrayHeader* other);
KChar Kotlin_String_get(const ArrayHeader* thiz, KInt index); KChar Kotlin_String_get(const ArrayHeader* thiz, KInt index);
ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array); ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array);
ArrayHeader* Kotlin_String_plusImpl(const ArrayHeader* thiz, const ArrayHeader* other); ArrayHeader* Kotlin_String_plusImpl(
const ArrayHeader* thiz, const ArrayHeader* other);
KInt Kotlin_String_getStringLength(const ArrayHeader* thiz); KInt Kotlin_String_getStringLength(const ArrayHeader* thiz);
KBool Kotlin_String_equals(const ArrayHeader* thiz, const ObjHeader* other);
#ifdef __cplusplus #ifdef __cplusplus
} }
@@ -4,6 +4,9 @@ package kotlin_native
external fun fromUtf8Array(array: ByteArray) : String external fun fromUtf8Array(array: ByteArray) : String
class String { class String {
@SymbolName("Kotlin_String_hashCode")
external public override fun hashCode(): Int
public operator fun plus(other: Any?): String { public operator fun plus(other: Any?): String {
return plusImpl(other.toString()) return plusImpl(other.toString())
} }
@@ -1,4 +1,7 @@
package kotlin_native.io package kotlin_native.io
@kotlin_native.SymbolName("Kotlin_io_Console_print") @kotlin_native.SymbolName("Kotlin_io_Console_print")
external public fun print(message: kotlin_native.String) external public fun print(message: kotlin_native.String)
@kotlin_native.SymbolName("Kotlin_io_Console_readLine")
external public fun readLine(): kotlin_native.String?