From 956fef182a621cc6c262b606b2cc0c76948e931c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Thu, 7 Aug 2025 21:39:58 +0200 Subject: [PATCH] feat(weak): Support WasmJS --- weak/build.gradle.kts | 5 ++ weak/src/wasmJsMain/kotlin/WeakMap.wasmjs.kt | 38 +++++++++++++ weak/src/wasmJsMain/kotlin/WeakRef.wasmjs.kt | 56 ++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 weak/src/wasmJsMain/kotlin/WeakMap.wasmjs.kt create mode 100644 weak/src/wasmJsMain/kotlin/WeakRef.wasmjs.kt diff --git a/weak/build.gradle.kts b/weak/build.gradle.kts index 7d5c7c6..ac9b313 100644 --- a/weak/build.gradle.kts +++ b/weak/build.gradle.kts @@ -41,6 +41,11 @@ kotlin { tvosArm64() tvosSimulatorArm64() mingwX64() + @Suppress("OPT_IN_USAGE") + wasmJs { + browser() + nodejs() + } sourceSets.jsMain.dependencies { implementation(libs.kotlinJs) diff --git a/weak/src/wasmJsMain/kotlin/WeakMap.wasmjs.kt b/weak/src/wasmJsMain/kotlin/WeakMap.wasmjs.kt new file mode 100644 index 0000000..e1251e4 --- /dev/null +++ b/weak/src/wasmJsMain/kotlin/WeakMap.wasmjs.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025, OpenSavvy and contributors. + * + * 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 opensavvy.pedestal.weak + +import opensavvy.pedestal.weak.algorithms.WeakKeyArrayMap + +/** + * Instantiates a new, empty [WeakMap]. + * + * This implementation uses [WeakKeyArrayMap]. + */ +@ExperimentalWeakApi +actual fun WeakMap(): WeakMap = + WeakKeyArrayMap() + +/** + * Instantiates a new [WeakMap] by copying [values]. + * + * This implementation uses [WeakKeyArrayMap]. + */ +@ExperimentalWeakApi +actual fun WeakMap(values: Map): WeakMap = + WeakKeyArrayMap() + .also { it.setAll(values) } diff --git a/weak/src/wasmJsMain/kotlin/WeakRef.wasmjs.kt b/weak/src/wasmJsMain/kotlin/WeakRef.wasmjs.kt new file mode 100644 index 0000000..e7d95f8 --- /dev/null +++ b/weak/src/wasmJsMain/kotlin/WeakRef.wasmjs.kt @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025, OpenSavvy and contributors. + * + * 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 opensavvy.pedestal.weak + +import opensavvy.pedestal.weak.algorithms.EmptyWeakRef + +@JsName("WeakRef") +private external class JsWeakRef { + constructor(target: JsAny) + + fun deref(): JsAny? +} + +private class WrappedWeakRef( + target: T, +) : WeakRef { + val js = JsWeakRef(target.toJsReference()) + + override fun read(): T? = + js.deref()?.unsafeCast>()?.get() + + override fun toString() = + js.toString() +} + +/** + * Implementation of [WeakRef] backed by a JS [WeakRef](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef). + * + * JS doesn't make a difference between weak and soft references. + */ +actual fun WeakRef(value: T): WeakRef = + if (value == null) EmptyWeakRef() + else WrappedWeakRef(value) + +/** + * Implementation of [WeakRef] backed by a JS [WeakRef](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef). + * + * JS doesn't make a difference between weak and soft references. + */ +@Suppress("FunctionName") +actual fun SoftRef(value: T): WeakRef = + WeakRef(value) -- 2.51.2