Skip to content

Commit

Permalink
Removed TypeKey
Browse files Browse the repository at this point in the history
  • Loading branch information
IVIanuu committed Feb 21, 2024
1 parent de2c107 commit 538beab
Show file tree
Hide file tree
Showing 9 changed files with 3 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.ivianuu.injekt.common

import com.ivianuu.injekt.*
import kotlinx.atomicfu.locks.*
import kotlin.reflect.*

class Scope<N> : SynchronizedObject() {
@PublishedApi internal val values = hashMapOf<Any, Any?>()
Expand All @@ -15,9 +16,6 @@ class Scope<N> : SynchronizedObject() {
(if (value !== NULL) value else null) as T
}

inline operator fun <T> invoke(key: TypeKey<T> = inject, init: () -> T): T =
invoke(key.value, init)

companion object {
@PublishedApi internal val NULL = Any()
}
Expand All @@ -27,10 +25,9 @@ class Scope<N> : SynchronizedObject() {
@Target(AnnotationTarget.CLASS, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.TYPE)
annotation class Scoped<N> {
@Provide companion object {
@Provide inline fun <@Spread T : @Scoped<N> S, S : Any, N> scoped(
@Provide inline fun <@Spread T : @Scoped<N> S, reified S : Any, N> scoped(
scope: Scope<N>,
key: TypeKey<S>,
crossinline init: () -> T,
): S = scope(key) { init() }
): S = scope(typeOf<S>()) { init() }
}
}
17 changes: 0 additions & 17 deletions common/src/commonMain/kotlin/com/ivianuu/injekt/common/TypeKey.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class Context(val module: ModuleDescriptor, val trace: BindingTrace?) : TypeChec
module.findClassAcrossModuleDependencies(ClassId.topLevel(InjektFqNames.Function))!!
.toClassifierRef(ctx).defaultType.copy(arguments = listOf(STAR_PROJECTION_TYPE))
}
val typeKeyClassifier by lazy(LazyThreadSafetyMode.NONE) {
module.findClassAcrossModuleDependencies(ClassId.topLevel(InjektFqNames.TypeKey))
?.toClassifierRef(ctx)
}
}

private val slices = mutableMapOf<String, BasicWritableSlice<*, *>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ object InjektFqNames {
val InjectablesPackage = InternalPackage.child("injectables".asNameId())
val InjectablesLookup = InjectablesPackage.child("\$\$\$\$\$".asNameId())

val CommonPackage = InjektPackage.child("common".asNameId())
val TypeKey = CommonPackage.child("TypeKey".asNameId())

val Composable = FqName("androidx.compose.runtime.Composable")

val Any = StandardNames.FqNames.any.toSafe()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,6 @@ class LambdaInjectable(
) : ValueParameterDescriptor by delegate
}

class TypeKeyInjectable(
override val type: TypeRef,
override val ownerScope: InjectablesScope
) : Injectable {
override val callableFqName = FqName("typeKeyOf<${type.renderToString()}>")
override val dependencies = type.allTypes
.filter { it.classifier.isTypeParameter }
.mapIndexed { index, typeParameter ->
InjectableRequest(
type = ownerScope.ctx.typeKeyClassifier!!.defaultType
.withArguments(listOf(typeParameter.classifier.defaultType)),
callableFqName = callableFqName,
callableTypeArguments = type.classifier.typeParameters.zip(type.arguments).toMap(),
parameterName = "${typeParameter.classifier.fqName.shortName()}Key".asNameId(),
parameterIndex = index
)
}
}

data class InjectableRequest(
val type: TypeRef,
val callableFqName: FqName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ class InjectablesScope(
collectionElementType = collectionElementType
)
}
request.type.classifier.fqName == InjektFqNames.TypeKey ->
TypeKeyInjectable(request.type, this)
else -> null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class InjectCallTransformer(
is CallableInjectable -> callableExpression(result, candidate)
is LambdaInjectable -> lambdaExpression(result, candidate)
is ListInjectable -> listExpression(result, candidate)
is TypeKeyInjectable -> typeKeyExpression(result, candidate)
}

if (!result.candidate.type.isNullableType ||
Expand Down Expand Up @@ -254,71 +253,6 @@ class InjectCallTransformer(
+irGet(tmpList)
}

private val typeKey = irCtx.referenceClass(InjektFqNames.TypeKey)
private val typeKeyValue = typeKey?.owner?.properties
?.single { it.name.asString() == "value" }
private val typeKeyConstructor = typeKey?.constructors?.single()
private val stringPlus = irCtx.irBuiltIns.stringClass
.functions
.map { it.owner }
.first { it.name.asString() == "plus" }

private fun ScopeContext.typeKeyExpression(
result: ResolutionResult.Success.Value,
injectable: TypeKeyInjectable
): IrExpression = irBuilder.run {
val expressions = mutableListOf<IrExpression>()
var currentString = ""
fun commitCurrentString() {
if (currentString.isNotEmpty()) {
expressions += irString(currentString)
currentString = ""
}
}

fun appendToCurrentString(value: String) {
currentString += value
}

fun appendTypeParameterExpression(expression: IrExpression) {
commitCurrentString()
expressions += expression
}

injectable.type.arguments.single().render(
renderType = { typeToRender ->
if (!typeToRender.classifier.isTypeParameter) true else {
appendTypeParameterExpression(
irCall(typeKeyValue!!.getter!!).apply {
val dependencyResult = result.dependencyResults.values.single {
it.cast<ResolutionResult.Success.Value>()
.candidate.type.arguments.single().classifier == typeToRender.classifier
}
dispatchReceiver = expressionFor(dependencyResult.cast())
}
)
false
}
},
append = { appendToCurrentString(it) }
)

commitCurrentString()

val stringExpression = if (expressions.size == 1) expressions.single()
else expressions.reduce { acc, expression ->
irCall(stringPlus).apply {
dispatchReceiver = acc
putValueArgument(0, expression)
}
}

irCall(typeKeyConstructor!!).apply {
putTypeArgument(0, injectable.type.arguments.single().toIrType(irCtx).cast())
putValueArgument(0, stringExpression)
}
}

private fun ScopeContext.callableExpression(
result: ResolutionResult.Success.Value,
injectable: CallableInjectable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,20 +457,6 @@ class ResolveTest {
"""
)

@Test fun testSmartcastWithInject() = codegen(
"""
class MyType {
fun <T> doSomething(key: TypeKey<T> = inject) {
}
}
fun invoke(myType: MyType?) {
if (myType != null) {
myType.doSomething<String>()
}
}
"""
)

@Test fun testInvocationOfFunctionDeclaredInSuperClassWithInjectParameters() = singleAndMultiCodegen(
"""
open class MySuperClass {
Expand Down

This file was deleted.

0 comments on commit 538beab

Please sign in to comment.