diff --git a/build.gradle.kts b/build.gradle.kts index e4a81f18f..fc398cf98 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -73,6 +73,7 @@ allprojects { mavenLocal() mavenCentral() maven { url = uri("https://maven.pkg.jetbrains.space/public/p/kotlinx-html/maven") } + maven { url = uri("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")} google() } diff --git a/buildSrc/src/main/kotlin/nativeTargets.kt b/buildSrc/src/main/kotlin/nativeTargets.kt index 3a685ac11..8aeef43ff 100644 --- a/buildSrc/src/main/kotlin/nativeTargets.kt +++ b/buildSrc/src/main/kotlin/nativeTargets.kt @@ -29,6 +29,7 @@ import org.gradle.kotlin.dsl.getByName import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset +import org.jetbrains.kotlin.gradle.plugin.extraProperties import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractKotlinNativeTargetPreset import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.konan.target.HostManager @@ -105,22 +106,21 @@ fun Project.addNativeTargets() { if (nativeState != NativeState.HOST || host == Host.Macos) { macosX64 { addSourceSets() } + macosArm64 { addSourceSets() } iosArm64 { addSourceSets() } - iosArm32 { addSourceSets() } + iosSimulatorArm64 { addSourceSets() } iosX64 { addSourceSets() } + iosArm32 { addSourceSets() } watchosX86 { addSourceSets() } + watchosSimulatorArm64() { addSourceSets() } watchosX64 { addSourceSets() } watchosArm32 { addSourceSets() } watchosArm64 { addSourceSets() } + tvosSimulatorArm64 { addSourceSets() } tvosArm64 { addSourceSets() } tvosX64 { addSourceSets() } - - addTarget("iosSimulatorArm64") - addTarget("watchosSimulatorArm64") - addTarget("tvosSimulatorArm64") - addTarget("macosArm64") } if (nativeState != NativeState.HOST || host == Host.Windows) { @@ -155,3 +155,5 @@ private fun KotlinMultiplatformExtension.targets(configure: Action): Unit = private fun KotlinMultiplatformExtension.sourceSets(configure: Action>): Unit = (this as ExtensionAware).extensions.configure("sourceSets", configure) + +val Project.isWasmSupported: Boolean get() = extraProperties.get("xmlutil.wasmEnabled")?.toString()?.toLowerCase() == "true" diff --git a/core/build.gradle.kts b/core/build.gradle.kts index c5816f226..70c490f3d 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -106,7 +106,7 @@ kotlin { } } } - js(BOTH) { + js { browser() compilations.all { kotlinOptions { @@ -120,6 +120,18 @@ kotlin { } } } + if (isWasmSupported) { + wasm { + nodejs() + browser() + compilations.all { + kotlinOptions { + sourceMap = true + verbose = true + } + } + } + } } targets.forEach { target -> @@ -148,7 +160,7 @@ kotlin { } val javaShared by creating { - dependsOn(commonDom) + dependsOn(commonMain) } val jvmMain by getting { @@ -190,7 +202,6 @@ kotlin { } val jsMain by getting { - dependsOn(commonDom) } val jsTest by getting { @@ -209,6 +220,13 @@ kotlin { implementation(kotlin("test")) } } + if (isWasmSupported) { + val wasmMain by getting { + dependsOn(commonDom) + dependencies { + } + } + } } sourceSets.all { languageSettings.apply { diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/NamespaceContext.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/NamespaceContext.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/NamespaceContext.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/NamespaceContext.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/PlatformXmlReader.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/PlatformXmlReader.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/PlatformXmlReader.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/PlatformXmlReader.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/PlatformXmlWriter.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/PlatformXmlWriter.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/PlatformXmlWriter.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/PlatformXmlWriter.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/QName.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/QName.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/QName.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/QName.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/XmlStreaming.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/XmlStreaming.kt similarity index 95% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/XmlStreaming.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/XmlStreaming.kt index 580fb5544..a14e0cf82 100644 --- a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/XmlStreaming.kt +++ b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/XmlStreaming.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021. + * Copyright (c) 2023. * * This file is part of xmlutil. * @@ -42,7 +42,7 @@ public actual object XmlStreaming { } public actual fun newReader(input: CharSequence): XmlReader { - return KtXmlReader(StringReader(input)) + return KtXmlReader(StringReader(input.toString())) } public actual fun newReader(reader: Reader): XmlReader { @@ -50,7 +50,7 @@ public actual object XmlStreaming { } public actual fun newGenericReader(input: CharSequence): XmlReader = - newGenericReader(StringReader(input)) + newGenericReader(StringReader(input.toString())) public actual fun newGenericReader(reader: Reader): XmlReader = KtXmlReader(reader) diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/XmlStreamingFactory.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/XmlStreamingFactory.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/XmlStreamingFactory.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/XmlStreamingFactory.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/AttrImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/AttrImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/AttrImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/AttrImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CDATASectionImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CDATASectionImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CDATASectionImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CDATASectionImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CharacterDataImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CharacterDataImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CharacterDataImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CharacterDataImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CommentImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CommentImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CommentImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/CommentImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentFragmentImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentFragmentImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentFragmentImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentFragmentImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentTypeImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentTypeImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentTypeImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/DocumentTypeImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ElementImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ElementImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ElementImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ElementImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeListImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeListImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeListImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/NodeListImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ProcessingInstructionImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ProcessingInstructionImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ProcessingInstructionImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/ProcessingInstructionImpl.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/SimpleDOMImplementation.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/SimpleDOMImplementation.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/SimpleDOMImplementation.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/SimpleDOMImplementation.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/TextImpl.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/TextImpl.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/dom/TextImpl.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/dom/TextImpl.kt diff --git a/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.commonDom.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.commonDom.kt new file mode 100644 index 000000000..7eeb9dfe2 --- /dev/null +++ b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.commonDom.kt @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2023. + * + * This file is part of xmlutil. + * + * This file is licenced to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You should have received a copy of the license with the source distribution. + * Alternatively, 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 nl.adaptivity.xmlutil.core.impl.multiplatform + +import kotlin.reflect.KClass + +public actual abstract class Reader { + public actual open fun read(): Int { + val b = CharArray(1) + if (read(b, 0, 1) < 0) return -1 + return b[0].code + } + + public actual abstract fun read(buf: CharArray, offset: Int, len: Int): Int +} + +public actual interface AutoCloseable { + public actual fun close() +} + +public actual interface Closeable : AutoCloseable + +public actual inline fun T.use(block: (T) -> R): R { + var exception: Throwable? = null + try { + return block(this) + } catch (e: Throwable) { + exception = e + throw e + } finally { + when { + this == null -> {} + exception == null -> close() + else -> + try { + close() + } catch (closeException: Throwable) { + // cause.addSuppressed(closeException) // ignored here + } + } + } +} + +public actual val KClass<*>.maybeAnnotations: List get() = emptyList() + +public actual abstract class Writer : Appendable { + public open fun write(text: String) { + append(text) + } + + override fun append(value: CharSequence?): Appendable { + return append(value, 0, value?.length ?: 0) + } + + /** Write buffers to the underlying file (where valid). */ + public open fun flush() {} +} + +public actual open class StringWriter : Writer() { + private val buffer = StringBuilder() + override fun write(text: String) { + buffer.append(text) + } + + override fun toString(): String { + return buffer.toString() + } + + override fun append(value: Char): Appendable = apply { + buffer.append(value) + } + + override fun append(value: CharSequence?): Appendable = apply { + buffer.append(value) + } + + override fun append( + value: CharSequence?, + startIndex: Int, + endIndex: Int + ): Appendable = apply { + buffer.append(value, startIndex, endIndex) + } +} + +/* +public actual abstract class Reader { + public actual open fun read(): Int { + val b = CharArray(1) + if (read(b, 0, 1) < 0) return -1 + return b[0].code + } + + public actual abstract fun read(buf: CharArray, offset: Int, len: Int): Int +} +*/ + +public actual open class StringReader(private val source: CharSequence) : Reader() { + + public actual constructor(source: String) : this(source as CharSequence) + + private var pos: Int = 0 + + override fun read(): Int = when { + pos >= source.length -> -1 + else -> source[pos++].code + } + + override fun read(buf: CharArray, offset: Int, len: Int): Int { + if (pos >= source.length) return -1 + val count = minOf(len, source.length - pos) + for (i in 0 until count) { + buf[offset + i] = source[pos + i] + } + pos += count + return count + } +} + +public actual annotation class Language actual constructor( + actual val value: String, + actual val prefix: String, + actual val suffix: String +) diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Attr.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Attr.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Attr.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Attr.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/CDATASection.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/CDATASection.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/CDATASection.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/CDATASection.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/CharacterData.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/CharacterData.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/CharacterData.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/CharacterData.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Comment.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Comment.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Comment.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Comment.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DOMException.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DOMException.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DOMException.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DOMException.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DOMImplementation.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DOMImplementation.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DOMImplementation.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DOMImplementation.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Document.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Document.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Document.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Document.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DocumentFragment.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DocumentFragment.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DocumentFragment.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DocumentFragment.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DocumentType.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DocumentType.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/DocumentType.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/DocumentType.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Element.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Element.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Element.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Element.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/NamedNodeMap.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/NamedNodeMap.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/NamedNodeMap.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/NamedNodeMap.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Node.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Node.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Node.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Node.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/NodeList.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/NodeList.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/NodeList.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/NodeList.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/ProcessingInstruction.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/ProcessingInstruction.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/ProcessingInstruction.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/ProcessingInstruction.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Text.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Text.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/dom/Text.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/dom/Text.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/siblingsToFragment.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/siblingsToFragment.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/siblingsToFragment.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/siblingsToFragment.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/CompactFragment.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/CompactFragment.kt similarity index 99% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/CompactFragment.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/CompactFragment.kt index 2912d6b51..59a65c428 100644 --- a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/CompactFragment.kt +++ b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/CompactFragment.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021. + * Copyright (c) 2023. * * This file is part of xmlutil. * diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/XMLFragmentStreamReader.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/XMLFragmentStreamReader.kt similarity index 99% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/XMLFragmentStreamReader.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/XMLFragmentStreamReader.kt index 6786553b3..ac5f60661 100644 --- a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/XMLFragmentStreamReader.kt +++ b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/XMLFragmentStreamReader.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021. + * Copyright (c) 2023. * * This file is part of xmlutil. * diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/impl/CombiningReader.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/impl/CombiningReader.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/impl/CombiningReader.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/impl/CombiningReader.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/impl/FragmentNamespaceContext.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/impl/FragmentNamespaceContext.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/impl/FragmentNamespaceContext.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/impl/FragmentNamespaceContext.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/impl/nativeDomutil.kt b/core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/impl/nativeDomutil.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/util/impl/nativeDomutil.kt rename to core/src/commonDom/kotlin/nl/adaptivity/xmlutil/util/impl/nativeDomutil.kt diff --git a/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/SimpleNamespaceContext.kt b/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/SimpleNamespaceContext.kt index f2c17aeb4..65b66ff9f 100644 --- a/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/SimpleNamespaceContext.kt +++ b/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/SimpleNamespaceContext.kt @@ -33,7 +33,6 @@ import nl.adaptivity.xmlutil.XMLConstants.XMLNS_ATTRIBUTE import nl.adaptivity.xmlutil.XMLConstants.XMLNS_ATTRIBUTE_NS_URI import nl.adaptivity.xmlutil.XMLConstants.XML_NS_PREFIX import nl.adaptivity.xmlutil.XMLConstants.XML_NS_URI -import nl.adaptivity.xmlutil.core.impl.multiplatform.name import kotlin.collections.set import kotlin.jvm.JvmName @@ -239,7 +238,7 @@ public open class SimpleNamespaceContext internal constructor(public val buffer: @OptIn(ExperimentalSerializationApi::class) override val descriptor: SerialDescriptor = - SerialDescriptor(SimpleNamespaceContext::class.name, actualSerializer.descriptor) + SerialDescriptor("nl.adaptivity.xmlutil.SimpleNamespaceContext", actualSerializer.descriptor) public fun from(originalNSContext: Iterable): SimpleNamespaceContext = when (originalNSContext) { is SimpleNamespaceContext -> originalNSContext diff --git a/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/commonMultiplatform.kt b/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.kt similarity index 99% rename from core/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/commonMultiplatform.kt rename to core/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.kt index 667741347..e0f91120f 100644 --- a/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/commonMultiplatform.kt +++ b/core/src/commonMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.kt @@ -21,7 +21,6 @@ package nl.adaptivity.xmlutil.core.impl.multiplatform import nl.adaptivity.xmlutil.XmlUtilInternal -import kotlin.Throws import kotlin.reflect.KClass @Target( diff --git a/core/src/javaShared/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/javaMultiplatform.kt b/core/src/javaShared/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.javaShared.kt similarity index 100% rename from core/src/javaShared/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/javaMultiplatform.kt rename to core/src/javaShared/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/multiplatform.javaShared.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/_IOException.kt b/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/IOException.native.kt similarity index 100% rename from core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/_IOException.kt rename to core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/IOException.native.kt diff --git a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/nativeMultiplatform.kt b/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/nativeMultiplatform.kt index d6917fe32..e20f485e5 100644 --- a/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/nativeMultiplatform.kt +++ b/core/src/nativeMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/nativeMultiplatform.kt @@ -42,121 +42,6 @@ public actual fun assert(value: Boolean) { kotlin.assert(value) } -public actual interface AutoCloseable { - public actual fun close() -} - -public actual interface Closeable : AutoCloseable - -public actual inline fun T.use(block: (T) -> R): R { - var exception: Throwable? = null - try { - return block(this) - } catch (e: Throwable) { - exception = e - throw e - } finally { - when { - this == null -> {} - exception == null -> close() - else -> - try { - close() - } catch (closeException: Throwable) { - // cause.addSuppressed(closeException) // ignored here - } - } - } -} - -public actual val KClass<*>.maybeAnnotations: List get() = emptyList() - -public actual abstract class Writer : Appendable { - public open fun write(text: String) { - append(text) - } - - override fun append(value: CharSequence?): Appendable { - return append(value, 0, value?.length ?: 0) - } - - /** Write buffers to the underlying file (where valid). */ - public open fun flush() {} -} - -public actual open class StringWriter : Writer() { - private val buffer = StringBuilder() - override fun write(text: String) { - buffer.append(text) - } - - override fun toString(): String { - return buffer.toString() - } - - override fun append(value: Char): Appendable = apply { - buffer.append(value) - } - - override fun append(value: CharSequence?): Appendable = apply { - buffer.append(value) - } - - override fun append( - value: CharSequence?, - startIndex: Int, - endIndex: Int - ): Appendable = apply { - buffer.append(value, startIndex, endIndex) - } -} - -public actual abstract class Reader { - public actual open fun read(): Int { - val b = CharArray(1) - if (read(b, 0, 1) < 0) return -1 - return b[0].code - } - - public actual abstract fun read(buf: CharArray, offset: Int, len: Int): Int -} - -public actual open class StringReader(private val source: CharSequence) : Reader() { - - public actual constructor(source: String) : this(source as CharSequence) - - private var pos: Int = 0 - - override fun read(): Int = when { - pos >= source.length -> -1 - else -> source[pos++].code - } - - override fun read(buf: CharArray, offset: Int, len: Int): Int { - if (pos >= source.length) return -1 - val count = minOf(len, source.length - pos) - for (i in 0 until count) { - buf[offset + i] = source[pos + i] - } - pos += count - return count - } -} - -@Retention(AnnotationRetention.SOURCE) -@Target( - AnnotationTarget.FUNCTION, - AnnotationTarget.FIELD, - AnnotationTarget.VALUE_PARAMETER, - AnnotationTarget.LOCAL_VARIABLE, - AnnotationTarget.ANNOTATION_CLASS -) -public actual annotation class Language( - actual val value: String, - actual val prefix: String, - actual val suffix: String -) - public inline fun T.use(block: (T) -> R): R { try { return block(this) diff --git a/core/src/wasmMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/IOException.wasm.kt b/core/src/wasmMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/IOException.wasm.kt new file mode 100644 index 000000000..aaf51d690 --- /dev/null +++ b/core/src/wasmMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/IOException.wasm.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023. + * + * This file is part of xmlutil. + * + * This file is licenced to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You should have received a copy of the license with the source distribution. + * Alternatively, 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 nl.adaptivity.xmlutil.core.impl.multiplatform + +public actual open class IOException : Exception { + public actual constructor() : super() + + public actual constructor(message: String?) : super(message) + + public actual constructor(message: String?, cause: Throwable?) : super(message, cause) + + public actual constructor(cause: Throwable?) : super(cause) +} diff --git a/core/src/wasmMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/commonMultiplatform.wasm.kt b/core/src/wasmMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/commonMultiplatform.wasm.kt new file mode 100644 index 000000000..e6f8bca88 --- /dev/null +++ b/core/src/wasmMain/kotlin/nl/adaptivity/xmlutil/core/impl/multiplatform/commonMultiplatform.wasm.kt @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2023. + * + * This file is part of xmlutil. + * + * This file is licenced to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You should have received a copy of the license with the source distribution. + * Alternatively, 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 nl.adaptivity.xmlutil.core.impl.multiplatform + +import nl.adaptivity.xmlutil.XmlUtilInternal +import kotlin.reflect.KClass + +public inline fun T.use(block: (T) -> R): R { + try { + return block(this) + } finally { + close() + } +} + + +@Target( + AnnotationTarget.FUNCTION, + AnnotationTarget.PROPERTY_GETTER, + AnnotationTarget.PROPERTY_SETTER, + AnnotationTarget.CONSTRUCTOR +) +@Retention(AnnotationRetention.SOURCE) +public actual annotation class Throws actual constructor(actual vararg val exceptionClasses: KClass) + +@Suppress("UNCHECKED_CAST_TO_EXTERNAL_INTERFACE") +@XmlUtilInternal +public actual val KClass<*>.name: String + get() = "foobar"//className(this as Clazz) + +internal external interface Clazz + +@JsFun("(clazz) => clazz.name") +internal external fun className(clazz: Clazz): String + +@XmlUtilInternal +public actual fun assert(value: Boolean, lazyMessage: () -> String) { + if (!value) { + throw AssertionError(lazyMessage()) + } +} + +@XmlUtilInternal +public actual fun assert(value: Boolean) { + if (!value) { + throw AssertionError() + } +} + +public actual abstract class OutputStream : Closeable { + public actual abstract fun write(b: Int) + public actual open fun write(b: ByteArray) { + for (byte in b) write(byte.toInt()) + } + + public actual open fun write(b: ByteArray, off: Int, len: Int) { + for (i in off until (off + len)) { + write(b[i].toInt()) + } + } + +} + +public actual abstract class InputStream : Closeable { + public actual open fun read(b: ByteArray, off: Int, len: Int): Int { + for (i in off until (off + len)) { + val value = read() + if (value < 0) return i - off - 1 + b[i] = value.toByte() + } + return len + } + + public actual fun read(b: ByteArray): Int { + for (i in b.indices) { + val value = read() + if (value < 0) return i - 1 + b[i] = value.toByte() + } + return b.size + } + + public actual abstract fun read(): Int + +} diff --git a/examples/src/main/kotlin/net/devrieze/serialization/examples/custompolymorphic/XmlFruit.kt b/examples/src/main/kotlin/net/devrieze/serialization/examples/custompolymorphic/XmlFruit.kt index a39d00782..c3dba791e 100644 --- a/examples/src/main/kotlin/net/devrieze/serialization/examples/custompolymorphic/XmlFruit.kt +++ b/examples/src/main/kotlin/net/devrieze/serialization/examples/custompolymorphic/XmlFruit.kt @@ -30,7 +30,6 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import net.devrieze.serialization.examples.custompolymorphic.XmlFruit.FruitType -import nl.adaptivity.xmlutil.core.impl.multiplatform.name /** * A @@ -93,8 +92,13 @@ data class XmlFruit( * Simple serializer that serializes a [FruitType] as the string in its property [FruitType.serialName]. */ class FruitTypeSerializer: KSerializer { - override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(FruitTypeSerializer::class.name, PrimitiveKind.STRING) + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor( + "net.devrieze.serialization.examples.custompolymorphic.FruitType", + PrimitiveKind.STRING + ) + override fun deserialize(decoder: Decoder): FruitType = FruitType.values().first { decoder.decodeString() == it.serialName } + override fun serialize(encoder: Encoder, value: FruitType): Unit = encoder.encodeString(value.serialName) } } diff --git a/gradle.properties b/gradle.properties index fffcfc7a4..f4e43b560 100644 --- a/gradle.properties +++ b/gradle.properties @@ -7,3 +7,4 @@ xmlutil_util_version=0.86.2-SNAPSHOT xmlutil_versiondesc= kotlin.code.style=official native.deploy= +xmlutil.wasmEnabled=false diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index eb3426130..b115ecd45 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,6 +2,7 @@ ktor = "1.6.8" junit5-jupiter = "5.9.2" kotlinx-serialization = "1.5.1" +#kotlinx-serialization = "1.5.2-wasm0" kotlin = "1.9.0" dokka = "1.8.20" binaryValidator = "0.13.2" diff --git a/serialization/build.gradle.kts b/serialization/build.gradle.kts index d7e3e85d9..f34b5c74e 100644 --- a/serialization/build.gradle.kts +++ b/serialization/build.gradle.kts @@ -73,7 +73,7 @@ kotlin { } jvm("android") - js(BOTH) { + js { browser() compilations.all { kotlinOptions { @@ -250,10 +250,6 @@ tasks.register("cleanTest") { dependsOn(tasks.named("cleanAllTests")) } -tasks.named("jsLegacyBrowserTest") { - filter.excludeTestsMatching("nl.adaptivity.xml.serialization.OrderedFieldsTest") -} - tasks.withType { logger.lifecycle("Enabling xml reports on task ${project.name}:${name}") reports { diff --git a/serialutil/build.gradle.kts b/serialutil/build.gradle.kts index 196c06af6..c76d9b6d7 100644 --- a/serialutil/build.gradle.kts +++ b/serialutil/build.gradle.kts @@ -48,7 +48,7 @@ kotlin { targets { jvm() jvm("android") - js(BOTH) { + js { browser() nodejs() compilations.all { diff --git a/testutil/build.gradle.kts b/testutil/build.gradle.kts index 80ffa92a4..fa6812048 100644 --- a/testutil/build.gradle.kts +++ b/testutil/build.gradle.kts @@ -50,7 +50,7 @@ val moduleName = "io.github.pdvrieze.testutil" kotlin { targets { jvm() - js(BOTH) { + js { browser() nodejs() compilations.all { @@ -65,7 +65,12 @@ kotlin { } } } - + if (isWasmSupported) { + wasm { + nodejs() + browser() + } + } } @Suppress("UNUSED_VARIABLE") diff --git a/xmlserializable/build.gradle.kts b/xmlserializable/build.gradle.kts index b30e973de..0bcc06b8f 100644 --- a/xmlserializable/build.gradle.kts +++ b/xmlserializable/build.gradle.kts @@ -79,7 +79,7 @@ kotlin { cleanTestTask.dependsOn(tasks.named("clean${target.name[0].toUpperCase()}${target.name.substring(1)}Test")) } } - js(BOTH) { + js { browser() compilations.all { kotlinOptions {