Skip to content

Commit

Permalink
add UUID library
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Apr 18, 2024
1 parent 70469e9 commit b420ccd
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 1 deletion.
3 changes: 2 additions & 1 deletion doc/usermanual/The Standard Library.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ At this moment, the Standard Library consists of following Kotlin/Common standar
* kotlin.ranges
* kotlin.text
and the following external library:
and the following external libraries:

* https://github.com/sunny-chung/kdatetime-multiplatform[KDateTime]
* https://github.com/benasher44/uuid[A Kotlin Multiplatform UUID]
Currently, not all of their functions and properties are imported, but it is planned to support all of them, as long as the inclusion does not bring security issues.

Expand Down
1 change: 1 addition & 0 deletions stdlib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- UUID library module
- Global function `setKotliteStdlibLogMinLevel(severity: Severity)`

### Removed
Expand Down
14 changes: 14 additions & 0 deletions stdlib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ kotlin {
implementation("co.touchlab:kermit:1.0.0")
implementation(project(":kotlite-interpreter"))
implementation("io.github.sunny-chung:kdatetime-multiplatform:1.0.0")
implementation("com.benasher44:uuid:0.8.4")
}
// kotlin.srcDir("build/generated/common/")
kotlin.srcDir(tasks.named("kotliteStdlibHeaderProcess").map { it.outputs })
Expand Down Expand Up @@ -225,5 +226,18 @@ kotliteStdLibHeaderProcessor {
"com.sunnychung.lib.multiplatform.kotlite.stdlib.range.LongRangeValue",
)
),
"Uuid" to KotliteModuleConfig(
imports = listOf(
"com.benasher44.uuid.Uuid",
"com.benasher44.uuid.bytes",
"com.benasher44.uuid.variant",
"com.benasher44.uuid.version",
"com.benasher44.uuid.uuidFrom",
"com.benasher44.uuid.uuidOf",
"com.benasher44.uuid.uuid4",
"com.sunnychung.lib.multiplatform.kotlite.stdlib.byte.ByteArrayValue",
"com.sunnychung.lib.multiplatform.kotlite.stdlib.uuid.UuidValue",
)
),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ class AllStdLibModules(outputToConsoleFunction: (String) -> Unit = { print(it) }
ByteLibModule(),
RangeLibModule(),
KDateTimeLibModule(),
UuidLibModule(),
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sunnychung.lib.multiplatform.kotlite.stdlib

import com.benasher44.uuid.uuid4
import com.sunnychung.lib.multiplatform.kotlite.model.CustomFunctionDefinition
import com.sunnychung.lib.multiplatform.kotlite.model.ProvidedClassDefinition
import com.sunnychung.lib.multiplatform.kotlite.model.SourcePosition
import com.sunnychung.lib.multiplatform.kotlite.model.StringValue
import com.sunnychung.lib.multiplatform.kotlite.stdlib.uuid.UuidClass

class UuidLibModule : AbstractUuidLibModule() {
override val classes: List<ProvidedClassDefinition> = super.classes + listOf(
UuidClass.clazz
)
override val functions: List<CustomFunctionDefinition> = super.functions + listOf(
CustomFunctionDefinition(
position = SourcePosition("Uuid", 1, 1),
receiverType = null,
functionName = "uuidString",
returnType = "String",
parameterTypes = emptyList(),
executable = { interpreter, receiver, args, typeArgs ->
val uuidString = uuid4().toString()
StringValue(uuidString, interpreter.symbolTable())
}
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sunnychung.lib.multiplatform.kotlite.stdlib.uuid

import com.benasher44.uuid.Uuid
import com.sunnychung.lib.multiplatform.kotlite.model.DelegatedValue
import com.sunnychung.lib.multiplatform.kotlite.model.ProvidedClassDefinition
import com.sunnychung.lib.multiplatform.kotlite.model.SourcePosition
import com.sunnychung.lib.multiplatform.kotlite.model.SymbolTable

fun UuidValue(value: Uuid, symbolTable: SymbolTable)
= DelegatedValue<Uuid>(value, UuidClass.clazz, emptyList(), symbolTable)

object UuidClass {
val clazz = ProvidedClassDefinition(
fullQualifiedName = "Uuid",
typeParameters = emptyList(),
isInstanceCreationAllowed = false,
primaryConstructorParameters = emptyList(),
constructInstance = { _, _, _ -> throw UnsupportedOperationException() },
position = SourcePosition("Uuid", 1, 1),
)
}
64 changes: 64 additions & 0 deletions stdlib/src/commonTest/kotlin/UuidLibTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import com.sunnychung.lib.multiplatform.kotlite.model.ExecutionEnvironment
import com.sunnychung.lib.multiplatform.kotlite.model.IntValue
import com.sunnychung.lib.multiplatform.kotlite.model.StringValue
import com.sunnychung.lib.multiplatform.kotlite.stdlib.ByteLibModule
import com.sunnychung.lib.multiplatform.kotlite.stdlib.CollectionsLibModule
import com.sunnychung.lib.multiplatform.kotlite.stdlib.CoreLibModule
import com.sunnychung.lib.multiplatform.kotlite.stdlib.TextLibModule
import com.sunnychung.lib.multiplatform.kotlite.stdlib.UuidLibModule
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class UuidLibTest {

@Test
fun generateUuidV4() {
val env = ExecutionEnvironment().apply {
install(CoreLibModule())
install(CollectionsLibModule())
install(TextLibModule())
install(ByteLibModule())
install(UuidLibModule())
}
val interpreter = interpreter("""
val uuids = mutableSetOf<String>()
repeat(20000) {
uuids += uuid4().toString()
}
val n = uuids.size
val f = uuids.first()
""".trimIndent(), executionEnvironment = env)
interpreter.eval()
val symbolTable = interpreter.symbolTable()
assertEquals(20000, (symbolTable.findPropertyByDeclaredName("n") as IntValue).value)
assertEquals(36, (symbolTable.findPropertyByDeclaredName("f") as StringValue).value.length)
assertTrue("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}".toRegex()
.matches((symbolTable.findPropertyByDeclaredName("f") as StringValue).value))
}

@Test
fun generateUuidV4Shorthand() {
val env = ExecutionEnvironment().apply {
install(CoreLibModule())
install(CollectionsLibModule())
install(TextLibModule())
install(ByteLibModule())
install(UuidLibModule())
}
val interpreter = interpreter("""
val uuids = mutableSetOf<String>()
repeat(20000) {
uuids += uuidString()
}
val n = uuids.size
val f = uuids.first()
""".trimIndent(), executionEnvironment = env)
interpreter.eval()
val symbolTable = interpreter.symbolTable()
assertEquals(20000, (symbolTable.findPropertyByDeclaredName("n") as IntValue).value)
assertEquals(36, (symbolTable.findPropertyByDeclaredName("f") as StringValue).value.length)
assertTrue("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}".toRegex()
.matches((symbolTable.findPropertyByDeclaredName("f") as StringValue).value))
}
}
11 changes: 11 additions & 0 deletions stdlib/src/kotlinheader/Uuid.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
fun uuid4(): Uuid
fun uuidFrom(string: String): Uuid
fun uuidOf(bytes: ByteArray): Uuid

val Uuid.mostSignificantBits: Long get()
val Uuid.leastSignificantBits: Long get()
val Uuid.bytes: ByteArray get()
val Uuid.variant: Int get()
val Uuid.version: Int get()

fun Uuid.toString(): String

0 comments on commit b420ccd

Please sign in to comment.