Add more console operations, nicer naming in natives. (#46)

This commit is contained in:
Nikolay Igotti
2016-11-08 17:38:36 +03:00
committed by GitHub
parent 5b7d0257e8
commit 91035b692b
8 changed files with 75 additions and 39 deletions
+7 -1
View File
@@ -153,7 +153,7 @@ task local_variable(type: UnitKonanTest) {
} }
task hello0(type: RunKonanTest) { task hello0(type: RunKonanTest) {
goldValue = "Hello, world!" goldValue = "Hello, world!\n"
source = "runtime/basic/hello0.kt" source = "runtime/basic/hello0.kt"
} }
@@ -170,6 +170,12 @@ task hello2(type: RunKonanTest) {
source = "runtime/basic/hello2.kt" source = "runtime/basic/hello2.kt"
} */ } */
/* TODO: enable, once can call toString() on Int.
task hello3(type: RunKonanTest) {
goldValue = "239"
source = "runtime/basic/hello3.kt"
} */
task if_else(type: UnitKonanTest) { task if_else(type: UnitKonanTest) {
source = "codegen/branching/if_else.kt" source = "codegen/branching/if_else.kt"
} }
+1 -2
View File
@@ -1,4 +1,3 @@
// TODO: remove kotlin_native.io once overrides are in place.
fun main(args : Array<String>) { fun main(args : Array<String>) {
kotlin.io.print("Hello, world!") println("Hello, world!")
} }
+1 -1
View File
@@ -1,4 +1,4 @@
// TODO: remove kotlin_native.io once overrides are in place. // TODO: remove kotlin_native.io once overrides are in place.
fun main(args : Array<String>) { fun main(args : Array<String>) {
kotlin.io.print(kotlin.io.readLine()) print(readLine())
} }
@@ -0,0 +1,3 @@
fun main(args : Array<String>) {
print(239)
}
+26 -18
View File
@@ -12,17 +12,17 @@
extern "C" { extern "C" {
// Any.kt // Any.kt
KBool Kotlin_Any_equals(const ObjHeader* thiz, const ObjHeader* other) { KBool Kotlin_Any_equals(KConstRef thiz, KConstRef other) {
return thiz == other; return thiz == other;
} }
KInt Kotlin_Any_hashCode(const ObjHeader* thiz) { KInt Kotlin_Any_hashCode(KConstRef thiz) {
// Here we will use different mechanism for stable hashcode, using meta-objects // Here we will use different mechanism for stable hashcode, using meta-objects
// if moving collector will be used. // if moving collector will be used.
return reinterpret_cast<uintptr_t>(thiz); return reinterpret_cast<uintptr_t>(thiz);
} }
ArrayHeader* Kotlin_Any_toString(const ObjHeader* thiz) { KString Kotlin_Any_toString(KConstRef thiz) {
return nullptr; return nullptr;
} }
@@ -141,13 +141,24 @@ KInt Kotlin_IntArray_getArrayLength(const ArrayHeader* array) {
} }
// io/Console.kt // io/Console.kt
void Kotlin_io_Console_print(const ArrayHeader* array) { void Kotlin_io_Console_print(KString message) {
RuntimeAssert(array->type_info() == theStringTypeInfo, "Must use a string"); RuntimeAssert(message->type_info() == theStringTypeInfo, "Must use a string");
// TODO: system stdout must be aware about UTF-8. // TODO: system stdout must be aware about UTF-8.
write(STDOUT_FILENO, ByteArrayAddressOfElementAt(array, 0), array->count_); write(STDOUT_FILENO, ByteArrayAddressOfElementAt(message, 0), message->count_);
} }
ArrayHeader* Kotlin_io_Console_readLine() { void Kotlin_io_Console_println(KString message) {
RuntimeAssert(message->type_info() == theStringTypeInfo, "Must use a string");
// TODO: system stdout must be aware about UTF-8.
write(STDOUT_FILENO, ByteArrayAddressOfElementAt(message, 0), message->count_);
Kotlin_io_Console_println0();
}
void Kotlin_io_Console_println0() {
write(STDOUT_FILENO, "\n", 1);
}
KString Kotlin_io_Console_readLine() {
char data[2048]; char data[2048];
if (!fgets(data, sizeof(data) - 1, stdin)) { if (!fgets(data, sizeof(data) - 1, stdin)) {
return nullptr; return nullptr;
@@ -161,13 +172,13 @@ ArrayHeader* Kotlin_io_Console_readLine() {
} }
// String.kt // String.kt
KInt Kotlin_String_compareTo(const ArrayHeader* thiz, const ArrayHeader* 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(const ArrayHeader* thiz, KInt index) { KChar Kotlin_String_get(KString thiz, KInt index) {
// TODO: support full UTF-8. // TODO: support full UTF-8.
if (static_cast<uint32_t>(index) >= thiz->count_) { if (static_cast<uint32_t>(index) >= thiz->count_) {
ThrowArrayIndexOutOfBoundsException(); ThrowArrayIndexOutOfBoundsException();
@@ -175,11 +186,11 @@ KChar Kotlin_String_get(const ArrayHeader* thiz, KInt index) {
return *ByteArrayAddressOfElementAt(thiz, index); return *ByteArrayAddressOfElementAt(thiz, index);
} }
KInt Kotlin_String_getStringLength(const ArrayHeader* thiz) { KInt Kotlin_String_getStringLength(KString thiz) {
return thiz->count_; return thiz->count_;
} }
ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array) { KString Kotlin_String_fromUtf8Array(const ArrayHeader* array) {
RuntimeAssert(array->type_info() == theByteArrayTypeInfo, "Must use a byte array"); RuntimeAssert(array->type_info() == theByteArrayTypeInfo, "Must use a byte array");
uint32_t length = ArraySizeBytes(array); uint32_t length = ArraySizeBytes(array);
// TODO: support full UTF-8. // TODO: support full UTF-8.
@@ -191,8 +202,7 @@ ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array) {
return result; return result;
} }
ArrayHeader* Kotlin_String_plusImpl( KString Kotlin_String_plusImpl(KString thiz, KString other) {
const ArrayHeader* thiz, const ArrayHeader* other) {
// TODO: support UTF-8 // TODO: support UTF-8
RuntimeAssert(thiz->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");
@@ -210,8 +220,7 @@ ArrayHeader* Kotlin_String_plusImpl(
return result; return result;
} }
KBool Kotlin_String_equals( KBool Kotlin_String_equals(KString thiz, KConstRef other) {
const ArrayHeader* thiz, const ObjHeader* other) {
if (other == nullptr || other->type_info() != theStringTypeInfo) return 0; if (other == nullptr || other->type_info() != theStringTypeInfo) return 0;
const ArrayHeader* otherString = reinterpret_cast<const ArrayHeader*>(other); const ArrayHeader* otherString = reinterpret_cast<const ArrayHeader*>(other);
return thiz->count_ == otherString->count_ && return thiz->count_ == otherString->count_ &&
@@ -220,14 +229,13 @@ KBool Kotlin_String_equals(
thiz->count_) == 0; thiz->count_) == 0;
} }
KInt Kotlin_String_hashCode(const ArrayHeader* thiz) { KInt Kotlin_String_hashCode(KString thiz) {
// TODO: consider caching strings hashes. // TODO: consider caching strings hashes.
// TODO: maybe use some simpler hashing algorithm? // TODO: maybe use some simpler hashing algorithm?
return CityHash64(ByteArrayAddressOfElementAt(thiz, 0), thiz->count_); return CityHash64(ByteArrayAddressOfElementAt(thiz, 0), thiz->count_);
} }
KRef Kotlin_String_subSequence( KRef Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex) {
const ArrayHeader* thiz, KInt startIndex, KInt endIndex) {
RuntimeAssert(false, "Unsupported operation"); RuntimeAssert(false, "Unsupported operation");
return nullptr; return nullptr;
} }
+20 -15
View File
@@ -11,6 +11,8 @@ typedef uint16_t KChar;
typedef int32_t KInt; typedef int32_t KInt;
typedef ObjHeader* KRef; typedef ObjHeader* KRef;
typedef const ObjHeader* KConstRef;
typedef const ArrayHeader* KString;
// Optimized versions not accessing type info. // Optimized versions not accessing type info.
inline KByte* ByteArrayAddressOfElementAt(ArrayHeader* obj, KInt index) { inline KByte* ByteArrayAddressOfElementAt(ArrayHeader* obj, KInt index) {
@@ -52,9 +54,9 @@ extern "C" {
#endif #endif
// Any.kt // Any.kt
KBool Kotlin_Any_equals(const ObjHeader* thiz, const ObjHeader* other); KBool Kotlin_Any_equals(KConstRef thiz, KConstRef other);
KInt Kotlin_Any_hashCode(const ObjHeader* thiz); KInt Kotlin_Any_hashCode(KConstRef thiz);
ArrayHeader* Kotlin_Any_toString(const ObjHeader* thiz); KString Kotlin_Any_toString(KConstRef thiz);
// Arrays.kt // Arrays.kt
// TODO: those must be compiler intrinsics afterwards. // TODO: those must be compiler intrinsics afterwards.
@@ -79,20 +81,23 @@ void Kotlin_IntArray_set(ArrayHeader* thiz, KInt index, KInt value);
KInt Kotlin_IntArray_getArrayLength(const ArrayHeader* thiz); 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(KString message);
ArrayHeader* Kotlin_io_Console_readLine(); void Kotlin_io_Console_println(KString message);
void Kotlin_io_Console_println0();
KString Kotlin_io_Console_readLine();
// Primitives.kt.
KString Kotlin_Int_toString(KInt value);
// String.kt // String.kt
KInt Kotlin_String_hashCode(const ArrayHeader* thiz); KInt Kotlin_String_hashCode(KString thiz);
KBool Kotlin_String_equals(const ArrayHeader* thiz, const ObjHeader* other); KBool Kotlin_String_equals(KString thiz, KConstRef other);
KInt Kotlin_String_compareTo(const ArrayHeader* thiz, const ArrayHeader* other); KInt Kotlin_String_compareTo(KString thiz, KString other);
KChar Kotlin_String_get(const ArrayHeader* thiz, KInt index); KChar Kotlin_String_get(KString thiz, KInt index);
ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array); KString Kotlin_String_fromUtf8Array(const ArrayHeader* array);
ArrayHeader* Kotlin_String_plusImpl( KString Kotlin_String_plusImpl(KString thiz, KString other);
const ArrayHeader* thiz, const ArrayHeader* other); KInt Kotlin_String_getStringLength(KString thiz);
KInt Kotlin_String_getStringLength(const ArrayHeader* thiz); KRef Kotlin_String_subSequence(KString thiz, KInt startIndex, KInt endIndex);
KRef Kotlin_String_subSequence(
const ArrayHeader* thiz, KInt startIndex, KInt endIndex);
#ifdef __cplusplus #ifdef __cplusplus
} }
@@ -144,6 +144,10 @@ public class Byte : Number(), Comparable<Byte> {
external public override fun toLong(): Long external public override fun toLong(): Long
external public override fun toFloat(): Float external public override fun toFloat(): Float
external public override fun toDouble(): Double external public override fun toDouble(): Double
// Konan-specific.
@SymbolName("Kotlin_Int_toString")
external public override fun toString(): String
} }
/** /**
+13 -2
View File
@@ -1,7 +1,18 @@
package kotlin.io package kotlin.io
@kotlin.SymbolName("Kotlin_io_Console_print") @kotlin.SymbolName("Kotlin_io_Console_print")
external public fun print(message: kotlin.String) external public fun print(message: String)
/*
public fun print(message: Int) {
print(message.toString())
} */
@kotlin.SymbolName("Kotlin_io_Console_println")
external public fun println(message: String)
@kotlin.SymbolName("Kotlin_io_Console_println0")
external public fun println()
@kotlin.SymbolName("Kotlin_io_Console_readLine") @kotlin.SymbolName("Kotlin_io_Console_readLine")
external public fun readLine(): kotlin.String external public fun readLine(): String