How to modify the singleton object in kotlin native - kotlin

I am adding Kotlin native linuxX64 target support to some existing library. Library is compiled successfuly but while running the test cases, I am getting following runtime error:
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.Array#1249428
at kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception (0x271205)
at kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException (0x2711c5)
at kfun:kotlin.native.concurrent.InvalidMutabilityException.<init>(kotlin.String)kotlin.native.concurrent.InvalidMutabilityException (0x272595)
at ThrowInvalidMutabilityException (0x3b0b53)
at (0x3b5733)
Even the Object example given in Kotlin language tutorial is not working on giving the similar runtime exception.
I know the problem is due to the frozen objects. But I could not find the proper way to modify the frozen members of singleton object.

After searching a bit I got the answer. We can update the frozen object using the Atomic Reference.

Related

Soong build Error: this class can only be used as an annotation or as an argument to #UseExperimental

I am Using soong build system with kotlin and want to opt in for experimental APIs to be used in the code
I have added #file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class) in kotling file but upon compiling I get error stated as
error: this class can only be used as an annotation or as an argument to #UseExperim
ental
#file:OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
Can anyone tell me what additional things I need to do to make it work ?

Iterating through Kotlin map from C native export

We have a Kotlin package that we native build and export to C. We have the header file with all the nested struct and pinned-style pointers.
In the Kotlin code, there is a Map which we want to access. We can get a hold of the Kotlin package enum (the key of the Map), but what's the C code for actually indexing into the "kref kotlin Map object" to get to the value in the map?
Basically, we'd like to know how to manipulate Map, List and Array from C code. Actual steps and/or reference doc would be appreciated.
Kotlin/Native compiler does not export any of the collection's functions to the native library API. This decision was taken some time ago, with the idea to minimize the verbosity of the library header. However, this leads to the problem you faced. Right now, the recommended approach is to write wrapper functions in your Kotlin code.For an example of this approach, please see this ticket at the Kotlin issue tracker. I also recommend subscribing to it, to get the updates on the problem's state ASAP. Posting this in case the ticket won't be available for someone:
fun getListElement(list: List<Any?>, index: Int) = list.get(index)
/// function accessing the list element by index

What is .freeze() in Kotlin/Native?

I see yet another function in Kotlin/Native, that does not exist in the Kotlin JVM or JS. What does it?
From Kotlin native's Concurrency docs
Freezing is a runtime operation making given object subgraph immutable, by modifying the object header so that future mutation attempts lead to throwing an InvalidMutabilityException. It is deep, so if an object has a pointer to another objects - transitive closure of such objects will be frozen. Freezing is the one way transformation, frozen objects cannot be unfrozen. Frozen objects have a nice property that due to their immutability, they can be freely shared between multiple workers/threads not breaking the "mutable XOR shared" invariant.
Sharing a more recent and easier explanation from Kotlin Multiplatform Mobile docs here:
The Native runtime adds an extension function freeze() to all classes.
Calling freeze() will freeze an object, and everything referenced by
the object, recursively.
Eg:
data class MoreData(val strData: String, var width: Float)
data class SomeData(val moreData: MoreData, var count: Int)
//...
val sd = SomeData(MoreData("abc", 10.0), 0)
sd.freeze()
freeze() is a one-way operation. You can't unfreeze something.
freeze() is not available in shared Kotlin code, but several libraries
provide expect and actual declarations for using it in shared code.
However, if you're using a concurrency library, like
kotlinx.coroutines, it will likely freeze data that crosses thread
boundaries automatically.
freeze is not unique to Kotlin. You can also
find it in Ruby and JavaScript.

Can not build thisJoinPoint lazily for this advice since it has no suitable guard

What is a "suitable guard" and what does it look like?
Linked this question because it refers to the same compiler message and the answer mentions a guard but not how to create one. Looked through the AspectJ docs but did not find and answer there.
This Lint warning is usually switched off in AJDT (AspectJ Development Tools) within Eclipse, but you can activate it as a warning or even error like this (I had to do it to actually see it at all when trying to reproduce your issue):
You can just ignore the Lint warning because basically it only says that there is no way for certain pointcuts to populate the thisJoinPoint object lazily during runtime because the pointcut has no dynamic component like if(), cflow() or similar, which is actually good news because it means that all your joinpoints can be determined statically during compile/weave time and are thus faster than dynamic pointcuts. On the other hand, the warning says that the tjp object always has to be created because for some reason it is also always needed during runtime and thus cannot be instantiated lazily.

Is it really a limitation to use interfaces such as IList<T>.someMethod in AOT code?

In the mono project documentation this limitation is outlined:
Limitation: Generic Interface Instantiation
The following class of interface dispatch is not supported in FullAOT
mode:
interface IFoo<T> {
...
void SomeMethod ();
}
Since Mono has no way of determining from the static analysis what
method will implement IFoo.SomeMethod this particular pattern is
not supported.
We have been using code like this unbeknownst to this limitation, and are currently attempting to figure out if some stability issues and this are related. This seems to function as expected, and so we are skeptical this is an issue still. Our code compiles to AOT with no errors, and runs without throwing any errors. Is this just old documentation?
An added bonus question: If this isn't supposed to work...why does it work for the built-in C# classes such as IList without issue but it shouldn't work otherwise?