Skip to content

Commit fd5e198

Browse files
committed
v0.9.76
1 parent 8118d8c commit fd5e198

File tree

9 files changed

+223
-43
lines changed

9 files changed

+223
-43
lines changed

buildSrc/src/main/kotlin/kool/pointers.kt

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fun pointers(target: File) {
2626

2727
val TypeBuffer = Type + "Buffer"
2828
var maybeTimes = if ("Byte" in Type) "" else " * $Type.BYTES.toULong()"
29-
val type = Type.decapitalize()
29+
val type = Type.decapitalized()
3030
val unsigned = Type[0] == 'U'
3131
imports += when {
3232
unsigned -> listOf("kool.ubuffers.$TypeBuffer", "kool.ubuffers.as$TypeBuffer")
@@ -35,7 +35,7 @@ fun pointers(target: File) {
3535
}
3636
val Ype = if (unsigned) Type.drop(1) else Type
3737
val maybeToU = if (unsigned) ".to$Type()" else ""
38-
val maybeAsU = if (unsigned) ".as$TypeBuffer()" else ""
38+
// val maybeAsU = if (unsigned) ".as$TypeBuffer()" else ""
3939
val maybeToS = if (unsigned) ".to$Ype()" else ""
4040

4141
val PtrType = "Ptr<$Type>"
@@ -76,24 +76,78 @@ fun pointers(target: File) {
7676
imports += "java.nio.ByteBuffer"
7777
if (unsigned)
7878
+"""
79-
inline fun $PtrType.get$Type(index: Int): $Type = get$Type(index.toULong())
80-
inline fun $PtrType.get$Type(index: UInt): $Type = get$Type(index.toULong())
81-
inline fun $PtrType.get$Type(index: Long): $Type = get$Type(index.toULong())
82-
inline fun $PtrType.get$Type(index: ULong = 0uL): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
83-
inline fun $PtrType.invoke$Type(index: Int): $Type = invoke$Type(index.toULong())
84-
inline fun $PtrType.invoke$Type(index: UInt): $Type = invoke$Type(index.toULong())
85-
inline fun $PtrType.invoke$Type(index: Long): $Type = invoke$Type(index.toULong())
86-
inline fun $PtrType.invoke$Type(index: ULong = 0uL): $Type = get$Type(index)"""
79+
@JvmName("getInt")
80+
inline operator infix fun $PtrType.get(index: Int): $Type = get(index.toULong())
81+
@JvmName("getUInt")
82+
inline infix operator fun $PtrType.get(index: UInt): $Type = get(index.toULong())
83+
@JvmName("getLong")
84+
inline infix operator fun $PtrType.get(index: Long): $Type = get(index.toULong())
85+
@JvmName("getULong")
86+
inline fun $PtrType.get(): $Type = get(0uL)
87+
@JvmName("getULong")
88+
inline infix operator fun $PtrType.get(index: ULong): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
89+
@JvmName("invokeInt")
90+
inline infix operator fun $PtrType.invoke(index: Int): $Type = invoke(index.toULong())
91+
@JvmName("invokeUInt")
92+
inline infix operator fun $PtrType.invoke(index: UInt): $Type = invoke(index.toULong())
93+
@JvmName("invokeLong")
94+
inline infix operator fun $PtrType.invoke(index: Long): $Type = invoke(index.toULong())
95+
@JvmName("invokeULong")
96+
inline operator fun $PtrType.invoke(): $Type = get(0uL)
97+
@JvmName("invokeULong")
98+
inline infix operator fun $PtrType.invoke(index: ULong): $Type = get(index)
99+
@JvmName("plus${Type}Int")
100+
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = Ptr(address + offset.toULong())
101+
@JvmName("plus${Type}UInt")
102+
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = Ptr(address + offset.toULong())
103+
@JvmName("plus${Type}Long")
104+
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = Ptr(address + offset.toULong())
105+
@JvmName("plus${Type}ULong")
106+
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset)
107+
@JvmName("minus${Type}Int")
108+
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = Ptr(address - offset.toULong())
109+
@JvmName("minus${Type}UInt")
110+
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = Ptr(address - offset.toULong())
111+
@JvmName("minus${Type}Long")
112+
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = Ptr(address - offset.toULong())
113+
@JvmName("minus${Type}ULong")
114+
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset)
115+
@JvmName("inc$Type")
116+
inline operator fun $PtrType.inc(): $PtrType = Ptr(address + $Type.BYTES.toUInt())
117+
@JvmName("dec$Type")
118+
inline operator fun $PtrType.dec(): $PtrType = Ptr(address - $Type.BYTES.toUInt())"""
87119
else
88120
+"""
89-
inline operator fun $PtrType.get(index: Int): $Type = get(index.toULong())
90-
inline operator fun $PtrType.get(index: UInt): $Type = get(index.toULong())
91-
inline operator fun $PtrType.get(index: Long): $Type = get(index.toULong())
92-
inline operator fun $PtrType.get(index: ULong = 0uL): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
93-
inline operator fun $PtrType.invoke(index: Int): $Type = invoke(index.toULong())
94-
inline operator fun $PtrType.invoke(index: UInt): $Type = invoke(index.toULong())
95-
inline operator fun $PtrType.invoke(index: Long): $Type = invoke(index.toULong())
96-
inline operator fun $PtrType.invoke(index: ULong = 0uL): $Type = get(index)"""
121+
inline infix operator fun $PtrType.get(index: Int): $Type = get(index.toULong())
122+
inline infix operator fun $PtrType.get(index: UInt): $Type = get(index.toULong())
123+
inline infix operator fun $PtrType.get(index: Long): $Type = get(index.toULong())
124+
inline fun $PtrType.get(): $Type = get(0uL)
125+
inline infix operator fun $PtrType.get(index: ULong): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
126+
inline infix operator fun $PtrType.invoke(index: Int): $Type = invoke(index.toULong())
127+
inline infix operator fun $PtrType.invoke(index: UInt): $Type = invoke(index.toULong())
128+
inline infix operator fun $PtrType.invoke(index: Long): $Type = invoke(index.toULong())
129+
inline operator fun $PtrType.invoke(): $Type = get(0uL)
130+
inline infix operator fun $PtrType.invoke(index: ULong): $Type = get(index)
131+
@JvmName("plus${Type}Int")
132+
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = Ptr(address + offset.toULong())
133+
@JvmName("plus${Type}UInt")
134+
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = Ptr(address + offset.toULong())
135+
@JvmName("plus${Type}Long")
136+
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = Ptr(address + offset.toULong())
137+
@JvmName("plus${Type}ULong")
138+
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset)
139+
@JvmName("minus${Type}Int")
140+
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = Ptr(address - offset.toULong())
141+
@JvmName("minus${Type}UInt")
142+
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = Ptr(address - offset.toULong())
143+
@JvmName("minus${Type}Long")
144+
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = Ptr(address - offset.toULong())
145+
@JvmName("minus${Type}ULong")
146+
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset)
147+
@JvmName("inc$Type")
148+
inline operator fun $PtrType.inc(): $PtrType = Ptr(address + $Type.BYTES.toUInt())
149+
@JvmName("dec$Type")
150+
inline operator fun $PtrType.dec(): $PtrType = Ptr(address - $Type.BYTES.toUInt())"""
97151
+"""
98152
inline operator fun $PtrType.set(index: Int, $type: $Type) = set(index.toULong(), $type)
99153
inline operator fun $PtrType.set(index: UInt, $type: $Type) = set(index.toULong(), $type)

buildSrc/src/main/kotlin/kool/stack.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kool
22

33
import kool.gen.Generator
44
import kool.gen.generate
5+
import org.gradle.configurationcache.extensions.capitalized
56
import java.io.File
67

78
fun stack(target: File) {
@@ -64,15 +65,13 @@ fun stack(target: File) {
6465
}
6566

6667
val maybeAsUns = if (unsigned) ".as$TypeBuffer()" else ""
67-
val maybeU = if (unsigned) "U" else ""
6868
val mallocType = when (type) {
6969
"Byte", "UByte" -> ""
7070
else -> if (unsigned) type.drop(1) else type
7171
}
72-
val invoke = if (unsigned) "invoke$type" else "invoke"
7372

7473
+"""
75-
inline fun <R> read${type}FromAdr(block: (Adr) -> R): $type = with { Ptr$type().apply { block(adr) }.$invoke() }
74+
inline fun <R> read${type}FromAdr(block: (Adr) -> R): $type = with { Ptr$type().apply { block(adr) }.invoke() }
7675
7776
inline fun <R> read${type}FromBuf(block: ($TypeBuffer) -> R): $type = with {
7877
val buf = malloc$mallocType(1)$maybeAsUns
@@ -82,8 +81,8 @@ fun stack(target: File) {
8281
}
8382

8483
for (enc in listOf("ascii", "utf8", "utf16")) {
85-
val ENC = enc.toUpperCase()
86-
val Enc = enc.capitalize()
84+
val ENC = enc.uppercase()
85+
val Enc = enc.capitalized()
8786
+"""
8887
/** It mallocs, passes the address and reads the null terminated string */
8988
inline fun <R> read${Enc}FromAdr(maxSize: Int, block: (Adr) -> R): String = with {
@@ -108,15 +107,15 @@ fun stack(target: File) {
108107
// --------------------------------------------- setters ---------------------------------------------"""
109108

110109
for (Type in types.filter { it != "Char" && it != "Pointer" }) {
111-
val type = Type.toLowerCase()
110+
val type = Type.lowercase()
112111
+"""
113112
inline fun <R> writeToAdr($type: $Type, block: (Adr) -> R): R = with { block(ptrOf($type).adr) }
114113
inline fun <R> writeToBuf($type: $Type, block: (${Type}Buffer) -> R): R = with { block(bufferOf($type)) }"""
115114
}
116115

117116
for (enc in listOf("ascii", "utf8", "utf16")) {
118-
val ENC = enc.toUpperCase()
119-
val Enc = enc.capitalize()
117+
val ENC = enc.uppercase()
118+
val Enc = enc.capitalized()
120119
+"""
121120
inline fun <R> write${Enc}ToAdr(chars: CharSequence, nullTerminated: Boolean = true, block: (Adr) -> R): R =
122121
with {

buildSrc/src/main/kotlin/kool/stackExts.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kool
22

33
import kool.gen.Generator
44
import kool.gen.generate
5+
import org.gradle.configurationcache.extensions.capitalized
56
import java.io.File
67

78
fun stackExts(target: File) {
@@ -27,7 +28,7 @@ fun stackExts(target: File) {
2728
val `Ptr Type` = "Ptr<$type>"
2829
val PtrType = "Ptr$type"
2930
val typeBuffer = type + "Buffer"
30-
val ts = type.toLowerCase() + 's'
31+
val ts = type.lowercase() + 's'
3132

3233
for (i in 1..5) {
3334
var biJoint = (1..i).joinToString { "b$it: $type" }
@@ -103,10 +104,8 @@ fun stackExts(target: File) {
103104
else -> "java.nio.${Type}Buffer"
104105
}
105106

106-
val invoke = if (unsigned) "invoke$Type" else "invoke"
107-
108107
+"""
109-
inline fun <R> MemoryStack.read${Type}FromAdr(block: (Adr) -> R): $Type = Ptr<$Type>().apply { block(adr) }.$invoke()
108+
inline fun <R> MemoryStack.read${Type}FromAdr(block: (Adr) -> R): $Type = Ptr<$Type>().apply { block(adr) }.invoke()
110109
inline fun <R> MemoryStack.read${Type}FromBuf(block: (${Type}Buffer) -> R): $Type = ${Type}Buffer(1).also { block(it) }[0]
111110
"""
112111
}
@@ -125,8 +124,8 @@ fun stackExts(target: File) {
125124
}"""
126125

127126
for (enc in listOf("ascii", "utf8", "utf16")) {
128-
val ENC = enc.toUpperCase()
129-
val Enc = enc.capitalize()
127+
val ENC = enc.uppercase()
128+
val Enc = enc.capitalized()
130129
+"""
131130
/** It mallocs, passes the address and reads the null terminated string */
132131
inline fun <R> MemoryStack.read${Enc}FromAdr(maxSize: Int, block: (Adr) -> R): String {
@@ -148,8 +147,8 @@ fun stackExts(target: File) {
148147
// inline fun MemoryStack.pointerAdr(ptr: Ptr): Adr = ptrOf(ptr).adr
149148
// inline fun MemoryStack.pointerBuffer(ptr: Ptr): ByteBuffer = ${type}s($type)"""
150149
for (enc in listOf("ascii", "utf8", "utf16")) {
151-
val ENC = enc.toUpperCase()
152-
val Enc = enc.capitalize()
150+
val ENC = enc.uppercase()
151+
val Enc = enc.capitalized()
153152
+"""
154153
fun MemoryStack.write${Enc}ToAdr(chars: CharSequence, nullTerminated: Boolean = true): Adr = n$ENC(chars, nullTerminated).let { pointerAddress }.toULong()
155154
fun MemoryStack.write${Enc}ToBuffer(chars: CharSequence, nullTerminated: Boolean = true): ByteBuffer = $ENC(chars, nullTerminated)"""

buildSrc/src/main/kotlin/kool/unsafe.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fun unsafe(target: File) {
3636
}?.run {
3737
isAccessible = true
3838
get(null) as Unsafe
39-
} ?: throw UnsupportedOperationException("LWJGL requires sun.misc.Unsafe to be available.")
39+
} ?: throw UnsupportedOperationException("Kool requires sun.misc.Unsafe to be available.")
4040
}
4141
"""
4242

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package kool
22

3+
import java.util.*
4+
35
val jvmInline = "@JvmInline"
46
val overloadResolutionByLambdaReturnType = "@OverloadResolutionByLambdaReturnType"
5-
fun jvmName(name: String) = "@kotlin.jvm.JvmName(\"$name\")"
7+
fun jvmName(name: String) = "@kotlin.jvm.JvmName(\"$name\")"
8+
9+
fun CharSequence.decapitalized(): String =
10+
when {
11+
isEmpty() -> ""
12+
else -> get(0).let { initial ->
13+
when {
14+
initial.isUpperCase() -> initial.lowercase(Locale.getDefault()) + substring(1)
15+
else -> toString()
16+
}
17+
}
18+
}

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pluginManagement {
1010

1111
gradle.rootProject {
1212
group = "kotlin.graphics"
13-
version = "0.9.75"
13+
version = "0.9.76"
1414
}
1515

1616
//includeBuild("../build-logic")

src/main/kotlin/kool/Ptr.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ package kool
44
import org.lwjgl.system.MemoryUtil
55

66
@JvmInline
7-
value class Ptr<out T> constructor(val address: Address = NULL.address) {
7+
value class Ptr<out T>(val address: Address = NULL.address) : Comparable<Ptr<@UnsafeVariance T>>{
88
inline val adr: Adr
99
get() = address
1010
inline val isValid: Boolean
1111
get() = adr != 0uL
1212
inline val isNotValid: Boolean
1313
get() = adr == 0uL
1414

15-
inline operator fun plus(offset: Int): Ptr<T> = plus(offset.toULong())
16-
inline operator fun plus(offset: Long): Ptr<T> = plus(offset.toULong())
17-
inline operator fun plus(offset: ULong): Ptr<T> = Ptr(address + offset)
18-
1915
inline fun <T> toPtr(): Ptr<T> = Ptr(adr)
16+
17+
override fun compareTo(other: Ptr<@UnsafeVariance T>): Int = address.compareTo(other.address)
2018
companion object {
2119
val NULL: Ptr<Nothing>
2220
get() = Ptr(MemoryUtil.NULL.toULong())
@@ -27,4 +25,7 @@ inline fun <T> Ptr(long: Long): Ptr<T> = Ptr(long.toULong())
2725

2826
typealias Pointer = ULong
2927
typealias Adr = ULong
30-
typealias Address = ULong
28+
typealias Address = ULong
29+
30+
//inline operator fun Ptr<*>.plus(offset: Long): Ptr<*> = plus(offset.toULong())
31+
//inline operator fun <T> Ptr<T>.plus(offset: ULong): Ptr<T> = Ptr(address + offset)

src/main/kotlin/kool/utils.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.lwjgl.system.MathUtil
77
import org.lwjgl.system.MemoryUtil
88
import org.lwjgl.system.Pointer
99
import java.nio.*
10+
import java.util.*
1011
import kotlin.reflect.*
1112

1213
inline val Pointer.adr: Adr

0 commit comments

Comments
 (0)