Skip to content

Commit 80c0b2a

Browse files
Swift 5.0 compatibility
1 parent c61a72f commit 80c0b2a

File tree

12 files changed

+40
-76
lines changed

12 files changed

+40
-76
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:4.0
1+
// swift-tools-version:5.0
22
import PackageDescription
33

44
#if os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || CYGWIN

Sources/Perl/Array.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public final class PerlArray : PerlValue {
5151
self.init(incUnchecked: UnsafeSvContext(rebind: avc))
5252
}
5353

54-
convenience init(noinc svc: UnsafeSvContext) throws {
54+
required convenience init(noinc svc: UnsafeSvContext) throws {
5555
guard svc.type == SVt_PVAV else {
5656
throw PerlError.unexpectedValueType(fromUnsafeSvContext(noinc: svc), want: PerlArray.self)
5757
}

Sources/Perl/CString.swift

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,11 @@
11
extension String {
22
init(cString: UnsafePointer<CChar>, withLength length: Int) {
3-
// self = String.decodeCString(UnsafePointer(cString), withLength: length, as: UTF8.self, repairingInvalidCodeUnits: true)!.result
4-
self = cString.withMemoryRebound(to: UInt8.self, capacity: length) {
5-
String._fromCodeUnitSequenceWithRepair(UTF8.self, input: UnsafeBufferPointer(start: $0, count: length)).0
6-
}
3+
let utf8buffer = UnsafeBufferPointer(start: UnsafeRawPointer(cString).assumingMemoryBound(to: UInt8.self), count: length)
4+
self = String(decoding: utf8buffer, as: UTF8.self)
75
}
86

9-
/* @_specialize(UTF8)
10-
@warn_unused_result
11-
static func decodeCString<Encoding: UnicodeCodec>(
12-
_ cString: UnsafePointer<Encoding.CodeUnit>?, withLength length: Int,
13-
as encoding: Encoding.Type, repairingInvalidCodeUnits isRepairing: Bool = true)
14-
-> (result: String, repairsMode: Bool)? {
15-
16-
guard let cString = cString else { return nil }
17-
let buffer = UnsafeBufferPointer<Encoding.CodeUnit>(start: cString, count: length)
18-
19-
var string = String()
20-
var enc = Encoding.init()
21-
var iter = buffer.makeIterator()
22-
var hadError = false
23-
while true {
24-
let res = enc.decode(&iter)
25-
switch res {
26-
case .scalarValue(let us):
27-
string.append(us)
28-
case .emptyInput:
29-
return (result: string, repairsMode: hadError)
30-
case .error:
31-
string.append(UnicodeScalar(0xfffd))
32-
hadError = true
33-
}
34-
}
35-
}*/
36-
377
func withCStringWithLength<Result>(_ body: (UnsafePointer<CChar>, Int) throws -> Result) rethrows -> Result {
38-
let array = ContiguousArray<UTF8.CodeUnit>(self.utf8)
39-
return try array.withUnsafeBufferPointer { buf in
40-
try buf.baseAddress!.withMemoryRebound(to: CChar.self, capacity: buf.count) {
41-
try body($0, buf.count)
42-
}
43-
}
8+
let length = utf8.count
9+
return try withCString { try body($0, length) }
4410
}
4511
}

Sources/Perl/Call.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension PerlSub {
1919

2020
extension PerlInterpreter {
2121
func unsafeCall<C : Collection>(sv: UnsafeSvPointer, args: C, flags: Int32) throws -> UnsafeStackBufferPointer
22-
where C.Iterator.Element == UnsafeSvPointer, C.IndexDistance == Int {
22+
where C.Iterator.Element == UnsafeSvPointer {
2323
let stack = UnsafeCallStack(perl: self, args: args)
2424
let count = pointee.call_sv(sv, G_EVAL|flags)
2525
let result = stack.popReturned(count: Int(count))

Sources/Perl/Hash.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class PerlHash : PerlValue {
5454
self.init(incUnchecked: UnsafeSvContext(rebind: hvc))
5555
}
5656

57-
convenience init(noinc svc: UnsafeSvContext) throws {
57+
required convenience init(noinc svc: UnsafeSvContext) throws {
5858
guard svc.type == SVt_PVHV else {
5959
throw PerlError.unexpectedValueType(fromUnsafeSvContext(noinc: svc), want: PerlHash.self)
6060
}

Sources/Perl/Object.swift

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import CPerl
4747
/// }
4848
/// ```
4949
open class PerlObject : PerlValue, PerlScalarConvertible {
50-
convenience init(noinc svc: UnsafeSvContext) throws {
50+
public required convenience init(noinc svc: UnsafeSvContext) throws {
5151
guard svc.isObject else {
5252
throw PerlError.notObject(fromUnsafeSvContext(noinc: svc))
5353
}
@@ -158,16 +158,6 @@ open class PerlObject : PerlValue, PerlScalarConvertible {
158158
classMapping[classname] = swiftClass
159159
}
160160

161-
// Workaround for https://bugs.swift.org/browse/SR-5056
162-
public required init(noincUnchecked svc: UnsafeSvContext) {
163-
super.init(noincUnchecked: svc)
164-
}
165-
166-
// Workaround for https://bugs.swift.org/browse/SR-5056
167-
public required init(incUnchecked svc: UnsafeSvContext) {
168-
super.init(incUnchecked: svc)
169-
}
170-
171161
private convenience init(_fromUnsafeSvContextNoinc svc: UnsafeSvContext) throws {
172162
guard let classname = svc.classname else {
173163
throw PerlError.notObject(Perl.fromUnsafeSvContext(noinc: svc))
@@ -204,8 +194,8 @@ open class PerlObject : PerlValue, PerlScalarConvertible {
204194

205195
// Dirty hack to initialize instance of another class (subclass).
206196
extension PerlScalarConvertible where Self : PerlObject {
207-
init(as derivedClass: Self.Type, noinc svc: UnsafeSvContext) {
208-
self = derivedClass.init(noincUnchecked: svc)
197+
init(as derivedClass: PerlObject.Type, noinc svc: UnsafeSvContext) {
198+
self = derivedClass.init(noincUnchecked: svc) as! Self
209199
}
210200
}
211201

Sources/Perl/Scalar.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import CPerl
3030
/// let hashref: PerlScalar = ["type": "string", "value": 10]
3131
/// ```
3232
public final class PerlScalar : PerlValue {
33-
convenience init(noinc svc: UnsafeSvContext) throws {
33+
required convenience init(noinc svc: UnsafeSvContext) throws {
3434
guard svc.type.rawValue < SVt_PVAV.rawValue else {
3535
throw PerlError.unexpectedValueType(fromUnsafeSvContext(noinc: svc), want: PerlScalar.self)
3636
}
@@ -285,12 +285,13 @@ public final class PerlScalar : PerlValue {
285285
}
286286

287287
extension PerlScalar : Equatable, Hashable {
288-
/// The hash value of the stringified form of the scalar.
288+
/// Hashes the essential components of this value by feeding them into the
289+
/// given hasher.
289290
///
290-
/// Hash values are not guaranteed to be equal across different executions of
291-
/// your program. Do not save hash values to use during a future execution.
292-
public var hashValue: Int {
293-
return withUnsafeSvContext { Int($0.hash) }
291+
/// - Parameter hasher: The hasher to use when combining the components
292+
/// of this instance.
293+
public func hash(into hasher: inout Hasher) {
294+
withUnsafeSvContext { hasher.combine($0.hash) }
294295
}
295296

296297
/// Returns a Boolean value indicating whether two scalars stringify to identical strings.

Sources/Perl/Stack.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ protocol UnsafeStack {
88

99
extension UnsafeStack {
1010
fileprivate func pushTo<C : Collection>(sp: inout UnsafeMutablePointer<UnsafeSvPointer>, from source: C)
11-
where C.Iterator.Element == UnsafeSvPointer, C.IndexDistance == Int {
11+
where C.Iterator.Element == UnsafeSvPointer {
1212
if !source.isEmpty {
1313
sp = perl.pointee.EXTEND(sp, source.count)
1414
for sv in source {
@@ -37,7 +37,7 @@ struct UnsafeXSubStack : UnsafeStack {
3737
}
3838

3939
func xsReturn<C : Collection>(_ result: C)
40-
where C.Iterator.Element == UnsafeSvPointer, C.IndexDistance == Int {
40+
where C.Iterator.Element == UnsafeSvPointer {
4141
var sp = perl.pointee.PL_stack_base + Int(ax)
4242
pushTo(sp: &sp, from: result)
4343
}
@@ -121,7 +121,7 @@ struct UnsafeCallStack : UnsafeStack {
121121
let perl: PerlInterpreter
122122

123123
init<C : Collection>(perl: PerlInterpreter, args: C)
124-
where C.Iterator.Element == UnsafeSvPointer, C.IndexDistance == Int {
124+
where C.Iterator.Element == UnsafeSvPointer {
125125
self.perl = perl
126126
var sp = perl.pointee.PL_stack_sp
127127
perl.pointee.PUSHMARK(sp)

Sources/Perl/Subroutine.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public final class PerlSub : PerlValue {
5656
self.init(incUnchecked: UnsafeSvContext(rebind: cvc))
5757
}
5858

59-
convenience init(noinc svc: UnsafeSvContext) throws {
59+
required convenience init(noinc svc: UnsafeSvContext) throws {
6060
guard svc.type == SVt_PVCV else {
6161
throw PerlError.unexpectedValueType(fromUnsafeSvContext(noinc: svc), want: PerlSub.self)
6262
}

Sources/Perl/UnsafeCV.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct UnsafeCvContext {
2424
svt_free: {
2525
(perl, sv, magic) in
2626
let bodyPointer = UnsafeMutableRawPointer(sv!).assumingMemoryBound(to: CV.self).pointee.bodyPointer
27-
bodyPointer.deinitialize()
28-
bodyPointer.deallocate(capacity: 1)
27+
bodyPointer.deinitialize(count: 1)
28+
bodyPointer.deallocate()
2929
return 0
3030
},
3131
svt_copy: nil,

0 commit comments

Comments
 (0)