Skip to content

Commit

Permalink
Support wasm target (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepenz authored Aug 5, 2024
1 parent f304cad commit e1770b3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ from [kotlinx.serialization-json](https://github.com/Kotlin/kotlinx.serializatio
|-------------------|
| jvm |
| js |
| wasmJs |
| macosX64 |
| macosArm64 |
| iosArm64 |
Expand Down
33 changes: 32 additions & 1 deletion json-schema-validator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@file:OptIn(ExperimentalWasmDsl::class)

import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetWithTests
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
import java.util.Locale

Expand Down Expand Up @@ -145,6 +148,13 @@ kotlin {
generateTypeScriptDefinitions()
nodejs()
}
wasmJs {
// The wasmJsBrowserTest prints all executed tests as one unformatted string
// Have not found a way to suppress printing all this into console
browser()
nodejs()
}

applyDefaultHierarchyTemplate()

val macOsTargets =
Expand All @@ -168,7 +178,7 @@ kotlin {
)

sourceSets {
commonMain {
val commonMain by getting {
kotlin.srcDirs(generatedSourceDirectory)

dependencies {
Expand All @@ -182,11 +192,31 @@ kotlin {
) {
because("simplifies work with unicode codepoints")
}
}
}

val wasmJsMain by getting

val nonWasmJsMain by creating {
dependsOn(commonMain)

dependencies {
implementation(libs.normalize.get().toString()) {
because("provides normalization required by IDN-hostname format")
}
}
}

val jvmMain by getting {
dependsOn(nonWasmJsMain)
}
val jsMain by getting {
dependsOn(nonWasmJsMain)
}
val nativeMain by getting {
dependsOn(nonWasmJsMain)
}

commonTest {
dependencies {
implementation(libs.kotest.assertions.core)
Expand Down Expand Up @@ -253,6 +283,7 @@ kotlin {
dependsOnTargetTests(linuxTargets)
dependsOn(tasks.getByName("jvmTest"))
dependsOn(tasks.getByName("jsTest"))
dependsOn(tasks.getByName("wasmJsTest"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import io.github.optimumcode.json.schema.FormatValidator
import io.github.optimumcode.json.schema.internal.formats.IdnHostnameFormatValidator.BidiLabelType.LTR
import io.github.optimumcode.json.schema.internal.formats.IdnHostnameFormatValidator.BidiLabelType.NONE
import io.github.optimumcode.json.schema.internal.formats.IdnHostnameFormatValidator.BidiLabelType.RTL
import io.github.optimumcode.json.schema.internal.hostname.Normalizer
import io.github.optimumcode.json.schema.internal.hostname.Punycode
import io.github.optimumcode.json.schema.internal.hostname.isNormalized
import io.github.optimumcode.json.schema.internal.unicode.CharacterCategory
import io.github.optimumcode.json.schema.internal.unicode.CharacterCategory.ENCLOSING_MARK
import io.github.optimumcode.json.schema.internal.unicode.CharacterCategory.NONSPACING_MARK
Expand Down Expand Up @@ -108,7 +108,7 @@ internal object IdnHostnameFormatValidator : AbstractStringFormatValidator() {
label
}

if (!Normalizer.isNormalized(unicode)) {
if (!isNormalized(unicode)) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
package io.github.optimumcode.json.schema.internal.hostname

import doist.x.normalize.Form
import doist.x.normalize.normalize

internal object Normalizer {
fun isNormalized(label: String): Boolean {
return label.normalize(Form.NFC) == label
}
}
internal expect fun isNormalized(label: String): Boolean
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.optimumcode.json.schema.internal.hostname

import doist.x.normalize.Form
import doist.x.normalize.normalize

internal actual fun isNormalized(label: String): Boolean {
return label.normalize(Form.NFC) == label
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.optimumcode.json.schema.internal.hostname

internal actual fun isNormalized(label: String): Boolean = js("label.normalize('NFC') === label")
1 change: 1 addition & 0 deletions test-suites/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ kotlin {
js(IR) {
nodejs()
}
// wasmJs target is not added because the okio does not provide a dependency to use FileSystem API in wasmJs target
applyDefaultHierarchyTemplate()

val macOsTargets =
Expand Down

1 comment on commit e1770b3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'KMP JSON schema validator'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: e1770b3 Previous: f304cad Ratio
macosX64.CommonAvgTimeBench.validateBasic ( {"objectPath":"openapi.json","schemaPath":"openapi_schema.json"} ) 4306.773060677966 us/op 2656.9748599462364 us/op 1.62
macosX64.CommonAvgTimeBench.validateBasic ( {"objectPath":"openapi-invalid.json","schemaPath":"openapi_schema.json"} ) 4296.92980204918 us/op 2765.1261474860335 us/op 1.55
macosX64.CommonAvgTimeBench.validateDetailed ( {"objectPath":"openapi.json","schemaPath":"openapi_schema.json"} ) 9029.220563709678 us/op 6019.334005294117 us/op 1.50

This comment was automatically generated by workflow using github-action-benchmark.

CC: @OptimumCode

Please sign in to comment.