From 63d72d3bb358580ec1b51d25b7e327f3efd931b2 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 13 Aug 2018 18:35:07 +0300 Subject: [PATCH] Make stdlib map freezing aware. (#1876) --- backend.native/tests/build.gradle | 5 +++ .../tests/runtime/workers/freeze5.kt | 42 +++++++++++++++++++ .../main/kotlin/kotlin/collections/HashMap.kt | 11 +++-- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 backend.native/tests/runtime/workers/freeze5.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index e5dcf5b89d8..790707af0b0 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -707,6 +707,11 @@ task freeze4(type: RunKonanTest) { source = "runtime/workers/freeze4.kt" } +task freeze5(type: RunKonanTest) { + goldValue = "OK\n" + source = "runtime/workers/freeze5.kt" +} + task atomic0(type: RunKonanTest) { disabled = (project.testTarget == 'wasm32') // Workers need pthreads. goldValue = "35\n" + "20\n" + "OK\n" diff --git a/backend.native/tests/runtime/workers/freeze5.kt b/backend.native/tests/runtime/workers/freeze5.kt new file mode 100644 index 00000000000..a55e8443a2e --- /dev/null +++ b/backend.native/tests/runtime/workers/freeze5.kt @@ -0,0 +1,42 @@ +package runtime.workers.freeze5 + +import kotlin.test.* +object Keys { + internal val myMap: Map> = mapOf( + "val1" to listOf("a1", "a2", "a3"), + "val2" to listOf("b1", "b2") + ) + + fun getKey(name: String): String { + for (key in myMap.keys) { + if (key == name) { + return key + } + } + return "" + } + + fun getValue(name: String): String { + for (value in myMap.values) { + if (value.contains(name)) { + return name + } + } + return "" + } + + fun getEntry(name: String): String { + for (entry in myMap.entries) { + if (entry.key == name) { + return entry.key + } + } + return "" + } +} +@Test fun runTest() { + assertEquals("val2", Keys.getKey("val2")) + assertEquals("a1", Keys.getValue("a1")) + assertEquals("val1", Keys.getEntry("val1")) + println("OK") +} \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt index ac505c6bb0b..1f8f4a868af 100644 --- a/runtime/src/main/kotlin/kotlin/collections/HashMap.kt +++ b/runtime/src/main/kotlin/kotlin/collections/HashMap.kt @@ -16,6 +16,8 @@ package kotlin.collections +import konan.worker.isFrozen + actual class HashMap private constructor( private var keysArray: Array, private var valuesArray: Array?, // allocated only when actually used, always null in pure HashSet @@ -113,7 +115,8 @@ actual class HashMap private constructor( val cur = keysView return if (cur == null) { val new = HashSet(this) - keysView = new + if (!isFrozen) + keysView = new new } else cur } @@ -122,7 +125,8 @@ actual class HashMap private constructor( val cur = valuesView return if (cur == null) { val new = HashMapValues(this) - valuesView = new + if (!isFrozen) + valuesView = new new } else cur } @@ -131,7 +135,8 @@ actual class HashMap private constructor( val cur = entriesView return if (cur == null) { val new = HashMapEntrySet(this) - entriesView = new + if (!isFrozen) + entriesView = new return new } else cur }