From 91c6ee1ebb795178f74a37f977783e97de4a72df Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Fri, 3 Sep 2021 14:58:54 +0300 Subject: [PATCH] [K/N] Replace SymbolName with GCUnsafeCall in stdlib --- .../JsRuntime/src/main/kotlin/jsinterop.kt | 22 +++++----- .../kotlinx/cinterop/ForeignException.kt | 3 +- .../kotlin/kotlinx/cinterop/ObjectiveCImpl.kt | 38 ++++++++--------- .../cinterop/ObjectiveCKClassSupport.kt | 5 ++- .../native/internal/ObjCExportCoroutines.kt | 4 +- .../kotlin/native/internal/ObjCExportUtils.kt | 42 +++++++++---------- 6 files changed, 56 insertions(+), 58 deletions(-) diff --git a/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt b/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt index 894493191c1..a9a6ab77ec7 100644 --- a/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt +++ b/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt @@ -17,7 +17,7 @@ package kotlinx.wasm.jsinterop import kotlin.native.* -import kotlin.native.internal.ExportForCppRuntime +import kotlin.native.internal.* import kotlinx.cinterop.* typealias Arena = Int @@ -28,15 +28,15 @@ typealias Pointer = Int * @Retain annotation is required to preserve functions from internalization and DCE. */ @RetainForTarget("wasm32") -@SymbolName("Konan_js_allocateArena") +@GCUnsafeCall("Konan_js_allocateArena") external public fun allocateArena(): Arena @RetainForTarget("wasm32") -@SymbolName("Konan_js_freeArena") +@GCUnsafeCall("Konan_js_freeArena") external public fun freeArena(arena: Arena) @RetainForTarget("wasm32") -@SymbolName("Konan_js_pushIntToArena") +@GCUnsafeCall("Konan_js_pushIntToArena") external public fun pushIntToArena(arena: Arena, value: Int) const val upperWord = 0xffffffff.toLong() shl 32 @@ -50,15 +50,15 @@ fun doubleLower(value: Double): Int = (value.toBits() and 0x00000000ffffffff) .toInt() @RetainForTarget("wasm32") -@SymbolName("ReturnSlot_getDouble") +@GCUnsafeCall("ReturnSlot_getDouble") external public fun ReturnSlot_getDouble(): Double @RetainForTarget("wasm32") -@SymbolName("Kotlin_String_utf16pointer") +@GCUnsafeCall("Kotlin_String_utf16pointer") external public fun stringPointer(message: String): Pointer @RetainForTarget("wasm32") -@SymbolName("Kotlin_String_utf16length") +@GCUnsafeCall("Kotlin_String_utf16length") external public fun stringLengthBytes(message: String): Int typealias KtFunction = ((ArrayList)->R) @@ -105,19 +105,19 @@ open class JsArray(arena: Arena, index: Object): JsValue(arena, index) { } @RetainForTarget("wasm32") -@SymbolName("Konan_js_getInt") +@GCUnsafeCall("Konan_js_getInt") external public fun getInt(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int; @RetainForTarget("wasm32") -@SymbolName("Konan_js_getProperty") +@GCUnsafeCall("Konan_js_getProperty") external public fun Konan_js_getProperty(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int; @RetainForTarget("wasm32") -@SymbolName("Konan_js_setFunction") +@GCUnsafeCall("Konan_js_setFunction") external public fun setFunction(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int , function: Int) @RetainForTarget("wasm32") -@SymbolName("Konan_js_setString") +@GCUnsafeCall("Konan_js_setString") external public fun setString(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int, stringPtr: Pointer, stringLength: Int ) fun setter(obj: JsValue, property: String, string: String) { diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt index 9872a71a2b6..db94c707f08 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt @@ -5,6 +5,7 @@ package kotlinx.cinterop import kotlin.native.internal.ExportForCppRuntime +import kotlin.native.internal.GCUnsafeCall public class ForeignException internal constructor(val nativeException: Any?): Exception() { override val message: String = nativeException?.let { @@ -12,7 +13,7 @@ public class ForeignException internal constructor(val nativeException: Any?): E }?: "" // Current implementation expects NSException type only, which is ensured by CodeGenerator. - @SymbolName("Kotlin_ObjCExport_ExceptionDetails") + @GCUnsafeCall("Kotlin_ObjCExport_ExceptionDetails") private external fun kotlin_ObjCExport_ExceptionDetails(nativeException: Any): String? } diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt index 1f52e7d1ac5..67b5098138f 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt @@ -18,11 +18,7 @@ package kotlinx.cinterop import kotlin.native.* -import kotlin.native.internal.ExportTypeInfo -import kotlin.native.internal.ExportForCppRuntime -import kotlin.native.internal.TypedIntrinsic -import kotlin.native.internal.IntrinsicType -import kotlin.native.internal.FilterExceptions +import kotlin.native.internal.* interface ObjCObject interface ObjCClass : ObjCObject @@ -63,16 +59,16 @@ private fun ObjCObjectBase.superInitCheck(superInitCallResult: ObjCObject?) { internal fun Any?.uncheckedCast(): T = @Suppress("UNCHECKED_CAST") (this as T) // Note: if this is called for non-frozen object on a wrong worker, the program will terminate. -@SymbolName("Kotlin_Interop_refFromObjC") +@GCUnsafeCall("Kotlin_Interop_refFromObjC") external fun interpretObjCPointerOrNull(objcPtr: NativePtr): T? @ExportForCppRuntime inline fun interpretObjCPointer(objcPtr: NativePtr): T = interpretObjCPointerOrNull(objcPtr)!! -@SymbolName("Kotlin_Interop_refToObjC") +@GCUnsafeCall("Kotlin_Interop_refToObjC") external fun Any?.objcPtr(): NativePtr -@SymbolName("Kotlin_Interop_createKotlinObjectHolder") +@GCUnsafeCall("Kotlin_Interop_createKotlinObjectHolder") external fun createKotlinObjectHolder(any: Any?): NativePtr // Note: if this is called for non-frozen underlying ref on a wrong worker, the program will terminate. @@ -81,7 +77,7 @@ inline fun unwrapKotlinObjectHolder(holder: Any?): T { } @PublishedApi -@SymbolName("Kotlin_Interop_unwrapKotlinObjectHolder") +@GCUnsafeCall("Kotlin_Interop_unwrapKotlinObjectHolder") external internal fun unwrapKotlinObjectHolderImpl(ptr: NativePtr): Any class ObjCObjectVar(rawPtr: NativePtr) : CVariable(rawPtr) { @@ -175,11 +171,11 @@ internal external fun getMessengerStret(superClass: NativePtr): COpaquePointer? internal class ObjCWeakReferenceImpl : kotlin.native.ref.WeakReferenceImpl() { - @SymbolName("Konan_ObjCInterop_getWeakReference") + @GCUnsafeCall("Konan_ObjCInterop_getWeakReference") external override fun get(): Any? } -@SymbolName("Konan_ObjCInterop_initWeakReference") +@GCUnsafeCall("Konan_ObjCInterop_initWeakReference") private external fun ObjCWeakReferenceImpl.init(objcPtr: NativePtr) @kotlin.native.internal.ExportForCppRuntime internal fun makeObjCWeakReferenceImpl(objcPtr: NativePtr): ObjCWeakReferenceImpl { @@ -191,37 +187,37 @@ private external fun ObjCWeakReferenceImpl.init(objcPtr: NativePtr) // Konan runtme: @Deprecated("Use plain Kotlin cast of String to NSString", level = DeprecationLevel.ERROR) -@SymbolName("Kotlin_Interop_CreateNSStringFromKString") +@GCUnsafeCall("Kotlin_Interop_CreateNSStringFromKString") external fun CreateNSStringFromKString(str: String?): NativePtr @Deprecated("Use plain Kotlin cast of NSString to String", level = DeprecationLevel.ERROR) -@SymbolName("Kotlin_Interop_CreateKStringFromNSString") +@GCUnsafeCall("Kotlin_Interop_CreateKStringFromNSString") external fun CreateKStringFromNSString(ptr: NativePtr): String? @PublishedApi -@SymbolName("Kotlin_Interop_CreateObjCObjectHolder") +@GCUnsafeCall("Kotlin_Interop_CreateObjCObjectHolder") internal external fun createObjCObjectHolder(ptr: NativePtr): Any? // Objective-C runtime: -@SymbolName("objc_retainAutoreleaseReturnValue") +@GCUnsafeCall("objc_retainAutoreleaseReturnValue") external fun objc_retainAutoreleaseReturnValue(ptr: NativePtr): NativePtr -@SymbolName("Kotlin_objc_autoreleasePoolPush") +@GCUnsafeCall("Kotlin_objc_autoreleasePoolPush") external fun objc_autoreleasePoolPush(): NativePtr -@SymbolName("Kotlin_objc_autoreleasePoolPop") +@GCUnsafeCall("Kotlin_objc_autoreleasePoolPop") external fun objc_autoreleasePoolPop(ptr: NativePtr) -@SymbolName("Kotlin_objc_allocWithZone") +@GCUnsafeCall("Kotlin_objc_allocWithZone") @FilterExceptions private external fun objc_allocWithZone(clazz: NativePtr): NativePtr -@SymbolName("Kotlin_objc_retain") +@GCUnsafeCall("Kotlin_objc_retain") external fun objc_retain(ptr: NativePtr): NativePtr -@SymbolName("Kotlin_objc_release") +@GCUnsafeCall("Kotlin_objc_release") external fun objc_release(ptr: NativePtr) -@SymbolName("Kotlin_objc_lookUpClass") +@GCUnsafeCall("Kotlin_objc_lookUpClass") external fun objc_lookUpClass(name: NativePtr): NativePtr diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt index cf0f94290e0..880f357680d 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt @@ -6,6 +6,7 @@ package kotlinx.cinterop import kotlin.native.internal.KClassImpl +import kotlin.native.internal.GCUnsafeCall import kotlin.reflect.KClass /** @@ -34,8 +35,8 @@ fun getOriginalKotlinClass(objCProtocol: ObjCProtocol): KClass<*>? { return KClassImpl(typeInfo) } -@SymbolName("Kotlin_ObjCInterop_getTypeInfoForClass") +@GCUnsafeCall("Kotlin_ObjCInterop_getTypeInfoForClass") private external fun getTypeInfoForClass(ptr: NativePtr): NativePtr -@SymbolName("Kotlin_ObjCInterop_getTypeInfoForProtocol") +@GCUnsafeCall("Kotlin_ObjCInterop_getTypeInfoForProtocol") private external fun getTypeInfoForProtocol(ptr: NativePtr): NativePtr \ No newline at end of file diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt index 907a13ef096..a6bb8b3e3e2 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt @@ -61,9 +61,9 @@ internal fun getCoroutineSuspended(): Any = COROUTINE_SUSPENDED internal fun interceptedContinuation(continuation: Continuation): Continuation = continuation.intercepted() @FilterExceptions -@SymbolName("Kotlin_ObjCExport_runCompletionSuccess") +@GCUnsafeCall("Kotlin_ObjCExport_runCompletionSuccess") private external fun runCompletionSuccess(completionHolder: Any, result: Any?) @FilterExceptions -@SymbolName("Kotlin_ObjCExport_runCompletionFailure") +@GCUnsafeCall("Kotlin_ObjCExport_runCompletionFailure") private external fun runCompletionFailure(completionHolder: Any, exception: Throwable, exceptionTypes: NativePtr) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportUtils.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportUtils.kt index 1454a625c26..c7092557b05 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportUtils.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportUtils.kt @@ -20,10 +20,10 @@ internal class NSArrayAsKList : AbstractList(), ObjCObjectWrapper { override val size: Int get() = getSize() - @SymbolName("Kotlin_NSArrayAsKList_getSize") + @GCUnsafeCall("Kotlin_NSArrayAsKList_getSize") private external fun getSize(): Int - @SymbolName("Kotlin_NSArrayAsKList_get") + @GCUnsafeCall("Kotlin_NSArrayAsKList_get") external override fun get(index: Int): Any? } @@ -31,19 +31,19 @@ internal class NSMutableArrayAsKMutableList : AbstractMutableList(), ObjCO override val size: Int get() = getSize() - @SymbolName("Kotlin_NSArrayAsKList_getSize") + @GCUnsafeCall("Kotlin_NSArrayAsKList_getSize") private external fun getSize(): Int - @SymbolName("Kotlin_NSArrayAsKList_get") + @GCUnsafeCall("Kotlin_NSArrayAsKList_get") external override fun get(index: Int): Any? - @SymbolName("Kotlin_NSMutableArrayAsKMutableList_add") + @GCUnsafeCall("Kotlin_NSMutableArrayAsKMutableList_add") external override fun add(index: Int, element: Any?): Unit - @SymbolName("Kotlin_NSMutableArrayAsKMutableList_removeAt") + @GCUnsafeCall("Kotlin_NSMutableArrayAsKMutableList_removeAt") external override fun removeAt(index: Int): Any? - @SymbolName("Kotlin_NSMutableArrayAsKMutableList_set") + @GCUnsafeCall("Kotlin_NSMutableArrayAsKMutableList_set") external override fun set(index: Int, element: Any?): Any? } @@ -51,16 +51,16 @@ internal class NSSetAsKSet : AbstractSet(), KonanSet, ObjCObjectWrap override val size: Int get() = getSize() - @SymbolName("Kotlin_NSSetAsKSet_getSize") + @GCUnsafeCall("Kotlin_NSSetAsKSet_getSize") private external fun getSize(): Int - @SymbolName("Kotlin_NSSetAsKSet_contains") + @GCUnsafeCall("Kotlin_NSSetAsKSet_contains") external override fun contains(element: Any?): Boolean - @SymbolName("Kotlin_NSSetAsKSet_getElement") + @GCUnsafeCall("Kotlin_NSSetAsKSet_getElement") external override fun getElement(element: Any?): Any? - @SymbolName("Kotlin_NSSetAsKSet_iterator") + @GCUnsafeCall("Kotlin_NSSetAsKSet_iterator") external override fun iterator(): Iterator } @@ -88,24 +88,24 @@ internal class NSDictionaryAsKMap : Map, ObjCObjectWrapper { override val size: Int get() = getSize() - @SymbolName("Kotlin_NSDictionaryAsKMap_getSize") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_getSize") private external fun getSize(): Int override fun isEmpty(): Boolean = (size == 0) - @SymbolName("Kotlin_NSDictionaryAsKMap_containsKey") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_containsKey") override external fun containsKey(key: Any?): Boolean - @SymbolName("Kotlin_NSDictionaryAsKMap_containsValue") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_containsValue") override external fun containsValue(value: Any?): Boolean - @SymbolName("Kotlin_NSDictionaryAsKMap_get") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_get") external override operator fun get(key: Any?): Any? - @SymbolName("Kotlin_NSDictionaryAsKMap_getOrThrowConcurrentModification") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_getOrThrowConcurrentModification") private external fun getOrThrowConcurrentModification(key: Any?): Any? - @SymbolName("Kotlin_NSDictionaryAsKMap_containsEntry") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_containsEntry") private external fun containsEntry(key: Any?, value: Any?): Boolean // Views @@ -115,7 +115,7 @@ internal class NSDictionaryAsKMap : Map, ObjCObjectWrapper { override val entries: Set> get() = this.Entries() - @SymbolName("Kotlin_NSDictionaryAsKMap_keyIterator") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_keyIterator") private external fun keyIterator(): Iterator private inner class Keys : AbstractSet() { @@ -127,7 +127,7 @@ internal class NSDictionaryAsKMap : Map, ObjCObjectWrapper { override fun contains(element: Any?): Boolean = this@NSDictionaryAsKMap.containsKey(element) } - @SymbolName("Kotlin_NSDictionaryAsKMap_valueIterator") + @GCUnsafeCall("Kotlin_NSDictionaryAsKMap_valueIterator") private external fun valueIterator(): Iterator private inner class Values : AbstractCollection() { @@ -179,7 +179,7 @@ internal class NSDictionaryAsKMap : Map, ObjCObjectWrapper { internal class NSEnumeratorAsKIterator : AbstractIterator() { - @SymbolName("Kotlin_NSEnumeratorAsKIterator_computeNext") + @GCUnsafeCall("Kotlin_NSEnumeratorAsKIterator_computeNext") override external fun computeNext() @ExportForCppRuntime @@ -279,7 +279,7 @@ class ObjCErrorException( } @PublishedApi -@SymbolName("Kotlin_ObjCExport_trapOnUndeclaredException") +@GCUnsafeCall("Kotlin_ObjCExport_trapOnUndeclaredException") @ExportForCppRuntime internal external fun trapOnUndeclaredException(exception: Throwable)