-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ffi: move type conversion to FfiType
- Loading branch information
Showing
7 changed files
with
158 additions
and
83 deletions.
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 |
---|---|---|
@@ -1,7 +1,27 @@ | ||
package gay.pizza.pork.ffi | ||
|
||
import com.kenai.jffi.MemoryIO | ||
|
||
data class FfiAddress(val location: Long) { | ||
companion object { | ||
val Null = FfiAddress(0L) | ||
|
||
fun allocate(size: Long): FfiAddress = | ||
FfiAddress(MemoryIO.getInstance().allocateMemory(size, true)) | ||
} | ||
|
||
fun read(size: Int): ByteArray = read(0, size) | ||
|
||
fun read(offset: Int, size: Int): ByteArray { | ||
val bytes = ByteArray(size) { 0 } | ||
MemoryIO.getInstance().getByteArray(location, bytes, offset, size) | ||
return bytes | ||
} | ||
|
||
fun readNullTerminated(): ByteArray = | ||
MemoryIO.getInstance().getZeroTerminatedByteArray(location) | ||
|
||
fun readString(): String = String(readNullTerminated()) | ||
|
||
fun free(): Unit = MemoryIO.getInstance().freeMemory(location) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package gay.pizza.pork.ffi | ||
|
||
import com.kenai.jffi.MemoryIO | ||
|
||
class FfiString(val address: FfiAddress) { | ||
fun read(): String = address.readString() | ||
|
||
fun free() { | ||
address.free() | ||
} | ||
|
||
companion object { | ||
fun allocate(input: String): FfiString { | ||
val bytes = input.toByteArray() | ||
val buffer = FfiAddress.allocate(bytes.size + 1L) | ||
MemoryIO.getInstance().putZeroTerminatedByteArray(buffer.location, bytes, 0, bytes.size) | ||
return FfiString(buffer) | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
ffi/src/main/kotlin/gay/pizza/pork/ffi/FfiStringWrapper.kt
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,14 +1,43 @@ | ||
package gay.pizza.pork.ffi | ||
|
||
import com.kenai.jffi.InvocationBuffer | ||
import gay.pizza.pork.evaluator.None | ||
import java.util.* | ||
|
||
class FfiStruct : FfiType { | ||
private val fields = mutableListOf<FfiStructField>() | ||
private val fields = TreeMap<String, FfiStructField>() | ||
|
||
data class FfiStructField(val name: String, val type: FfiType) | ||
|
||
fun add(field: String, type: FfiType) { | ||
fields.add(FfiStructField(field, type)) | ||
fields[field] = FfiStructField(field, type) | ||
} | ||
|
||
override val size: Long | ||
get() = fields.sumOf { it.type.size } | ||
get() = fields.values.sumOf { it.type.size } | ||
|
||
override fun put(buffer: InvocationBuffer, value: Any?) { | ||
when (value) { | ||
is Map<*, *> -> { | ||
for (field in fields.values) { | ||
val item = value[field.name] ?: None | ||
field.type.put(buffer, item) | ||
} | ||
} | ||
|
||
is List<*> -> { | ||
for ((index, field) in fields.values.withIndex()) { | ||
field.type.put(buffer, value[index]) | ||
} | ||
} | ||
|
||
else -> { | ||
throw RuntimeException("Unknown value type: $value") | ||
} | ||
} | ||
} | ||
|
||
override fun value(ffi: Any?): Any { | ||
return None | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package gay.pizza.pork.ffi | ||
|
||
import com.kenai.jffi.InvocationBuffer | ||
|
||
interface FfiType { | ||
val size: Long | ||
|
||
fun put(buffer: InvocationBuffer, value: Any?) | ||
fun value(ffi: Any?): Any | ||
} |