Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: d814bd4d3a84e5b2eade4e8a2c9bfdbcc79a2f80
      
https://github.com/WebKit/WebKit/commit/d814bd4d3a84e5b2eade4e8a2c9bfdbcc79a2f80
  Author: Richard Robinson <[email protected]>
  Date:   2026-04-24 (Fri, 24 Apr 2026)

  Changed paths:
    M Source/WTF/WTF.xcodeproj/project.pbxproj
    M Source/WTF/wtf/CMakeLists.txt
    M Source/WTF/wtf/Forward.h
    M Source/WTF/wtf/RetainPtr.h
    A Source/WTF/wtf/RetainRef.h
    M Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
    A Tools/TestWebKitAPI/Tests/WTF/cf/RetainRef.cpp
    A Tools/TestWebKitAPI/Tests/WTF/cocoa/RetainRef.mm
    A Tools/TestWebKitAPI/Tests/WTF/cocoa/RetainRefARC.mm

  Log Message:
  -----------
  Add RetainRef, a non-nullable version of RetainPtr
https://bugs.webkit.org/show_bug.cgi?id=313208
rdar://175479803

Reviewed by Chris Dumez.

Introduce RetainRef, which is a non-nullable version of RetainPtr. This follows 
the pattern
established by Ref and RefPtr.

RetainRef should be used for Objective C pointer values which semantically 
cannot be nil.
This allows for this information to be encoded in the type system. The non-null 
guarantee is
established at construction (every constructor that takes a raw pointer asserts 
non-null) and preserved by all subsequent operations.

A future PR will add proper nullability annotations to both RetainRef and 
RetainPtr.

Ideally, `adoptNS` would be like `adoptNSRef` and return a `RetainRef`, however 
that is out of
scope for this PR; a future PR may do so.

Compared to RetainPtr, RetainRef intentionally removes:

- The default constructor
- `clear()`
- `operator bool()` / `operator!()`

A RetainPtr can be converted to a RetainRef using `releaseNonNull`.

Header organization mirrors Ref/RefPtr: RetainRef.h is the foundational header
and RetainPtr.h includes it. To support that direction without circular
includes, the shared type machinery (IsNSType, RetainPtrType, the CF/NS macro
shims) was moved into RetainRef.h, and releaseNonNull() is defined inline in
RetainPtr's class body.

ARC compatibility uses the same `#define RetainRef RetainRefArc` trick already
used by RetainPtr, so the same translation unit can be compiled with and
without ARC. Forward.h declares both types under the matching defines.

HashTraits<RetainRef<P>> mirrors RefHashTraits<P>: TakeType is RetainPtr<P>
(so taking from an empty slot yields a nullable type), constructEmptyValue and
isEmptyValue use RetainRef's HashTableEmptyValue ctor, and DefaultHash uses
PtrHash.

Tests: Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
       Tools/TestWebKitAPI/Tests/WTF/cf/RetainRef.cpp
       Tools/TestWebKitAPI/Tests/WTF/cocoa/RetainRef.mm
       Tools/TestWebKitAPI/Tests/WTF/cocoa/RetainRefARC.mm

* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/Forward.h:
* Source/WTF/wtf/RetainPtr.h:
(WTF::RetainPtr::releaseNonNull):

- Include RetainRef.h
- Remove the now-shared IsNSType / RetainPtrType definitions and CF/NS macro 
shims
(which moved to RetainRef.h)
- Add releaseNonNull() inline.

* Source/WTF/wtf/RetainRef.h: Added.
(WTF::RetainRef::RetainRef):
(WTF::RetainRef::isHashTableDeletedValue const):
(WTF::RetainRef::isHashTableEmptyValue const):
(WTF::RetainRef::hashTableEmptyValue):
(WTF::RetainRef::hashTableDeletedValue):
(WTF::RetainRef::retainFoundationPtr):
(WTF::RetainRef::releaseFoundationPtr):
(WTF::RetainRef::autoreleaseFoundationPtr):
(WTF::RetainRef::~RetainRef):
(WTF::RetainRef::autorelease):
(WTF::RetainRef::getAutoreleased):
(WTF::RetainRef::bridgingAutorelease):
(WTF::RetainRef::operator=):
(WTF::RetainRef::swap):
(WTF::adoptCFRef):
(WTF::adoptNSRef):
(WTF::retainRef):

Implement RetainRef.

* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WTF/cf/RetainRef.cpp: Added.
(TestWebKitAPI::TEST(RetainRef, AdoptCFRef)):
(TestWebKitAPI::TEST(RetainRef, RetainRefFromValue)):
(TestWebKitAPI::TEST(RetainRef, CopyConstructor)):
(TestWebKitAPI::TEST(RetainRef, MoveConstructor)):
(TestWebKitAPI::TEST(RetainRef, FromRetainPtrReleaseNonNull)):
(TestWebKitAPI::TEST(RetainRef, ExplicitFromRetainPtr)):
(TestWebKitAPI::TEST(RetainRef, Assignment)):
(TestWebKitAPI::TEST(RetainRef, HashMap)):
* Tools/TestWebKitAPI/Tests/WTF/cocoa/RetainRef.mm: Added.
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, AdoptNSRef)):
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, RetainRefFromValueNS)):
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, CopyConstructorNS)):
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, MoveConstructorNS)):
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, FromRetainPtrReleaseNonNullNS)):
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, ExplicitFromRetainPtrNS)):
(TestWebKitAPI::TEST(RETAIN_REF_TEST_NAME, HashMapNS)):
* Tools/TestWebKitAPI/Tests/WTF/cocoa/RetainRefARC.mm: Added.

- NS tests, with the AUTORELEASEPOOL_FOR_ARC_DEBUG and uintptr_t pattern to 
keep retain-count assertions accurate when ARC's autoreleased copies inflate 
the count in debug builds.
- ARC variant via the RETAIN_REF_TEST_NAME macro pattern that RetainPtr's tests 
already use.

Canonical link: https://commits.webkit.org/311963@main



To unsubscribe from these emails, change your notification settings at 
https://github.com/WebKit/WebKit/settings/notifications

Reply via email to