-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use unsafe cast in inline Compose code (#2349)
Regular casts emit calls into Kotlin's isInterface function and ClassCastException-throwing function. We know these are the types, though, and unsafe cast allows eliminating all that inline code. Example diff: if ($composer_1.get_inserting_25mlsw_k$()) { - var tmp = ensureNotNull($composer_1.get_applier_bupu8u_k$()); - var applier = isInterface(tmp, RedwoodApplier) ? tmp : THROW_CCE(); + var applier = $composer_1.get_applier_bupu8u_k$(); var tmp_0 = $composer_1; tmp_0.createNode_ahrd54_k$(function () { - // Inline function 'com.example.redwood.testapp.compose.Button.<anonymous>' call - var tmp = applier.get_widgetSystem_mxid3w_k$(); - var tmp$ret$0 = (isInterface(tmp, WidgetFactoryOwner) ? tmp : THROW_CCE()).get_TestSchema_txdklg_k$().Button_x3fioi_k$(); - return new WidgetNode(applier, tmp$ret$0); + var tmp$ret$6 = applier.get_widgetSystem_mxid3w_k$().get_TestSchema_txdklg_k$().Button_x3fioi_k$(); + return new WidgetNode(applier, tmp$ret$6); }); } else { $composer_1.useNode_io5s9l_k$();
- Loading branch information
1 parent
95ad195
commit 631cdad
Showing
6 changed files
with
99 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,9 @@ final fun app.cash.redwood.compose/app_cash_redwood_compose_ChildrenNode$stablep | |
final fun app.cash.redwood.compose/app_cash_redwood_compose_NodeApplier$stableprop_getter(): kotlin/Int // app.cash.redwood.compose/app_cash_redwood_compose_NodeApplier$stableprop_getter|app_cash_redwood_compose_NodeApplier$stableprop_getter(){}[0] | ||
final fun app.cash.redwood.compose/app_cash_redwood_compose_RedwoodComposeContent$stableprop_getter(): kotlin/Int // app.cash.redwood.compose/app_cash_redwood_compose_RedwoodComposeContent$stableprop_getter|app_cash_redwood_compose_RedwoodComposeContent$stableprop_getter(){}[0] | ||
final fun app.cash.redwood.compose/app_cash_redwood_compose_WidgetNode$stableprop_getter(): kotlin/Int // app.cash.redwood.compose/app_cash_redwood_compose_WidgetNode$stableprop_getter|app_cash_redwood_compose_WidgetNode$stableprop_getter(){}[0] | ||
final inline fun <#A: app.cash.redwood.widget/WidgetFactoryOwner<#B>, #B: kotlin/Any> (app.cash.redwood.compose/RedwoodApplier<#B>).app.cash.redwood.compose/widgetSystem(): #A // app.cash.redwood.compose/widgetSystem|[email protected]<0:1>(){0§<app.cash.redwood.widget.WidgetFactoryOwner<0:1>>;1§<kotlin.Any>}[0] | ||
final inline fun <#A: app.cash.redwood.widget/WidgetFactoryOwner<#C>, #B: app.cash.redwood.widget/Widget<#C>, #C: kotlin/Any> app.cash.redwood.compose/RedwoodComposeNode(crossinline kotlin/Function1<#A, #B>, kotlin/Function1<androidx.compose.runtime/Updater<app.cash.redwood.compose/WidgetNode<#B, #C>>, kotlin/Unit>, kotlin/Function3<app.cash.redwood.compose/RedwoodComposeContent<#B>, androidx.compose.runtime/Composer, kotlin/Int, kotlin/Unit>, androidx.compose.runtime/Composer?, kotlin/Int) // app.cash.redwood.compose/RedwoodComposeNode|RedwoodComposeNode(kotlin.Function1<0:0,0:1>;kotlin.Function1<androidx.compose.runtime.Updater<app.cash.redwood.compose.WidgetNode<0:1,0:2>>,kotlin.Unit>;kotlin.Function3<app.cash.redwood.compose.RedwoodComposeContent<0:1>,androidx.compose.runtime.Composer,kotlin.Int,kotlin.Unit>;androidx.compose.runtime.Composer?;kotlin.Int){0§<app.cash.redwood.widget.WidgetFactoryOwner<0:2>>;1§<app.cash.redwood.widget.Widget<0:2>>;2§<kotlin.Any>}[0] | ||
final inline fun <#A: kotlin/Any> (androidx.compose.runtime/Composer).app.cash.redwood.compose/redwoodApplier(): app.cash.redwood.compose/RedwoodApplier<#A> // app.cash.redwood.compose/redwoodApplier|[email protected](){0§<kotlin.Any>}[0] | ||
|
||
// Targets: [ios] | ||
final object app.cash.redwood.compose/DisplayLinkClock : androidx.compose.runtime/MonotonicFrameClock { // app.cash.redwood.compose/DisplayLinkClock|null[0] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
redwood-compose/src/jsMain/kotlin/app/cash/redwood/compose/actuals.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.compose | ||
|
||
import androidx.compose.runtime.Composer | ||
import app.cash.redwood.RedwoodCodegenApi | ||
import app.cash.redwood.widget.WidgetFactoryOwner | ||
|
||
@PublishedApi | ||
@RedwoodCodegenApi | ||
@Suppress("NOTHING_TO_INLINE") | ||
internal actual inline fun <V : Any> Composer.redwoodApplier(): RedwoodApplier<V> { | ||
return applier.unsafeCast<RedwoodApplier<V>>() | ||
} | ||
|
||
@PublishedApi | ||
@RedwoodCodegenApi | ||
@Suppress("NOTHING_TO_INLINE") | ||
internal actual inline fun <O : WidgetFactoryOwner<V>, V : Any> RedwoodApplier<V>.widgetSystem(): O { | ||
return widgetSystem.unsafeCast<O>() | ||
} |
42 changes: 42 additions & 0 deletions
42
redwood-compose/src/nonJsMain/kotlin/app/cash/redwood/compose/actuals.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.compose | ||
|
||
import androidx.compose.runtime.Composer | ||
import app.cash.redwood.RedwoodCodegenApi | ||
import app.cash.redwood.widget.WidgetFactoryOwner | ||
|
||
@PublishedApi | ||
@RedwoodCodegenApi | ||
@Suppress("NOTHING_TO_INLINE") | ||
internal actual inline fun <V : Any> Composer.redwoodApplier(): RedwoodApplier<V> { | ||
// Perform an explicit !! on the return value to avoid the Kotlin compiler inserting a huge | ||
// string into the output as an error message for an otherwise implicit null check. | ||
@Suppress( | ||
// Safe so long as you use generated composition function. | ||
"UNCHECKED_CAST", | ||
"UNNECESSARY_NOT_NULL_ASSERTION", | ||
) | ||
return applier!! as RedwoodApplier<V> | ||
} | ||
|
||
@PublishedApi | ||
@RedwoodCodegenApi | ||
@Suppress("NOTHING_TO_INLINE") | ||
internal actual inline fun <O : WidgetFactoryOwner<V>, V : Any> RedwoodApplier<V>.widgetSystem(): O { | ||
@Suppress("UNCHECKED_CAST") | ||
return widgetSystem as O | ||
} |