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,100 @@
import Foundation
import MurMurHash32
public class AdaptedPostboxEncoder {
public final class RawObjectData: Encodable {
public let typeHash: Int32
public let data: Data
public init(typeHash: Int32, data: Data) {
self.typeHash = typeHash
self.data = data
}
public func encode(to encoder: Encoder) throws {
preconditionFailure()
}
}
public init() {
}
public func encode(_ value: Encodable) throws -> Data {
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
let encoder = _AdaptedPostboxEncoder(typeHash: typeHash)
try value.encode(to: encoder)
return encoder.makeData(addHeader: false, isDictionary: false).0
}
}
final class _AdaptedPostboxEncoder {
var codingPath: [CodingKey] = []
var userInfo: [CodingUserInfoKey : Any] = [:]
let typeHash: Int32
fileprivate var container: AdaptedPostboxEncodingContainer?
init(typeHash: Int32) {
self.typeHash = typeHash
}
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
if let container = self.container {
return container.makeData(addHeader: addHeader, isDictionary: isDictionary)
} else {
let buffer = WriteBuffer()
if addHeader {
var typeHash: Int32 = self.typeHash
buffer.write(&typeHash, offset: 0, length: 4)
}
let innerEncoder = PostboxEncoder()
let data = innerEncoder.makeData()
if addHeader {
var length: Int32 = Int32(data.count)
buffer.write(&length, offset: 0, length: 4)
}
buffer.write(data)
return (buffer.makeData(), .Object)
}
}
}
extension _AdaptedPostboxEncoder: Encoder {
fileprivate func assertCanCreateContainer() {
precondition(self.container == nil)
}
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
assertCanCreateContainer()
let container = KeyedContainer<Key>(codingPath: self.codingPath, userInfo: self.userInfo, typeHash: self.typeHash)
self.container = container
return KeyedEncodingContainer(container)
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
assertCanCreateContainer()
let container = UnkeyedContainer(codingPath: self.codingPath, userInfo: self.userInfo)
self.container = container
return container
}
func singleValueContainer() -> SingleValueEncodingContainer {
preconditionFailure()
}
}
protocol AdaptedPostboxEncodingContainer: AnyObject {
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType)
}
@@ -0,0 +1,113 @@
import Foundation
import MurMurHash32
extension _AdaptedPostboxEncoder {
final class KeyedContainer<Key> where Key: CodingKey {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
let typeHash: Int32
let encoder: PostboxEncoder
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any], typeHash: Int32) {
self.codingPath = codingPath
self.userInfo = userInfo
self.typeHash = typeHash
self.encoder = PostboxEncoder()
}
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
let buffer = WriteBuffer()
if addHeader {
var typeHash: Int32 = self.typeHash
buffer.write(&typeHash, offset: 0, length: 4)
}
let data = self.encoder.makeData()
if addHeader {
var length: Int32 = Int32(data.count)
buffer.write(&length, offset: 0, length: 4)
}
buffer.write(data)
return (buffer.makeData(), .Object)
}
}
}
extension _AdaptedPostboxEncoder.KeyedContainer: KeyedEncodingContainerProtocol {
func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {
if let value = value as? Data {
self.encoder.encodeData(value, forKey: key.stringValue)
} else if let value = value as? AdaptedPostboxEncoder.RawObjectData {
self.encoder.encodeInnerObjectDataWithHeader(typeHash: value.typeHash, data: value.data, valueType: .Object, forKey: key.stringValue)
} else {
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
let innerEncoder = _AdaptedPostboxEncoder(typeHash: typeHash)
try! value.encode(to: innerEncoder)
let typeOfValue = type(of: value)
let typeString = "\(typeOfValue)"
var isDictionary = false
if typeString.hasPrefix("Dictionary<") {
isDictionary = true
}
let (data, valueType) = innerEncoder.makeData(addHeader: true, isDictionary: isDictionary)
self.encoder.encodeInnerObjectData(data, valueType: valueType, forKey: key.stringValue)
}
}
func encodeNil(forKey key: Key) throws {
self.encoder.encodeNil(forKey: key.stringValue)
}
func encode(_ value: Int32, forKey key: Key) throws {
self.encoder.encodeInt32(value, forKey: key.stringValue)
}
func encode(_ value: Int64, forKey key: Key) throws {
self.encoder.encodeInt64(value, forKey: key.stringValue)
}
func encode(_ value: Int, forKey key: Key) throws {
assertionFailure()
self.encoder.encodeInt32(Int32(value), forKey: key.stringValue)
}
func encode(_ value: Bool, forKey key: Key) throws {
self.encoder.encodeBool(value, forKey: key.stringValue)
}
func encode(_ value: Double, forKey key: Key) throws {
self.encoder.encodeDouble(value, forKey: key.stringValue)
}
func encode(_ value: String, forKey key: Key) throws {
self.encoder.encodeString(value, forKey: key.stringValue)
}
func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
preconditionFailure()
}
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func superEncoder() -> Encoder {
preconditionFailure()
}
func superEncoder(forKey key: Key) -> Encoder {
preconditionFailure()
}
}
extension _AdaptedPostboxEncoder.KeyedContainer: AdaptedPostboxEncodingContainer {}
@@ -0,0 +1,85 @@
import Foundation
extension _AdaptedPostboxEncoder {
final class SingleValueContainer {
var codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any]
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
}
}
extension _AdaptedPostboxEncoder.SingleValueContainer: SingleValueEncodingContainer {
func encodeNil() throws {
preconditionFailure()
}
func encode(_ value: Bool) throws {
preconditionFailure()
}
func encode(_ value: String) throws {
preconditionFailure()
}
func encode(_ value: Double) throws {
preconditionFailure()
}
func encode(_ value: Float) throws {
preconditionFailure()
}
func encode(_ value: Int) throws {
preconditionFailure()
}
func encode(_ value: Int8) throws {
preconditionFailure()
}
func encode(_ value: Int16) throws {
preconditionFailure()
}
func encode(_ value: Int32) throws {
preconditionFailure()
}
func encode(_ value: Int64) throws {
preconditionFailure()
}
func encode(_ value: UInt) throws {
preconditionFailure()
}
func encode(_ value: UInt8) throws {
preconditionFailure()
}
func encode(_ value: UInt16) throws {
preconditionFailure()
}
func encode(_ value: UInt32) throws {
preconditionFailure()
}
func encode(_ value: UInt64) throws {
preconditionFailure()
}
func encode<T>(_ value: T) throws where T : Encodable {
preconditionFailure()
}
}
extension _AdaptedPostboxEncoder.SingleValueContainer: AdaptedPostboxEncodingContainer {
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
preconditionFailure()
}
}
@@ -0,0 +1,186 @@
import Foundation
import MurMurHash32
extension _AdaptedPostboxEncoder {
final class UnkeyedContainer {
fileprivate enum Item {
case int32(Int32)
case int64(Int64)
case string(String)
case object(Data)
case data(Data)
}
let codingPath: [CodingKey]
let userInfo: [CodingUserInfoKey: Any]
fileprivate var items: [Item] = []
var count: Int {
return self.items.count
}
init(codingPath: [CodingKey], userInfo: [CodingUserInfoKey : Any]) {
self.codingPath = codingPath
self.userInfo = userInfo
}
func makeData(addHeader: Bool, isDictionary: Bool) -> (Data, ValueType) {
precondition(addHeader)
if self.items.isEmpty {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
return (buffer.makeData(), .Int32Array)
} else if self.items.allSatisfy({ if case .int32 = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .int32(var value) in self.items {
buffer.write(&value, offset: 0, length: 4)
}
return (buffer.makeData(), .Int32Array)
} else if self.items.allSatisfy({ if case .int64 = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .int64(var value) in self.items {
buffer.write(&value, offset: 0, length: 8)
}
return (buffer.makeData(), .Int64Array)
} else if self.items.allSatisfy({ if case .string = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .string(let value) in self.items {
let data = value.data(using: .utf8, allowLossyConversion: true) ?? (String("").data(using: .utf8)!)
var valueLength: Int32 = Int32(data.count)
buffer.write(&valueLength, offset: 0, length: 4)
buffer.write(data)
}
return (buffer.makeData(), .StringArray)
} else if self.items.allSatisfy({ if case .object = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
if isDictionary {
precondition(length % 2 == 0)
length /= 2
}
buffer.write(&length, offset: 0, length: 4)
for case .object(let data) in self.items {
buffer.write(data)
}
return (buffer.makeData(), isDictionary ? .ObjectDictionary : .ObjectArray)
} else if self.items.allSatisfy({ if case .data = $0 { return true } else { return false } }) {
let buffer = WriteBuffer()
var length: Int32 = Int32(self.items.count)
buffer.write(&length, offset: 0, length: 4)
for case .data(let data) in self.items {
var valueLength: Int32 = Int32(data.count)
buffer.write(&valueLength, offset: 0, length: 4)
buffer.write(data)
}
return (buffer.makeData(), .BytesArray)
} else {
preconditionFailure()
}
}
}
}
extension _AdaptedPostboxEncoder.UnkeyedContainer: UnkeyedEncodingContainer {
func encodeNil() throws {
preconditionFailure()
}
func encode<T>(_ value: T) throws where T : Encodable {
if value is Int32 {
try self.encode(value as! Int32)
} else if value is Int64 {
try self.encode(value as! Int64)
} else if value is String {
try self.encode(value as! String)
} else if value is Data {
try self.encodeData(value as! Data)
} else if let value = value as? AdaptedPostboxEncoder.RawObjectData {
let buffer = WriteBuffer()
var typeHash: Int32 = value.typeHash
buffer.write(&typeHash, offset: 0, length: 4)
var length: Int32 = Int32(value.data.count)
buffer.write(&length, offset: 0, length: 4)
buffer.write(value.data)
self.items.append(.object(buffer.makeData()))
} else {
let typeHash: Int32 = murMurHashString32("\(type(of: value))")
let innerEncoder = _AdaptedPostboxEncoder(typeHash: typeHash)
try! value.encode(to: innerEncoder)
let (data, _) = innerEncoder.makeData(addHeader: true, isDictionary: false)
let buffer = WriteBuffer()
buffer.write(data)
self.items.append(.object(buffer.makeData()))
}
}
func encode(_ value: Int32) throws {
self.items.append(.int32(value))
}
func encode(_ value: Int64) throws {
self.items.append(.int64(value))
}
func encode(_ value: String) throws {
self.items.append(.string(value))
}
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
preconditionFailure()
}
func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
preconditionFailure()
}
func superEncoder() -> Encoder {
preconditionFailure()
}
}
private extension _AdaptedPostboxEncoder.UnkeyedContainer {
func encodeData(_ value: Data) throws {
self.items.append(.data(value))
}
}
extension _AdaptedPostboxEncoder.UnkeyedContainer: AdaptedPostboxEncodingContainer {
func makeData() -> Data {
preconditionFailure()
}
}