Use unsigned types in interop (#1913)
This commit is contained in:
committed by
GitHub
parent
29cddb46bf
commit
8f1b94f38b
@@ -4,7 +4,7 @@ fun main(args: Array<String>) {
|
||||
val values = intArrayOf(14, 12, 9, 13, 8)
|
||||
val count = values.size
|
||||
|
||||
cstdlib.qsort(values.refTo(0), count.toLong(), IntVar.size, staticCFunction { a, b ->
|
||||
cstdlib.qsort(values.refTo(0), count.toULong(), IntVar.size.convert(), staticCFunction { a, b ->
|
||||
val aValue = a!!.reinterpret<IntVar>()[0]
|
||||
val bValue = b!!.reinterpret<IntVar>()[0]
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ fun assertEquals(value1: Any?, value2: Any?) {
|
||||
throw AssertionError("Expected $value1, got $value2")
|
||||
}
|
||||
|
||||
fun check(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
fun check(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long) {
|
||||
assertEquals(x1, s.x1)
|
||||
assertEquals(x1, getX1(s.ptr))
|
||||
|
||||
@@ -26,7 +26,7 @@ fun check(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
assertEquals(x6, getX6(s.ptr))
|
||||
}
|
||||
|
||||
fun assign(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
fun assign(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long) {
|
||||
s.x1 = x1
|
||||
s.x2 = x2
|
||||
s.x3 = x3
|
||||
@@ -35,7 +35,7 @@ fun assign(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
s.x6 = x6
|
||||
}
|
||||
|
||||
fun assignReversed(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
fun assignReversed(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long) {
|
||||
s.x6 = x6
|
||||
s.x5 = x5
|
||||
s.x4 = x4
|
||||
@@ -44,7 +44,7 @@ fun assignReversed(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long
|
||||
s.x1 = x1
|
||||
}
|
||||
|
||||
fun test(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
fun test(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long) {
|
||||
assign(s, x1, x2, x3, x4, x5, x6)
|
||||
check(s, x1, x2, x3, x4, x5, x6)
|
||||
|
||||
@@ -53,10 +53,10 @@ fun test(s: S, x1: Long, x2: B2, x3: Short, x4: Int, x5: Int, x6: Long) {
|
||||
|
||||
// Also check with some insignificant bits modified:
|
||||
|
||||
assign(s, x1 + 2, x2, (x3 + 8).toShort(), x4 - 16, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
assign(s, x1 + 2, x2, (x3 + 8u).toUShort(), x4 - 16u, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
check(s, x1, x2, x3, x4, x5, x6)
|
||||
|
||||
assignReversed(s, x1 + 2, x2, (x3 + 8).toShort(), x4 - 16, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
assignReversed(s, x1 + 2, x2, (x3 + 8u).toUShort(), x4 - 16u, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
check(s, x1, x2, x3, x4, x5, x6)
|
||||
}
|
||||
|
||||
@@ -66,9 +66,9 @@ fun main(args: Array<String>) {
|
||||
for (x1 in -1L..0L)
|
||||
for (x2 in B2.values())
|
||||
for (x3 in 0..7)
|
||||
for (x4 in intArrayOf(0, 6, 15))
|
||||
for (x4 in uintArrayOf(0u, 6u, 15u))
|
||||
for (x5 in intArrayOf(-16, -2, -1, 0, 5, 15))
|
||||
for (x6 in longArrayOf(Long.MIN_VALUE/2, -1L shl 36, -325L, 0, 1L shl 48, Long.MAX_VALUE/2))
|
||||
test(s, x1, x2, x3.toShort(), x4, x5, x6)
|
||||
test(s, x1, x2, x3.toUShort(), x4, x5, x6)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,4 +51,12 @@ typedef _Bool (*isIntPositivePtrType)(int);
|
||||
|
||||
static isIntPositivePtrType getIsIntPositivePtr() {
|
||||
return &__isIntPositive;
|
||||
}
|
||||
|
||||
static unsigned int getMaxUInt(void) {
|
||||
return 0xffffffff;
|
||||
}
|
||||
|
||||
static typeof(&getMaxUInt) getMaxUIntGetter() {
|
||||
return &getMaxUInt;
|
||||
}
|
||||
@@ -7,7 +7,7 @@ fun main(args: Array<String>) {
|
||||
return
|
||||
}
|
||||
|
||||
val port = atoi(args[0]).toShort()
|
||||
val port = atoi(args[0]).toUShort()
|
||||
|
||||
memScoped {
|
||||
|
||||
@@ -19,13 +19,13 @@ fun main(args: Array<String>) {
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
|
||||
with(serverAddr) {
|
||||
memset(this.ptr, 0, sockaddr_in.size)
|
||||
sin_family = AF_INET.narrow()
|
||||
sin_addr.s_addr = htons(0).toInt()
|
||||
memset(this.ptr, 0, sockaddr_in.size.convert())
|
||||
sin_family = AF_INET.convert()
|
||||
sin_addr.s_addr = htons(0u).convert()
|
||||
sin_port = htons(port)
|
||||
}
|
||||
|
||||
bind(listenFd, serverAddr.ptr.reinterpret(), sockaddr_in.size.toInt())
|
||||
bind(listenFd, serverAddr.ptr.reinterpret(), sockaddr_in.size.toUInt())
|
||||
.ensureUnixCallResult { it == 0 }
|
||||
|
||||
listen(listenFd, 10)
|
||||
@@ -35,21 +35,21 @@ fun main(args: Array<String>) {
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
|
||||
while (true) {
|
||||
val length = read(commFd, buffer, bufferLength)
|
||||
val length = read(commFd, buffer, bufferLength.convert())
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
|
||||
if (length == 0L) {
|
||||
break
|
||||
}
|
||||
|
||||
write(commFd, buffer, length)
|
||||
write(commFd, buffer, length.convert())
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Not available through interop because declared as macro:
|
||||
fun htons(value: Short) = ((value.toInt() ushr 8) or (value.toInt() shl 8)).toShort()
|
||||
fun htons(value: UShort) = ((value.toInt() ushr 8) or (value.toInt() shl 8)).toUShort()
|
||||
|
||||
fun throwUnixError(): Nothing {
|
||||
perror(null) // TODO: store error message to exception instead.
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import kotlinx.cinterop.*
|
||||
import cfunptr.*
|
||||
import kotlin.test.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val atoiPtr = getAtoiPtr()!!
|
||||
@@ -23,6 +24,8 @@ fun main(args: Array<String>) {
|
||||
|
||||
printIntPtr(isIntPositivePtr(42).ifThenOneElseZero())
|
||||
printIntPtr(isIntPositivePtr(-42).ifThenOneElseZero())
|
||||
|
||||
assertEquals(getMaxUIntGetter()!!(), UInt.MAX_VALUE)
|
||||
}
|
||||
|
||||
fun Boolean.ifThenOneElseZero() = if (this) 1 else 0
|
||||
@@ -13,7 +13,7 @@ private val windowHeight = 480
|
||||
|
||||
fun display() {
|
||||
// Clear Screen and Depth Buffer
|
||||
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
|
||||
glClear((GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT).convert())
|
||||
glLoadIdentity()
|
||||
|
||||
// Define a viewing transformation
|
||||
@@ -40,13 +40,13 @@ fun display() {
|
||||
|
||||
fun initialize() {
|
||||
// select projection matrix
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glMatrixMode(GL_PROJECTION.convert())
|
||||
|
||||
// set the viewport
|
||||
glViewport(0, 0, windowWidth, windowHeight)
|
||||
|
||||
// set matrix mode
|
||||
glMatrixMode(GL_PROJECTION)
|
||||
glMatrixMode(GL_PROJECTION.convert())
|
||||
|
||||
// reset projection matrix
|
||||
glLoadIdentity()
|
||||
@@ -56,29 +56,29 @@ fun initialize() {
|
||||
gluPerspective(45.0, aspect, 1.0, 500.0)
|
||||
|
||||
// specify which matrix is the current matrix
|
||||
glMatrixMode(GL_MODELVIEW)
|
||||
glShadeModel(GL_SMOOTH)
|
||||
glMatrixMode(GL_MODELVIEW.convert())
|
||||
glShadeModel(GL_SMOOTH.convert())
|
||||
|
||||
// specify the clear value for the depth buffer
|
||||
glClearDepth(1.0)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
glDepthFunc(GL_LEQUAL)
|
||||
glEnable(GL_DEPTH_TEST.convert())
|
||||
glDepthFunc(GL_LEQUAL.convert())
|
||||
|
||||
// specify implementation-specific hints
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT.convert(), GL_NICEST.convert())
|
||||
|
||||
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, cValuesOf(0.1f, 0.1f, 0.1f, 1.0f))
|
||||
glLightfv(GL_LIGHT0, GL_DIFFUSE, cValuesOf(0.6f, 0.6f, 0.6f, 1.0f))
|
||||
glLightfv(GL_LIGHT0, GL_SPECULAR, cValuesOf(0.7f, 0.7f, 0.3f, 1.0f))
|
||||
glLightModelfv(GL_LIGHT_MODEL_AMBIENT.convert(), cValuesOf(0.1f, 0.1f, 0.1f, 1.0f))
|
||||
glLightfv(GL_LIGHT0.convert(), GL_DIFFUSE.convert(), cValuesOf(0.6f, 0.6f, 0.6f, 1.0f))
|
||||
glLightfv(GL_LIGHT0.convert(), GL_SPECULAR.convert(), cValuesOf(0.7f, 0.7f, 0.3f, 1.0f))
|
||||
|
||||
glEnable(GL_LIGHT0)
|
||||
glEnable(GL_COLOR_MATERIAL)
|
||||
glShadeModel(GL_SMOOTH)
|
||||
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE)
|
||||
glDepthFunc(GL_LEQUAL)
|
||||
glEnable(GL_DEPTH_TEST)
|
||||
glEnable(GL_LIGHTING)
|
||||
glEnable(GL_LIGHT0)
|
||||
glEnable(GL_LIGHT0.convert())
|
||||
glEnable(GL_COLOR_MATERIAL.convert())
|
||||
glShadeModel(GL_SMOOTH.convert())
|
||||
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE.convert(), GL_FALSE)
|
||||
glDepthFunc(GL_LEQUAL.convert())
|
||||
glEnable(GL_DEPTH_TEST.convert())
|
||||
glEnable(GL_LIGHTING.convert())
|
||||
glEnable(GL_LIGHT0.convert())
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f)
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
|
||||
// Display Mode
|
||||
glutInitDisplayMode(GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH)
|
||||
glutInitDisplayMode((GLUT_RGB or GLUT_DOUBLE or GLUT_DEPTH).convert())
|
||||
|
||||
// Set window size
|
||||
glutInitWindowSize(windowWidth, windowHeight)
|
||||
|
||||
@@ -22,8 +22,8 @@ fun main(args: Array<String>) {
|
||||
val destPtr = alloc<CArrayPointerVar<ByteVar>>()
|
||||
destPtr.value = destBytes
|
||||
|
||||
sourceLength.value = sourceByteArray.size.signExtend();
|
||||
destLength.value = golden.size.signExtend();
|
||||
sourceLength.value = sourceByteArray.size.convert()
|
||||
destLength.value = golden.size.convert()
|
||||
|
||||
val conversion = iconv_open("UTF-8", "LATIN1")
|
||||
|
||||
|
||||
@@ -118,9 +118,9 @@ fun testTypeOps() {
|
||||
assertTrue(NSValue.asAny() is NSObjectProtocolMeta)
|
||||
assertFalse(NSValue.asAny() is NSObjectProtocol) // Must be true, but not implemented properly yet.
|
||||
|
||||
assertEquals(3, ("foo" as NSString).length())
|
||||
assertEquals(4, ((1..4).joinToString("") as NSString).length())
|
||||
assertEquals(2, (listOf(0, 1) as NSArray).count())
|
||||
assertEquals(3u, ("foo" as NSString).length())
|
||||
assertEquals(4u, ((1..4).joinToString("") as NSString).length())
|
||||
assertEquals(2u, (listOf(0, 1) as NSArray).count())
|
||||
assertEquals(42L, (42 as NSNumber).longLongValue())
|
||||
|
||||
assertFails { "bar" as NSNumber }
|
||||
|
||||
@@ -2,7 +2,7 @@ import kotlinx.cinterop.*
|
||||
|
||||
import platform.zlib.*
|
||||
|
||||
val source = immutableBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer()
|
||||
val source = immutableBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer().reinterpret<UByteVar>()
|
||||
val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer()
|
||||
|
||||
fun main(args: Array<String>) = memScoped {
|
||||
@@ -10,9 +10,9 @@ fun main(args: Array<String>) = memScoped {
|
||||
buffer.usePinned { pinned ->
|
||||
val z = alloc<z_stream>().apply {
|
||||
next_in = source
|
||||
avail_in = 8
|
||||
next_out = pinned.addressOf(0)
|
||||
avail_out = buffer.size
|
||||
avail_in = 8u
|
||||
next_out = pinned.addressOf(0).reinterpret()
|
||||
avail_out = buffer.size.toUInt()
|
||||
}.ptr
|
||||
|
||||
if (inflateInit2(z, -15) == Z_OK && inflate(z, Z_FINISH) == Z_STREAM_END && inflateEnd(z) == Z_OK)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import kotlinx.cinterop.signExtend
|
||||
import kotlinx.cinterop.convert
|
||||
import platform.posix.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// Just check the typealias is in scope.
|
||||
val sizet: size_t = 0.signExtend<size_t>()
|
||||
val sizet: size_t = 0.convert<size_t>()
|
||||
println("sizet = $sizet")
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import kotlinx.cinterop.signExtend
|
||||
import kotlinx.cinterop.convert
|
||||
import platform.posix.*
|
||||
|
||||
fun foo() {
|
||||
println("linked library")
|
||||
val size: size_t = 17.signExtend<size_t>()
|
||||
val size: size_t = 17.convert<size_t>()
|
||||
val e = fabs(1.toDouble())
|
||||
println("and symbols from posix available: $size; $e")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user