WebKit icon indicating copy to clipboard operation
WebKit copied to clipboard

[WTF] Add Invocable concept for callbacks and start adoption

Open kmiller68 opened this issue 1 year ago โ€ข 19 comments

ed879d3520c048a360bf7b520799f149a4283e66

[WTF] Add Invocable concept for callbacks and start adoption
https://bugs.webkit.org/show_bug.cgi?id=275398
rdar://129668606

Reviewed by NOBODY (OOPS!).

Right now in WebKit if you want to have a callback the standard practice
is to write your function as:

`template<typename Functor> foo(const Functor& callback)`
or
`foo(const auto& callback)`

this tends to make it unclear what the expected signature of the callback
is. Now that we have concepts it would have been nice to do something like:

`foo(const std::invocable<...> auto& callback)`

however that doesn't provide a clean way to declare the return type, as
far as I could tell. This patch adds a new concept to WTF, which should,
make this a bit cleaner, `Invocable`. Invocable can be used like the following:

`foo(const Invocable<T(U, const V&, ...)> auto& callback)`

This allows us to use function declaration like syntax to tell folks what
type of callback we're expecting.

In the process of adding Invocable concepts to arguments there were two
other notable changes:

1) HashTable/Map/Set took functions by r-value reference, which seemed
weird so I tried changing it. I then noticed that a lot of users of `ensure`
were passing lambdas that capture variables by copy rather than reference.
Since `ensure`'s use of the lambda is scoped to the call there's no reason to
copy data in.

2) the HashTranslator versions of add now take a callback rather than a
arbitrary r-value reference type. This makes the logic of the code a bit
easier to follow and allows adding invocable concepts. Since the new wrapper
lambdas are marked ALWAYS_INLINE_LAMBDA there shouldn't be any performance
overhead.

* Source/JavaScriptCore/b3/B3LowerToAir.cpp:
* Source/JavaScriptCore/dfg/DFGTierUpCheckInjectionPhase.cpp:
(JSC::DFG::TierUpCheckInjectionPhase::run):
* Source/WTF/wtf/FixedVector.h:
* Source/WTF/wtf/FunctionTraits.h:
* Source/WTF/wtf/HashMap.h:
(WTF::HashMapTranslator::hash):
(WTF::HashMapTranslator::equal):
(WTF::HashMapTranslator::translate):
(WTF::HashMapEnsureTranslator::hash):
(WTF::HashMapEnsureTranslator::equal):
(WTF::HashMapEnsureTranslator::translate):
(WTF::HashMapTranslatorAdapter::hash):
(WTF::HashMapTranslatorAdapter::equal):
(WTF::HashMapTranslatorAdapter::translate):
(WTF::HashMapEnsureTranslatorAdapter::hash):
(WTF::HashMapEnsureTranslatorAdapter::equal):
(WTF::HashMapEnsureTranslatorAdapter::translate):
(WTF::TableTraitsArg>::inlineAdd):
(WTF::TableTraitsArg>::inlineEnsure):
(WTF::TableTraitsArg>::ensure):
(WTF::TableTraitsArg>::add):
(WTF::Y>::removeIf):
* Source/WTF/wtf/HashSet.h:
(WTF::HashSetTranslator::hash):
(WTF::HashSetTranslator::equal):
(WTF::HashSetTranslator::translate):
(WTF::HashSetTranslatorAdapter::hash):
(WTF::HashSetTranslatorAdapter::equal):
(WTF::HashSetTranslatorAdapter::translate):
(WTF::HashSetEnsureTranslatorAdaptor::hash):
(WTF::HashSetEnsureTranslatorAdaptor::equal):
(WTF::HashSetEnsureTranslatorAdaptor::translate):
(WTF::TableTraits>::ensure):
(WTF::TableTraits>::add):
(WTF::W>::removeIf):
* Source/WTF/wtf/HashTable.h:
(WTF::IdentityHashTranslator::hash):
(WTF::IdentityHashTranslator::equal):
(WTF::IdentityHashTranslator::translate):
(WTF::HashTable::add):
(WTF::KeyTraits>::addUniqueForInitialization):
(WTF::KeyTraits>::add):
(WTF::KeyTraits>::addPassingHashCode):
(WTF::KeyTraits>::HashTable):
* Source/WTF/wtf/ListHashSet.h:
(WTF::U>::add):
(WTF::U>::appendOrMoveToLast):
(WTF::U>::prependOrMoveToFirst):
(WTF::U>::insertBefore):
* Source/WTF/wtf/LocklessBag.h:
* Source/WTF/wtf/PriorityQueue.h:
* Source/WTF/wtf/RobinHoodHashTable.h:
(WTF::RobinHoodHashTable::add):
(WTF::SizePolicy>::add):
(WTF::SizePolicy>::addPassingHashCode):
* Source/WTF/wtf/StdLibExtras.h:
(WTF::requires):
* Source/WTF/wtf/TinyPtrSet.h:
(WTF::TinyPtrSet::forEach const):
(WTF::TinyPtrSet::genericFilter):
* Source/WTF/wtf/Vector.h:
(WTF::Vector::containsIf const):
(WTF::Malloc>::contains const):
(WTF::Malloc>::findIf const):
(WTF::Malloc>::find const):
(WTF::Malloc>::reverseFind const):
(WTF::Malloc>::reverseFindIf const):
(WTF::Malloc>::appendIfNotContains):
(WTF::Malloc>::appendUsingFunctor):
(WTF::Malloc>::removeFirst):
(WTF::template<Signature<bool):
(WTF::Malloc>::removeLast):
(WTF::Malloc>::removeLastMatching):
(WTF::Malloc>::removeAll):
(WTF::Malloc>::map const):
(WTF:: const const):
(WTF::Malloc>::removeFirstMatching): Deleted.
(WTF::Malloc>::removeAllMatching): Deleted.
(WTF::Malloc>::map const const): Deleted.
(WTF::Malloc>::map const const const): Deleted.
* Source/WebCore/Modules/mediastream/libwebrtc/LibWebRTCMediaEndpoint.cpp:
(WebCore::LibWebRTCMediaEndpoint::mediaStreamFromRTCStreamId):
* Source/WebCore/platform/xr/openxr/OpenXRSwapchain.cpp:
(PlatformXR::OpenXRSwapchain::create):
* Source/WebCore/svg/SVGToOTFFontConversion.cpp:
(WebCore::SVGToOTFFontConverter::SVGToOTFFontConverter):
* Source/WebCore/workers/service/background-fetch/BackgroundFetchEngine.cpp:
(WebCore::BackgroundFetchEngine::startBackgroundFetch):
* Source/WebCore/workers/service/background-fetch/BackgroundFetchManager.cpp:
(WebCore::BackgroundFetchManager::backgroundFetchRegistrationInstance):
* Source/WebKit/GPUProcess/GPUConnectionToWebProcess.cpp:
(WebKit::GPUConnectionToWebProcess::createRenderingBackend):
(WebKit::GPUConnectionToWebProcess::createGraphicsContextGL):
(WebKit::GPUConnectionToWebProcess::createGPU):
* Source/WebKit/Platform/IPC/ThreadSafeObjectHeap.h:
(IPC::HeldType>::add):
* Source/WebKit/UIProcess/DeviceIdHashSaltStorage.cpp:
(WebKit::DeviceIdHashSaltStorage::loadStorageFromDisk):
(WebKit::DeviceIdHashSaltStorage::completeDeviceIdHashSaltForOriginCall):
* Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp:
(TestWebKitAPI::TEST(WTF_HashMap, Ref_Value)):
* Tools/TestWebKitAPI/Tests/WTF/RobinHoodHashMap.cpp:
(TestWebKitAPI::TEST(WTF_RobinHoodHashMap, Ref_Value)):

https://github.com/WebKit/WebKit/commit/ed879d3520c048a360bf7b520799f149a4283e66

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
โŒ ๐Ÿงช style โœ… ๐Ÿ›  ios โœ… ๐Ÿ›  mac โœ… ๐Ÿ›  wpe โœ… ๐Ÿ›  wincairo
โœ… ๐Ÿงช bindings โœ… ๐Ÿ›  ios-sim โœ… ๐Ÿ›  mac-AS-debug โœ… ๐Ÿงช wpe-wk2 โœ… ๐Ÿงช wincairo-tests
โœ… ๐Ÿงช webkitperl โŒ ๐Ÿงช ios-wk2 โœ… ๐Ÿงช api-mac โœ… ๐Ÿงช api-wpe
โŒ ๐Ÿงช ios-wk2-wpt โœ… ๐Ÿงช mac-wk1 โœ… ๐Ÿ›  wpe-cairo
โœ… ๐Ÿ›  ๐Ÿงช jsc loading ๐Ÿงช api-ios โœ… ๐Ÿงช mac-wk2 โœ… ๐Ÿ›  gtk
โœ… ๐Ÿ›  ๐Ÿงช jsc-arm64 โœ… ๐Ÿ›  vision โœ… ๐Ÿงช mac-AS-debug-wk2 โœ… ๐Ÿงช gtk-wk2
โœ… ๐Ÿ›  vision-sim โœ… ๐Ÿงช mac-wk2-stress โœ… ๐Ÿงช api-gtk
โœ… ๐Ÿงช vision-wk2 โœ… ๐Ÿ›  jsc-armv7
โœ… ๐Ÿ›  tv โœ… ๐Ÿงช jsc-armv7-tests
โœ… ๐Ÿ›  tv-sim
โœ… ๐Ÿ›  watch
โœ… ๐Ÿ›  watch-sim

kmiller68 avatar Jun 12 '24 11:06 kmiller68

Committed 280952@main (b945d56c49fa): https://commits.webkit.org/280952@main

Reviewed commits have been landed. Closing PR #29744 and removing active labels.

webkit-commit-queue avatar Jul 14 '24 17:07 webkit-commit-queue