Skip to content

Commit

Permalink
Add wasmJs target support. Rewrite several code generators to use Kot…
Browse files Browse the repository at this point in the history
…linPoet. Add tests checking that every generated tag is instantiated without issues.
  • Loading branch information
IlyaGulya authored and e5l committed Jan 12, 2024
1 parent b2a6483 commit ce1183a
Show file tree
Hide file tree
Showing 26 changed files with 5,841 additions and 875 deletions.
95 changes: 34 additions & 61 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Build_gradle.MavenPomFile
import kotlinx.html.js.packageJson
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

/**
* This build script supports following parameters:
* -PversionTag - works together with "branch-build" profile and overrides "-SNAPSHOT" suffix of the version.
*/
plugins {
kotlin("multiplatform") version "1.9.21"
kotlin("multiplatform") version "1.9.22"
id("maven-publish")
id("signing")
}
Expand Down Expand Up @@ -90,7 +92,7 @@ kotlin {
}
}
}
js(IR) {
js {
moduleName = project.name
browser {
testTask {
Expand All @@ -105,6 +107,16 @@ kotlin {
pom { name by "${project.name}-js" }
}
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
moduleName = project.name
browser()

mavenPublication {
groupId = group as String
pom { name by "${project.name}-wasm-js" }
}
}

mingwX64()
linuxX64()
Expand All @@ -123,6 +135,17 @@ kotlin {
macosX64()
macosArm64()

@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
common {
group("jsCommon") {
withJs()
// TODO: switch to `withWasmJs()` after upgrade to Kotlin 2.0
withWasm()
}
}
}

metadata {
mavenPublication {
groupId = group as String
Expand All @@ -140,7 +163,7 @@ kotlin {
sourceSets {
commonMain {
dependencies {
implementation(kotlin("stdlib-common"))
implementation(kotlin("stdlib"))
}
}

Expand All @@ -150,61 +173,6 @@ kotlin {
}
}

val jsMain by getting {
dependencies {
implementation(kotlin("stdlib-js"))
}
}

val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}

val jvmMain by getting {
dependencies {
implementation(kotlin("stdlib"))
}
}

val jvmTest by getting {
dependencies {
implementation(kotlin("test-junit"))
}
}

val nativeMain by creating
val nativeTest by creating

val nativeTargets = listOf(
"mingwX64",
"linuxX64",
"linuxArm64",
"iosX64",
"iosArm64",
"iosArm32",
"iosSimulatorArm64",
"watchosX86",
"watchosX64",
"watchosArm32",
"watchosArm64",
"watchosSimulatorArm64",
"tvosX64",
"tvosArm64",
"tvosSimulatorArm64",
"macosX64",
"macosArm64",
"watchosDeviceArm64",
)

val commonMain by getting
nativeMain.dependsOn(commonMain)

nativeTargets.forEach { target ->
findByName("${target}Main")?.dependsOn(nativeMain)
findByName("${target}Test")?.dependsOn(nativeTest)
}
}
}

Expand All @@ -226,9 +194,14 @@ tasks.register<Task>("generate") {

doLast {
kotlinx.html.generate.generate(
"kotlinx.html",
"src/commonMain/kotlin/generated",
"src/jsMain/kotlin/generated"
pkg = "kotlinx.html",
todir = "src/commonMain/kotlin/generated",
jsdir = "src/jsMain/kotlin/generated",
wasmJsDir = "src/wasmJsMain/kotlin/generated"
)
kotlinx.html.generate.generateJsTagTests(
jsdir = "src/jsTest/kotlin/generated",
wasmJsDir = "src/wasmJsTest/kotlin/generated",
)
}
}
Expand Down
1 change: 1 addition & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ repositories {
dependencies {
implementation(kotlin("stdlib"))
implementation("com.sun.xsom:xsom:20140925")
implementation("com.squareup:kotlinpoet:1.15.3")
}
58 changes: 52 additions & 6 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/attributes.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package kotlinx.html.generate

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName

fun String.quote() = "\"$this\""

fun Appendable.attributePseudoDelegate(request: AttributeRequest) {
Expand Down Expand Up @@ -52,13 +59,15 @@ fun Appendable.facade(repository: Repository, facade: AttributeFacade) {
}
}

fun Appendable.eventProperty(parent: String, attribute: AttributeInfo) {
fun Appendable.eventProperty(parent: String, attribute: AttributeInfo, shouldUnsafeCast: Boolean) {
val type = "(org.w3c.dom.events.Event) -> Unit"
variable(receiver = parent, variable = Var(
variable(
receiver = parent, variable = Var(
name = attribute.fieldName + "Function",
type = type,
mutable = true
))
)
)
emptyLine()

getter().defineIs(StringBuilder().apply {
Expand All @@ -67,11 +76,48 @@ fun Appendable.eventProperty(parent: String, attribute: AttributeInfo) {
})
setter {
receiverDot("consumer")
functionCall("onTagEvent", listOf(
val newValue = if (shouldUnsafeCast) {
"newValue.unsafeCast<(Event) -> Unit>()"
} else {
"newValue"
}
functionCall(
"onTagEvent", listOf(
"this",
attribute.name.quote(),
"newValue.unsafeCast<(Event) -> Unit>()"
))
newValue
)
)
}
emptyLine()
}

fun eventProperty(parent: TypeName, attribute: AttributeInfo, shouldUnsafeCast: Boolean): PropertySpec {
val propertyType = LambdaTypeName.get(
returnType = ClassName("kotlin", "Unit"),
parameters = listOf(ParameterSpec.unnamed(ClassName("kotlinx.html.org.w3c.dom.events", "Event"))),
)
return PropertySpec.builder(attribute.fieldName + "Function", propertyType)
.mutable()
.receiver(parent)
.getter(
FunSpec.getterBuilder()
.addStatement("throw UnsupportedOperationException(\"You can't read variable ${attribute.fieldName}\")")
.build()
)
.setter(
FunSpec.setterBuilder()
.addParameter("newValue", propertyType)
.addStatement(
"consumer.onTagEvent(this, %S, %L)",
attribute.name,
if (shouldUnsafeCast) {
"newValue.unsafeCast<(Event) -> Unit>()"
} else {
"newValue"
}
)
.build()
)
.build()
}
Loading

0 comments on commit ce1183a

Please sign in to comment.