Skip to content

Commit

Permalink
add wasmJs
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Dec 2, 2024
1 parent 9880bb3 commit ce2c19f
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 10 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ compose-jb = "1.7.1"
## ⬆ = "1.8.0-dev1905"
## ⬆ = "1.8.0-dev1916"
## ⬆ = "1.8.0-dev1920"
kotlinx-browser = "0.3"
vanniktech-publish = "0.30.0"
okio = "3.9.1"
mockative = "3.0.1"
Expand All @@ -69,3 +70,4 @@ androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "a
androidx-runner = { module = "androidx.test:runner", version.ref = "androidx-runner" }

co-touchlab-kermit = { module = "co.touchlab:kermit", version.ref = "kermit" }
kotlinx-browser = { module = "org.jetbrains.kotlinx:kotlinx-browser", version.ref = "kotlinx-browser" }
123 changes: 120 additions & 3 deletions kotlin-js-store/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions libphonenumber/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@file:OptIn(ExperimentalWasmDsl::class)

import com.vanniktech.maven.publish.SonatypeHost
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl

plugins {
alias(libs.plugins.org.jetbrains.kotlin.multiplatform)
Expand All @@ -21,9 +24,12 @@ kotlin {
browser()
nodejs()
}
wasmJs {
browser()
}
iosX64();iosArm64();iosSimulatorArm64()
macosX64();macosArm64()
applyDefaultHierarchyTemplate()
// applyDefaultHierarchyTemplate()
sourceSets {
all {
languageSettings.optIn("kotlin.ExperimentalStdlibApi")
Expand Down Expand Up @@ -80,8 +86,17 @@ kotlin {
implementation(libs.androidx.runner)
}
}
val nativeMain by getting {
val nativeMain by creating {
dependsOn(nonJvmMain)
}
val wasmJsMain by getting {
dependsOn(nonJvmMain)
dependencies {
implementation(libs.kotlinx.browser)
}
}
val wasmJsTest by getting {
dependsOn(nonJvmTest)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.michaelrocks.libphonenumber.kotlin.metadata

import io.michaelrocks.libphonenumber.kotlin.MetadataLoader
import io.michaelrocks.libphonenumber.kotlin.metadata.init.ComposeResourceMetadataLoader

actual fun defaultMetadataLoader(): MetadataLoader {
return ComposeResourceMetadataLoader()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2022 The Libphonenumber Authors
* Copyright (C) 2022 Michael Rozumyanskiy
*
* 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 io.michaelrocks.libphonenumber.kotlin.metadata.init

import co.touchlab.kermit.Logger
import io.github.luca992.libphonenumber_kotlin.libphonenumber.generated.resources.Res
import io.michaelrocks.libphonenumber.kotlin.MetadataLoader
import io.michaelrocks.libphonenumber.kotlin.io.InputStream
import io.michaelrocks.libphonenumber.kotlin.io.OkioInputStream
import okio.Buffer
import org.w3c.xhr.XMLHttpRequest

/**
* A [MetadataLoader] implementation that reads phone number metadata files as classpath
* resources.
*/

class ComposeResourceMetadataLoader : MetadataLoader {

fun fetchFileSynchronously(url: String): ByteArray? {
val xhr = XMLHttpRequest()
xhr.open("GET", url, false) // `false` makes it synchronous
xhr.overrideMimeType("text/plain; charset=x-user-defined") // Interpret response as binary
xhr.send()

return if (xhr.status == 200.toShort()) {
val responseText = xhr.responseText // Read response as text
// Convert text response to ByteArray
responseText.encodeToByteArray().map { it.toInt() and 0xFF }
ByteArray(responseText.length) { i -> responseText[i].code.toByte() }
} else {
println("Failed to fetch file. Status: ${xhr.status}")
null
}
}


override fun loadMetadata(phoneMetadataResource: String): InputStream? {
return try {
val buffer = Buffer()
val path = Res.getUri(phoneMetadataResource)
println("loadMetadata path: $path")
val result = fetchFileSynchronously(path)
return if (result != null) {
buffer.write(result)
OkioInputStream(buffer)
} else {
println("Failed to fetch file.")
null
}
} catch (t: Throwable) {
logger.v("Failed to load metadata from $phoneMetadataResource.path", t)
null
}
}

companion object {
private val logger = Logger.withTag(
ComposeResourceMetadataLoader::class.simpleName.toString()
)
}
}
9 changes: 4 additions & 5 deletions library-test-resources/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ kotlin {
browser()
nodejs()
}
// wasm{
// browser()
// nodejs()
// d8()
// }
wasmJs {
browser()
nodejs()
}
iosX64();iosArm64();iosSimulatorArm64()
macosX64();macosArm64()
applyDefaultHierarchyTemplate()
Expand Down
7 changes: 7 additions & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@file:OptIn(ExperimentalWasmDsl::class)

import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget

plugins {
Expand All @@ -17,6 +20,10 @@ kotlin {
browser()
binaries.executable()
}
wasmJs {
browser()
binaries.executable()
}
fun macosTargets(config: KotlinNativeTarget.() -> Unit) {
macosX64(config)
macosArm64(config)
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions sample/src/wasmJsMain/kotlin/main.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.window.CanvasBasedWindow
import io.luca992.libphonenumber.sample.App

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
CanvasBasedWindow("libphonenumber-kotlin wasmJs sample") {
Column(modifier = Modifier.fillMaxSize()) {
App("WasmJs Web")
}
}
}
Loading

0 comments on commit ce2c19f

Please sign in to comment.