[K/N][Tests] Migrate test illegal_sharing_with_weak.kt

^KT-61259
This commit is contained in:
Vladimir Sukharev
2024-01-17 22:12:24 +01:00
parent 647a907204
commit 2d0009ff99
9 changed files with 85 additions and 55 deletions
@@ -0,0 +1,61 @@
// TARGET_BACKEND: NATIVE
// DISABLE_NATIVE: isAppleTarget=false
// MODULE: cinterop
// FILE: objclib.def
language = Objective-C
headers = objclib.h
headerFilter = objclib.h
// FILE: objclib.h
#import <objc/NSObject.h>
static NSObject* __weak globalObject = nil;
void setObject(NSObject* obj) {
globalObject = obj;
}
// Make sure this function persists, because the test expects to find this function in the stack trace.
__attribute__((noinline))
bool isObjectAliveShouldCrash() {
return globalObject != nil;
}
// MODULE: main(cinterop)
// FILE: main.kt
@file:OptIn(ObsoleteWorkersApi::class, kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.native.concurrent.*
import kotlin.test.*
import kotlinx.cinterop.autoreleasepool
import objclib.*
val sb = StringBuilder()
fun box(): String {
autoreleasepool {
run()
}
// Experimental MM supports arbitrary object sharing.
assertEquals("Before\nAfter true\n", sb.toString())
return "OK"
}
private class NSObjectImpl : NSObject() {
var x = 111
}
fun run() = withWorker {
val obj = NSObjectImpl()
setObject(obj)
sb.appendLine("Before")
val isAlive = try {
execute(TransferMode.SAFE, {}) {
isObjectAliveShouldCrash()
}.result
} catch (e: Throwable) {
false
}
sb.appendLine("After $isAlive")
}