-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70469e9
commit b420ccd
Showing
8 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ib/src/commonMain/kotlin/com/sunnychung/lib/multiplatform/kotlite/stdlib/UuidLibModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
) | ||
) | ||
} |
21 changes: 21 additions & 0 deletions
21
...b/src/commonMain/kotlin/com/sunnychung/lib/multiplatform/kotlite/stdlib/uuid/UuidValue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |