MP fixes: more platforms

This commit is contained in:
Sergey Chernov 2024-06-08 20:27:51 +07:00
parent a5f2128e1d
commit cc8c9ecc5d
6 changed files with 958 additions and 57 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
/gradle/wrapper/gradle-wrapper.jar /gradle/wrapper/gradle-wrapper.jar
/gradle/wrapper/gradle-wrapper.properties /gradle/wrapper/gradle-wrapper.properties
/node_modules /node_modules
.kotlin

View File

@ -1,14 +1,14 @@
plugins { plugins {
kotlin("multiplatform") version "1.9.22" kotlin("multiplatform") version "2.0.0"
kotlin("plugin.serialization") version "1.9.22" kotlin("plugin.serialization") version "2.0.0"
id("org.jetbrains.dokka") version "1.9.10" id("org.jetbrains.dokka") version "1.9.10"
`maven-publish` `maven-publish`
} }
val serialization_version = "1.3.4" val serialization_version = "1.6.4-SNAPSHOT"
group = "net.sergeych" group = "net.sergeych"
version = "0.1.3" version = "0.1.5-SNAPSHOT"
repositories { repositories {
mavenCentral() mavenCentral()
@ -17,7 +17,7 @@ repositories {
} }
kotlin { kotlin {
jvmToolchain(17) jvmToolchain(8)
jvm { jvm {
// compilations.all { // compilations.all {
// kotlinOptions.jvmTarget = "1.8" // kotlinOptions.jvmTarget = "1.8"
@ -49,12 +49,14 @@ kotlin {
} }
} }
macosArm64() // macosArm64()
iosX64() // iosX64()
iosArm64() // iosArm64()
macosX64() // macosX64()
iosSimulatorArm64() // iosSimulatorArm64()
linuxX64()
linuxArm64()
mingwX64()
// val hostOs = System.getProperty("os.name") // val hostOs = System.getProperty("os.name")
// val isMingwX64 = hostOs.startsWith("Windows") // val isMingwX64 = hostOs.startsWith("Windows")
// val nativeTarget = when { // val nativeTarget = when {
@ -64,12 +66,6 @@ kotlin {
// else -> throw GradleException("Host OS is not supported in Kotlin/Native.") // else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
// } // }
linuxX64("native") {
binaries.staticLib {
baseName = "mp_bintools"
}
}
wasmJs { wasmJs {
browser() browser()
binaries.executable() binaries.executable()
@ -90,13 +86,25 @@ kotlin {
} }
val commonMain by getting { val commonMain by getting {
dependencies { dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
// this is actually a bug: we need only the core, but bare core causes strange errors // this is actually a bug: we need only the core, but bare core causes strange errors
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
api("net.sergeych:mp_stools:[1.4.7,)") api("net.sergeych:mp_stools:[1.4.7,)")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
} }
} }
val nativeMain by creating {
dependsOn(commonMain)
dependencies {
}
}
val linuxX64Main by getting {
dependsOn(nativeMain)
}
val linuxArm64Main by getting {
dependsOn(nativeMain)
}
val commonTest by getting { val commonTest by getting {
dependencies { dependencies {
implementation(kotlin("test")) implementation(kotlin("test"))
@ -109,8 +117,7 @@ kotlin {
} }
} }
val jsTest by getting val jsTest by getting
val nativeMain by getting // val nativeTest by getting
val nativeTest by getting
val wasmJsMain by getting { val wasmJsMain by getting {
dependencies { dependencies {
} }

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,9 @@ interface CRC<T> {
fun crc8(data: ByteArray, polynomial: UByte = 0xA7.toUByte()): UByte = fun crc8(data: ByteArray, polynomial: UByte = 0xA7.toUByte()): UByte =
CRC8(polynomial).also { it.update(data) }.value CRC8(polynomial).also { it.update(data) }.value
fun crc8(data: UByteArray, polynomial: UByte = 0xA7.toUByte()): UByte =
CRC8(polynomial).also { it.update(data) }.value
/** /**
* Calculate CRC16 for a data array using a given polynomial (CRC16-CCITT polynomial (0x1021) by default) * Calculate CRC16 for a data array using a given polynomial (CRC16-CCITT polynomial (0x1021) by default)
*/ */

View File

@ -77,19 +77,28 @@ interface KVStorage {
/** /**
* Write * Write
*/ */
inline fun <reified T: Any>KVStorage.write(key: String,value: T) { inline fun <reified T>KVStorage.write(key: String,value: T) {
this[key] = BipackEncoder.encode(value) this[key] = BipackEncoder.encode<T>(value)
}
@Suppress("unused")
inline fun <reified T>KVStorage.save(key: String,value: T?) {
if( value != null ) write(key, value)
else delete(key)
} }
inline fun <reified T:Any>KVStorage.read(key: String): T? = inline fun <reified T:Any>KVStorage.read(key: String): T? =
this[key]?.let { BipackDecoder.decode(it) } this[key]?.let { BipackDecoder.decode<T>(it) }
@Suppress("unused")
inline fun <reified T:Any>KVStorage.load(key: String): T? = read(key)
inline operator fun <reified T> KVStorage.invoke(defaultValue: T,overrideName: String? = null) = inline operator fun <reified T> KVStorage.invoke(defaultValue: T,overrideName: String? = null) =
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName) KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
inline fun <reified T> KVStorage.stored(defaultValue: T, overrideName: String? = null) = inline fun <reified T> KVStorage.stored(defaultValue: T, overrideName: String? = null) =
KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName) KVStorageDelegate<T>(this, typeOf<T>(), defaultValue, overrideName)
inline fun <reified T> KVStorage.optStored(overrideName: String? = null) = inline fun <reified T> KVStorage.optStored(overrideName: String? = null) =
KVStorageDelegate<T?>(this, typeOf<T?>(), null, overrideName) KVStorageDelegate<T?>(this, typeOf<T?>(), null, overrideName)

View File

@ -0,0 +1,38 @@
import net.sergeych.bintools.*
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
class StorageTest {
@Test
fun storageTest() {
val s1 = defaultNamedStorage("test_mp_bintools")
for (n in s1.keys.toList()) s1.delete(n)
assertTrue(s1.keys.isEmpty())
var foo by s1("unknown")
assertEquals(foo, "unknown")
foo = "bar"
assertEquals(foo, "bar")
var answer by s1.optStored<Int>()
assertNull(answer)
answer = 42
assertEquals(answer, 42)
answer = 43
println("----------------------------------------------------------------")
val s2 = defaultNamedStorage("test_mp_bintools")
val foo1 by s2.stored("?", "foo")
val answer1: Int? by s2.optStored("answer")
assertEquals("bar", foo1)
assertEquals(43, answer1)
for (i in 0..<13) {
s2.write("test_$i", "payload_$i")
}
}
}