Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/NewCodable/CommonDecodableAdoption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ extension Int64: CommonDecodable {
}
}

extension Int128: CommonDecodable {
@_alwaysEmitIntoClient
@inline(__always)
public static func decode<D: CommonDecoder & ~Escapable>(from decoder: inout D) throws(CodingError.Decoding) -> Self {
return try decoder.decode(Self.self)
}
}

extension UInt: CommonDecodable {
@_alwaysEmitIntoClient
@inline(__always)
Expand Down Expand Up @@ -106,6 +114,14 @@ extension UInt64: CommonDecodable {
}
}

extension UInt128: CommonDecodable {
@_alwaysEmitIntoClient
@inline(__always)
public static func decode<D: CommonDecoder & ~Escapable>(from decoder: inout D) throws(CodingError.Decoding) -> Self {
return try decoder.decode(Self.self)
}
}

extension Float: CommonDecodable {
@_alwaysEmitIntoClient
@inline(__always)
Expand Down
12 changes: 12 additions & 0 deletions Sources/NewCodable/CommonDecodableProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public protocol CommonDecoder: ~Escapable {
@_lifetime(self: copy self)
mutating func decode(_ hint: Int64.Type) throws(CodingError.Decoding) -> Int64

@_lifetime(self: copy self)
mutating func decode(_ hint: Int128.Type) throws(CodingError.Decoding) -> Int128

@_lifetime(self: copy self)
mutating func decode(_ hint: UInt.Type) throws(CodingError.Decoding) -> UInt

Expand All @@ -138,6 +141,9 @@ public protocol CommonDecoder: ~Escapable {
@_lifetime(self: copy self)
mutating func decode(_ hint: UInt64.Type) throws(CodingError.Decoding) -> UInt64

@_lifetime(self: copy self)
mutating func decode(_ hint: UInt128.Type) throws(CodingError.Decoding) -> UInt128

@_lifetime(self: copy self)
mutating func decode(_ hint: Float.Type) throws(CodingError.Decoding) -> Float

Expand Down Expand Up @@ -393,6 +399,9 @@ public extension CommonDecoder where Self: ~Escapable {
@_lifetime(self: copy self)
mutating func decode(_ hint: Int64.Type) throws(CodingError.Decoding) -> Int64 { throw CodingError.unsupportedDecodingType("Int64") }

@_lifetime(self: copy self)
mutating func decode(_ hint: Int128.Type) throws(CodingError.Decoding) -> Int128 { throw CodingError.unsupportedDecodingType("Int128") }

@_lifetime(self: copy self)
mutating func decode(_ hint: UInt.Type) throws(CodingError.Decoding) -> UInt { throw CodingError.unsupportedDecodingType("UInt") }

Expand All @@ -408,6 +417,9 @@ public extension CommonDecoder where Self: ~Escapable {
@_lifetime(self: copy self)
mutating func decode(_ hint: UInt64.Type) throws(CodingError.Decoding) -> UInt64 { throw CodingError.unsupportedDecodingType("UInt64") }

@_lifetime(self: copy self)
mutating func decode(_ hint: UInt128.Type) throws(CodingError.Decoding) -> UInt128 { throw CodingError.unsupportedDecodingType("UInt128") }

@_lifetime(self: copy self)
mutating func decode(_ hint: Float.Type) throws(CodingError.Decoding) -> Float { throw CodingError.unsupportedDecodingType("Float") }

Expand Down
12 changes: 12 additions & 0 deletions Sources/NewCodable/CommonEncodableAdoption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ extension Int64: CommonEncodable {
}
}

extension Int128: CommonEncodable {
public func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable) throws(CodingError.Encoding) {
try encoder.encode(self)
}
}

extension UInt: CommonEncodable {
public func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable) throws(CodingError.Encoding) {
try encoder.encode(self)
Expand Down Expand Up @@ -85,6 +91,12 @@ extension UInt64: CommonEncodable {
}
}

extension UInt128: CommonEncodable {
public func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable) throws(CodingError.Encoding) {
try encoder.encode(self)
}
}

extension Float: CommonEncodable {
public func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable) throws(CodingError.Encoding) {
try encoder.encode(self)
Expand Down
22 changes: 22 additions & 0 deletions Sources/NewCodable/JSON/JSONDecoderProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ public protocol JSONDecoderProtocol: ~Escapable {
@_lifetime(self: copy self)
mutating func decode(_ hint: Int64.Type) throws(CodingError.Decoding) -> Int64

@_lifetime(self: copy self)
mutating func decode(_ hint: Int128.Type) throws(CodingError.Decoding) -> Int128

@_lifetime(self: copy self)
mutating func decode(_ hint: UInt.Type) throws(CodingError.Decoding) -> UInt

Expand All @@ -224,6 +227,9 @@ public protocol JSONDecoderProtocol: ~Escapable {
@_lifetime(self: copy self)
mutating func decode(_ hint: UInt64.Type) throws(CodingError.Decoding) -> UInt64

@_lifetime(self: copy self)
mutating func decode(_ hint: UInt128.Type) throws(CodingError.Decoding) -> UInt128

@_lifetime(self: copy self)
mutating func decode(_ hint: Float.Type) throws(CodingError.Decoding) -> Float

Expand Down Expand Up @@ -387,6 +393,14 @@ extension Int64: JSONDecodable {
}
}

extension Int128: JSONDecodable {
@inline(__always)
@_alwaysEmitIntoClient
public static func decode<D: JSONDecoderProtocol & ~Escapable>(from decoder: inout D) throws(CodingError.Decoding) -> Self {
return try decoder.decode(Self.self)
}
}

extension UInt: JSONDecodable {
@inline(__always)
@_alwaysEmitIntoClient
Expand Down Expand Up @@ -427,6 +441,14 @@ extension UInt64: JSONDecodable {
}
}

extension UInt128: JSONDecodable {
@inline(__always)
@_alwaysEmitIntoClient
public static func decode<D: JSONDecoderProtocol & ~Escapable>(from decoder: inout D) throws(CodingError.Decoding) -> Self {
return try decoder.decode(Self.self)
}
}

extension Float: JSONDecodable {
@inline(__always)
@_alwaysEmitIntoClient
Expand Down
14 changes: 14 additions & 0 deletions Sources/NewCodable/JSON/JSONEncoderProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ extension Int64: JSONEncodable {
}
}

extension Int128: JSONEncodable {
@inlinable
public func encode(to encoder: inout JSONDirectEncoder) throws(CodingError.Encoding) {
try encoder.encode(self)
}
}

extension UInt: JSONEncodable {
@inlinable
public func encode(to encoder: inout JSONDirectEncoder) throws(CodingError.Encoding) {
Expand Down Expand Up @@ -133,6 +140,13 @@ extension UInt64: JSONEncodable {
}
}

extension UInt128: JSONEncodable {
@inlinable
public func encode(to encoder: inout JSONDirectEncoder) throws(CodingError.Encoding) {
try encoder.encode(self)
}
}

extension Float: JSONEncodable {
@inlinable
public func encode(to encoder: inout JSONDirectEncoder) throws(CodingError.Encoding) {
Expand Down
21 changes: 21 additions & 0 deletions Sources/NewCodable/JSON/JSONParserDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,17 @@ extension JSONParserDecoder {
return try state.decode(hint)
}

@_lifetime(self: copy self)
public mutating func decode(_ hint: Int128.Type) throws(CodingError.Decoding) -> Int128 {
do {
try state.reader.consumeWhitespaceAndPeek()
} catch {
throw error.at(self.codingPath)
}
return try state.decode(hint)
}


@_lifetime(self: copy self)
public mutating func decode(_ hint: UInt.Type) throws(CodingError.Decoding) -> UInt {
do {
Expand Down Expand Up @@ -1067,6 +1078,16 @@ extension JSONParserDecoder {
}
return try state.decode(hint)
}

@_lifetime(self: copy self)
public mutating func decode(_ hint: UInt128.Type) throws(CodingError.Decoding) -> UInt128 {
do {
try state.reader.consumeWhitespaceAndPeek()
} catch {
throw error.at(self.codingPath)
}
return try state.decode(hint)
}

@_lifetime(self: copy self)
public mutating func decode(_ hint: Float.Type) throws(CodingError.Decoding) -> Float {
Expand Down
16 changes: 10 additions & 6 deletions Sources/NewCodable/JSON/JSONPrimitive.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,10 @@ extension JSONPrimitive {
return try number[type]
}
func decode(_ type: Int128.Type) throws -> Int128 {
fatalError("TODO")
// guard case .number(let number) = self else {
// fatalError("ERROR")
// }
// return try number[type]
guard case .number(let number) = self else {
fatalError("ERROR")
}
return try number[type]
}
func decode(_ type: UInt.Type) throws -> UInt {
guard case .number(let number) = self else {
Expand Down Expand Up @@ -305,7 +304,12 @@ extension JSONPrimitive {
}
return try number[type]
}
func decode(_ type: UInt128.Type) throws -> UInt128 { fatalError("TODO") }
func decode(_ type: UInt128.Type) throws -> UInt128 {
guard case .number(let number) = self else {
fatalError("ERROR")
}
return try number[type]
}
func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
let decoder = _JSONValueDecoder(self)
return try type.init(from: decoder)
Expand Down
19 changes: 16 additions & 3 deletions Sources/NewCodable/JSON/ParserState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ extension JSONParserDecoder {
@_lifetime(self: copy self) mutating func decode(_ t: Int16.Type) throws(CodingError.Decoding) -> Int16 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: Int32.Type) throws(CodingError.Decoding) -> Int32 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: Int64.Type) throws(CodingError.Decoding) -> Int64 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: Int128.Type) throws(CodingError.Decoding) -> Int128 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: UInt.Type) throws(CodingError.Decoding) -> UInt { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: UInt8.Type) throws(CodingError.Decoding) -> UInt8 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: UInt16.Type) throws(CodingError.Decoding) -> UInt16 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: UInt32.Type) throws(CodingError.Decoding) -> UInt32 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: UInt64.Type) throws(CodingError.Decoding) -> UInt64 { try decode() }
@_lifetime(self: copy self) mutating func decode(_ t: UInt128.Type) throws(CodingError.Decoding) -> UInt128 { try decode() }

@usableFromInline
internal struct FloatingPointNonConformingStringValueVisitor<T: BinaryFloatingPoint & PrevalidatedJSONNumberBufferConvertible>: DecodingStringVisitor {
Expand Down Expand Up @@ -173,7 +175,6 @@ extension JSONParserDecoder {
@inlinable
@inline(__always)
mutating func decode<T: FixedWidthInteger>() throws(CodingError.Decoding) -> T {
// TODO: TEST NEGATIVE FLOATS HERE. I think `parseInteger` consumes the `-` and doesn't restore it on returning .retryAsFloatingPoint
do throws(_JSONDecodingError) {
switch try reader.parseInteger(as: T.self) ^^ .jsonError {
case .pureInteger(let integer):
Expand All @@ -186,7 +187,13 @@ extension JSONParserDecoder {
throw .json(JSONError.numberIsNotRepresentableInSwift(parsed: String(double)))
}

// TODO: Classic JSONDecoder would retry Decimal -> integer parsing
// Double only has 53 bits of significand, so values with magnitude >= 2^53
// may have been rounded. Reject them to avoid silently returning wrong integers.
// TODO: Classic JSONDecoder would retry Decimal -> integer parsing for these values.
if double.magnitude >= Double(sign: .plus, exponent: Double.significandBitCount + 1, significand: 1) {
throw .json(JSONError.numberIsNotRepresentableInSwift(parsed: String(double)))
}

return integer
case .notANumber:
throw .decoding(decodingError(expectedTypeDescription: "integer number"))
Expand Down Expand Up @@ -1198,7 +1205,13 @@ extension JSONParserDecoder {
switch peek() {
case ._minus:
moveReaderIndex(forwardBy: 1)
return try _parseIntegerDigits(isNegative: true)
let result = try _parseIntegerDigits(isNegative: true) as IntegerParseResult<Result>
if case .retryAsFloatingPoint = result {
// _parseIntegerDigits rewinds to its own start, but the '-' was consumed
// before it was called. Rewind one more byte so parseFloatingPoint sees it.
moveReaderIndex(forwardBy: -1)
}
return result
case ._plus:
moveReaderIndex(forwardBy: 1)
fallthrough
Expand Down
Loading
Loading