Merge commit '7621e2f8dec938cf48181c8b10afc9b01f444e68' into beta

This commit is contained in:
Ilya Laktyushin
2025-12-06 02:17:48 +04:00
commit 8344b97e03
28070 changed files with 7995182 additions and 0 deletions
@@ -0,0 +1,152 @@
import Foundation
public final class AdaptedPostboxDecoder {
enum ContentType {
case object
case int32Array
case int64Array
case objectArray
case stringArray
case dataArray
case objectDict
}
public final class RawObjectData: Decodable {
public let data: Data
public let typeHash: Int32
public init(data: Data, typeHash: Int32) {
self.data = data
self.typeHash = typeHash
}
public init(from decoder: Decoder) throws {
preconditionFailure()
}
}
public init() {
}
public func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
return try self.decode(type, from: data, contentType: .object)
}
func decode<T>(_ type: T.Type, from data: Data, contentType: ContentType) throws -> T where T : Decodable {
if type == AdaptedPostboxDecoder.RawObjectData.self {
if case .object = contentType {
return AdaptedPostboxDecoder.RawObjectData(data: data, typeHash: 0) as! T
} else {
preconditionFailure()
}
}
let decoder = _AdaptedPostboxDecoder(data: data, contentType: contentType)
return try T(from: decoder)
}
}
extension AdaptedPostboxDecoder.ContentType {
init?(valueType: ObjectDataValueType) {
switch valueType {
case .Int32:
return nil
case .Int64:
return nil
case .Bool:
return nil
case .Double:
return nil
case .String:
return nil
case .Object:
self = .object
case .Int32Array:
self = .int32Array
case .Int64Array:
self = .int64Array
case .ObjectArray:
self = .objectArray
case .ObjectDictionary:
self = .objectDict
case .Bytes:
return nil
case .Nil:
return nil
case .StringArray:
self = .stringArray
case .BytesArray:
self = .dataArray
}
}
}
final class _AdaptedPostboxDecoder {
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any] = [:]
var container: AdaptedPostboxDecodingContainer?
fileprivate let data: Data
fileprivate let contentType: AdaptedPostboxDecoder.ContentType
init(data: Data, contentType: AdaptedPostboxDecoder.ContentType) {
self.data = data
self.contentType = contentType
}
}
extension _AdaptedPostboxDecoder: Decoder {
fileprivate func assertCanCreateContainer() {
precondition(self.container == nil)
}
func container<Key>(keyedBy type: Key.Type) -> KeyedDecodingContainer<Key> where Key : CodingKey {
assertCanCreateContainer()
let container = KeyedContainer<Key>(data: self.data, codingPath: self.codingPath, userInfo: self.userInfo)
self.container = container
return KeyedDecodingContainer(container)
}
func unkeyedContainer() -> UnkeyedDecodingContainer {
assertCanCreateContainer()
let decoder = PostboxDecoder(buffer: MemoryBuffer(data: self.data))
var content: UnkeyedContainer.Content?
switch self.contentType {
case .object:
preconditionFailure()
case .int32Array:
content = .int32Array(decoder.decodeInt32ArrayRaw())
case .int64Array:
content = .int64Array(decoder.decodeInt64ArrayRaw())
case .objectArray:
content = .objectArray(decoder.decodeObjectDataArrayRaw())
case .stringArray:
content = .stringArray(decoder.decodeStringArrayRaw())
case .dataArray:
content = .dataArray(decoder.decodeBytesArrayRaw().map { $0.makeData() })
case .objectDict:
content = .objectDict(decoder.decodeObjectDataDictRaw())
}
if let content = content {
let container = UnkeyedContainer(data: self.data, codingPath: self.codingPath, userInfo: self.userInfo, content: content)
self.container = container
return container
} else {
preconditionFailure()
}
}
func singleValueContainer() -> SingleValueDecodingContainer {
preconditionFailure()
}
}
protocol AdaptedPostboxDecodingContainer: AnyObject {
}
@@ -0,0 +1,134 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class KeyedContainer<Key> where Key: CodingKey {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
let decoder: PostboxDecoder
init(data: Data, codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
self.decoder = PostboxDecoder(buffer: MemoryBuffer(data: data))
}
}
}
private func decodingErrorBreakpoint() {
#if DEBUG
print("Decoding error. Install a breakpoint at decodingErrorBreakpoint to debug.")
#endif
}
extension _AdaptedPostboxDecoder.KeyedContainer: KeyedDecodingContainerProtocol {
var allKeys: [Key] {
preconditionFailure()
}
func contains(_ key: Key) -> Bool {
return self.decoder.containsKey(key.stringValue)
}
func decodeNil(forKey key: Key) throws -> Bool {
return self.decoder.decodeNilForKey(key.stringValue)
}
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable {
if let (data, valueType) = self.decoder.decodeObjectDataForKey(key.stringValue) {
if type == AdaptedPostboxDecoder.RawObjectData.self {
if case let .Object(typeHash) = valueType {
return AdaptedPostboxDecoder.RawObjectData(data: data, typeHash: typeHash) as! T
} else {
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
if let mappedType = AdaptedPostboxDecoder.ContentType(valueType: valueType) {
return try AdaptedPostboxDecoder().decode(T.self, from: data, contentType: mappedType)
} else {
switch valueType {
case .Bytes:
guard let resultData = PostboxDecoder.parseDataRaw(data: data) else {
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
if let resultData = resultData as? T {
return resultData
} else {
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
default:
decodingErrorBreakpoint()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {
if let value = self.decoder.decodeOptionalInt32ForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {
if let value = self.decoder.decodeOptionalInt64ForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {
if let value = self.decoder.decodeOptionalBoolForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: String.Type, forKey key: Key) throws -> String {
if let value = self.decoder.decodeOptionalStringForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func decode(_ type: Double.Type, forKey key: Key) throws -> Double {
if let value = self.decoder.decodeOptionalDoubleForKey(key.stringValue) {
return value
} else {
decodingErrorBreakpoint()
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.codingPath + [key], debugDescription: ""))
}
}
func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {
preconditionFailure()
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func superDecoder() throws -> Decoder {
preconditionFailure()
}
func superDecoder(forKey key: Key) throws -> Decoder {
preconditionFailure()
}
}
extension _AdaptedPostboxDecoder.KeyedContainer: AdaptedPostboxDecodingContainer {}
@@ -0,0 +1,82 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class SingleValueContainer {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
}
}
extension _AdaptedPostboxDecoder.SingleValueContainer: SingleValueDecodingContainer {
func decodeNil() -> Bool {
preconditionFailure()
}
func decode(_ type: Bool.Type) throws -> Bool {
preconditionFailure()
}
func decode(_ type: String.Type) throws -> String {
preconditionFailure()
}
func decode(_ type: Double.Type) throws -> Double {
preconditionFailure()
}
func decode(_ type: Float.Type) throws -> Float {
preconditionFailure()
}
func decode(_ type: Int.Type) throws -> Int {
preconditionFailure()
}
func decode(_ type: Int8.Type) throws -> Int8 {
preconditionFailure()
}
func decode(_ type: Int16.Type) throws -> Int16 {
preconditionFailure()
}
func decode(_ type: Int32.Type) throws -> Int32 {
preconditionFailure()
}
func decode(_ type: Int64.Type) throws -> Int64 {
preconditionFailure()
}
func decode(_ type: UInt.Type) throws -> UInt {
preconditionFailure()
}
func decode(_ type: UInt8.Type) throws -> UInt8 {
preconditionFailure()
}
func decode(_ type: UInt16.Type) throws -> UInt16 {
preconditionFailure()
}
func decode(_ type: UInt32.Type) throws -> UInt32 {
preconditionFailure()
}
func decode(_ type: UInt64.Type) throws -> UInt64 {
preconditionFailure()
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
preconditionFailure()
}
}
extension _AdaptedPostboxDecoder.SingleValueContainer: AdaptedPostboxDecodingContainer {}
@@ -0,0 +1,161 @@
import Foundation
extension _AdaptedPostboxDecoder {
final class UnkeyedContainer {
enum Content {
case int32Array([Int32])
case int64Array([Int64])
case objectArray([Data])
case stringArray([String])
case dataArray([Data])
case objectDict([(Data, Data)])
var count: Int {
switch self {
case let .int32Array(array):
return array.count
case let .int64Array(array):
return array.count
case let .objectArray(array):
return array.count
case let .stringArray(array):
return array.count
case let .dataArray(array):
return array.count
case let .objectDict(dict):
return dict.count * 2
}
}
}
let codingPath: [CodingKey]
let userInfo: [CodingUserInfoKey: Any]
let content: Content
var count: Int? {
return self.content.count
}
var isAtEnd: Bool {
return self.currentIndex >= self.content.count
}
fileprivate var _currentIndex: Int = 0
var currentIndex: Int {
return self._currentIndex
}
init(data: Data, codingPath: [CodingKey], userInfo: [CodingUserInfoKey: Any], content: Content) {
self.codingPath = codingPath
self.userInfo = userInfo
self.content = content
}
}
}
extension _AdaptedPostboxDecoder.UnkeyedContainer: UnkeyedDecodingContainer {
func decodeNil() throws -> Bool {
preconditionFailure()
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
if type == Data.self {
switch self.content {
case let .dataArray(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
default:
assertionFailure()
throw DecodingError.typeMismatch(Data.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
} else {
switch self.content {
case let .objectArray(array):
let index = self._currentIndex
self._currentIndex += 1
let data = array[index]
return try AdaptedPostboxDecoder().decode(T.self, from: data)
case let .objectDict(dict):
let index = self._currentIndex
self._currentIndex += 1
let dataPair = dict[index / 2]
let data: Data
if index % 2 == 0 {
data = dataPair.0
} else {
data = dataPair.1
}
return try AdaptedPostboxDecoder().decode(T.self, from: data)
case let .int32Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
case let .int64Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
case let .stringArray(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index] as! T
default:
assertionFailure()
throw DecodingError.typeMismatch(T.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
}
func decode(_ type: Int32.Type) throws -> Int32 {
switch self.content {
case let .int32Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index]
default:
throw DecodingError.typeMismatch(Int32.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
func decode(_ type: Int64.Type) throws -> Int64 {
switch self.content {
case let .int64Array(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index]
default:
throw DecodingError.typeMismatch(Int64.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
func decode(_ type: String.Type) throws -> String {
switch self.content {
case let .stringArray(array):
let index = self._currentIndex
self._currentIndex += 1
return array[index]
default:
throw DecodingError.typeMismatch(String.self, DecodingError.Context(codingPath: self.codingPath, debugDescription: ""))
}
}
func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
preconditionFailure()
}
func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func superDecoder() throws -> Decoder {
preconditionFailure()
}
}
extension _AdaptedPostboxDecoder.UnkeyedContainer: AdaptedPostboxDecodingContainer {}