[JS IR] add a flag which enable safe property access
If an accessor is not available at runtime we fall back to the property access. This is useful in cases when JS objects are casted to Kotlin classes implicitly. This pattern did work in the old BE, which lead to a significant amount of code which doesn't work anymore.
This commit is contained in:
committed by
TeamCityServer
parent
8b18818bcc
commit
6633a9edc0
@@ -94,6 +94,11 @@ private fun buildRoots(modules: Iterable<IrModuleFragment>, context: JsIrBackend
|
||||
rootDeclarations += dceRuntimeDiagnostic.unreachableDeclarationMethod(context).owner
|
||||
}
|
||||
|
||||
if (context.legacyPropertyAccess) {
|
||||
rootDeclarations += context.intrinsics.safePropertyGet.owner
|
||||
rootDeclarations += context.intrinsics.safePropertySet.owner
|
||||
}
|
||||
|
||||
JsMainFunctionDetector.getMainFunctionOrNull(modules.last())?.let { mainFunction ->
|
||||
rootDeclarations += mainFunction
|
||||
if (mainFunction.isSuspend) {
|
||||
|
||||
@@ -315,6 +315,10 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
||||
val readSharedBox = defineReadSharedBox()
|
||||
val writeSharedBox = defineWriteSharedBox()
|
||||
|
||||
|
||||
val safePropertyGet = getInternalFunction("safePropertyGet")
|
||||
val safePropertySet = getInternalFunction("safePropertySet")
|
||||
|
||||
val jsUndefined = defineJsUndefinedIntrinsic()
|
||||
|
||||
// Helpers:
|
||||
|
||||
@@ -47,6 +47,7 @@ class JsIrBackendContext(
|
||||
override val es6mode: Boolean = false,
|
||||
val dceRuntimeDiagnostic: DceRuntimeDiagnostic? = null,
|
||||
val propertyLazyInitialization: Boolean = false,
|
||||
val legacyPropertyAccess: Boolean = false,
|
||||
) : JsCommonBackendContext {
|
||||
val fileToInitializationFuns: MutableMap<IrFile, IrSimpleFunction?> = mutableMapOf()
|
||||
val fileToInitializerPureness: MutableMap<IrFile, Boolean> = mutableMapOf()
|
||||
|
||||
@@ -52,6 +52,7 @@ fun compile(
|
||||
multiModule: Boolean = false,
|
||||
relativeRequirePath: Boolean = false,
|
||||
propertyLazyInitialization: Boolean,
|
||||
legacyPropertyAccess: Boolean = false,
|
||||
): CompilerResult {
|
||||
val (moduleFragment: IrModuleFragment, dependencyModules, irBuiltIns, symbolTable, deserializer) =
|
||||
loadIr(project, mainModule, analyzer, configuration, allDependencies, friendDependencies, irFactory)
|
||||
@@ -73,6 +74,7 @@ fun compile(
|
||||
es6mode = es6mode,
|
||||
dceRuntimeDiagnostic = dceRuntimeDiagnostic,
|
||||
propertyLazyInitialization = propertyLazyInitialization,
|
||||
legacyPropertyAccess = legacyPropertyAccess
|
||||
)
|
||||
|
||||
// Load declarations referenced during `context` initialization
|
||||
|
||||
+21
-2
@@ -110,7 +110,7 @@ fun translateCall(
|
||||
// @JsName-annotated external property accessors are translated as function calls
|
||||
if (function.getJsName() == null) {
|
||||
val property = function.correspondingPropertySymbol?.owner
|
||||
if (property != null && property.isEffectivelyExternal()) {
|
||||
if (property != null && (property.isEffectivelyExternal())) {
|
||||
val nameRef = JsNameRef(context.getNameForProperty(property), jsDispatchReceiver)
|
||||
return when (function) {
|
||||
property.getter -> nameRef
|
||||
@@ -220,7 +220,26 @@ fun translateCall(
|
||||
}
|
||||
}
|
||||
} else {
|
||||
JsInvocation(ref, listOfNotNull(jsExtensionReceiver) + arguments)
|
||||
val defaultResult = JsInvocation(ref, listOfNotNull(jsExtensionReceiver) + arguments)
|
||||
|
||||
val alternativeResult = if (jsDispatchReceiver != null && jsExtensionReceiver == null && context.staticContext.backendContext.legacyPropertyAccess) {
|
||||
val property = function.correspondingPropertySymbol?.owner
|
||||
if (property != null) {
|
||||
val propertyName = context.getNameForProperty(property)
|
||||
val args = mutableListOf(jsDispatchReceiver, JsStringLiteral(symbolName.ident), JsStringLiteral(propertyName.ident))
|
||||
val fnName = when (function) {
|
||||
property.getter -> context.getNameForStaticFunction(context.staticContext.backendContext.intrinsics.safePropertyGet.owner)
|
||||
property.setter -> {
|
||||
args += arguments
|
||||
context.getNameForStaticFunction(context.staticContext.backendContext.intrinsics.safePropertySet.owner)
|
||||
}
|
||||
else -> error("Function must be an accessor of corresponding property")
|
||||
}
|
||||
JsInvocation(fnName.makeRef(), args)
|
||||
} else null
|
||||
} else null
|
||||
|
||||
alternativeResult ?: defaultResult
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user