Skip to content

Commit

Permalink
Fix WASI build issue (#142)
Browse files Browse the repository at this point in the history
* Fix WASI build issue

* Fix PropertyListEncoder and BitwiseCopyable on WASI
  • Loading branch information
Kyle-Ye authored Oct 9, 2024
1 parent cc6524d commit fbf68cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions Sources/OpenSwiftUICore/Data/Protobuf/ProtobufEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,14 @@ extension ProtobufEncoder {
}

func binaryPlistData<T>(for value: T) throws -> Data where T: Encodable {
#if os(WASI)
fatalError("PropertyListEncoder is not avaiable on WASI")
#else
let encoder = PropertyListEncoder()
encoder.outputFormat = .binary
encoder.userInfo = userInfo
return try encoder.encode([value])
#endif
}

@inline(__always)
Expand Down Expand Up @@ -467,6 +471,7 @@ extension ProtobufEncoder {
encodeVarintZZ(Int(value))
}

#if compiler(>=6.0)
@inline(__always)
private mutating func encodeBitwiseCopyable<T>(_ value: T) where T: BitwiseCopyable {
let oldSize = size
Expand All @@ -478,6 +483,19 @@ extension ProtobufEncoder {
buffer.advanced(by: oldSize).storeBytes(of: value, as: T.self)
}
}
#else // FIXME: Remove this after we drop WASI 5.10 support
@inline(__always)
private mutating func encodeBitwiseCopyable<T>(_ value: T) {
let oldSize = size
let newSize = oldSize + MemoryLayout<T>.size
if capacity < newSize {
growBufferSlow(to: newSize).storeBytes(of: value, as: T.self)
} else {
size = newSize
buffer.advanced(by: oldSize).storeBytes(of: value, as: T.self)
}
}
#endif

package mutating func encodeBool(_ value: Bool) {
encodeBitwiseCopyable(value)
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUICore/Util/UnsafePointer+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension UnsafePointer {

@_transparent
package static var null: UnsafePointer<Pointee> {
UnsafePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ff00) | (-MemoryLayout<Pointee>.alignment))!
UnsafePointer(bitPattern: Int(bitPattern: UInt.max - 0xff) | (-MemoryLayout<Pointee>.alignment))!
}
}

Expand All @@ -29,7 +29,7 @@ extension UnsafeMutablePointer {

@_transparent
package static var null: UnsafeMutablePointer<Pointee> {
UnsafeMutablePointer(bitPattern: Int(bitPattern: 0xffff_ffff_ffff_ff00) | (-MemoryLayout<Pointee>.alignment))!
UnsafeMutablePointer(bitPattern: Int(bitPattern: UInt.max - 0xff) | (-MemoryLayout<Pointee>.alignment))!
}
}

Expand Down

0 comments on commit fbf68cc

Please sign in to comment.