Skip to content

Commit a3c1fda

Browse files
committed
v0.9.77
1 parent c32a865 commit a3c1fda

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,25 @@ fun pointers(target: File) {
9797
@JvmName("invokeULong")
9898
inline infix operator fun $PtrType.invoke(index: ULong): $Type = get(index)
9999
@JvmName("plus${Type}Int")
100-
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = Ptr(address + offset.toULong())
100+
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = plus(offset.toULong())
101101
@JvmName("plus${Type}UInt")
102-
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = Ptr(address + offset.toULong())
102+
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = plus(offset.toULong())
103103
@JvmName("plus${Type}Long")
104-
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = Ptr(address + offset.toULong())
104+
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = plus(offset.toULong())
105105
@JvmName("plus${Type}ULong")
106-
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset)
106+
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset * $Type.BYTES.toULong())
107107
@JvmName("minus${Type}Int")
108-
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = Ptr(address - offset.toULong())
108+
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = minus(offset.toULong())
109109
@JvmName("minus${Type}UInt")
110-
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = Ptr(address - offset.toULong())
110+
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = minus(offset.toULong())
111111
@JvmName("minus${Type}Long")
112-
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = Ptr(address - offset.toULong())
112+
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = minus(offset.toULong())
113113
@JvmName("minus${Type}ULong")
114-
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset)
114+
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset * $Type.BYTES.toULong())
115115
@JvmName("inc$Type")
116-
inline operator fun $PtrType.inc(): $PtrType = Ptr(address + $Type.BYTES.toUInt())
116+
inline operator fun $PtrType.inc(): $PtrType = plus(1)
117117
@JvmName("dec$Type")
118-
inline operator fun $PtrType.dec(): $PtrType = Ptr(address - $Type.BYTES.toUInt())"""
118+
inline operator fun $PtrType.dec(): $PtrType = minus(1)"""
119119
else
120120
+"""
121121
inline infix operator fun $PtrType.get(index: Int): $Type = get(index.toULong())
@@ -129,33 +129,30 @@ fun pointers(target: File) {
129129
inline operator fun $PtrType.invoke(): $Type = get(0uL)
130130
inline infix operator fun $PtrType.invoke(index: ULong): $Type = get(index)
131131
@JvmName("plus${Type}Int")
132-
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = Ptr(address + offset.toULong())
132+
inline infix operator fun $PtrType.plus(offset: Int): $PtrType = plus(offset.toULong())
133133
@JvmName("plus${Type}UInt")
134-
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = Ptr(address + offset.toULong())
134+
inline infix operator fun $PtrType.plus(offset: UInt): $PtrType = plus(offset.toULong())
135135
@JvmName("plus${Type}Long")
136-
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = Ptr(address + offset.toULong())
136+
inline infix operator fun $PtrType.plus(offset: Long): $PtrType = plus(offset.toULong())
137137
@JvmName("plus${Type}ULong")
138-
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset)
138+
inline infix operator fun $PtrType.plus(offset: ULong): $PtrType = Ptr(address + offset * $Type.BYTES.toULong())
139139
@JvmName("minus${Type}Int")
140-
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = Ptr(address - offset.toULong())
140+
inline infix operator fun $PtrType.minus(offset: Int): $PtrType = minus(offset.toULong())
141141
@JvmName("minus${Type}UInt")
142-
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = Ptr(address - offset.toULong())
142+
inline infix operator fun $PtrType.minus(offset: UInt): $PtrType = minus(offset.toULong())
143143
@JvmName("minus${Type}Long")
144-
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = Ptr(address - offset.toULong())
144+
inline infix operator fun $PtrType.minus(offset: Long): $PtrType = minus(offset.toULong())
145145
@JvmName("minus${Type}ULong")
146-
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset)
146+
inline infix operator fun $PtrType.minus(offset: ULong): $PtrType = Ptr(address - offset * $Type.BYTES.toUInt())
147147
@JvmName("inc$Type")
148-
inline operator fun $PtrType.inc(): $PtrType = Ptr(address + $Type.BYTES.toUInt())
148+
inline operator fun $PtrType.inc(): $PtrType = plus(1)
149149
@JvmName("dec$Type")
150-
inline operator fun $PtrType.dec(): $PtrType = Ptr(address - $Type.BYTES.toUInt())"""
150+
inline operator fun $PtrType.dec(): $PtrType = minus(1)"""
151151
+"""
152152
inline operator fun $PtrType.set(index: Int, $type: $Type) = set(index.toULong(), $type)
153153
inline operator fun $PtrType.set(index: UInt, $type: $Type) = set(index.toULong(), $type)
154154
inline operator fun $PtrType.set(index: Long, $type: $Type) = set(index.toULong(), $type)
155-
inline operator fun $PtrType.set(index: ULong, $type: $Type) = unsafe.set(adr + index$maybeTimes, $type$maybeToS)
156-
157-
// inline operator fun plus(pByte: BytePtr): BytePtr = BytePtr(adr + pByte.adr)
158-
"""
155+
inline operator fun $PtrType.set(index: ULong, $type: $Type) = unsafe.set(adr + index$maybeTimes, $type$maybeToS)"""
159156
}
160157

161158
if (Type == "Pointer") {

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.76"
13+
version = "0.9.77"
1414
}
1515

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

src/test/kotlin/kool/PointersTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlin.test.Test
55
class PointersTest {
66

77
val count = 100
8+
val lastIndex = count - 1
89
val range = 0..99
910
@Test
1011
fun testByte() = stack {
@@ -13,6 +14,10 @@ class PointersTest {
1314
p[i] = i.toByte()
1415
for (i in range)
1516
assert(p[i] == i.toByte())
17+
for (i in range)
18+
assert((p + i)[0] == i.toByte())
19+
for (i in range)
20+
assert((p + lastIndex - i)[0] == (lastIndex - i).toByte())
1621
for (i in range)
1722
assert(p++[0] == i.toByte())
1823
p--
@@ -26,6 +31,10 @@ class PointersTest {
2631
p[i] = i.toShort()
2732
for (i in range)
2833
assert(p[i] == i.toShort())
34+
for (i in range)
35+
assert((p + i)[0] == i.toShort())
36+
for (i in range)
37+
assert((p + lastIndex - i)[0] == (lastIndex - i).toShort())
2938
for (i in range)
3039
assert(p++[0] == i.toShort())
3140
p--
@@ -39,6 +48,10 @@ class PointersTest {
3948
p[i] = i
4049
for (i in range)
4150
assert(p[i] == i)
51+
for (i in range)
52+
assert((p + i)[0] == i)
53+
for (i in range)
54+
assert((p + lastIndex - i)[0] == lastIndex - i)
4255
for (i in range)
4356
assert(p++[0] == i)
4457
p--
@@ -52,6 +65,10 @@ class PointersTest {
5265
p[i] = i.toLong()
5366
for (i in range)
5467
assert(p[i] == i.toLong())
68+
for (i in range)
69+
assert((p + i)[0] == i.toLong())
70+
for (i in range)
71+
assert((p + lastIndex - i)[0] == (lastIndex - i).toLong())
5572
for (i in range)
5673
assert(p++[0] == i.toLong())
5774
p--
@@ -65,6 +82,10 @@ class PointersTest {
6582
p[i] = i.toUByte()
6683
for (i in range)
6784
assert(p[i] == i.toUByte())
85+
for (i in range)
86+
assert((p + i)[0] == i.toUByte())
87+
for (i in range)
88+
assert((p + lastIndex - i)[0] == (lastIndex - i).toUByte())
6889
for (i in range)
6990
assert(p++[0] == i.toUByte())
7091
p--
@@ -78,6 +99,10 @@ class PointersTest {
7899
p[i] = i.toUShort()
79100
for (i in range)
80101
assert(p[i] == i.toUShort())
102+
for (i in range)
103+
assert((p + i)[0] == i.toUShort())
104+
for (i in range)
105+
assert((p + lastIndex - i)[0] == (lastIndex - i).toUShort())
81106
for (i in range)
82107
assert(p++[0] == i.toUShort())
83108
p--
@@ -91,6 +116,10 @@ class PointersTest {
91116
p[i] = i.toUInt()
92117
for (i in range)
93118
assert(p[i] == i.toUInt())
119+
for (i in range)
120+
assert((p + i)[0] == i.toUInt())
121+
for (i in range)
122+
assert((p + lastIndex - i)[0] == (lastIndex - i).toUInt())
94123
for (i in range)
95124
assert(p++[0] == i.toUInt())
96125
p--
@@ -104,6 +133,10 @@ class PointersTest {
104133
p[i] = i.toULong()
105134
for (i in range)
106135
assert(p[i] == i.toULong())
136+
for (i in range)
137+
assert((p + i)[0] == i.toULong())
138+
for (i in range)
139+
assert((p + lastIndex - i)[0] == (lastIndex - i).toULong())
107140
for (i in range)
108141
assert(p++[0] == i.toULong())
109142
p--

0 commit comments

Comments
 (0)