[JS IR] Allow restriction of function argument by external type

Add a special annotation @JsExternalTypeArgument for
 marking function parameters. The marked parameter
 accepts an argument with an external type only.

^KT-57479 Fixed
This commit is contained in:
Alexander Korepanov
2023-03-21 15:35:52 +01:00
committed by Space Team
parent 4819593bf4
commit e8be3043cc
21 changed files with 394 additions and 1 deletions
@@ -252,3 +252,41 @@ public annotation class EagerInitialization
@Target(CLASS)
@SinceKotlin("1.9")
public annotation class JsExternalInheritorsOnly
/**
* When placed on a function parameter, requires the type of the passed argument to be external.
*
* The compiler mangles identifiers of properties from non-external interfaces and classes,
* and doesn't mangle from external ones. Requiring a type of the passing argument being external is necessary
* to avoid non-obvious bugs when identifier has an unstable and unpredictable name in generated JS code.
*
* Example:
*
* ```kotlin
*
* @OptIn(ExperimentalStdlibApi::class)
* fun extractUuid(@JsExternalArgument x: dynamic) = x.uuid as String
*
* external interface User {
* val uuid: String
* }
*
* interface Owner {
* val uuid: String
* }
*
* fun checkUser(user: User, owner: Owner): Boolean {
* val userUuid = extractUuid(user) // OK
* val ownerUuid = extractUuid(owner) // Compilation error! Possible bug
* return userUuid == ownerUuid
* }
* ```
*
* This annotation is experimental, meaning that the restrictions mentioned above are subject to change.
*/
@ExperimentalStdlibApi
@Retention(AnnotationRetention.BINARY)
@Target(VALUE_PARAMETER)
@SinceKotlin("1.9")
public annotation class JsExternalArgument