Skip to content

Commit ce13a51

Browse files
committed
Adding wasm target to wire runtime.
1 parent d8d66d8 commit ce13a51

File tree

10 files changed

+602
-3
lines changed

10 files changed

+602
-3
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ jobs:
2929
./gradlew generateTests -Pswift=false --stacktrace --warning-mode all
3030
if [ ! -z "$(git status --porcelain)" ]; then git diff; echo -e "\nTest files changed. Did you run ./gradlew generateTests?"; exit 1; fi
3131
32-
- name: Test Native and JS
32+
- name: Test Native, JS and Wasm
3333
run: |
34-
./gradlew -Dkjs=true -Dknative=true -Pswift=false samples:native:build samples:js:build samples:multi-target:build --stacktrace --warning-mode all
34+
./gradlew -Dkjs=true -Dknative=true -Dkwasm=true -Pswift=false samples:native:build samples:js:build samples:multi-target:build --stacktrace --warning-mode all
3535
3636
- name: Test
3737
run: |
38-
./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
38+
./gradlew -Dkjs=false -Dknative=false -Dkwasm=false -Pswift=false build --stacktrace --warning-mode all -x samples:native:build -x samples:js:build -x samples:multi-target:build
3939
4040
multiplatform:
4141
runs-on: macos-latest

wire-runtime/build.gradle.kts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import com.diffplug.gradle.spotless.SpotlessExtension
2+
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
23

34
plugins {
45
kotlin("multiplatform")
@@ -39,6 +40,14 @@ kotlin {
3940
tvosArm64()
4041
tvosSimulatorArm64()
4142
}
43+
44+
if (System.getProperty("kwasm", "true").toBoolean()) {
45+
@OptIn(ExperimentalWasmDsl::class)
46+
wasmWasi {
47+
nodejs()
48+
}
49+
}
50+
4251
sourceSets {
4352
val commonMain by getting {
4453
dependencies {
@@ -69,6 +78,13 @@ kotlin {
6978
}
7079
}
7180
}
81+
if (System.getProperty("kwasm", "true").toBoolean()) {
82+
val wasmWasiTest by getting {
83+
dependencies {
84+
implementation(libs.kotlin.test)
85+
}
86+
}
87+
}
7288
}
7389
}
7490

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2020 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
import com.squareup.wire.internal.NANOS_PER_SECOND
19+
import com.squareup.wire.internal.addExactLong
20+
import com.squareup.wire.internal.commonEquals
21+
import com.squareup.wire.internal.commonHashCode
22+
import com.squareup.wire.internal.floorDivLong
23+
import com.squareup.wire.internal.floorModLong
24+
25+
actual class Duration internal constructor(
26+
private val seconds: Long,
27+
private val nanos: Int,
28+
) {
29+
actual fun getSeconds(): Long = seconds
30+
actual fun getNano(): Int = nanos
31+
32+
override fun equals(other: Any?): Boolean = commonEquals(other)
33+
34+
override fun hashCode(): Int = commonHashCode()
35+
}
36+
37+
actual fun durationOfSeconds(
38+
seconds: Long,
39+
nano: Long,
40+
): Duration {
41+
val secs = addExactLong(seconds, floorDivLong(nano, NANOS_PER_SECOND))
42+
val nos = floorModLong(nano, NANOS_PER_SECOND).toInt()
43+
return Duration(secs, nos)
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (C) 2016 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
import kotlin.reflect.KClass
19+
20+
/**
21+
* An abstract [ProtoAdapter] that converts values of an enum to and from integers.
22+
*/
23+
actual abstract class EnumAdapter<E : WireEnum> protected actual constructor(
24+
type: KClass<E>,
25+
syntax: Syntax,
26+
identity: E?,
27+
) : ProtoAdapter<E>(FieldEncoding.VARINT, type, null, syntax, identity) {
28+
actual override fun encodedSize(value: E): Int = commonEncodedSize(value)
29+
30+
actual override fun encode(writer: ProtoWriter, value: E) {
31+
commonEncode(writer, value)
32+
}
33+
34+
actual override fun encode(writer: ReverseProtoWriter, value: E) {
35+
commonEncode(writer, value)
36+
}
37+
38+
actual override fun decode(reader: ProtoReader): E = commonDecode(reader, this::fromValue)
39+
40+
actual override fun decode(reader: ProtoReader32): E = commonDecode(reader, this::fromValue)
41+
42+
actual override fun redact(value: E): E = commonRedact(value)
43+
44+
/**
45+
* Converts an integer to an enum.
46+
* Returns null if there is no corresponding enum.
47+
*/
48+
protected actual abstract fun fromValue(value: Int): E?
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2020 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
import com.squareup.wire.internal.NANOS_PER_SECOND
19+
import com.squareup.wire.internal.addExactLong
20+
import com.squareup.wire.internal.commonEquals
21+
import com.squareup.wire.internal.commonHashCode
22+
import com.squareup.wire.internal.floorDivLong
23+
import com.squareup.wire.internal.floorModLong
24+
25+
actual class Instant internal constructor(
26+
private val epochSeconds: Long,
27+
private val nanos: Int,
28+
) {
29+
actual fun getEpochSecond(): Long = epochSeconds
30+
actual fun getNano(): Int = nanos
31+
32+
override fun equals(other: Any?): Boolean = commonEquals(other)
33+
34+
override fun hashCode(): Int = commonHashCode()
35+
}
36+
37+
actual fun ofEpochSecond(epochSecond: Long, nano: Long): Instant {
38+
val secs = addExactLong(epochSecond, floorDivLong(nano, NANOS_PER_SECOND))
39+
val nos = floorModLong(nano, NANOS_PER_SECOND).toInt()
40+
return Instant(secs, nos)
41+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (C) 2013 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
import okio.Buffer
19+
import okio.BufferedSink
20+
import okio.ByteString
21+
22+
/** A protocol buffer message. */
23+
actual abstract class Message<M : Message<M, B>, B : Message.Builder<M, B>>
24+
protected actual constructor(
25+
/** The [ProtoAdapter] for encoding and decoding messages of this type. */
26+
actual val adapter: ProtoAdapter<M>,
27+
/**
28+
* Returns a byte string containing the proto encoding of this message's unknown fields. Returns
29+
* an empty byte string if this message has no unknown fields.
30+
*/
31+
actual open val unknownFields: ByteString,
32+
) {
33+
/** If non-zero, the hash code of this message. Accessed by generated code. */
34+
protected actual var hashCode = 0
35+
36+
/**
37+
* Returns a new builder initialized with the data in this message.
38+
*/
39+
actual abstract fun newBuilder(): B
40+
41+
/** Encode this message and write it to `stream`. */
42+
actual fun encode(sink: BufferedSink) {
43+
@Suppress("UNCHECKED_CAST")
44+
adapter.encode(sink, this as M)
45+
}
46+
47+
/** Encode this message as a `byte[]`. */
48+
actual fun encode(): ByteArray {
49+
@Suppress("UNCHECKED_CAST")
50+
return adapter.encode(this as M)
51+
}
52+
53+
/** Encode this message as a `ByteString`. */
54+
actual fun encodeByteString(): ByteString {
55+
@Suppress("UNCHECKED_CAST")
56+
return adapter.encodeByteString(this as M)
57+
}
58+
59+
/**
60+
* Superclass for protocol buffer message builders.
61+
*/
62+
actual abstract class Builder<M : Message<M, B>, B : Builder<M, B>> protected actual constructor() {
63+
internal actual var unknownFieldsByteString = ByteString.EMPTY
64+
internal actual var unknownFieldsBuffer: Buffer? = null
65+
internal actual var unknownFieldsWriter: ProtoWriter? = null
66+
67+
actual fun addUnknownFields(unknownFields: ByteString): Builder<M, B> = apply {
68+
if (unknownFields.size > 0) {
69+
prepareForNewUnknownFields()
70+
unknownFieldsWriter!!.writeBytes(unknownFields)
71+
}
72+
}
73+
74+
actual fun addUnknownField(
75+
tag: Int,
76+
fieldEncoding: FieldEncoding,
77+
value: Any?,
78+
): Builder<M, B> = apply {
79+
prepareForNewUnknownFields()
80+
@Suppress("UNCHECKED_CAST")
81+
val protoAdapter = fieldEncoding.rawProtoAdapter() as ProtoAdapter<Any>
82+
protoAdapter.encodeWithTag(unknownFieldsWriter!!, tag, value)
83+
}
84+
85+
actual fun clearUnknownFields(): Builder<M, B> = apply {
86+
unknownFieldsByteString = ByteString.EMPTY
87+
if (unknownFieldsBuffer != null) {
88+
unknownFieldsBuffer!!.clear()
89+
unknownFieldsBuffer = null
90+
}
91+
unknownFieldsWriter = null
92+
}
93+
94+
actual fun buildUnknownFields(): ByteString {
95+
if (unknownFieldsBuffer != null) {
96+
// Reads and caches the unknown fields from the buffer.
97+
unknownFieldsByteString = unknownFieldsBuffer!!.readByteString()
98+
unknownFieldsBuffer = null
99+
unknownFieldsWriter = null
100+
}
101+
return unknownFieldsByteString
102+
}
103+
104+
actual abstract fun build(): M
105+
106+
private fun prepareForNewUnknownFields() {
107+
if (unknownFieldsBuffer == null) {
108+
unknownFieldsBuffer = Buffer()
109+
unknownFieldsWriter = ProtoWriter(unknownFieldsBuffer!!)
110+
// Writes the cached unknown fields to the buffer.
111+
unknownFieldsWriter!!.writeBytes(unknownFieldsByteString)
112+
unknownFieldsByteString = ByteString.EMPTY
113+
}
114+
}
115+
}
116+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2019 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
actual interface MessageSink<in T : Any> {
19+
actual fun write(message: T)
20+
21+
actual fun cancel()
22+
23+
actual fun close()
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (C) 2019 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.wire
17+
18+
actual interface MessageSource<out T : Any> {
19+
actual fun read(): T?
20+
21+
actual fun close()
22+
}

0 commit comments

Comments
 (0)