From f06aeef9cee8c3b22c4b0407ce91763927a08d16 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 7 Nov 2016 15:58:11 +0300 Subject: [PATCH] Some work for string concatenation. (#40) --- README.md | 14 ++++------- backend.native/tests/build.gradle | 9 ++++++- backend.native/tests/runtime/basic/hello2.kt | 4 +++ runtime/src/main/cpp/Natives.cpp | 6 +++++ runtime/src/main/cpp/Natives.h | 2 ++ runtime/src/main/kotlin/kotlin/Any.kt | 4 +-- .../src/main/kotlin/kotlin/CharSequence.kt | 25 +++++++++++++++++++ runtime/src/main/kotlin/kotlin/String.kt | 25 +++++++++++-------- runtime/src/main/kotlin/kotlin/Unit.kt | 2 +- 9 files changed, 68 insertions(+), 23 deletions(-) create mode 100644 backend.native/tests/runtime/basic/hello2.kt create mode 100644 runtime/src/main/kotlin/kotlin/CharSequence.kt diff --git a/README.md b/README.md index d3d833378eb..6b9f39961d4 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,15 @@ ## Build -First, build Kotlin IR branch tree with: - - pushd backend.native/kotlin-ir - ant -f ./update_dependencies.xml jb_update - ant -f ./build.xml - popd - -Then, download dependencies: +Download dependencies: gradle :dependencies:update -To build native translator just use: +To run native translator just use: gradle :backend.native:run And it will run simple example (currently prints out IR of test file). +For more tests, use: + + gradle :backend.native:tests:run diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 96f8f232324..338e6db95cb 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -26,7 +26,7 @@ abstract class KonanTest extends DefaultTask { abstract void compileTest(File sourceS, File runtimeS, File out) - + private File kt2bc(String ktSource) { def sourceKt = project.file(ktSource) def sourceBc = new File("${sourceKt.absolutePath}.bc") @@ -167,6 +167,13 @@ task hello1(type: RunKonanTest) { source = "runtime/basic/hello1.kt" } +/* TODO: enable, once calling plus operator is implemented. +task hello2(type: RunKonanTest) { + goldValue = "Hello World" + testData = "Hello World" + source = "runtime/basic/hello2.kt" +} */ + // TODO: waiting for boolean to be implemented //task bool_yes(type: KonanTest) { // source = "codegen/function/boolean.kt" diff --git a/backend.native/tests/runtime/basic/hello2.kt b/backend.native/tests/runtime/basic/hello2.kt new file mode 100644 index 00000000000..29d137d9b11 --- /dev/null +++ b/backend.native/tests/runtime/basic/hello2.kt @@ -0,0 +1,4 @@ +// TODO: remove kotlin_native.io once overrides are in place. +fun main(args : Array) { + print("you entered '" + readLine() + "'") +} diff --git a/runtime/src/main/cpp/Natives.cpp b/runtime/src/main/cpp/Natives.cpp index 57a469d4d24..83167ebf557 100644 --- a/runtime/src/main/cpp/Natives.cpp +++ b/runtime/src/main/cpp/Natives.cpp @@ -226,4 +226,10 @@ KInt Kotlin_String_hashCode(const ArrayHeader* thiz) { return CityHash64(ByteArrayAddressOfElementAt(thiz, 0), thiz->count_); } +KRef Kotlin_String_subSequence( + const ArrayHeader* thiz, KInt startIndex, KInt endIndex) { + RuntimeAssert(false, "Unsupported operation"); + return nullptr; } + +} // extern "C" diff --git a/runtime/src/main/cpp/Natives.h b/runtime/src/main/cpp/Natives.h index cbb614edafd..1ac38dad23b 100644 --- a/runtime/src/main/cpp/Natives.h +++ b/runtime/src/main/cpp/Natives.h @@ -91,6 +91,8 @@ ArrayHeader* Kotlin_String_fromUtf8Array(const ArrayHeader* array); ArrayHeader* Kotlin_String_plusImpl( const ArrayHeader* thiz, const ArrayHeader* other); KInt Kotlin_String_getStringLength(const ArrayHeader* thiz); +KRef Kotlin_String_subSequence( + const ArrayHeader* thiz, KInt startIndex, KInt endIndex); #ifdef __cplusplus } diff --git a/runtime/src/main/kotlin/kotlin/Any.kt b/runtime/src/main/kotlin/kotlin/Any.kt index a4c8f2b3688..8c3496b3f7f 100644 --- a/runtime/src/main/kotlin/kotlin/Any.kt +++ b/runtime/src/main/kotlin/kotlin/Any.kt @@ -2,8 +2,8 @@ package kotlin @ExportTypeInfo("theAnyTypeInfo") public open class Any { - // @SymbolName("Kotlin_Any_equals") - // external public open operator fun equals0(other: Any0?): Boolean + @SymbolName("Kotlin_Any_equals") + external public open operator fun equals(other: Any?): Boolean // TODO: do we need that in Any? @SymbolName("Kotlin_Any_hashCode") diff --git a/runtime/src/main/kotlin/kotlin/CharSequence.kt b/runtime/src/main/kotlin/kotlin/CharSequence.kt new file mode 100644 index 00000000000..c75f3812087 --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/CharSequence.kt @@ -0,0 +1,25 @@ +package kotlin + +/** + * Represents a readable sequence of [Char] values. + */ +public interface CharSequence { + /** + * Returns the length of this character sequence. + */ + public val length: Int + + /** + * Returns the character at the specified [index] in this character sequence. + */ + public operator fun get(index: Int): Char + + /** + * Returns a new character sequence that is a subsequence of this character sequence, + * starting at the specified [startIndex] and ending right before the specified [endIndex]. + * + * @param startIndex the start index (inclusive). + * @param endIndex the end index (exclusive). + */ + public fun subSequence(startIndex: Int, endIndex: Int): CharSequence +} \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/String.kt b/runtime/src/main/kotlin/kotlin/String.kt index 88b27fb3b3e..b9447c53c71 100644 --- a/runtime/src/main/kotlin/kotlin/String.kt +++ b/runtime/src/main/kotlin/kotlin/String.kt @@ -4,27 +4,32 @@ package kotlin external fun fromUtf8Array(array: ByteArray) : String @ExportTypeInfo("theStringTypeInfo") -class String { +class String : Comparable/* , CharSequence */ { @SymbolName("Kotlin_String_hashCode") external public override fun hashCode(): Int -/* TODO: calling to virtual method (plusImpl) results in link error; uncomment after supporting -calling virtual methods in translator - public operator fun plus(other: Any?): String { + // TODO: make it Any? + public operator fun plus(other: Any): String { return plusImpl(other.toString()) } -*/ - public val length: Int + /* + public fun override toString(): String { + return this + } */ + + public /* override */ val length: Int get() = getStringLength() // Can be O(N). @SymbolName("Kotlin_String_get") - external public fun get(index: Int): Char + external /* override */ public fun get(index: Int): Char + + @SymbolName("Kotlin_String_subSequence") + external /* override */ public fun subSequence(startIndex: Int, endIndex: Int): CharSequence - // external public fun subSequence(startIndex: Int, endIndex: Int): CharSequence @SymbolName("Kotlin_String_compareTo") - external public fun compareTo(other: String): Int + override external public fun compareTo(other: String): Int @SymbolName("Kotlin_String_getStringLength") external private fun getStringLength(): Int @@ -33,5 +38,5 @@ calling virtual methods in translator external private fun plusImpl(other:Any): String @SymbolName("Kotlin_String_equals") - external public override operator fun equals(other: Any?): Boolean + external public override fun equals(other: Any?): Boolean } \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/Unit.kt b/runtime/src/main/kotlin/kotlin/Unit.kt index 461d57cda16..b5c859f84f5 100644 --- a/runtime/src/main/kotlin/kotlin/Unit.kt +++ b/runtime/src/main/kotlin/kotlin/Unit.kt @@ -1,5 +1,5 @@ package kotlin public object Unit { - // override fun toString() = "kotlin.Unit" + override fun toString() = "kotlin.Unit" } \ No newline at end of file