Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all uses of freeze #1308

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ import realm_wrapper.realm_version_id_t
import kotlin.collections.set
import kotlin.native.internal.createCleaner

private inline fun <T> T.freeze(): T {
// Disable freeze in 1.7.20
return this
}

@SharedImmutable
actual val INVALID_CLASS_KEY: ClassKey by lazy { ClassKey(realm_wrapper.RLM_INVALID_CLASS_KEY.toLong()) }
@SharedImmutable
Expand Down Expand Up @@ -185,7 +180,7 @@ class CPointerWrapper<T : CapiT>(ptr: CPointer<out CPointed>?, managed: Boolean

@OptIn(ExperimentalStdlibApi::class)
val cleaner = if (managed) {
createCleaner(_ptr.freeze()) {
createCleaner(_ptr) {
if (released.compareAndSet(expect = false, update = true)) {
realm_release(ptr)
}
Expand Down Expand Up @@ -482,7 +477,7 @@ actual object RealmInterop {
val callback = DataInitializationCallback {
fileCreated.value = true
true
}.freeze()
}
realm_wrapper.realm_config_set_data_initialization_function(
config.cptr(),
staticCFunction { userdata, _ ->
Expand Down Expand Up @@ -2270,7 +2265,7 @@ actual object RealmInterop {
val threadId = safeUserData<String>(userdata)
throw Error("[SyncThread-$threadId] Error on sync thread: ${error?.toKString()}")
},
user_data = StableRef.create(appId.freeze()).asCPointer(),
user_data = StableRef.create(appId).asCPointer(),
free_userdata = staticCFunction { userdata ->
disposeUserData<String>(userdata)
}
Expand All @@ -2294,7 +2289,7 @@ actual object RealmInterop {
val userDataLogCallback = safeUserData<SyncLogCallback>(userData)
userDataLogCallback.log(logLevel.toShort(), message?.toKString())
},
StableRef.create(callback.freeze()).asCPointer(),
StableRef.create(callback).asCPointer(),
staticCFunction { userData -> disposeUserData<() -> SyncLogCallback>(userData) }
)
}
Expand Down Expand Up @@ -2427,7 +2422,7 @@ actual object RealmInterop {
false
}
},
StableRef.create(beforeHandler.freeze()).asCPointer(),
StableRef.create(beforeHandler).asCPointer(),
staticCFunction { userdata ->
disposeUserData<SyncBeforeClientResetHandler>(userdata)
}
Expand Down Expand Up @@ -2457,7 +2452,7 @@ actual object RealmInterop {
false
}
},
StableRef.create(afterHandler.freeze()).asCPointer(),
StableRef.create(afterHandler).asCPointer(),
staticCFunction { userdata ->
disposeUserData<SyncAfterClientResetHandler>(userdata)
}
Expand Down Expand Up @@ -3258,7 +3253,7 @@ actual object RealmInterop {
)
) ?: error("Couldn't create scheduler")
scheduler.set_scheduler(capi_scheduler)
scheduler.freeze()
scheduler
return capi_scheduler
}

Expand Down Expand Up @@ -3380,7 +3375,7 @@ actual object RealmInterop {
scope.launch(
scope.coroutineContext,
CoroutineStart.DEFAULT,
function.freeze()
function
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.interop.RealmSchemaPointer
import io.realm.kotlin.internal.interop.SchemaMode
import io.realm.kotlin.internal.platform.appFilesDirectory
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.internal.platform.prepareRealmFilePath
import io.realm.kotlin.internal.platform.realmObjectCompanionOrThrow
import io.realm.kotlin.internal.util.CoroutineDispatcherFactory
Expand Down Expand Up @@ -139,7 +138,7 @@ public open class ConfigurationImpl constructor(
override fun invoke(totalBytes: Long, usedBytes: Long): Boolean {
return callback.shouldCompact(totalBytes, usedBytes)
}
}.freeze()
}
}

// We need to prepare the the migration callback so it can be frozen for Kotlin Native, but
Expand Down Expand Up @@ -208,7 +207,7 @@ public open class ConfigurationImpl constructor(
)

migrationCallback?.let {
RealmInterop.realm_config_set_migration_function(nativeConfig, it.freeze())
RealmInterop.realm_config_set_migration_function(nativeConfig, it)
}

userEncryptionKey?.let { key: ByteArray ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,30 +106,6 @@ public expect fun prepareRealmFilePath(directoryPath: String, filename: String):
*/
public expect fun createDefaultSystemLogger(tag: String, logLevel: LogLevel = LogLevel.NONE): RealmLogger

/**
* Method to freeze state.
* Calls the platform implementation of 'freeze' on native, and is a noop on other platforms.
*
* Note, this method refers to Kotlin Natives notion of frozen objects, and not Realms variant
* of frozen objects.
*
* From Kotlin 1.7.20 freeze is deprecated, so this is a no-op on all platforms.
*/
public expect fun <T> T.freeze(): T

/**
* Determine if object is frozen.
* Will return false on non-native platforms.
*/
public expect val <T> T.isFrozen: Boolean

/**
* Call on an object which should never be frozen.
* Will help debug when something inadvertently is.
* This is a noop on non-native platforms.
*/
public expect fun Any.ensureNeverFrozen()

/**
* Return the current thread id.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ internal actual fun currentTime(): RealmInstant {
return RealmInstantImpl(jtInstant.epochSecond, jtInstant.nano)
}

public actual fun <T> T.freeze(): T = this

public actual val <T> T.isFrozen: Boolean
get() = false

public actual fun Any.ensureNeverFrozen() { /* Do nothing */ }

public actual fun fileExists(path: String): Boolean =
File(path).let { it.exists() && it.isFile }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ internal actual fun currentTime(): RealmInstant {
}
}

public actual fun <T> T.freeze(): T = this

public actual val <T> T.isFrozen: Boolean
get() = false

public actual fun Any.ensureNeverFrozen() {
/* Do nothing */
}

public actual fun fileExists(path: String): Boolean {
val fm = platform.Foundation.NSFileManager.defaultManager
memScoped {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import io.realm.kotlin.internal.platform.canWrite
import io.realm.kotlin.internal.platform.createDefaultSystemLogger
import io.realm.kotlin.internal.platform.directoryExists
import io.realm.kotlin.internal.platform.fileExists
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.internal.platform.prepareRealmDirectoryPath
import io.realm.kotlin.internal.util.CoroutineDispatcherFactory
import io.realm.kotlin.internal.util.Validation
Expand Down Expand Up @@ -284,7 +283,7 @@ public interface AppConfiguration {
appLogger.debug(message)
}
}
).freeze() // Kotlin network client needs to be frozen before passed to the C-API
)
}

return AppConfigurationImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import io.realm.kotlin.ext.asBsonObjectId
import io.realm.kotlin.internal.interop.ErrorCode
import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.interop.sync.ApiKeyWrapper
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.internal.util.use
import io.realm.kotlin.mongodb.auth.ApiKey
import io.realm.kotlin.mongodb.auth.ApiKeyAuth
Expand Down Expand Up @@ -48,7 +47,7 @@ internal class ApiKeyAuthImpl(override val app: AppImpl, override val user: User
name,
channelResultCallback<ApiKeyWrapper, ApiKey>(channel) { apiKeyData ->
unwrap(apiKeyData)
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand Down Expand Up @@ -76,7 +75,7 @@ internal class ApiKeyAuthImpl(override val app: AppImpl, override val user: User
id,
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive().getOrThrow()
}
Expand All @@ -100,7 +99,7 @@ internal class ApiKeyAuthImpl(override val app: AppImpl, override val user: User
id,
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -125,7 +124,7 @@ internal class ApiKeyAuthImpl(override val app: AppImpl, override val user: User
id,
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -150,7 +149,7 @@ internal class ApiKeyAuthImpl(override val app: AppImpl, override val user: User
id,
channelResultCallback<ApiKeyWrapper, ApiKey?>(channel) { apiKeyData: ApiKeyWrapper ->
unwrap(apiKeyData)
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -177,7 +176,7 @@ internal class ApiKeyAuthImpl(override val app: AppImpl, override val user: User
)
}
result
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import io.realm.kotlin.internal.platform.OS_VERSION
import io.realm.kotlin.internal.platform.RUNTIME
import io.realm.kotlin.internal.platform.RUNTIME_VERSION
import io.realm.kotlin.internal.platform.appFilesDirectory
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.mongodb.AppConfiguration
import io.realm.kotlin.mongodb.AppConfiguration.Companion.DEFAULT_BASE_URL

Expand Down Expand Up @@ -135,7 +134,7 @@ public class AppConfigurationImpl constructor(
framework = RUNTIME,
frameworkVersion = RUNTIME_VERSION
)
).freeze()
)
}

private fun initializeSyncClientConfig(sdkInfo: String?, applicationInfo: String?): RealmSyncClientConfigurationPointer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import io.realm.kotlin.internal.interop.RealmAppT
import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.interop.RealmUserPointer
import io.realm.kotlin.internal.interop.sync.NetworkTransport
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.internal.util.Validation
import io.realm.kotlin.internal.util.use
import io.realm.kotlin.mongodb.App
Expand Down Expand Up @@ -71,7 +70,7 @@ public class AppImpl(
Validation.checkType<CredentialsImpl>(credentials, "credentials").nativePointer,
channelResultCallback<RealmUserPointer, User>(channel) { userPointer ->
UserImpl(userPointer, this)
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.realm.kotlin.mongodb.internal

import io.realm.kotlin.internal.interop.RealmAppPointer
import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.internal.util.Validation
import io.realm.kotlin.internal.util.use
import io.realm.kotlin.mongodb.auth.EmailPasswordAuth
Expand All @@ -20,7 +19,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Validation.checkEmpty(password, "password"),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -35,7 +34,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Validation.checkEmpty(tokenId, "tokenId"),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -49,7 +48,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Validation.checkEmpty(email, "email"),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -63,7 +62,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Validation.checkEmpty(email, "email"),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -77,7 +76,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Validation.checkEmpty(email, "email"),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand All @@ -94,7 +93,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Bson.toJson(bsonValue),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
}
return channel.receive()
Expand All @@ -111,7 +110,7 @@ internal class EmailPasswordAuthImpl(private val app: RealmAppPointer) : EmailPa
Validation.checkEmpty(tokenId, "tokenId"),
channelResultCallback<Unit, Unit>(channel) {
// No-op
}.freeze()
}
)
return channel.receive()
.getOrThrow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.logging.LogLevel
import io.ktor.client.plugins.logging.Logger
import io.ktor.client.plugins.logging.Logging
import io.realm.kotlin.internal.platform.freeze

/**
* Work-around for https://github.com/realm/realm-kotlin/issues/480
Expand All @@ -18,7 +17,7 @@ internal fun createClient(timeoutMs: Long, customLogger: Logger?): HttpClient {
// Need to freeze value as it is used inside the client's init lambda block, which also
// freezes captured objects too, see:
// https://youtrack.jetbrains.com/issue/KTOR-1223#focus=Comments-27-4618681.0-0
val frozenTimeout = timeoutMs.freeze()
val frozenTimeout = timeoutMs
return createPlatformClient {
// Charset defaults to UTF-8 (https://ktor.io/docs/http-plain-text.html#configuration)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import io.realm.kotlin.internal.interop.RealmInterop
import io.realm.kotlin.internal.interop.RealmSubscriptionSetPointer
import io.realm.kotlin.internal.interop.SubscriptionSetCallback
import io.realm.kotlin.internal.interop.sync.CoreSubscriptionSetState
import io.realm.kotlin.internal.platform.freeze
import io.realm.kotlin.internal.util.Validation
import io.realm.kotlin.mongodb.exceptions.BadFlexibleSyncQueryException
import io.realm.kotlin.mongodb.sync.MutableSubscriptionSet
Expand Down Expand Up @@ -100,7 +99,7 @@ internal class SubscriptionSetImpl<T : BaseRealm>(
}
}
}
}.freeze()
}
RealmInterop.realm_sync_on_subscriptionset_state_change_async(
nativePointer,
CoreSubscriptionSetState.RLM_SYNC_SUBSCRIPTION_COMPLETE,
Expand Down
Loading