Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wasmJs support #3152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ jobs:

- name: Test Native and JS
run: |
./gradlew -Dkjs=true -Dknative=true -Pswift=false samples:native:build samples:js:build samples:multi-target:build --stacktrace --warning-mode all
./gradlew -Dkjs=true -Dkwasm=true -Dknative=true -Pswift=false samples:native:build samples:js:build samples:multi-target:build --stacktrace --warning-mode all
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
./gradlew -Dkjs=true -Dkwasm=true -Dknative=true -Pswift=false samples:native:build samples:js:build samples:multi-target:build --stacktrace --warning-mode all
./gradlew -Dkjs=true -Dkwasm=true -Dknative=true -Pswift=false samples:native:build samples:js:build samples:wasm:build samples:multi-target:build --stacktrace --warning-mode all


- name: Test
run: |
./gradlew -Dkjs=false -Dknative=false -Pswift=false build --stacktrace --warning-mode all -x samples:native:build -x samples:js:build -x samples:multi-target:build
./gradlew -Dkjs=false -Dkwasm=false -Dknative=false -Pswift=false build --stacktrace --warning-mode all -x samples:native:build -x samples:js:build -x samples:multi-target:build
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
./gradlew -Dkjs=false -Dkwasm=false -Dknative=false -Pswift=false build --stacktrace --warning-mode all -x samples:native:build -x samples:js:build -x samples:multi-target:build
./gradlew -Dkjs=false -Dkwasm=false -Dknative=false -Pswift=false build --stacktrace --warning-mode all -x samples:native:build -x samples:js:build -x samples:wasm:build -x samples:multi-target:build


multiplatform:
runs-on: macos-latest
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ kotlin-test-annotations = { module = "org.jetbrains.kotlin:kotlin-test-annotatio
kotlin-test-common = { module = "org.jetbrains.kotlin:kotlin-test-common" }
kotlin-test-js = { module = "org.jetbrains.kotlin:kotlin-test-js" }
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit" }
kotlin-test-wasm-js = { module = "org.jetbrains.kotlin:kotlin-test-wasm-js" }
kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinpoet" }
misk = { module = "com.squareup.misk:misk", version = "2024.10.13.223258-0dacf1d" }
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
Expand Down
34 changes: 34 additions & 0 deletions samples/wasm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
kotlin("multiplatform")
id("com.squareup.wire")
}

repositories {
mavenCentral()
}

kotlin {
if (System.getProperty("kwasm", "true").toBoolean()) {
wasmJs {
browser()
}
}
}

wire {
protoLibrary = true

kotlin {
buildersOnly = true
}
}


buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.squareup.wire:wire-gradle-plugin")
}
}
13 changes: 13 additions & 0 deletions samples/wasm/src/commonMain/proto/human/person.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package human;

message Person {
string name = 1;
PhoneNumber phone_number = 2;

message PhoneNumber {
string area = 1;
string number = 2;
}
}
25 changes: 25 additions & 0 deletions samples/wasm/src/wasmJsMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* 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
*
* https://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.
*/
import human.Person

fun main() {
val phoneNumber = Person.PhoneNumber.Builder()
.area("519")
.number("5550202")
.build()
val person = Person.Builder().name("Jacques").phone_number(phoneNumber).build()
println("Hello, Kotlin/Native! Here is ${person.name} and their number: ${person.phone_number}")
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ include(":samples:android-app-variants-sample")
include(":samples:android-lib-java-sample")
include(":samples:android-lib-kotlin-sample")
include(":samples:js")
include(":samples:wasm")
include(":samples:multi-target")
include(":samples:native")
include(":samples:simple-sample")
Expand Down
5 changes: 5 additions & 0 deletions wire-grpc-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ kotlin {
browser()
}
}
if (System.getProperty("kwasm", "true").toBoolean()) {
wasmJs {
browser()
}
}
if (System.getProperty("knative", "true").toBoolean()) {
iosX64()
iosArm64()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

actual abstract class GrpcClient {
actual abstract fun <S : Any, R : Any> newCall(method: GrpcMethod<S, R>): GrpcCall<S, R>
actual abstract fun <S : Any, R : Any> newStreamingCall(method: GrpcMethod<S, R>): GrpcStreamingCall<S, R>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

actual class GrpcHeaders {
actual operator fun get(name: String): String? = TODO()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

actual class GrpcHttpUrl {
actual fun resolve(link: String): GrpcHttpUrl? = TODO("Not yet implemented")
}

actual fun String.toHttpUrl(): GrpcHttpUrl = TODO("Not yet implemented")
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

actual class GrpcRequest

actual open class GrpcRequestBuilder {
actual open fun url(url: GrpcHttpUrl): GrpcRequestBuilder = TODO("Not yet implemented")
actual open fun addHeader(
name: String,
value: String,
): GrpcRequestBuilder = TODO("Not yet implemented")
actual open fun method(
method: String,
body: GrpcRequestBody?,
): GrpcRequestBuilder = TODO("Not yet implemented")
actual open fun build(): GrpcRequest = TODO("Not yet implemented")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

actual abstract class GrpcRequestBody
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

internal actual class GrpcResponse {
actual val body: GrpcResponseBody?
get() = TODO("Not yet implemented")

actual fun header(
name: String,
defaultValue: String?,
): String? {
TODO("Not yet implemented")
}

actual fun trailers(): GrpcHeaders = TODO("Not yet implemented")

actual fun close() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

import okio.BufferedSource

actual abstract class GrpcResponseBody {
actual abstract fun source(): BufferedSource
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire.internal

import com.squareup.wire.GrpcResponse
import okio.Sink
import okio.Source

internal actual interface Call {
actual fun cancel()
actual fun execute(): GrpcResponse
}

internal actual fun Sink.asGzip(): Sink {
throw UnsupportedOperationException("Gzip not implemented for WAsm JS")
}

internal actual fun Source.asGzip(): Source {
throw UnsupportedOperationException("Gzip not implemented for WAsm JS")
}

internal actual fun Throwable.addSuppressed(other: Throwable) {
}
12 changes: 12 additions & 0 deletions wire-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ kotlin {
browser()
}
}
if (System.getProperty("kwasm", "true").toBoolean()) {
wasmJs {
browser()
}
}
if (System.getProperty("knative", "true").toBoolean()) {
iosX64()
iosArm64()
Expand Down Expand Up @@ -69,6 +74,13 @@ kotlin {
}
}
}
if (System.getProperty("kwasm", "true").toBoolean()) {
val wasmJsTest by getting {
dependencies {
implementation(libs.kotlin.test.wasm.js)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* 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
*
* https://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 com.squareup.wire

import com.squareup.wire.internal.NANOS_PER_SECOND
import com.squareup.wire.internal.addExactLong
import com.squareup.wire.internal.commonEquals
import com.squareup.wire.internal.commonHashCode
import com.squareup.wire.internal.floorDivLong
import com.squareup.wire.internal.floorModLong

actual class Duration internal constructor(
private val seconds: Long,
private val nanos: Int,
) {
actual fun getSeconds(): Long = seconds
actual fun getNano(): Int = nanos

override fun equals(other: Any?): Boolean = commonEquals(other)

override fun hashCode(): Int = commonHashCode()
}

actual fun durationOfSeconds(
seconds: Long,
nano: Long,
): Duration {
val secs = addExactLong(seconds, floorDivLong(nano, NANOS_PER_SECOND))
val nos = floorModLong(nano, NANOS_PER_SECOND).toInt()
return Duration(secs, nos)
}
Loading