This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 564
Use unsigned types in interop #1913
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e1716d
Use unsigned types in interop
SvyatoslavScherbina ad878c7
fixup! Use unsigned types in interop
SvyatoslavScherbina 19870bf
fixup! Use unsigned types in interop
SvyatoslavScherbina c5dd4ef
fixup! Use unsigned types in interop
SvyatoslavScherbina 7dc0ae5
fixup! Use unsigned types in interop
SvyatoslavScherbina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,28 +468,24 @@ Sometimes the C libraries have function parameters or struct fields of | |
platform-dependent type, e.g. `long` or `size_t`. Kotlin itself doesn't provide | ||
neither implicit integer casts nor C-style integer casts (e.g. | ||
`(size_t) intValue`), so to make writing portable code in such cases easier, | ||
the following methods are provided: | ||
`convert` method is provided: | ||
|
||
* `fun ${type1}.signExtend<${type2}>(): ${type2}` | ||
* `fun ${type1}.narrow<${type2}>(): ${type2}` | ||
|
||
where each of `type1` and `type2` must be an integral type. | ||
``` | ||
fun ${type1}.convert<${type2}>(): ${type2} | ||
``` | ||
|
||
The `signExtend` converts the integer value to more wide, i.e. the result must | ||
have the same or greater size. | ||
The `narrow` converts the integer value to smaller one (possibly changing the | ||
value due to loosing significant bits), so the result must have the same or | ||
less size. | ||
where each of `type1` and `type2` must be an integral type, either signed or unsigned. | ||
|
||
Any allowed `.signExtend<${type}>` or `.narrow<${type}>` have the same | ||
semantics as one of the `.toByte`, `.toShort`, `.toInt` or `.toLong` methods, | ||
depending on `type`. | ||
`.convert<${type}>` has the same semantics as one of the | ||
`.toByte`, `.toShort`, `.toInt`, `.toLong`, | ||
`.toUByte`, `.toUShort`, `.toUInt` or `.toULong` | ||
methods, depending on `type`. | ||
|
||
The example of using `signExtend`: | ||
The example of using `convert`: | ||
|
||
``` | ||
fun zeroMemory(buffer: COpaquePointer, size: Int) { | ||
memset(buffer, 0, size.signExtend<size_t>()) | ||
memset(buffer, 0, size.convert<size_t>()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the refinement below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can convert be made an extension property at least? |
||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,16 +26,20 @@ inline operator fun <T : ByteVarOf<*>> CPointer<T>?.plus(index: Long): CPointer< | |
inline operator fun <T : ByteVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$Byte") | ||
inline operator fun <T : Byte> CPointer<ByteVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Byte") | ||
inline operator fun <T : Byte> CPointer<ByteVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$Byte") | ||
inline operator fun <T : Byte> CPointer<ByteVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Byte") | ||
inline operator fun <T : Byte> CPointer<ByteVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
@@ -48,16 +52,20 @@ inline operator fun <T : ShortVarOf<*>> CPointer<T>?.plus(index: Long): CPointer | |
inline operator fun <T : ShortVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$Short") | ||
inline operator fun <T : Short> CPointer<ShortVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Short") | ||
inline operator fun <T : Short> CPointer<ShortVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$Short") | ||
inline operator fun <T : Short> CPointer<ShortVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Short") | ||
inline operator fun <T : Short> CPointer<ShortVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
@@ -70,16 +78,20 @@ inline operator fun <T : IntVarOf<*>> CPointer<T>?.plus(index: Long): CPointer<T | |
inline operator fun <T : IntVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$Int") | ||
inline operator fun <T : Int> CPointer<IntVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Int") | ||
inline operator fun <T : Int> CPointer<IntVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$Int") | ||
inline operator fun <T : Int> CPointer<IntVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Int") | ||
inline operator fun <T : Int> CPointer<IntVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
@@ -92,20 +104,128 @@ inline operator fun <T : LongVarOf<*>> CPointer<T>?.plus(index: Long): CPointer< | |
inline operator fun <T : LongVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$Long") | ||
inline operator fun <T : Long> CPointer<LongVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Long") | ||
inline operator fun <T : Long> CPointer<LongVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$Long") | ||
inline operator fun <T : Long> CPointer<LongVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Long") | ||
inline operator fun <T : Long> CPointer<LongVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("plus\$UByte") | ||
inline operator fun <T : UByteVarOf<*>> CPointer<T>?.plus(index: Long): CPointer<T>? = | ||
interpretCPointer(this.rawValue + index * 1) | ||
|
||
@JvmName("plus\$UByte") | ||
inline operator fun <T : UByteVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$UByte") | ||
inline operator fun <T : UByte> CPointer<UByteVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$UByte") | ||
inline operator fun <T : UByte> CPointer<UByteVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$UByte") | ||
inline operator fun <T : UByte> CPointer<UByteVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$UByte") | ||
inline operator fun <T : UByte> CPointer<UByteVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("plus\$UShort") | ||
inline operator fun <T : UShortVarOf<*>> CPointer<T>?.plus(index: Long): CPointer<T>? = | ||
interpretCPointer(this.rawValue + index * 2) | ||
|
||
@JvmName("plus\$UShort") | ||
inline operator fun <T : UShortVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$UShort") | ||
inline operator fun <T : UShort> CPointer<UShortVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$UShort") | ||
inline operator fun <T : UShort> CPointer<UShortVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$UShort") | ||
inline operator fun <T : UShort> CPointer<UShortVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$UShort") | ||
inline operator fun <T : UShort> CPointer<UShortVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("plus\$UInt") | ||
inline operator fun <T : UIntVarOf<*>> CPointer<T>?.plus(index: Long): CPointer<T>? = | ||
interpretCPointer(this.rawValue + index * 4) | ||
|
||
@JvmName("plus\$UInt") | ||
inline operator fun <T : UIntVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$UInt") | ||
inline operator fun <T : UInt> CPointer<UIntVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$UInt") | ||
inline operator fun <T : UInt> CPointer<UIntVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$UInt") | ||
inline operator fun <T : UInt> CPointer<UIntVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$UInt") | ||
inline operator fun <T : UInt> CPointer<UIntVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("plus\$ULong") | ||
inline operator fun <T : ULongVarOf<*>> CPointer<T>?.plus(index: Long): CPointer<T>? = | ||
interpretCPointer(this.rawValue + index * 8) | ||
|
||
@JvmName("plus\$ULong") | ||
inline operator fun <T : ULongVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$ULong") | ||
inline operator fun <T : ULong> CPointer<ULongVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$ULong") | ||
inline operator fun <T : ULong> CPointer<ULongVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$ULong") | ||
inline operator fun <T : ULong> CPointer<ULongVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$ULong") | ||
inline operator fun <T : ULong> CPointer<ULongVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("plus\$Float") | ||
inline operator fun <T : FloatVarOf<*>> CPointer<T>?.plus(index: Long): CPointer<T>? = | ||
interpretCPointer(this.rawValue + index * 4) | ||
|
@@ -114,16 +234,20 @@ inline operator fun <T : FloatVarOf<*>> CPointer<T>?.plus(index: Long): CPointer | |
inline operator fun <T : FloatVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$Float") | ||
inline operator fun <T : Float> CPointer<FloatVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Float") | ||
inline operator fun <T : Float> CPointer<FloatVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$Float") | ||
inline operator fun <T : Float> CPointer<FloatVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Float") | ||
inline operator fun <T : Float> CPointer<FloatVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
@@ -136,16 +260,20 @@ inline operator fun <T : DoubleVarOf<*>> CPointer<T>?.plus(index: Long): CPointe | |
inline operator fun <T : DoubleVarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? = | ||
this + index.toLong() | ||
|
||
@JvmName("get\$Double") | ||
inline operator fun <T : Double> CPointer<DoubleVarOf<T>>.get(index: Int): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Double") | ||
inline operator fun <T : Double> CPointer<DoubleVarOf<T>>.set(index: Int, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
|
||
@JvmName("get\$Double") | ||
inline operator fun <T : Double> CPointer<DoubleVarOf<T>>.get(index: Long): T = | ||
(this + index)!!.pointed.value | ||
|
||
@JvmName("set\$Double") | ||
inline operator fun <T : Double> CPointer<DoubleVarOf<T>>.set(index: Long, value: T) { | ||
(this + index)!!.pointed.value = value | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use Kotlin/Native proggy to generate definitions :)? |
||
|
@@ -163,16 +291,20 @@ echo "@JvmName(\"plus\\\$$1\")" | |
echo "inline operator fun <T : ${1}VarOf<*>> CPointer<T>?.plus(index: Int): CPointer<T>? =" | ||
echo " this + index.toLong()" | ||
echo | ||
echo "@JvmName(\"get\\\$$1\")" | ||
echo "inline operator fun <T : $1> CPointer<${1}VarOf<T>>.get(index: Int): T =" | ||
echo " (this + index)!!.pointed.value" | ||
echo | ||
echo "@JvmName(\"set\\\$$1\")" | ||
echo "inline operator fun <T : $1> CPointer<${1}VarOf<T>>.set(index: Int, value: T) {" | ||
echo " (this + index)!!.pointed.value = value" | ||
echo '}' | ||
echo | ||
echo "@JvmName(\"get\\\$$1\")" | ||
echo "inline operator fun <T : $1> CPointer<${1}VarOf<T>>.get(index: Long): T =" | ||
echo " (this + index)!!.pointed.value" | ||
echo | ||
echo "@JvmName(\"set\\\$$1\")" | ||
echo "inline operator fun <T : $1> CPointer<${1}VarOf<T>>.set(index: Long, value: T) {" | ||
echo " (this + index)!!.pointed.value = value" | ||
echo '}' | ||
|
@@ -183,6 +315,10 @@ gen Byte 1 | |
gen Short 2 | ||
gen Int 4 | ||
gen Long 8 | ||
gen UByte 1 | ||
gen UShort 2 | ||
gen UInt 4 | ||
gen ULong 8 | ||
gen Float 4 | ||
gen Double 8 | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
fit
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
fit
doesn't fit.