Skip to content

Commit

Permalink
v0.9.76
Browse files Browse the repository at this point in the history
  • Loading branch information
elect86 committed Apr 13, 2023
1 parent 8118d8c commit fd5e198
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 43 deletions.
90 changes: 72 additions & 18 deletions buildSrc/src/main/kotlin/kool/pointers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun pointers(target: File) {

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

val PtrType = "Ptr<$Type>"
Expand Down Expand Up @@ -76,24 +76,78 @@ fun pointers(target: File) {
imports += "java.nio.ByteBuffer"
if (unsigned)
+"""
inline fun $PtrType.get$Type(index: Int): $Type = get$Type(index.toULong())
inline fun $PtrType.get$Type(index: UInt): $Type = get$Type(index.toULong())
inline fun $PtrType.get$Type(index: Long): $Type = get$Type(index.toULong())
inline fun $PtrType.get$Type(index: ULong = 0uL): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
inline fun $PtrType.invoke$Type(index: Int): $Type = invoke$Type(index.toULong())
inline fun $PtrType.invoke$Type(index: UInt): $Type = invoke$Type(index.toULong())
inline fun $PtrType.invoke$Type(index: Long): $Type = invoke$Type(index.toULong())
inline fun $PtrType.invoke$Type(index: ULong = 0uL): $Type = get$Type(index)"""
@JvmName("getInt")
inline operator infix fun $PtrType.get(index: Int): $Type = get(index.toULong())
@JvmName("getUInt")
inline infix operator fun $PtrType.get(index: UInt): $Type = get(index.toULong())
@JvmName("getLong")
inline infix operator fun $PtrType.get(index: Long): $Type = get(index.toULong())
@JvmName("getULong")
inline fun $PtrType.get(): $Type = get(0uL)
@JvmName("getULong")
inline infix operator fun $PtrType.get(index: ULong): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
@JvmName("invokeInt")
inline infix operator fun $PtrType.invoke(index: Int): $Type = invoke(index.toULong())
@JvmName("invokeUInt")
inline infix operator fun $PtrType.invoke(index: UInt): $Type = invoke(index.toULong())
@JvmName("invokeLong")
inline infix operator fun $PtrType.invoke(index: Long): $Type = invoke(index.toULong())
@JvmName("invokeULong")
inline operator fun $PtrType.invoke(): $Type = get(0uL)
@JvmName("invokeULong")
inline infix operator fun $PtrType.invoke(index: ULong): $Type = get(index)
@JvmName("plus${Type}Int")
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = Ptr(address + offset.toULong())
@JvmName("plus${Type}UInt")
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = Ptr(address + offset.toULong())
@JvmName("plus${Type}Long")
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = Ptr(address + offset.toULong())
@JvmName("plus${Type}ULong")
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset)
@JvmName("minus${Type}Int")
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = Ptr(address - offset.toULong())
@JvmName("minus${Type}UInt")
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = Ptr(address - offset.toULong())
@JvmName("minus${Type}Long")
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = Ptr(address - offset.toULong())
@JvmName("minus${Type}ULong")
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset)
@JvmName("inc$Type")
inline operator fun $PtrType.inc(): $PtrType = Ptr(address + $Type.BYTES.toUInt())
@JvmName("dec$Type")
inline operator fun $PtrType.dec(): $PtrType = Ptr(address - $Type.BYTES.toUInt())"""
else
+"""
inline operator fun $PtrType.get(index: Int): $Type = get(index.toULong())
inline operator fun $PtrType.get(index: UInt): $Type = get(index.toULong())
inline operator fun $PtrType.get(index: Long): $Type = get(index.toULong())
inline operator fun $PtrType.get(index: ULong = 0uL): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
inline operator fun $PtrType.invoke(index: Int): $Type = invoke(index.toULong())
inline operator fun $PtrType.invoke(index: UInt): $Type = invoke(index.toULong())
inline operator fun $PtrType.invoke(index: Long): $Type = invoke(index.toULong())
inline operator fun $PtrType.invoke(index: ULong = 0uL): $Type = get(index)"""
inline infix operator fun $PtrType.get(index: Int): $Type = get(index.toULong())
inline infix operator fun $PtrType.get(index: UInt): $Type = get(index.toULong())
inline infix operator fun $PtrType.get(index: Long): $Type = get(index.toULong())
inline fun $PtrType.get(): $Type = get(0uL)
inline infix operator fun $PtrType.get(index: ULong): $Type = unsafe.get$Ype(adr + index$maybeTimes)$maybeToU
inline infix operator fun $PtrType.invoke(index: Int): $Type = invoke(index.toULong())
inline infix operator fun $PtrType.invoke(index: UInt): $Type = invoke(index.toULong())
inline infix operator fun $PtrType.invoke(index: Long): $Type = invoke(index.toULong())
inline operator fun $PtrType.invoke(): $Type = get(0uL)
inline infix operator fun $PtrType.invoke(index: ULong): $Type = get(index)
@JvmName("plus${Type}Int")
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = Ptr(address + offset.toULong())
@JvmName("plus${Type}UInt")
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = Ptr(address + offset.toULong())
@JvmName("plus${Type}Long")
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = Ptr(address + offset.toULong())
@JvmName("plus${Type}ULong")
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset)
@JvmName("minus${Type}Int")
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = Ptr(address - offset.toULong())
@JvmName("minus${Type}UInt")
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = Ptr(address - offset.toULong())
@JvmName("minus${Type}Long")
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = Ptr(address - offset.toULong())
@JvmName("minus${Type}ULong")
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset)
@JvmName("inc$Type")
inline operator fun $PtrType.inc(): $PtrType = Ptr(address + $Type.BYTES.toUInt())
@JvmName("dec$Type")
inline operator fun $PtrType.dec(): $PtrType = Ptr(address - $Type.BYTES.toUInt())"""
+"""
inline operator fun $PtrType.set(index: Int, $type: $Type) = set(index.toULong(), $type)
inline operator fun $PtrType.set(index: UInt, $type: $Type) = set(index.toULong(), $type)
Expand Down
15 changes: 7 additions & 8 deletions buildSrc/src/main/kotlin/kool/stack.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kool

import kool.gen.Generator
import kool.gen.generate
import org.gradle.configurationcache.extensions.capitalized
import java.io.File

fun stack(target: File) {
Expand Down Expand Up @@ -64,15 +65,13 @@ fun stack(target: File) {
}

val maybeAsUns = if (unsigned) ".as$TypeBuffer()" else ""
val maybeU = if (unsigned) "U" else ""
val mallocType = when (type) {
"Byte", "UByte" -> ""
else -> if (unsigned) type.drop(1) else type
}
val invoke = if (unsigned) "invoke$type" else "invoke"

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

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

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

for (enc in listOf("ascii", "utf8", "utf16")) {
val ENC = enc.toUpperCase()
val Enc = enc.capitalize()
val ENC = enc.uppercase()
val Enc = enc.capitalized()
+"""
inline fun <R> write${Enc}ToAdr(chars: CharSequence, nullTerminated: Boolean = true, block: (Adr) -> R): R =
with {
Expand Down
15 changes: 7 additions & 8 deletions buildSrc/src/main/kotlin/kool/stackExts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kool

import kool.gen.Generator
import kool.gen.generate
import org.gradle.configurationcache.extensions.capitalized
import java.io.File

fun stackExts(target: File) {
Expand All @@ -27,7 +28,7 @@ fun stackExts(target: File) {
val `Ptr Type` = "Ptr<$type>"
val PtrType = "Ptr$type"
val typeBuffer = type + "Buffer"
val ts = type.toLowerCase() + 's'
val ts = type.lowercase() + 's'

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

val invoke = if (unsigned) "invoke$Type" else "invoke"

+"""
inline fun <R> MemoryStack.read${Type}FromAdr(block: (Adr) -> R): $Type = Ptr<$Type>().apply { block(adr) }.$invoke()
inline fun <R> MemoryStack.read${Type}FromAdr(block: (Adr) -> R): $Type = Ptr<$Type>().apply { block(adr) }.invoke()
inline fun <R> MemoryStack.read${Type}FromBuf(block: (${Type}Buffer) -> R): $Type = ${Type}Buffer(1).also { block(it) }[0]
"""
}
Expand All @@ -125,8 +124,8 @@ fun stackExts(target: File) {
}"""

for (enc in listOf("ascii", "utf8", "utf16")) {
val ENC = enc.toUpperCase()
val Enc = enc.capitalize()
val ENC = enc.uppercase()
val Enc = enc.capitalized()
+"""
/** It mallocs, passes the address and reads the null terminated string */
inline fun <R> MemoryStack.read${Enc}FromAdr(maxSize: Int, block: (Adr) -> R): String {
Expand All @@ -148,8 +147,8 @@ fun stackExts(target: File) {
// inline fun MemoryStack.pointerAdr(ptr: Ptr): Adr = ptrOf(ptr).adr
// inline fun MemoryStack.pointerBuffer(ptr: Ptr): ByteBuffer = ${type}s($type)"""
for (enc in listOf("ascii", "utf8", "utf16")) {
val ENC = enc.toUpperCase()
val Enc = enc.capitalize()
val ENC = enc.uppercase()
val Enc = enc.capitalized()
+"""
fun MemoryStack.write${Enc}ToAdr(chars: CharSequence, nullTerminated: Boolean = true): Adr = n$ENC(chars, nullTerminated).let { pointerAddress }.toULong()
fun MemoryStack.write${Enc}ToBuffer(chars: CharSequence, nullTerminated: Boolean = true): ByteBuffer = $ENC(chars, nullTerminated)"""
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/kool/unsafe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun unsafe(target: File) {
}?.run {
isAccessible = true
get(null) as Unsafe
} ?: throw UnsupportedOperationException("LWJGL requires sun.misc.Unsafe to be available.")
} ?: throw UnsupportedOperationException("Kool requires sun.misc.Unsafe to be available.")
}
"""

Expand Down
15 changes: 14 additions & 1 deletion buildSrc/src/main/kotlin/kool/utils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
package kool

import java.util.*

val jvmInline = "@JvmInline"
val overloadResolutionByLambdaReturnType = "@OverloadResolutionByLambdaReturnType"
fun jvmName(name: String) = "@kotlin.jvm.JvmName(\"$name\")"
fun jvmName(name: String) = "@kotlin.jvm.JvmName(\"$name\")"

fun CharSequence.decapitalized(): String =
when {
isEmpty() -> ""
else -> get(0).let { initial ->
when {
initial.isUpperCase() -> initial.lowercase(Locale.getDefault()) + substring(1)
else -> toString()
}
}
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pluginManagement {

gradle.rootProject {
group = "kotlin.graphics"
version = "0.9.75"
version = "0.9.76"
}

//includeBuild("../build-logic")
13 changes: 7 additions & 6 deletions src/main/kotlin/kool/Ptr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ package kool
import org.lwjgl.system.MemoryUtil

@JvmInline
value class Ptr<out T> constructor(val address: Address = NULL.address) {
value class Ptr<out T>(val address: Address = NULL.address) : Comparable<Ptr<@UnsafeVariance T>>{
inline val adr: Adr
get() = address
inline val isValid: Boolean
get() = adr != 0uL
inline val isNotValid: Boolean
get() = adr == 0uL

inline operator fun plus(offset: Int): Ptr<T> = plus(offset.toULong())
inline operator fun plus(offset: Long): Ptr<T> = plus(offset.toULong())
inline operator fun plus(offset: ULong): Ptr<T> = Ptr(address + offset)

inline fun <T> toPtr(): Ptr<T> = Ptr(adr)

override fun compareTo(other: Ptr<@UnsafeVariance T>): Int = address.compareTo(other.address)
companion object {
val NULL: Ptr<Nothing>
get() = Ptr(MemoryUtil.NULL.toULong())
Expand All @@ -27,4 +25,7 @@ inline fun <T> Ptr(long: Long): Ptr<T> = Ptr(long.toULong())

typealias Pointer = ULong
typealias Adr = ULong
typealias Address = ULong
typealias Address = ULong

//inline operator fun Ptr<*>.plus(offset: Long): Ptr<*> = plus(offset.toULong())
//inline operator fun <T> Ptr<T>.plus(offset: ULong): Ptr<T> = Ptr(address + offset)
1 change: 1 addition & 0 deletions src/main/kotlin/kool/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.lwjgl.system.MathUtil
import org.lwjgl.system.MemoryUtil
import org.lwjgl.system.Pointer
import java.nio.*
import java.util.*
import kotlin.reflect.*

inline val Pointer.adr: Adr
Expand Down
Loading

0 comments on commit fd5e198

Please sign in to comment.