Weak references. (#1459)
This commit is contained in:
@@ -2076,6 +2076,11 @@ task memory_escape2(type: RunKonanTest) {
|
||||
source = "runtime/memory/escape2.kt"
|
||||
}
|
||||
|
||||
task memory_weak0(type: RunKonanTest) {
|
||||
goldValue = "Data(s=Hello)\nnull\nOK\n"
|
||||
source = "runtime/memory/weak0.kt"
|
||||
}
|
||||
|
||||
task mpp1(type: RunStandaloneKonanTest) {
|
||||
source = "codegen/mpp/mpp1.kt"
|
||||
flags = ['-tr', '-Xmulti-platform']
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package runtime.memory.weak0
|
||||
|
||||
import kotlin.test.*
|
||||
import konan.ref.*
|
||||
|
||||
data class Data(val s: String)
|
||||
|
||||
fun localWeak(): WeakReference<Data> {
|
||||
val x = Data("Hello")
|
||||
val weak = WeakReference(x)
|
||||
|
||||
println(weak.get())
|
||||
return weak
|
||||
}
|
||||
|
||||
fun multiWeak(): Array<WeakReference<Data>> {
|
||||
val x = Data("Hello")
|
||||
val weaks = Array(100, { WeakReference(x) } )
|
||||
weaks.forEach {
|
||||
it -> if (it.get()?.s != "Hello") throw Error("bad reference")
|
||||
}
|
||||
return weaks
|
||||
}
|
||||
|
||||
@Test fun runTest() {
|
||||
val weak = localWeak()
|
||||
val value = weak.get()
|
||||
println(value?.toString())
|
||||
|
||||
val weaks = multiWeak()
|
||||
weaks.forEach {
|
||||
it -> if (it.get()?.s != null) throw Error("not null")
|
||||
}
|
||||
println("OK")
|
||||
}
|
||||
Reference in New Issue
Block a user