Cached API tables and cut allocations in the PlayDate bindings target.

This commit is contained in:
2026-07-25 10:43:32 +02:00
parent 6594794cec
commit dfeadabfb6
20 changed files with 272 additions and 89 deletions
+6 -2
View File
@@ -8,7 +8,7 @@
internal import CPlaydate
private var fileAPI: UnsafePointer<playdate_file> { Playdate.fileAPI }
private var fileAPI: UnsafePointer<playdate_file> { Playdate.fileAPI.unsafelyUnwrapped }
/// The most recent file system error as a thrown error.
private func lastFileError() -> PlaydateError {
@@ -184,7 +184,11 @@ extension File {
/// Writes the string's UTF-8 to the file. Returns the bytes written.
@discardableResult
public func write(_ string: String) throws(PlaydateError) -> Int {
try write(Array(string.utf8))
let result = string.withPlaydateUTF8 { bytes, count in
fileAPI.pointee.write.unsafelyUnwrapped(pointer, bytes, UInt32(count))
}
if result < 0 { throw lastFileError() }
return Int(result)
}
/// Flushes buffered writes to disk. Returns the bytes written.