⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 283476 in webkit


Ignore:
Timestamp:
Oct 3, 2021, 12:31:55 PM (5 years ago)
Author:
ddkilzer@apple.com
Message:

WTF::RetainPtr<> allows assignment of two pointer types that are not assignable
<https://webkit.org/b/230406>
<rdar://problem/83241893>

Reviewed by Darin Adler.

Source/WebKit:

  • Shared/API/Cocoa/WKRemoteObjectCoder.mm:

(transformTrustToData):

  • Change return type to match object.

(encodeError):

  • Use bridge_cast() to fix assignment.

(transformDataToTrust):

  • Change return type to match object.

(decodeError):

  • Use bridge_id_cast() to fix assignment.
  • Shared/RemoteLayerTree/RemoteLayerBackingStore.mm:

(WebKit::RemoteLayerBackingStore::applyBackingStoreToLayer):

  • Use bridge_id_cast() to fix assignment.
  • UIProcess/mac/WebDataListSuggestionsDropdownMac.h:
  • Use correct name of class that actually exists.

Source/WebKitLegacy/mac:

  • Plugins/WebBasePluginPackage.mm:

(+[WebBasePluginPackage pluginWithPath:]):

  • Use explicit type with RetainPtr<> to match return type.

Source/WTF:

  • wtf/RetainPtr.h:

(WTF::RetainPtr::RetainPtr):
(WTF::RetainPtr::checkType): Add.

  • Add type check for move constructor.
  • wtf/cocoa/TypeCastsCocoa.h:
  • Change #include to #import.

(WTF_CF_TO_NS_BRIDGE_TRANSFER): Add.
(WTF_CF_TO_NS_BRIDGE_TRANSFER): Add.

  • Helper macros for bridge_cast() and bridge_id_cast().

(WTF_DECLARE_CF_NS_BRIDGE_CAST): Add.

  • Macro used to define bridge_cast() functions for all known toll-free-bridged types.

(WTF::bridge_cast): Add.

  • Helper macro for toll-free-bridge casting from a known CF type to and from their Objective-C NS type. Prevents ref churn for rvale RetainPtr<> objects.

(WTF::bridge_id_cast): Add.

  • Helper macro for casting from any CFTypeRef to id. Prevents ref churn for rvalue RetainPtr<> objects.

(WTF::checked_objc_cast):

  • Simplified to match dynamic_objc_cast<> implementation in RetainPtr.h.
  • wtf/cocoa/VectorCocoa.h:

(WTF::createNSArray):

  • Change return type to match object.

Tools:

  • DumpRenderTree/mac/DumpRenderTree.mm:

(dump):

  • Use bridge_cast() to fix assignment.
  • TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
  • Add RetainPtrARC.mm and TypeCastsCocoaARC.mm to the project.
  • TestWebKitAPI/Tests/WTF/cocoa/TypeCastsCocoa.mm:
  • Update for dual compilation with MRR and with ARC. Change -retainCount methods to CFGetRetainCount().
  • Add @autoreleasepool blocks around groups of tests to find over-released bugs sooner (for future changes).
  • Add more CFGetRetainCount() to catch unexpected auto-released objects as well as leaks.

(TestWebKitAPI::helloWorldCStringLength): Add.
(TestWebKitAPI::TEST):

  • Add tests for bridge_cast() and bridge_id_cast().
  • Remove invalid tests after changing RetainPtr<> move constructor to check types.
  • TestWebKitAPI/Tests/WTF/cocoa/TypeCastsCocoaARC.mm: Add.
  • Run tests in TypeCastsCocoa.mm with ARC enabled.
  • TestWebKitAPI/Tests/WTF/ns/RetainPtr.mm:

(TestWebKitAPI::TEST):

  • Update for dual compilation with MRR and with ARC. Change -retainCount methods to CFGetRetainCount().
  • Make it possible to name tests differently for MRR vs. ARC using RETAIN_PTR_TEST_NAME macro.
  • TestWebKitAPI/Tests/WTF/ns/RetainPtrARC.mm: Add.
  • Run tests in RetainPtr.mm with ARC enabled.
  • TestWebKitAPI/Tests/WebKitCocoa/QuickLook.mm:

(readFile):

  • Use bridge_cast() to fix assignment.
  • Drive-by fix to keep NSData out of autoreleasePool.
Location:
trunk
Files:
2 added
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r283469 r283476  
     12021-10-03  David Kilzer  <ddkilzer@apple.com>
     2
     3        WTF::RetainPtr<> allows assignment of two pointer types that are not assignable
     4        <https://webkit.org/b/230406>
     5        <rdar://problem/83241893>
     6
     7        Reviewed by Darin Adler.
     8
     9        * wtf/RetainPtr.h:
     10        (WTF::RetainPtr::RetainPtr):
     11        (WTF::RetainPtr::checkType): Add.
     12        - Add type check for move constructor.
     13
     14        * wtf/cocoa/TypeCastsCocoa.h:
     15        - Change #include to #import.
     16        (WTF_CF_TO_NS_BRIDGE_TRANSFER): Add.
     17        (WTF_CF_TO_NS_BRIDGE_TRANSFER): Add.
     18        - Helper macros for bridge_cast() and bridge_id_cast().
     19        (WTF_DECLARE_CF_NS_BRIDGE_CAST): Add.
     20        - Macro used to define bridge_cast() functions for all known
     21          toll-free-bridged types.
     22        (WTF::bridge_cast): Add.
     23        - Helper macro for toll-free-bridge casting from a known CF type
     24          to and from their Objective-C NS type.  Prevents ref churn for
     25          rvale RetainPtr<> objects.
     26        (WTF::bridge_id_cast): Add.
     27        - Helper macro for casting from any CFTypeRef to id.  Prevents
     28          ref churn for rvalue RetainPtr<> objects.
     29        (WTF::checked_objc_cast):
     30        - Simplified to match dynamic_objc_cast<> implementation in
     31          RetainPtr.h.
     32
     33        * wtf/cocoa/VectorCocoa.h:
     34        (WTF::createNSArray):
     35        - Change return type to match object.
     36
    1372021-10-03  Basuke Suzuki  <basuke.suzuki@sony.com>
    238
  • trunk/Source/WTF/wtf/RetainPtr.h

    r282844 r283476  
    9191
    9292    constexpr RetainPtr(RetainPtr&& o) : m_ptr(toStorageType(o.leakRef())) { }
    93     template<typename U> constexpr RetainPtr(RetainPtr<U>&& o) : m_ptr(toStorageType(o.leakRef())) { }
     93    template<typename U> constexpr RetainPtr(RetainPtr<U>&& o) : m_ptr(toStorageType(checkType(o.leakRef()))) { }
    9494
    9595    // Hash table deleted values, which are only constructed and never copied or destroyed.
     
    138138    enum AdoptTag { Adopt };
    139139    constexpr RetainPtr(PtrType ptr, AdoptTag) : m_ptr(toStorageType(ptr)) { }
     140
     141    static constexpr PtrType checkType(PtrType ptr) { return ptr; }
    140142
    141143    static constexpr PtrType hashTableDeletedValue() { return reinterpret_cast<PtrType>(-1); }
  • trunk/Source/WTF/wtf/cocoa/TypeCastsCocoa.h

    r282844 r283476  
    2626#pragma once
    2727
    28 #include <wtf/Assertions.h>
    29 #include <wtf/RetainPtr.h>
     28#import <wtf/Assertions.h>
     29#import <wtf/RetainPtr.h>
    3030
    3131namespace WTF {
     32
     33// Use bridge_cast() to convert between CF <-> NS types without ref churn.
     34
     35#if __has_feature(objc_arc)
     36#define WTF_CF_TO_NS_BRIDGE_TRANSFER(type, value) ((__bridge_transfer type)value)
     37#define WTF_NS_TO_CF_BRIDGE_TRANSFER(type, value) ((type)reinterpret_cast<uintptr_t>(value))
     38#else
     39#define WTF_CF_TO_NS_BRIDGE_TRANSFER(type, value) ((__bridge type)value)
     40#define WTF_NS_TO_CF_BRIDGE_TRANSFER(type, value) ((__bridge type)value)
     41#endif
     42
     43#define WTF_DECLARE_CF_NS_BRIDGE_CAST(CFClassName, NSClassName) \
     44inline CFClassName##Ref bridge_cast(NSClassName *object) \
     45{ \
     46    return (__bridge CFClassName##Ref)object; \
     47} \
     48inline RetainPtr<CFClassName##Ref> bridge_cast(RetainPtr<NSClassName>&& object) \
     49{ \
     50    return adoptCF(WTF_NS_TO_CF_BRIDGE_TRANSFER(CFClassName##Ref, object.leakRef())); \
     51} \
     52inline NSClassName *bridge_cast(CFClassName##Ref object) \
     53{ \
     54    return (__bridge NSClassName *)object; \
     55} \
     56inline RetainPtr<NSClassName> bridge_cast(RetainPtr<CFClassName##Ref>&& object) \
     57{ \
     58    return adoptNS(WTF_CF_TO_NS_BRIDGE_TRANSFER(NSClassName *, object.leakRef())); \
     59}
     60
     61WTF_DECLARE_CF_NS_BRIDGE_CAST(CFArray, NSArray)
     62WTF_DECLARE_CF_NS_BRIDGE_CAST(CFAttributedString, NSAttributedString)
     63WTF_DECLARE_CF_NS_BRIDGE_CAST(CFCharacterSet, NSCharacterSet)
     64WTF_DECLARE_CF_NS_BRIDGE_CAST(CFData, NSData)
     65WTF_DECLARE_CF_NS_BRIDGE_CAST(CFDate, NSDate)
     66WTF_DECLARE_CF_NS_BRIDGE_CAST(CFDictionary, NSDictionary)
     67WTF_DECLARE_CF_NS_BRIDGE_CAST(CFError, NSError)
     68WTF_DECLARE_CF_NS_BRIDGE_CAST(CFFileSecurity, NSFileSecurity)
     69WTF_DECLARE_CF_NS_BRIDGE_CAST(CFLocale, NSLocale)
     70WTF_DECLARE_CF_NS_BRIDGE_CAST(CFNull, NSNull)
     71WTF_DECLARE_CF_NS_BRIDGE_CAST(CFNumber, NSNumber)
     72WTF_DECLARE_CF_NS_BRIDGE_CAST(CFSet, NSSet)
     73WTF_DECLARE_CF_NS_BRIDGE_CAST(CFString, NSString)
     74WTF_DECLARE_CF_NS_BRIDGE_CAST(CFTimeZone, NSTimeZone)
     75WTF_DECLARE_CF_NS_BRIDGE_CAST(CFURL, NSURL)
     76
     77inline NSNumber *bridge_cast(CFBooleanRef object)
     78{
     79    return (__bridge NSNumber *)object;
     80}
     81inline RetainPtr<NSNumber> bridge_cast(RetainPtr<CFBooleanRef>&& object)
     82{
     83    return adoptNS(WTF_CF_TO_NS_BRIDGE_TRANSFER(NSNumber *, object.leakRef()));
     84}
     85
     86WTF_DECLARE_CF_NS_BRIDGE_CAST(CFMutableArray, NSMutableArray)
     87WTF_DECLARE_CF_NS_BRIDGE_CAST(CFMutableAttributedString, NSMutableAttributedString)
     88WTF_DECLARE_CF_NS_BRIDGE_CAST(CFMutableData, NSMutableData)
     89WTF_DECLARE_CF_NS_BRIDGE_CAST(CFMutableDictionary, NSMutableDictionary)
     90WTF_DECLARE_CF_NS_BRIDGE_CAST(CFMutableSet, NSMutableSet)
     91WTF_DECLARE_CF_NS_BRIDGE_CAST(CFMutableString, NSMutableString)
     92
     93// Use bridge_id_cast to convert from CF -> id without ref churn.
     94
     95inline id bridge_id_cast(CFTypeRef object)
     96{
     97    return (__bridge id)object;
     98}
     99
     100inline RetainPtr<id> bridge_id_cast(RetainPtr<CFTypeRef>&& object)
     101{
     102    return adoptNS(WTF_CF_TO_NS_BRIDGE_TRANSFER(id, object.leakRef()));
     103}
     104
     105#undef WTF_DECLARE_CF_NS_BRIDGE_CAST
     106#undef WTF_NS_TO_CF_BRIDGE_TRANSFER
     107#undef WTF_CF_TO_NS_BRIDGE_TRANSFER
    32108
    33109// Use checked_objc_cast<> instead of dynamic_objc_cast<> when a specific NS type is required.
     
    35111template<typename T> T* checked_objc_cast(id object)
    36112{
    37     using ValueType = std::remove_pointer_t<T>;
    38     using PtrType = ValueType*;
    39 
    40113    if (!object)
    41114        return nullptr;
    42115
    43     RELEASE_ASSERT_WITH_SECURITY_IMPLICATION([object isKindOfClass:[ValueType class]]);
     116    RELEASE_ASSERT_WITH_SECURITY_IMPLICATION([object isKindOfClass:[T class]]);
    44117
    45     return reinterpret_cast<PtrType>(object);
     118    return reinterpret_cast<T*>(object);
    46119}
    47120
     
    61134} // namespace WTF
    62135
     136using WTF::bridge_cast;
     137using WTF::bridge_id_cast;
    63138using WTF::checked_objc_cast;
  • trunk/Source/WTF/wtf/cocoa/VectorCocoa.h

    r278253 r283476  
    4848//    std::optional<VectorElementType> makeVectorElement(const VectorElementType*, id arrayElement);
    4949
    50 template<typename CollectionType> RetainPtr<NSArray> createNSArray(CollectionType&&);
     50template<typename CollectionType> RetainPtr<NSMutableArray> createNSArray(CollectionType&&);
    5151template<typename VectorElementType> Vector<VectorElementType> makeVector(NSArray *);
    5252
     
    5454// The map function has the same interface as the makeNSArrayElement function above, but can be any
    5555// function including a lambda, a function-like object, or Function<>.
    56 template<typename CollectionType, typename MapFunctionType> RetainPtr<NSArray> createNSArray(CollectionType&&, MapFunctionType&&);
     56template<typename CollectionType, typename MapFunctionType> RetainPtr<NSMutableArray> createNSArray(CollectionType&&, MapFunctionType&&);
    5757
    5858// This overload of makeVector takes a function to map each Objective-C object to a vector element.
     
    6868}
    6969
    70 template<typename CollectionType> RetainPtr<NSArray> createNSArray(CollectionType&& collection)
     70template<typename CollectionType> RetainPtr<NSMutableArray> createNSArray(CollectionType&& collection)
    7171{
    7272    auto array = adoptNS([[NSMutableArray alloc] initWithCapacity:std::size(collection)]);
     
    7676}
    7777
    78 template<typename CollectionType, typename MapFunctionType> RetainPtr<NSArray> createNSArray(CollectionType&& collection, MapFunctionType&& function)
     78template<typename CollectionType, typename MapFunctionType> RetainPtr<NSMutableArray> createNSArray(CollectionType&& collection, MapFunctionType&& function)
    7979{
    8080    auto array = adoptNS([[NSMutableArray alloc] initWithCapacity:std::size(collection)]);
  • trunk/Source/WebKit/ChangeLog

    r283470 r283476  
     12021-10-03  David Kilzer  <ddkilzer@apple.com>
     2
     3        WTF::RetainPtr<> allows assignment of two pointer types that are not assignable
     4        <https://webkit.org/b/230406>
     5        <rdar://problem/83241893>
     6
     7        Reviewed by Darin Adler.
     8
     9        * Shared/API/Cocoa/WKRemoteObjectCoder.mm:
     10        (transformTrustToData):
     11        - Change return type to match object.
     12        (encodeError):
     13        - Use bridge_cast() to fix assignment.
     14        (transformDataToTrust):
     15        - Change return type to match object.
     16        (decodeError):
     17        - Use bridge_id_cast() to fix assignment.
     18        * Shared/RemoteLayerTree/RemoteLayerBackingStore.mm:
     19        (WebKit::RemoteLayerBackingStore::applyBackingStoreToLayer):
     20        - Use bridge_id_cast() to fix assignment.
     21
     22        * UIProcess/mac/WebDataListSuggestionsDropdownMac.h:
     23        - Use correct name of class that actually exists.
     24
    1252021-10-03  Simon Fraser  <simon.fraser@apple.com>
    226
  • trunk/Source/WebKit/Shared/API/Cocoa/WKRemoteObjectCoder.mm

    r275680 r283476  
    4040#import <wtf/Scope.h>
    4141#import <wtf/SetForScope.h>
     42#import <wtf/cocoa/TypeCastsCocoa.h>
    4243#import <wtf/text/CString.h>
    4344
     
    333334}
    334335
    335 static RetainPtr<NSData> transformTrustToData(SecTrustRef trust)
     336static RetainPtr<CFDataRef> transformTrustToData(SecTrustRef trust)
    336337{
    337338    if (CFGetTypeID(trust) != SecTrustGetTypeID())
     
    364365        if (!copy)
    365366            copy = adoptNS([error.userInfo mutableCopy]);
    366         copy.get()[peerTrustKey] = transformTrustToData((SecTrustRef)trust).get();
     367        copy.get()[peerTrustKey] = bridge_cast(transformTrustToData((SecTrustRef)trust).get());
    367368    }
    368369    if (!copy)
     
    386387}
    387388
    388 static RetainPtr<id> transformDataToTrust(NSData *data)
     389static RetainPtr<SecTrustRef> transformDataToTrust(NSData *data)
    389390{
    390391    if (CFGetTypeID(data) != CFDataGetTypeID())
     
    408409        if (!copy)
    409410            copy = adoptNS([error.get().userInfo mutableCopy]);
    410         copy.get()[peerTrustKey] = transformDataToTrust(trust).get();
     411        copy.get()[peerTrustKey] = bridge_id_cast(transformDataToTrust(trust).get());
    411412    }
    412413    if (!copy)
  • trunk/Source/WebKit/Shared/RemoteLayerTree/RemoteLayerBackingStore.mm

    r282639 r283476  
    4848#import <mach/mach_port.h>
    4949#import <pal/spi/cocoa/QuartzCoreSPI.h>
     50#import <wtf/cocoa/TypeCastsCocoa.h>
    5051
    5152#if ENABLE(CG_DISPLAY_LIST_BACKED_IMAGE_BUFFER)
     
    433434        [&] (ShareableBitmap::Handle& handle) {
    434435            ASSERT(m_type == Type::Bitmap);
    435             auto bitmap = ShareableBitmap::create(handle);
    436             contents = bitmap->makeCGImageCopy();
     436            contents = bridge_id_cast(ShareableBitmap::create(handle)->makeCGImageCopy());
    437437        },
    438438        [&] (MachSendRight& machSendRight) {
     
    445445            }
    446446            case RemoteLayerBackingStore::LayerContentsType::CAMachPort:
    447                 contents = adoptCF(CAMachPortCreate(machSendRight.leakSendRight()));
     447                contents = bridge_id_cast(adoptCF(CAMachPortCreate(machSendRight.leakSendRight())));
    448448                break;
    449449            }
  • trunk/Source/WebKit/UIProcess/mac/WebDataListSuggestionsDropdownMac.h

    r237307 r283476  
    3131#import <wtf/RetainPtr.h>
    3232
    33 OBJC_CLASS WKDataListSuggestionsView;
     33OBJC_CLASS WKDataListSuggestionsController;
    3434
    3535namespace WebKit {
     
    5252
    5353    NSView *m_view;
    54     RetainPtr<WKDataListSuggestionsView> m_dropdownUI;
     54    RetainPtr<WKDataListSuggestionsController> m_dropdownUI;
    5555};
    5656
  • trunk/Source/WebKitLegacy/mac/ChangeLog

    r283431 r283476  
     12021-10-03  David Kilzer  <ddkilzer@apple.com>
     2
     3        WTF::RetainPtr<> allows assignment of two pointer types that are not assignable
     4        <https://webkit.org/b/230406>
     5        <rdar://problem/83241893>
     6
     7        Reviewed by Darin Adler.
     8
     9        * Plugins/WebBasePluginPackage.mm:
     10        (+[WebBasePluginPackage pluginWithPath:]):
     11        - Use explicit type with RetainPtr<> to match return type.
     12
    1132021-10-01  Chris Dumez  <cdumez@apple.com>
    214
  • trunk/Source/WebKitLegacy/mac/Plugins/WebBasePluginPackage.mm

    r281791 r283476  
    6868+ (WebBasePluginPackage *)pluginWithPath:(NSString *)pluginPath
    6969{
    70    
    71     auto pluginPackage = adoptNS([[WebPluginPackage alloc] initWithPath:pluginPath]);
     70    RetainPtr<WebBasePluginPackage> pluginPackage = adoptNS([[WebPluginPackage alloc] initWithPath:pluginPath]);
    7271
    7372    if (!pluginPackage) {
  • trunk/Tools/ChangeLog

    r283468 r283476  
     12021-10-03  David Kilzer  <ddkilzer@apple.com>
     2
     3        WTF::RetainPtr<> allows assignment of two pointer types that are not assignable
     4        <https://webkit.org/b/230406>
     5        <rdar://problem/83241893>
     6
     7        Reviewed by Darin Adler.
     8
     9        * DumpRenderTree/mac/DumpRenderTree.mm:
     10        (dump):
     11        - Use bridge_cast() to fix assignment.
     12        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     13        - Add RetainPtrARC.mm and TypeCastsCocoaARC.mm to the project.
     14        * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsCocoa.mm:
     15        - Update for dual compilation with MRR and with ARC.  Change
     16          -retainCount methods to CFGetRetainCount().
     17        - Add @autoreleasepool blocks around groups of tests to find
     18          over-released bugs sooner (for future changes).
     19        - Add more CFGetRetainCount() to catch unexpected auto-released
     20          objects as well as leaks.
     21        (TestWebKitAPI::helloWorldCStringLength): Add.
     22        (TestWebKitAPI::TEST):
     23        - Add tests for bridge_cast() and bridge_id_cast().
     24        - Remove invalid tests after changing RetainPtr<> move
     25          constructor to check types.
     26        * TestWebKitAPI/Tests/WTF/cocoa/TypeCastsCocoaARC.mm: Add.
     27        - Run tests in TypeCastsCocoa.mm with ARC enabled.
     28        * TestWebKitAPI/Tests/WTF/ns/RetainPtr.mm:
     29        (TestWebKitAPI::TEST):
     30        - Update for dual compilation with MRR and with ARC.  Change
     31          -retainCount methods to CFGetRetainCount().
     32        - Make it possible to name tests differently for MRR vs. ARC
     33          using RETAIN_PTR_TEST_NAME macro.
     34        * TestWebKitAPI/Tests/WTF/ns/RetainPtrARC.mm: Add.
     35        - Run tests in RetainPtr.mm with ARC enabled.
     36        * TestWebKitAPI/Tests/WebKitCocoa/QuickLook.mm:
     37        (readFile):
     38        - Use bridge_cast() to fix assignment.
     39        - Drive-by fix to keep NSData out of autoreleasePool.
     40
    1412021-10-03  Myles C. Maxfield  <mmaxfield@apple.com>
    242
  • trunk/Tools/DumpRenderTree/mac/DumpRenderTree.mm

    r281994 r283476  
    108108#import <wtf/WorkQueue.h>
    109109#import <wtf/cocoa/CrashReporter.h>
     110#import <wtf/cocoa/TypeCastsCocoa.h>
    110111#import <wtf/cocoa/VectorCocoa.h>
    111112#import <wtf/text/StringBuilder.h>
     
    16211622        } else if (gTestRunner->dumpDOMAsWebArchive()) {
    16221623            WebArchive *webArchive = [[mainFrame DOMDocument] webArchive];
    1623             resultString = WebCoreTestSupport::createXMLStringFromWebArchiveData((__bridge CFDataRef)[webArchive data]);
     1624            resultString = bridge_cast(WebCoreTestSupport::createXMLStringFromWebArchiveData(bridge_cast([webArchive data])));
    16241625            resultMimeType = @"application/x-webarchive";
    16251626        } else if (gTestRunner->dumpSourceAsWebArchive()) {
    16261627            WebArchive *webArchive = [[mainFrame dataSource] webArchive];
    1627             resultString = WebCoreTestSupport::createXMLStringFromWebArchiveData((__bridge CFDataRef)[webArchive data]);
     1628            resultString = bridge_cast(WebCoreTestSupport::createXMLStringFromWebArchiveData(bridge_cast([webArchive data])));
    16281629            resultMimeType = @"application/x-webarchive";
    16291630        } else if (gTestRunner->isPrinting())
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r283276 r283476  
    263263                44077BB123144B5000179E2D /* DataDetectorsTestIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44077BB0231449D200179E2D /* DataDetectorsTestIOS.mm */; };
    264264                4433A396208044140091ED57 /* SynchronousTimeoutTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4433A395208044130091ED57 /* SynchronousTimeoutTests.mm */; };
     265                44652CB726FCD405005EC272 /* TypeCastsCocoaARC.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44652CB626FCD405005EC272 /* TypeCastsCocoaARC.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
    265266                448110C2253F40300097FC33 /* WebPreferencesTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 448110C1253F40240097FC33 /* WebPreferencesTest.mm */; };
    266267                44817A2F1F0486BF00003810 /* WKRequestActivatedElementInfo.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44817A2E1F0486BF00003810 /* WKRequestActivatedElementInfo.mm */; };
     
    270271                44CDE4D426EE6E4A009F6ACB /* TypeCastsCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44CDE4D326EE6E41009F6ACB /* TypeCastsCocoa.mm */; };
    271272                44CF31FD249941E8009CB6CB /* ContextMenuAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 44CF31FB24993F66009CB6CB /* ContextMenuAction.cpp */; };
     273                44D5008E26FE9ED6000EB12F /* RetainPtrARC.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44D5008D26FE9ED6000EB12F /* RetainPtrARC.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
    272274                4612C2B9210A6ACE00B788A6 /* LoadFileThenReload.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4612C2B8210A6ABF00B788A6 /* LoadFileThenReload.mm */; };
    273275                4628C8E92367ABD100B073F0 /* WKSecurityOrigin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4628C8E82367ABBC00B073F0 /* WKSecurityOrigin.cpp */; };
     
    20992101                442BBF681C91CAD90017087F /* RefLogger.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefLogger.cpp; sourceTree = "<group>"; };
    21002102                4433A395208044130091ED57 /* SynchronousTimeoutTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SynchronousTimeoutTests.mm; sourceTree = "<group>"; };
     2103                44652CB626FCD405005EC272 /* TypeCastsCocoaARC.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypeCastsCocoaARC.mm; sourceTree = "<group>"; };
    21012104                448110C1253F40240097FC33 /* WebPreferencesTest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WebPreferencesTest.mm; sourceTree = "<group>"; };
    21022105                44817A2E1F0486BF00003810 /* WKRequestActivatedElementInfo.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKRequestActivatedElementInfo.mm; sourceTree = "<group>"; };
     
    21062109                44CDE4D326EE6E41009F6ACB /* TypeCastsCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypeCastsCocoa.mm; sourceTree = "<group>"; };
    21072110                44CF31FB24993F66009CB6CB /* ContextMenuAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContextMenuAction.cpp; sourceTree = "<group>"; };
     2111                44D5008D26FE9ED6000EB12F /* RetainPtrARC.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = RetainPtrARC.mm; path = ns/RetainPtrARC.mm; sourceTree = "<group>"; };
    21082112                4612C2B8210A6ABF00B788A6 /* LoadFileThenReload.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LoadFileThenReload.mm; sourceTree = "<group>"; };
    21092113                4628C8E82367ABBC00B073F0 /* WKSecurityOrigin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKSecurityOrigin.cpp; sourceTree = "<group>"; };
     
    42854289                                1ADAD14E1D77A9F600212586 /* BlockPtr.mm */,
    42864290                                BC029B1B1486B25900817DA9 /* RetainPtr.mm */,
     4291                                44D5008D26FE9ED6000EB12F /* RetainPtrARC.mm */,
    42874292                        );
    42884293                        name = ns;
     
    49995004                                1C46169E26BA510700F8C9F6 /* TextStreamCocoa.mm */,
    50005005                                44CDE4D326EE6E41009F6ACB /* TypeCastsCocoa.mm */,
     5006                                44652CB626FCD405005EC272 /* TypeCastsCocoaARC.mm */,
    50015007                                E3C21A7B21B25CA2003B31A3 /* URLExtras.mm */,
    50025008                        );
     
    53175323                                44AC8BC621D0245A00CAFB34 /* RetainPtr.cpp in Sources */,
    53185324                                7C83DF241D0A590C00FEBCF3 /* RetainPtr.mm in Sources */,
     5325                                44D5008E26FE9ED6000EB12F /* RetainPtrARC.mm in Sources */,
    53195326                                E355A6322615718F001C1129 /* RobinHoodHashMap.cpp in Sources */,
    53205327                                E355A63026157174001C1129 /* RobinHoodHashSet.cpp in Sources */,
     
    53495356                                0F2C20B81DCD545000542D9E /* Time.cpp in Sources */,
    53505357                                44CDE4D426EE6E4A009F6ACB /* TypeCastsCocoa.mm in Sources */,
     5358                                44652CB726FCD405005EC272 /* TypeCastsCocoaARC.mm in Sources */,
    53515359                                E324A6F02041C82000A76593 /* UniqueArray.cpp in Sources */,
    53525360                                E3A1E78221B25B7A008C6007 /* URL.cpp in Sources */,
  • trunk/Tools/TestWebKitAPI/Tests/WTF/cocoa/TypeCastsCocoa.mm

    r282844 r283476  
    2828#import <wtf/StdLibExtras.h>
    2929
     30#if __has_feature(objc_arc) && !defined(TypeCastsCocoa)
     31#error This tests TypeCastsCocoa.h with ARC disabled.
     32#endif
     33
    3034@interface MyObjectSubtype : NSObject
    3135@end
     
    3842using namespace WTF;
    3943
     44static const char* helloWorldCString = "Hello world";
     45static size_t helloWorldCStringLength()
     46{
     47    static auto length = strlen(helloWorldCString);
     48    return length;
     49}
     50
     51TEST(TypeCastsCocoa, bridge_cast)
     52{
     53    @autoreleasepool {
     54        auto objectNS = adoptNS([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     55        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     56        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     57
     58        auto objectCF = bridge_cast(WTFMove(objectNS));
     59        auto objectCFPtr = reinterpret_cast<uintptr_t>(objectCF.get());
     60        EXPECT_EQ(nil, objectNS.get());
     61        EXPECT_EQ(objectNSPtr, objectCFPtr);
     62        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCFPtr));
     63
     64        objectNS = bridge_cast(WTFMove(objectCF));
     65        objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     66        EXPECT_EQ(NULL, objectCF.get());
     67        EXPECT_EQ(objectCFPtr, objectNSPtr);
     68        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     69    }
     70
     71    @autoreleasepool {
     72        auto objectCF = adoptCF(CFStringCreateWithBytes(NULL, (const UInt8*)helloWorldCString, helloWorldCStringLength(), kCFStringEncodingUTF8, false));
     73        auto objectCFPtr = reinterpret_cast<uintptr_t>(objectCF.get());
     74        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCFPtr));
     75
     76        auto objectNS = bridge_cast(WTFMove(objectCF));
     77        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     78        EXPECT_EQ(NULL, objectCF.get());
     79        EXPECT_EQ(objectCFPtr, objectNSPtr);
     80        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     81
     82        objectCF = bridge_cast(WTFMove(objectNS));
     83        objectCFPtr = reinterpret_cast<uintptr_t>(objectCF.get());
     84        EXPECT_EQ(nil, objectNS.get());
     85        EXPECT_EQ(objectNSPtr, objectCFPtr);
     86        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCFPtr));
     87    }
     88}
     89
     90TEST(TypeCastsCocoa, bridge_id_cast)
     91{
     92    @autoreleasepool {
     93        RetainPtr<CFTypeRef> objectCF;
     94        auto objectID = bridge_id_cast(WTFMove(objectCF));
     95        EXPECT_EQ(NULL, objectCF.get());
     96        EXPECT_EQ(nil, objectID.get());
     97    }
     98
     99    @autoreleasepool {
     100        auto objectCF = adoptCF(CFStringCreateWithBytes(NULL, (const UInt8*)helloWorldCString, helloWorldCStringLength(), kCFStringEncodingUTF8, false));
     101        auto objectCFPtr = reinterpret_cast<uintptr_t>(objectCF.get());
     102        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCFPtr));
     103
     104        auto objectID = bridge_id_cast(WTFMove(objectCF));
     105        auto objectIDPtr = reinterpret_cast<uintptr_t>(objectID.get());
     106        EXPECT_EQ(NULL, objectCF.get());
     107        EXPECT_EQ(objectCFPtr, objectIDPtr);
     108        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectIDPtr));
     109    }
     110}
     111
    40112TEST(TypeCastsCocoa, checked_objc_cast)
    41113{
    42114    EXPECT_EQ(nil, checked_objc_cast<NSString>(nil));
    43115
    44     {
    45         auto object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    46         EXPECT_EQ(object.get(), checked_objc_cast<NSString>(object.get()));
    47         EXPECT_EQ(object.get(), checked_objc_cast<NSObject>(object.get()));
    48     }
    49 
    50     {
    51         auto object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    52         EXPECT_EQ(object.get(), checked_objc_cast<NSString>(object.get()));
    53     }
    54 
    55     {
    56         auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
    57         EXPECT_EQ(object.get(), checked_objc_cast<NSObject>(object.get()));
     116    @autoreleasepool {
     117        auto objectNS = adoptNS((id)[[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     118        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     119        EXPECT_EQ(objectNS.get(), checked_objc_cast<NSString>((__bridge id)(CFTypeRef)objectNSPtr));
     120        EXPECT_EQ(objectNS.get(), checked_objc_cast<NSObject>((__bridge id)(CFTypeRef)objectNSPtr));
     121        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     122    }
     123
     124    @autoreleasepool {
     125        auto objectNS = adoptNS((NSObject *)[[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     126        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     127        EXPECT_EQ(objectNS.get(), checked_objc_cast<NSString>((__bridge NSObject *)(CFTypeRef)objectNSPtr));
     128        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     129    }
     130
     131    @autoreleasepool {
     132        auto objectNS = adoptNS([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     133        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     134        EXPECT_EQ(objectNS.get(), checked_objc_cast<NSObject>((__bridge NSString *)(CFTypeRef)objectNSPtr));
     135        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
    58136    }
    59137}
     
    63141    EXPECT_EQ(nil, dynamic_objc_cast<NSString>(nil));
    64142
    65     {
    66         auto object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    67         EXPECT_EQ(object.get(), dynamic_objc_cast<NSString>(object.get()));
    68         EXPECT_EQ(object.get(), dynamic_objc_cast<NSObject>(object.get()));
    69         EXPECT_EQ(nil, dynamic_objc_cast<NSArray>(object.get()));
    70     }
    71 
    72     {
    73         auto object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    74         EXPECT_EQ(object.get(), dynamic_objc_cast<NSString>(object.get()));
    75         EXPECT_EQ(nil, dynamic_objc_cast<NSArray>(object.get()));
    76     }
    77 
    78     {
    79         auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
    80         EXPECT_EQ(object.get(), dynamic_objc_cast<NSObject>(object.get()));
    81         EXPECT_EQ(nil, dynamic_objc_cast<NSArray>(object.get()));
    82     }
    83 
    84     {
    85         auto object = adoptNS(reinterpret_cast<id>([[NSObject alloc] init]));
    86         EXPECT_EQ(object.get(), dynamic_objc_cast<NSObject>(object.get()));
    87         EXPECT_EQ(nil, dynamic_objc_cast<MyObjectSubtype>(object.get()));
    88     }
    89 
    90     {
    91         auto object = adoptNS([[NSObject alloc] init]);
    92         EXPECT_EQ(nil, dynamic_objc_cast<MyObjectSubtype>(object.get()));
    93     }
    94 }
    95 
    96 TEST(TypeCastsCocoa, dynamic_ns_cast_RetainPtr)
    97 {
    98     {
     143    @autoreleasepool {
     144        auto objectNS = adoptNS<id>([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     145        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     146        EXPECT_EQ(objectNS.get(), dynamic_objc_cast<NSString>(objectNS.get()));
     147        EXPECT_EQ(objectNS.get(), dynamic_objc_cast<NSObject>(objectNS.get()));
     148        EXPECT_EQ(nil, dynamic_objc_cast<NSArray>(objectNS.get()));
     149        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     150    }
     151
     152    @autoreleasepool {
     153        auto objectNS = adoptNS<NSObject *>([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     154        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     155        EXPECT_EQ(objectNS.get(), dynamic_objc_cast<NSString>(objectNS.get()));
     156        EXPECT_EQ(nil, dynamic_objc_cast<NSArray>(objectNS.get()));
     157        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     158    }
     159
     160    @autoreleasepool {
     161        auto objectNS = adoptNS([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     162        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     163        EXPECT_EQ(objectNS.get(), dynamic_objc_cast<NSObject>(objectNS.get()));
     164        EXPECT_EQ(nil, dynamic_objc_cast<NSArray>(objectNS.get()));
     165        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     166    }
     167
     168    @autoreleasepool {
     169        auto objectID = adoptNS<id>([[NSObject alloc] init]);
     170        auto objectIDPtr = reinterpret_cast<uintptr_t>(objectID.get());
     171        EXPECT_EQ(objectID.get(), dynamic_objc_cast<NSObject>(objectID.get()));
     172        EXPECT_EQ(nil, dynamic_objc_cast<MyObjectSubtype>(objectID.get()));
     173        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectIDPtr));
     174    }
     175
     176    @autoreleasepool {
     177        auto objectNS = adoptNS([[NSObject alloc] init]);
     178        auto objectNSPtr = reinterpret_cast<uintptr_t>(objectNS.get());
     179        EXPECT_EQ(nil, dynamic_objc_cast<MyObjectSubtype>(objectNS.get()));
     180        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectNSPtr));
     181    }
     182}
     183
     184TEST(TypeCastsCocoa, dynamic_objc_cast_RetainPtr)
     185{
     186    @autoreleasepool {
    99187        RetainPtr<NSString> object;
    100188        auto objectCast = dynamic_objc_cast<NSString>(WTFMove(object));
     
    103191    }
    104192
    105     {
    106         auto object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    107         id objectPtr = object.get();
     193    @autoreleasepool {
     194        auto object = adoptNS<id>([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     195        auto objectPtr = reinterpret_cast<uintptr_t>(object.get());
     196        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
     197
    108198        auto objectCast = dynamic_objc_cast<NSString>(WTFMove(object));
    109         EXPECT_EQ(nil, object.get());
    110         EXPECT_EQ(objectPtr, objectCast.get());
    111         objectPtr = nil; // For ARC.
    112         EXPECT_EQ(1U, [objectCast retainCount]);
    113 
    114         object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    115         objectPtr = object.get();
     199        auto objectCastPtr = reinterpret_cast<uintptr_t>(objectCast.get());
     200        EXPECT_EQ(nil, object.get());
     201        EXPECT_EQ(objectPtr, objectCastPtr);
     202        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCastPtr));
     203
     204        object = adoptNS<id>([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     205        objectPtr = reinterpret_cast<uintptr_t>(object.get());
     206        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
     207
    116208        auto objectCast2 = dynamic_objc_cast<NSObject>(WTFMove(object));
    117         EXPECT_EQ(nil, object.get());
    118         EXPECT_EQ(objectPtr, objectCast2.get());
    119         objectPtr = nil; // For ARC.
    120         EXPECT_EQ(1U, [objectCast2 retainCount]);
    121 
    122         object = adoptNS(reinterpret_cast<id>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    123         objectPtr = object.get();
    124         auto objectCast3 = dynamic_objc_cast<NSArray>(WTFMove(object));
    125         EXPECT_EQ(objectPtr, object.get());
    126         EXPECT_EQ(nil, objectCast3.get());
    127         objectPtr = nil; // For ARC.
    128         EXPECT_EQ(1U, [object retainCount]);
    129     }
    130 
    131     {
    132         auto object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    133         id objectPtr = object.get();
    134         auto objectCast = dynamic_objc_cast<NSString>(WTFMove(object));
    135         EXPECT_EQ(nil, object.get());
    136         EXPECT_EQ(objectPtr, objectCast.get());
    137         objectPtr = nil;
    138         EXPECT_EQ(1U, [objectCast retainCount]);
    139 
    140         object = adoptNS(reinterpret_cast<NSObject *>([[NSString alloc] initWithFormat:@"%s", "Hello world"]));
    141         objectPtr = object.get();
    142         auto objectCast2 = dynamic_objc_cast<NSArray>(WTFMove(object));
    143         EXPECT_EQ(objectPtr, object.get());
    144         EXPECT_EQ(nil, objectCast2.get());
    145         objectPtr = nil;
    146         EXPECT_EQ(1U, [object retainCount]);
    147     }
    148 
    149     {
    150         auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
    151         id objectPtr = object.get();
     209        auto objectCastPtr2 = reinterpret_cast<uintptr_t>(objectCast2.get());
     210        EXPECT_EQ(nil, object.get());
     211        EXPECT_EQ(objectPtr, objectCastPtr2);
     212        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCastPtr2));
     213
     214        object = adoptNS<id>([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     215        objectPtr = reinterpret_cast<uintptr_t>(object.get());
     216        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
     217
     218        auto objectCastBad = dynamic_objc_cast<NSArray>(WTFMove(object));
     219        auto objectPtr2 = reinterpret_cast<uintptr_t>(object.get());
     220        EXPECT_EQ(objectPtr, objectPtr2);
     221        EXPECT_EQ(nil, objectCastBad.get());
     222        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr2));
     223    }
     224
     225    @autoreleasepool {
     226        auto object = adoptNS([[NSString alloc] initWithFormat:@"%s", helloWorldCString]);
     227        auto objectPtr = reinterpret_cast<uintptr_t>(object.get());
     228        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
     229
    152230        auto objectCast = dynamic_objc_cast<NSObject>(WTFMove(object));
    153         EXPECT_EQ(nil, object.get());
    154         EXPECT_EQ(objectPtr, objectCast.get());
    155         objectPtr = nil;
    156         EXPECT_EQ(1U, [objectCast retainCount]);
    157 
    158         object = adoptNS([[NSString alloc] initWithFormat:@"%s", "Hello world"]);
    159         objectPtr = object.get();
    160         auto objectCast2 = dynamic_objc_cast<NSArray>(WTFMove(object));
    161         EXPECT_EQ(objectPtr, object.get());
    162         EXPECT_EQ(nil, objectCast2.get());
    163         objectPtr = nil;
    164         EXPECT_EQ(1U, [object retainCount]);
    165     }
    166 
    167     {
    168         auto object = adoptNS(reinterpret_cast<id>([[NSObject alloc] init]));
    169         id objectPtr = object.get();
     231        auto objectCastPtr = reinterpret_cast<uintptr_t>(objectCast.get());
     232        EXPECT_EQ(nil, object.get());
     233        EXPECT_EQ(objectPtr, objectCastPtr);
     234        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCastPtr));
     235    }
     236
     237    @autoreleasepool {
     238        auto object = adoptNS<id>([[NSObject alloc] init]);
     239        auto objectPtr = reinterpret_cast<uintptr_t>(object.get());
     240        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
     241
    170242        auto objectCast = dynamic_objc_cast<NSObject>(WTFMove(object));
    171         EXPECT_EQ(nil, object.get());
    172         EXPECT_EQ(objectPtr, objectCast.get());
    173         objectPtr = nil; // For ARC.
    174         EXPECT_EQ(1U, [objectCast retainCount]);
    175 
    176         object = adoptNS(reinterpret_cast<id>([[NSObject alloc] init]));
    177         objectPtr = object.get();
    178         auto objectCast2 = dynamic_objc_cast<MyObjectSubtype>(WTFMove(object));
    179         EXPECT_EQ(objectPtr, object.get());
    180         EXPECT_EQ(nil, objectCast2.get());
    181         objectPtr = nil; // For ARC.
    182         EXPECT_EQ(1U, [object retainCount]);
    183     }
    184 
    185     {
    186         auto object = adoptNS([[NSObject alloc] init]);
    187         id objectPtr = object.get();
    188         auto objectCast = dynamic_objc_cast<MyObjectSubtype>(WTFMove(object));
    189         EXPECT_EQ(objectPtr, object.get());
    190         EXPECT_EQ(nil, objectCast.get());
    191         objectPtr = nil; // For ARC.
    192         EXPECT_EQ(1U, [object retainCount]);
     243        auto objectCastPtr = reinterpret_cast<uintptr_t>(objectCast.get());
     244        EXPECT_EQ(nil, object.get());
     245        EXPECT_EQ(objectPtr, objectCastPtr);
     246        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectCastPtr));
     247
     248        object = adoptNS<id>([[NSObject alloc] init]);
     249        objectPtr = reinterpret_cast<uintptr_t>(object.get());
     250        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
     251
     252        auto objectCastBad = dynamic_objc_cast<MyObjectSubtype>(WTFMove(object));
     253        EXPECT_EQ(nil, objectCastBad.get());
     254        EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr));
    193255    }
    194256}
  • trunk/Tools/TestWebKitAPI/Tests/WTF/ns/RetainPtr.mm

    r278253 r283476  
    11/*
    2  * Copyright (C) 2011-2018 Apple Inc. All rights reserved.
     2 * Copyright (C) 2011-2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2828
    2929#import "config.h"
    30 
    3130#import <wtf/RetainPtr.h>
    3231
     32#if __has_feature(objc_arc)
     33#ifndef RETAIN_PTR_TEST_NAME
     34#error This tests RetainPtr.h with ARC disabled.
     35#endif
     36#define autorelease self
     37#endif
     38
     39#ifndef RETAIN_PTR_TEST_NAME
     40#define RETAIN_PTR_TEST_NAME RetainPtr
     41#endif
     42
    3343namespace TestWebKitAPI {
    3444
    35 TEST(RetainPtr, AdoptNS)
     45TEST(RETAIN_PTR_TEST_NAME, AdoptNS)
    3646{
    3747    RetainPtr<NSObject> object1 = adoptNS([[NSObject alloc] init]);
    38     EXPECT_EQ(1, CFGetRetainCount(object1.get()));
     48    auto objectPtr1 = reinterpret_cast<uintptr_t>(object1.get());
     49    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr1));
    3950
    4051    RetainPtr<NSObject *> object2 = adoptNS([[NSObject alloc] init]);
    41     EXPECT_EQ(1, CFGetRetainCount(object2.get()));
     52    auto objectPtr2 = reinterpret_cast<uintptr_t>(object2.get());
     53    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr2));
    4254
    4355    RetainPtr<id> object3 = adoptNS([[NSObject alloc] init]);
    44     EXPECT_EQ(1, CFGetRetainCount(object3.get()));
    45 }
    46 
    47 TEST(RetainPtr, ConstructionFromMutableNSType)
     56    auto objectPtr3 = reinterpret_cast<uintptr_t>(object3.get());
     57    EXPECT_EQ(1, CFGetRetainCount((CFTypeRef)objectPtr3));
     58}
     59
     60TEST(RETAIN_PTR_TEST_NAME, ConstructionFromMutableNSType)
    4861{
    4962    NSMutableString *string = [NSMutableString stringWithUTF8String:"foo"];
     
    6578}
    6679
    67 TEST(RetainPtr, ConstructionFromSameNSType)
     80TEST(RETAIN_PTR_TEST_NAME, ConstructionFromSameNSType)
    6881{
    6982    NSString *string = @"foo";
     
    8598}
    8699
    87 TEST(RetainPtr, ConstructionFromSimilarNSType)
     100TEST(RETAIN_PTR_TEST_NAME, ConstructionFromSimilarNSType)
    88101{
    89102    NSString *string = @"foo";
     
    105118}
    106119
    107 TEST(RetainPtr, ConstructionFromSimilarNSTypeReversed)
     120TEST(RETAIN_PTR_TEST_NAME, ConstructionFromSimilarNSTypeReversed)
    108121{
    109122    NSString *string = @"foo";
     
    125138}
    126139
    127 TEST(RetainPtr, MoveAssignmentFromMutableNSType)
     140TEST(RETAIN_PTR_TEST_NAME, MoveAssignmentFromMutableNSType)
    128141{
    129142    NSMutableString *string = [NSMutableString stringWithUTF8String:"foo"];
     
    147160}
    148161
    149 TEST(RetainPtr, MoveAssignmentFromSameNSType)
     162TEST(RETAIN_PTR_TEST_NAME, MoveAssignmentFromSameNSType)
    150163{
    151164    NSString *string = @"foo";
     
    169182}
    170183
    171 TEST(RetainPtr, MoveAssignmentFromSimilarNSType)
     184TEST(RETAIN_PTR_TEST_NAME, MoveAssignmentFromSimilarNSType)
    172185{
    173186    NSString *string = @"foo";
     
    191204}
    192205
    193 TEST(RetainPtr, MoveAssignmentFromSimilarNSTypeReversed)
     206TEST(RETAIN_PTR_TEST_NAME, MoveAssignmentFromSimilarNSTypeReversed)
    194207{
    195208    NSString *string = @"foo";
     
    213226}
    214227
    215 TEST(RetainPtr, OptionalRetainPtrNS)
     228TEST(RETAIN_PTR_TEST_NAME, OptionalRetainPtrNS)
    216229{
    217230    // Test assignment from adoptNS().
    218231    std::optional<RetainPtr<NSObject>> optionalObject1 = adoptNS([NSObject new]);
    219     EXPECT_EQ(1, CFGetRetainCount(optionalObject1.value().get()));
     232    auto optionalObjectPtr1 = reinterpret_cast<uintptr_t>(optionalObject1.value().get());
     233    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)optionalObjectPtr1));
    220234    RetainPtr<NSObject> object1 = optionalObject1.value();
    221235    EXPECT_EQ(optionalObject1.value(), object1);
     
    226240        optionalObject2 = retainPtr([[NSObject new] autorelease]);
    227241    }
    228     EXPECT_EQ(1, CFGetRetainCount(optionalObject2.value().get()));
     242    auto optionalObjectPtr2 = reinterpret_cast<uintptr_t>(optionalObject2.value().get());
     243    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)optionalObjectPtr2));
    229244    RetainPtr<NSObject> object2 = optionalObject2.value();
     245    auto objectPtr2 = reinterpret_cast<uintptr_t>(object2.get());
    230246    EXPECT_EQ(optionalObject2.value(), object2);
    231247
     
    247263
    248264    // Test move from std::optional<RetainPtr<NSObject>>.
    249     EXPECT_EQ(2, CFGetRetainCount(object2.get()));
     265    EXPECT_EQ(2L, CFGetRetainCount((CFTypeRef)objectPtr2));
    250266    optionalObject1 = WTFMove(optionalObject2);
    251     EXPECT_EQ(2, CFGetRetainCount(object2.get()));
     267    EXPECT_EQ(2L, CFGetRetainCount((CFTypeRef)objectPtr2));
    252268    EXPECT_TRUE(optionalObject1.value());
    253269    EXPECT_TRUE(optionalObject1.value().get());
     
    255271}
    256272
    257 TEST(RetainPtr, RetainPtrNS)
     273TEST(RETAIN_PTR_TEST_NAME, RetainPtrNS)
    258274{
    259275    RetainPtr<NSObject> object1;
     
    261277        object1 = retainPtr([[NSObject new] autorelease]);
    262278    }
    263     EXPECT_EQ(1, CFGetRetainCount(object1.get()));
     279    auto objectPtr1 = reinterpret_cast<uintptr_t>(object1.get());
     280    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr1));
    264281
    265282    RetainPtr<NSObject *> object2;
     
    267284        object2 = retainPtr([[NSObject new] autorelease]);
    268285    }
    269     EXPECT_EQ(1, CFGetRetainCount(object2.get()));
     286    auto objectPtr2 = reinterpret_cast<uintptr_t>(object2.get());
     287    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr2));
    270288
    271289    RetainPtr<id> object3;
     
    273291        object3 = retainPtr([[NSObject new] autorelease]);
    274292    }
    275     EXPECT_EQ(1, CFGetRetainCount(object3.get()));
     293    auto objectPtr3 = reinterpret_cast<uintptr_t>(object3.get());
     294    EXPECT_EQ(1L, CFGetRetainCount((CFTypeRef)objectPtr3));
    276295}
    277296
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/QuickLook.mm

    r273610 r283476  
    4545#import <WebKit/_WKDownloadDelegate.h>
    4646#import <wtf/NeverDestroyed.h>
    47 #import <wtf/RetainPtr.h>
     47#import <wtf/cocoa/TypeCastsCocoa.h>
    4848
    4949using namespace TestWebKitAPI;
     
    9898        fileType = typeIdentifier;
    9999
    100     mimeType = adoptCF(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)typeIdentifier, kUTTagClassMIMEType));
    101     fileData = [NSData dataWithContentsOfURL:fileURL];
     100    mimeType = bridge_cast(adoptCF(UTTypeCopyPreferredTagWithClass(bridge_cast(typeIdentifier), kUTTagClassMIMEType)));
     101    fileData = adoptNS([[NSData alloc] initWithContentsOfURL:fileURL]);
    102102}
    103103
Note: See TracChangeset for help on using the changeset viewer.