Skip to content

Commit

Permalink
3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed Nov 2, 2023
1 parent cb0c04c commit eea0754
Show file tree
Hide file tree
Showing 63 changed files with 5,452 additions and 1,652 deletions.
13 changes: 12 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed .kotlintest/spec_failures
Empty file.
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ To have a quick idea what this library offers, take a look at the [tests](https:

### Differences with kotlin stdlib:

- this project uses classes instead inline classes. To address this in critical scenarios where allocations may have a sensitive impact, primitive variable holding the utype value is a `var`, so you can re-use the same istance over and over again
- utypes extend `Number` abstract class
- this project uses classes instead inline classes. To address this in critical scenarios where allocations may have a sensitive impact, primitive variable holding the unsigned
type value is a `var`, so you can re-use the same instance over and over again
- unsigned types extend `Number` abstract class
- automatic conversions
- it is possible to string format by calling the corresponding `format()` method, eg: `ubyte.format("%08x")`
- all the utypes implement all the function, including `shl` and `shr` for `Ubyte` and `Ushort`
- if you add an `Ushort` to another `Ushort` you get an `Ushort` (and not an `Uint`)
- all the unsigned types implement all the function, including `shl` and `shr` for `Ubyte` and `Ushort`
- there is no automatic padding to integer for unsigned bytes and shorts, so if you add an `Ushort` to another `Ushort` you get an `Ushort` (and not an `Uint`)

### Install:

With Gradle `kx.util` plugin, everything is nicely aligned and the boilerplate code is gone

#### mary
```kotlin
repositories {
Expand Down
87 changes: 53 additions & 34 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,43 +1,75 @@
import magik.createGithubPublication
import magik.github
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import uns.gen.GenerateCode
import java.util.*

plugins {
kotlin("jvm") version embeddedKotlinVersion
kotlin("multiplatform") version embeddedKotlinVersion
id("elect86.magik") version "0.3.3"
`maven-publish`
signing
`jvm-test-suite`
// id("com.github.johnrengelman.shadow") version "8.1.1"
// id("com.github.johnrengelman.shadow") version "8.1.1"
}

repositories { mavenCentral() }

dependencies {
testImplementation("io.kotest:kotest-runner-junit5:5.7.1")
testImplementation("io.kotest:kotest-assertions-core:5.7.1")
kotlin {
jvm {
jvmToolchain(11)
withJava()
}
val hostOs = System.getProperty("os.name")
val isArm64 = System.getProperty("os.arch") == "aarch64"
val isMingwX64 = hostOs.startsWith("Windows")
val nativeTarget = when {
hostOs == "Mac OS X" && isArm64 -> macosArm64("native")
hostOs == "Mac OS X" && !isArm64 -> macosX64("native")
hostOs == "Linux" && isArm64 -> linuxArm64("native")
hostOs == "Linux" && !isArm64 -> linuxX64("native")
isMingwX64 -> mingwX64("native")
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.ionspin.kotlin:bignum:0.3.8")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jvmMain by getting
val jvmTest by getting
// val jsMain by getting
// val jsTest by getting
val nativeMain by getting
val nativeTest by getting
}
}

kotlin.jvmToolchain { languageVersion.set(JavaLanguageVersion.of(11)) }

tasks {
val generateCode by registering(GenerateCode::class)
kotlin.sourceSets.commonMain { kotlin.srcDir(generateCode) }
withType<KotlinCompile<*>>().all { kotlinOptions { freeCompilerArgs += listOf("-opt-in=kotlin.RequiresOptIn") } }
}

testing.suites {
val test by getting(JvmTestSuite::class) { useJUnitJupiter() }
}

java {
withJavadocJar()
withSourcesJar()
}
//java {
// withJavadocJar()
// withSourcesJar()
//}


configure<PublishingExtension> {
publications {
createGithubPublication("Github") {
createGithubPublication {
from(components["java"])
suppressAllPomMetadataWarnings()
}
Expand All @@ -50,31 +82,18 @@ configure<PublishingExtension> {
usage("java-runtime") { fromResolutionResult() }
}
pom {
name.set("kotlin-unsigned")
description.set("unsigned support for Kotlin via boxed types and unsigned operators")
url.set("https://github.com/kotlin-graphics/kotlin-unsigned")
licenses {
license {
name.set("MIT")
url.set("https://choosealicense.com/licenses/mit/")
}
}
name = "kotlin-unsigned"
description = "unsigned support for Kotlin via boxed types and unsigned operators"
url = "https://github.com/kotlin-graphics/kotlin-unsigned"
licenses { license { name = "MIT"; url = "https://choosealicense.com/licenses/mit/" } }
developers {
developer {
id.set("elect86")
name.set("Giuseppe Barbieri")
email.set("[email protected]")
}
developer {
id.set("bixilon")
name.set("Moritz Zwerger")
email.set("[email protected]")
}
developer { id = "elect86"; name = "Giuseppe Barbieri"; email = "[email protected]" }
developer { id = "bixilon"; name = "Moritz Zwerger"; email = "[email protected]" }
}
scm {
connection.set("scm:git:https://github.com/kotlin-graphics/kotlin-unsigned.git")
developerConnection.set("scm:git:ssh://[email protected]:kotlin-graphics/kotlin-unsigned.git")
url.set("https://github.com/kotlin-graphics/kotlin-unsigned")
connection = "scm:git:https://github.com/kotlin-graphics/kotlin-unsigned.git"
developerConnection = "scm:git:ssh://[email protected]:kotlin-graphics/kotlin-unsigned.git"
url = "https://github.com/kotlin-graphics/kotlin-unsigned"
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`kotlin-dsl`
}

repositories {
gradlePluginPortal()
}
1 change: 1 addition & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "buildSrc"
63 changes: 63 additions & 0 deletions buildSrc/src/main/kotlin/uns/arrays.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package uns

import uns.gen.Generator
import uns.gen.generate

fun arrays() {
generate("arrays") {
imports += "kotlin.jvm.JvmInline"
imports += "kotlin.jvm.JvmName"
imports += "kotlin.experimental.ExperimentalTypeInference"
experimentals += Generator.Experimentals.UnsignedTypes
experimentals += Generator.Experimentals.TypeInference
for (info in unsInfos) {
val Type = info.type
val TypeArray = Type + "Array"
val CounterArray = info.counterType + "Array"
val CounterType = info.counterType
+"""
@JvmInline
value class $TypeArray(val data: $CounterArray) {
operator fun get(index: I32): $Type = $Type(data[index])
operator fun set(index: I32, value: $Type) { data[index] = value.v }
val indices: IntRange
get() = data.indices
}
fun $TypeArray(size: I32): $TypeArray = $TypeArray($CounterArray(size))
@OverloadResolutionByLambdaReturnType
@JvmName("$TypeArray$CounterType")
inline fun $TypeArray(size: I32, init: (I32) -> $CounterType): $TypeArray = $TypeArray($CounterArray(size, init))
@OverloadResolutionByLambdaReturnType
@JvmName("$TypeArray$Type")
inline fun $TypeArray(size: I32, init: (I32) -> $Type): $TypeArray {
val array = $TypeArray($CounterArray(size))
for (i in array.indices)
array[i] = init(i)
return array
}"""
if (info.bits != 32)
+"""
@OverloadResolutionByLambdaReturnType
@JvmName("${TypeArray}I32")
inline fun $TypeArray(size: I32, init: (I32) -> I32): $TypeArray {
val array = $TypeArray($CounterArray(size))
for (i in array.indices)
array.data[i] = init(i).to$CounterType()
return array
}
@OverloadResolutionByLambdaReturnType
@JvmName("${TypeArray}U32")
inline fun $TypeArray(size: I32, init: (I32) -> U32): $TypeArray {
val array = $TypeArray($CounterArray(size))
for (i in array.indices)
array.data[i] = init(i).to$CounterType()
return array
}"""
}
}
}

47 changes: 47 additions & 0 deletions buildSrc/src/main/kotlin/uns/extensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uns

import org.gradle.configurationcache.extensions.capitalized
import uns.gen.generate

fun extensions() {

generate("extensions") {

imports += "com.ionspin.kotlin.bignum.integer.toBigInteger"

for (receiver in intTypeInfos + kotlinUnsTypeInfo) {
val ReceiverType = receiver.type
for (extInfo in intTypeInfos + kotlinUnsTypeInfo + floatingTypes) {
val ext = extInfo.extension
val extType = extInfo.type
val ExtType = when {
extInfo.isKotlinUns -> extInfo.type
else -> ext.capitalized()
}
val toType = when {
ReceiverType == "BigInt" -> when {
extInfo.isUx -> "$ExtType(${extInfo.counterType.lowercase()}Value())"
else -> extInfo.type.lowercase() + "Value()"
}
extInfo.isUx -> "$ExtType(this)"
extInfo.type == "BigInt" -> if (receiver.isUx) "toBigInt()" else "toBigInteger()"
else -> "to$extType()"
}
if (ReceiverType != extType) {
+"""
inline val $ReceiverType.$ext: $ExtType
get() = $toType
"""
fun alias(alias: Char) = +"""
inline val $ReceiverType.$alias: $ExtType
get() = $ext"""
when (ext) {
"i32" -> alias('i')
"i64" -> alias('L')
"f32" -> alias('f')
}
}
}
}
}
}
65 changes: 65 additions & 0 deletions buildSrc/src/main/kotlin/uns/gen/GenerateCode.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package uns.gen

import org.gradle.api.DefaultTask
import org.gradle.api.file.Directory
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.file.ProjectLayout
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import uns.arrays
import uns.extensions
import uns.operators
import uns.types
import javax.inject.Inject

abstract class GenerateCode : DefaultTask() {

init {
group = "build"
description = "Generate uns code"
}

@get:Inject
abstract val layout: ProjectLayout

@get:Inject
abstract val files: FileSystemOperations

@get:OutputDirectory
val targetDir: Directory = layout.projectDirectory.dir("src/genCommonMain/kotlin")

@TaskAction
fun generate() {
Generator.targetDir = targetDir.asFile

files.delete { delete(Generator.targetDir) }
// val targetJvm = targetJvmDir.get().asFile
// val targetNonJvm = targetNonJvmDir.get().asFile
arrays()
types()
extensions()
operators()
}
}

fun generate(file: String, block: Generator.() -> Unit) {
Generator().apply {
block()
if (imports.isNotEmpty())
builder.insert(0, '\n')
for (import in imports)
builder.insert(0, "import $import\n")
// if (`package`.isNotEmpty())
val f = file.replace('/', '.').substringBefore('.', "")
val pack = if (f.isEmpty()) "uns" else "uns.$f"
builder.insert(0, "package $pack\n\n")
if (experimentals.isNotEmpty()) {
val list = experimentals.joinToString { "${it.pkg}.Experimental${it.name}::class" }
builder.insert(0, "@file:OptIn($list)\n")
}
if (disableNameShadowing) builder.insert(0, "@file:Suppress(\"NAME_SHADOWING\")\n")
if (nothingToInline) builder.insert(0, "@file:Suppress(\"NOTHING_TO_INLINE\")\n")
write(file)
}
}

Loading

0 comments on commit eea0754

Please sign in to comment.