From 947f79dda4c7e254bf6112b6cea9815ff4fb386f Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 8 Dec 2023 11:31:10 -0500 Subject: [PATCH 1/2] Install a global uncaught exception handler --- redwood-treehouse-guest/build.gradle | 12 ++++++ .../redwood/treehouse/PrepareEnvironment.kt | 25 +++++++++++ .../redwood/treehouse/StandardAppLifecycle.kt | 5 +++ .../redwood/treehouse/PrepareEnvironmentJs.kt | 41 +++++++++++++++++++ .../treehouse/PrepareEnvironmentNonJs.kt | 24 +++++++++++ 5 files changed, 107 insertions(+) create mode 100644 redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt create mode 100644 redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt create mode 100644 redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt diff --git a/redwood-treehouse-guest/build.gradle b/redwood-treehouse-guest/build.gradle index 485b23b0f3..9ba84fc7bf 100644 --- a/redwood-treehouse-guest/build.gradle +++ b/redwood-treehouse-guest/build.gradle @@ -25,6 +25,18 @@ kotlin { api projects.redwoodTreehouseGuestCompose } } + nonJsMain { + dependsOn(commonMain) + } + androidMain { + dependsOn(nonJsMain) + } + jvmMain { + dependsOn(nonJsMain) + } + nativeMain { + dependsOn(nonJsMain) + } } } diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt new file mode 100644 index 0000000000..6f4b21c133 --- /dev/null +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironment.kt @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2023 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.treehouse + +import kotlinx.coroutines.CoroutineExceptionHandler + +/** + * Customize the runtime for this Treehouse application. + */ +internal expect fun prepareEnvironment( + coroutineExceptionHandler: CoroutineExceptionHandler, +) diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt index 2ca6f0264c..a1dccc7dc8 100644 --- a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt @@ -33,6 +33,7 @@ public class StandardAppLifecycle( internal val json: Json, internal val widgetVersion: UInt, ) : AppLifecycle { + private var started = false private lateinit var host: Host private lateinit var broadcastFrameClock: BroadcastFrameClock @@ -60,9 +61,13 @@ public class StandardAppLifecycle( internal val coroutineScope = CoroutineScope(coroutineExceptionHandler) override fun start(host: Host) { + check(!started) { "already started" } + this.started = true this.host = host this.broadcastFrameClock = BroadcastFrameClock { host.requestFrame() } this.frameClock = broadcastFrameClock + + prepareEnvironment(coroutineExceptionHandler) } override fun sendFrame(timeNanos: Long) { diff --git a/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt b/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt new file mode 100644 index 0000000000..2cad9cbd28 --- /dev/null +++ b/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 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.treehouse + +import kotlin.coroutines.CoroutineContext +import kotlinx.coroutines.CoroutineExceptionHandler + +/** + * This installs a global coroutine exception handler using an internal API. + * + * This is necessary for uncaught exceptions to be delivered to the host application. Otherwise + * they're logged to the console where they're easily missed. + * + * This assumes we're the [StandardAppLifecycle] owns the entire JS runtime. + */ +@Suppress("INVISIBLE_MEMBER") +internal actual fun prepareEnvironment( + coroutineExceptionHandler: CoroutineExceptionHandler, +) { + kotlinx.coroutines.internal.ensurePlatformExceptionHandlerLoaded( + object : CoroutineExceptionHandler by coroutineExceptionHandler { + override fun handleException(context: CoroutineContext, exception: Throwable) { + coroutineExceptionHandler.handleException(context, exception) + throw kotlinx.coroutines.internal.ExceptionSuccessfullyProcessed + } + }, + ) +} diff --git a/redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt b/redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt new file mode 100644 index 0000000000..77327aa69a --- /dev/null +++ b/redwood-treehouse-guest/src/nonJsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentNonJs.kt @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2023 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.treehouse + +import kotlinx.coroutines.CoroutineExceptionHandler + +internal actual fun prepareEnvironment( + coroutineExceptionHandler: CoroutineExceptionHandler, +) { + // Do nothing. +} From 1e87a9cf6640dcfb10b5dc639e4d4a432b50c23e Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 8 Dec 2023 11:56:56 -0500 Subject: [PATCH 2/2] Reference a coroutines feature request https://github.com/Kotlin/kotlinx.coroutines/issues/3978 --- .../kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt | 1 + .../kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt index a1dccc7dc8..e08118f6ce 100644 --- a/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt +++ b/redwood-treehouse-guest/src/commonMain/kotlin/app/cash/redwood/treehouse/StandardAppLifecycle.kt @@ -63,6 +63,7 @@ public class StandardAppLifecycle( override fun start(host: Host) { check(!started) { "already started" } this.started = true + this.host = host this.broadcastFrameClock = BroadcastFrameClock { host.requestFrame() } this.frameClock = broadcastFrameClock diff --git a/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt b/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt index 2cad9cbd28..68bbb31203 100644 --- a/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt +++ b/redwood-treehouse-guest/src/jsMain/kotlin/app/cash/redwood/treehouse/PrepareEnvironmentJs.kt @@ -26,7 +26,7 @@ import kotlinx.coroutines.CoroutineExceptionHandler * * This assumes we're the [StandardAppLifecycle] owns the entire JS runtime. */ -@Suppress("INVISIBLE_MEMBER") +@Suppress("INVISIBLE_MEMBER") // https://github.com/Kotlin/kotlinx.coroutines/issues/3978 internal actual fun prepareEnvironment( coroutineExceptionHandler: CoroutineExceptionHandler, ) {