Skip to content

[Stdlib] Make the SwiftNativeNSXXXBase classes gracefully handle being decoded with NSKeyedUnarchiver. #24394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2019
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
3 changes: 3 additions & 0 deletions stdlib/public/stubs/SwiftNativeNSXXXBase.mm.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ SWIFT_RUNTIME_STDLIB_API

@implementation __SwiftNativeNS${Class}Base

- (id)initWithCoder: (NSCoder *)coder {
return [self init];
}
- (id)retain {
auto SELF = reinterpret_cast<HeapObject *>(self);
swift_retain(SELF);
Expand Down
56 changes: 56 additions & 0 deletions test/Interpreter/SDK/SwiftNativeNSXXXCoding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

// REQUIRES: objc_interop

import Foundation
import StdlibUnittest

let testSuite = TestSuite("SwiftNativeNSXXXCoding")

// Ensure that T gracefully handles being decoded. It doesn't have to
// work, just not crash.
private func test<T: NSObject & NSCoding>(type: T.Type) {
if #available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) {
let swiftClassName = "__SwiftNative\(type)Base"
print(swiftClassName)
let archiver = NSKeyedArchiver(requiringSecureCoding: true)
archiver.setClassName(swiftClassName, for: T.self)
archiver.encode(T(), forKey: "key")
archiver.finishEncoding()
let d = archiver.encodedData

let unarchiver = try! NSKeyedUnarchiver(forReadingFrom: d)
_ = unarchiver.decodeObject(of: T.self, forKey: "key")
}
}


// Test all the classes listed in SwiftNativeNSXXXBase.mm.gyb except for
// NSEnumerator (which doesn't conform to NSCoding).

testSuite.test("NSArray") {
test(type: NSArray.self)
}

testSuite.test("NSDictionary") {
test(type: NSDictionary.self)
}

testSuite.test("NSSet") {
test(type: NSSet.self)
}

testSuite.test("NSString") {
test(type: NSString.self)
}

testSuite.test("NSData") {
test(type: NSData.self)
}

testSuite.test("NSIndexSet") {
test(type: NSIndexSet.self)
}

runAllTests()