Reused C macroses by defining bridges to them in def file

This commit is contained in:
Igor Chevdar
2017-05-10 06:26:10 +05:00
parent ec34adc66c
commit 33189b079f
4 changed files with 43 additions and 21 deletions
+5 -12
View File
@@ -203,22 +203,15 @@ class IOException: RuntimeException {
}
val errno: Int
get() = __error()!!.pointed.value
get() = interop_errno()
fun FD_ZERO(set: fd_set) {
memset(set.fds_bits, 0, sizeOf<fd_set>())
}
fun FD_ZERO(set: fd_set): Unit = interop_FD_ZERO(set.ptr)
fun FD_SET(bit: Int, set: fd_set) {
set.fds_bits[bit / 32] = set.fds_bits[bit / 32] or (1 shl (bit % 32))
}
fun FD_SET(bit: Int, set: fd_set): Unit = interop_FD_SET(bit, set.ptr)
fun FD_ISSET(bit: Int, set: fd_set): Boolean {
return set.fds_bits[bit / 32] and (1 shl (bit % 32)) != 0
}
fun FD_ISSET(bit: Int, set: fd_set) = interop_FD_ISSET(bit, set.ptr) != 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: Short) = interop_htons(value.toInt()).toShort()
fun getUnixError() = strerror(errno)!!.toKString()
+22
View File
@@ -1,2 +1,24 @@
headers = sys/socket.h sys/errno.h sys/select.h fcntl.h netdb.h stdio.h string.h unistd.h stdlib.h netinet/in.h
excludeDependentModules.osx = true
---
static int interop_errno() {
return errno;
}
static int interop_htons(int x) {
return htons(x);
}
static void interop_FD_ZERO(fd_set *set) {
FD_ZERO(set);
}
static void interop_FD_SET(int bit, fd_set *set) {
FD_SET(bit, set);
}
static int interop_FD_ISSET(int bit, fd_set *set) {
return FD_ISSET(bit, set);
}
+5 -8
View File
@@ -64,24 +64,21 @@ fun main(args: Array<String>) {
}
}
// Not available through interop because declared as macro:
fun htons(value: Short) = ((value.toInt() ushr 8) or (value.toInt() shl 8)).toShort()
val errno: Int
get() = interop_errno()
fun throwUnixError(): Nothing {
perror(null) // TODO: store error message to exception instead.
throw Error("UNIX call failed")
}
fun htons(value: Short) = interop_htons(value.toInt()).toShort()
inline fun Int.ensureUnixCallResult(predicate: (Int) -> Boolean): Int {
if (!predicate(this)) {
throwUnixError()
throw Error(strerror(errno)!!.toKString())
}
return this
}
inline fun Long.ensureUnixCallResult(predicate: (Long) -> Boolean): Long {
if (!predicate(this)) {
throwUnixError()
throw Error(strerror(errno)!!.toKString())
}
return this
}
+11 -1
View File
@@ -1,2 +1,12 @@
headers = sys/socket.h netdb.h stdio.h string.h unistd.h stdlib.h netinet/in.h
headers = sys/socket.h sys/errno.h netdb.h stdio.h string.h unistd.h stdlib.h netinet/in.h
excludeDependentModules.osx = true
---
static int interop_errno() {
return errno;
}
static int interop_htons(int x) {
return htons(x);
}