Skip to content

Commit

Permalink
Eliminate pointless covariant return in protocol widgets (#1637)
Browse files Browse the repository at this point in the history
This caused a method in the bytecode that was never used.

For example:

     public final class com.example.redwood.testing.widget.redwoodlayout.ProtocolBox<W> implements app.cash.redwood.protocol.widget.ProtocolNode<W> {
    -  private final app.cash.redwood.layout.widget.Box<W> widget;
    +  private final app.cash.redwood.layout.widget.Box<W> _widget;
       private final kotlinx.serialization.json.Json json;
       private final app.cash.redwood.protocol.widget.ProtocolMismatchHandler mismatchHandler;
       private app.cash.redwood.widget.Widget$Children<W> container;
       private final kotlinx.serialization.KSerializer<app.cash.redwood.layout.api.Constraint> serializer_0;
       private final kotlinx.serialization.KSerializer<app.cash.redwood.ui.Margin> serializer_1;
       private final kotlinx.serialization.KSerializer<app.cash.redwood.layout.api.CrossAxisAlignment> serializer_2;
       public com.example.redwood.testing.widget.redwoodlayout.ProtocolBox(app.cash.redwood.layout.widget.Box<W>, kotlinx.serialization.json.Json, app.cash.redwood.protocol.widget.ProtocolMismatchHandler);
    -  public app.cash.redwood.layout.widget.Box<W> getWidget();
    -  public app.cash.redwood.widget.Widget getWidget();
    +  public app.cash.redwood.widget.Widget<W> getWidget();
       public void apply(app.cash.redwood.protocol.PropertyChange, app.cash.redwood.protocol.EventSink);
       public app.cash.redwood.widget.Widget$Children<W> children-dBpC-2Y(int);
       public void updateModifier(java.util.List<app.cash.redwood.protocol.ModifierElement>);
       public void attachTo(app.cash.redwood.widget.Widget$Children<W>);
     }
  • Loading branch information
JakeWharton authored Oct 26, 2023
1 parent 8720e04 commit 9ce959c
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,28 @@ internal fun generateProtocolNodeFactory(

/*
internal class ProtocolButton<W : Any>(
override val widget: Button<W>,
widget: Button<W>,
private val json: Json,
private val mismatchHandler: ProtocolMismatchHandler,
) : ProtocolNode<W> {
private var _widget: Button<W> = widget
override val widget: Widget<W> get() = _widget
private var container: Widget.Children<W>? = null
private val serializer_0: KSerializer<String?> = json.serializersModule.serializer()
private val serializer_1: KSerializer<Boolean> = json.serializersModule.serializer()
public override fun apply(change: PropertyChange, eventSink: EventSink): Unit {
when (change.tag.value) {
1 -> widget.text(json.decodeFromJsonElement(serializer_0, change.value))
2 -> widget.enabled(json.decodeFromJsonElement(serializer_1, change.value))
1 -> _widget.text(json.decodeFromJsonElement(serializer_0, change.value))
2 -> _widget.enabled(json.decodeFromJsonElement(serializer_1, change.value))
3 -> {
val onClick: (() -> Unit)? = if (change.value.jsonPrimitive.boolean) {
{ eventSink.sendEvent(Event(change.id, EventTag(3))) }
} else {
null
}
widget.onClick(onClick)
_widget.onClick(onClick)
}
else -> mismatchHandler.onUnknownProperty(WidgetTag(12), change.tag)
}
Expand All @@ -174,7 +177,7 @@ internal class ProtocolButton<W : Any>(
}
public override fun updateModifiers() {
widget.modifier = elements.toModifiers(json, mismatchHandler)
_widget.modifier = elements.toModifiers(json, mismatchHandler)
container?.onModifierUpdated()
}
Expand Down Expand Up @@ -207,10 +210,19 @@ internal fun generateProtocolNode(
.build(),
)
.addProperty(
PropertySpec.builder("widget", widgetType, OVERRIDE)
PropertySpec.builder("_widget", widgetType, PRIVATE)
.initializer("widget")
.build(),
)
.addProperty(
PropertySpec.builder("widget", RedwoodWidget.Widget.parameterizedBy(typeVariableW), OVERRIDE)
.getter(
FunSpec.getterBuilder()
.addStatement("return _widget")
.build(),
)
.build(),
)
.addProperty(
PropertySpec.builder("json", KotlinxSerialization.Json, PRIVATE)
.initializer("json")
Expand Down Expand Up @@ -249,7 +261,7 @@ internal fun generateProtocolNode(
}

addStatement(
"%L -> widget.%N(json.decodeFromJsonElement(serializer_%L, change.value))",
"%L -> _widget.%N(json.decodeFromJsonElement(serializer_%L, change.value))",
trait.tag,
trait.name,
serializerId,
Expand Down Expand Up @@ -298,7 +310,7 @@ internal fun generateProtocolNode(
addStatement("throw %T()", Stdlib.AssertionError)
}
endControlFlow()
addStatement("widget.%1N(%1N)", trait.name)
addStatement("_widget.%1N(%1N)", trait.name)
endControlFlow()
}

Expand Down Expand Up @@ -336,7 +348,7 @@ internal fun generateProtocolNode(
if (childrens.isNotEmpty()) {
beginControlFlow("return when (tag.value)")
for (children in childrens) {
addStatement("%L -> widget.%N", children.tag, children.name)
addStatement("%L -> _widget.%N", children.tag, children.name)
}
beginControlFlow("else ->")
addStatement(
Expand All @@ -363,7 +375,7 @@ internal fun generateProtocolNode(
FunSpec.builder("updateModifier")
.addModifiers(OVERRIDE)
.addParameter("elements", LIST.parameterizedBy(Protocol.ModifierElement))
.addStatement("widget.modifier = elements.%M(json, mismatchHandler)", host.toModifier)
.addStatement("_widget.modifier = elements.%M(json, mismatchHandler)", host.toModifier)
.addStatement("container?.onModifierUpdated()")
.build(),
)
Expand Down

0 comments on commit 9ce959c

Please sign in to comment.