Title: [218660] trunk
Revision
218660
Author
[email protected]
Date
2017-06-21 15:53:14 -0700 (Wed, 21 Jun 2017)

Log Message

Allow constructing a WTF:Function from a function pointer
https://bugs.webkit.org/show_bug.cgi?id=173660

Reviewed by Alex Christensen.

Source/WebCore:

Construct WTF:Function directly from a function pointer when possible
instead of constructing a lambda to do so.

* Modules/encryptedmedia/InitDataRegistry.cpp:
(WebCore::InitDataRegistry::InitDataRegistry):
* page/Page.cpp:
* page/mac/PageMac.mm:
(WebCore::Page::platformInitialize):
* platform/cf/MainThreadSharedTimerCF.cpp:
(WebCore::setupPowerObserver):
* platform/mac/WebCoreNSURLExtras.mm:
* rendering/SimpleLineLayout.cpp:
(WebCore::SimpleLineLayout::canUseForWithReason):
* workers/Worker.cpp:
(WebCore::Worker::Worker):

Source/WTF:

Allow constructing a WTF:Function from a function pointer and
assigning a function pointer to a WTF:Function.

* wtf/Function.h:

Tools:

Add API test coverage.

* TestWebKitAPI/Tests/WTF/Function.cpp:
(TestWebKitAPI::returnThree):
(TestWebKitAPI::returnFour):
(TestWebKitAPI::returnPassedValue):
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (218659 => 218660)


--- trunk/Source/WTF/ChangeLog	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WTF/ChangeLog	2017-06-21 22:53:14 UTC (rev 218660)
@@ -1,3 +1,15 @@
+2017-06-21  Chris Dumez  <[email protected]>
+
+        Allow constructing a WTF:Function from a function pointer
+        https://bugs.webkit.org/show_bug.cgi?id=173660
+
+        Reviewed by Alex Christensen.
+
+        Allow constructing a WTF:Function from a function pointer and
+        assigning a function pointer to a WTF:Function.
+
+        * wtf/Function.h:
+
 2017-06-20  Simon Fraser  <[email protected]>
 
         Remove WILL_REVEAL_EDGE_EVENTS code

Modified: trunk/Source/WTF/wtf/Function.h (218659 => 218660)


--- trunk/Source/WTF/wtf/Function.h	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WTF/wtf/Function.h	2017-06-21 22:53:14 UTC (rev 218660)
@@ -44,6 +44,12 @@
     {
     }
 
+    template<typename FunctionType, class = typename std::enable_if<std::is_pointer<FunctionType>::value && std::is_function<typename std::remove_pointer<FunctionType>::type>::value>::type>
+    Function(FunctionType f)
+        : m_callableWrapper(std::make_unique<CallableWrapper<FunctionType>>(WTFMove(f)))
+    {
+    }
+
     Out operator()(In... in) const
     {
         if (m_callableWrapper)
@@ -60,6 +66,13 @@
         return *this;
     }
 
+    template<typename FunctionType, class = typename std::enable_if<std::is_pointer<FunctionType>::value && std::is_function<typename std::remove_pointer<FunctionType>::type>::value>::type>
+    Function& operator=(FunctionType f)
+    {
+        m_callableWrapper = std::make_unique<CallableWrapper<FunctionType>>(WTFMove(f));
+        return *this;
+    }
+
     Function& operator=(std::nullptr_t)
     {
         m_callableWrapper = nullptr;

Modified: trunk/Source/WebCore/ChangeLog (218659 => 218660)


--- trunk/Source/WebCore/ChangeLog	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/ChangeLog	2017-06-21 22:53:14 UTC (rev 218660)
@@ -1,3 +1,26 @@
+2017-06-21  Chris Dumez  <[email protected]>
+
+        Allow constructing a WTF:Function from a function pointer
+        https://bugs.webkit.org/show_bug.cgi?id=173660
+
+        Reviewed by Alex Christensen.
+
+        Construct WTF:Function directly from a function pointer when possible
+        instead of constructing a lambda to do so.
+
+        * Modules/encryptedmedia/InitDataRegistry.cpp:
+        (WebCore::InitDataRegistry::InitDataRegistry):
+        * page/Page.cpp:
+        * page/mac/PageMac.mm:
+        (WebCore::Page::platformInitialize):
+        * platform/cf/MainThreadSharedTimerCF.cpp:
+        (WebCore::setupPowerObserver):
+        * platform/mac/WebCoreNSURLExtras.mm:
+        * rendering/SimpleLineLayout.cpp:
+        (WebCore::SimpleLineLayout::canUseForWithReason):
+        * workers/Worker.cpp:
+        (WebCore::Worker::Worker):
+
 2017-06-21  Antoine Quint  <[email protected]>
 
         CSS text properties affect <video> shadow root

Modified: trunk/Source/WebCore/Modules/encryptedmedia/InitDataRegistry.cpp (218659 => 218660)


--- trunk/Source/WebCore/Modules/encryptedmedia/InitDataRegistry.cpp	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/Modules/encryptedmedia/InitDataRegistry.cpp	2017-06-21 22:53:14 UTC (rev 218660)
@@ -131,9 +131,9 @@
 
 InitDataRegistry::InitDataRegistry()
 {
-    registerInitDataType("keyids", { &sanitizeKeyids, &extractKeyIDsKeyids });
-    registerInitDataType("cenc", { &sanitizeCenc, &extractKeyIDsCenc });
-    registerInitDataType("webm", { &sanitizeWebM, &extractKeyIDsWebM });
+    registerInitDataType("keyids", { sanitizeKeyids, extractKeyIDsKeyids });
+    registerInitDataType("cenc", { sanitizeCenc, extractKeyIDsCenc });
+    registerInitDataType("webm", { sanitizeWebM, extractKeyIDsWebM });
 }
 
 InitDataRegistry::~InitDataRegistry() = default;

Modified: trunk/Source/WebCore/page/Page.cpp (218659 => 218660)


--- trunk/Source/WebCore/page/Page.cpp	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/page/Page.cpp	2017-06-21 22:53:14 UTC (rev 218660)
@@ -281,7 +281,7 @@
     if (!allPages) {
         allPages = new HashSet<Page*>;
         
-        networkStateNotifier().addNetworkStateChangeListener([] (bool isOnLine) { networkStateChanged(isOnLine); });
+        networkStateNotifier().addNetworkStateChangeListener(WTF::Function<void (bool)> { networkStateChanged });
     }
 
     ASSERT(!allPages->contains(this));

Modified: trunk/Source/WebCore/page/mac/PageMac.mm (218659 => 218660)


--- trunk/Source/WebCore/page/mac/PageMac.mm	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/page/mac/PageMac.mm	2017-06-21 22:53:14 UTC (rev 218660)
@@ -61,8 +61,8 @@
 #if ENABLE(TREE_DEBUGGING)
     static std::once_flag onceFlag;
     std::call_once(onceFlag, [] {
-        registerNotifyCallback("com.apple.WebKit.showRenderTree", [] { printRenderTreeForLiveDocuments(); });
-        registerNotifyCallback("com.apple.WebKit.showLayerTree", [] { printLayerTreeForLiveDocuments(); });
+        registerNotifyCallback("com.apple.WebKit.showRenderTree", printRenderTreeForLiveDocuments);
+        registerNotifyCallback("com.apple.WebKit.showLayerTree", printLayerTreeForLiveDocuments);
     });
 #endif
 }

Modified: trunk/Source/WebCore/platform/cf/MainThreadSharedTimerCF.cpp (218659 => 218660)


--- trunk/Source/WebCore/platform/cf/MainThreadSharedTimerCF.cpp	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/platform/cf/MainThreadSharedTimerCF.cpp	2017-06-21 22:53:14 UTC (rev 218660)
@@ -57,7 +57,7 @@
 #if PLATFORM(MAC)
     static PowerObserver* powerObserver;
     if (!powerObserver)
-        powerObserver = std::make_unique<PowerObserver>([] { restartSharedTimer(); }).release();
+        powerObserver = std::make_unique<PowerObserver>(restartSharedTimer).release();
 #elif PLATFORM(IOS)
     static bool registeredForApplicationNotification = false;
     if (!registeredForApplicationNotification) {

Modified: trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm (218659 => 218660)


--- trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/platform/mac/WebCoreNSURLExtras.mm	2017-06-21 22:53:14 UTC (rev 218660)
@@ -294,7 +294,7 @@
     { \
         static const int32_t suffixLength = sizeof(suffix) / sizeof(suffix[0]); \
         if (length > suffixLength && 0 == memcmp(buffer + length - suffixLength, suffix, sizeof(suffix))) \
-            return isSecondLevelDomainNameAllowedByTLDRules(buffer, length - suffixLength, [](UChar c) { return function(c); }); \
+            return isSecondLevelDomainNameAllowedByTLDRules(buffer, length - suffixLength, function); \
     }
 
 static bool isRussianDomainNameCharacter(UChar ch)

Modified: trunk/Source/WebCore/rendering/SimpleLineLayout.cpp (218659 => 218660)


--- trunk/Source/WebCore/rendering/SimpleLineLayout.cpp	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/rendering/SimpleLineLayout.cpp	2017-06-21 22:53:14 UTC (rev 218660)
@@ -254,9 +254,9 @@
 #ifndef NDEBUG
     static std::once_flag onceFlag;
     std::call_once(onceFlag, [] {
-        registerNotifyCallback("com.apple.WebKit.showSimpleLineLayoutCoverage", [] { printSimpleLineLayoutCoverage(); });
-        registerNotifyCallback("com.apple.WebKit.showSimpleLineLayoutReasons", [] { printSimpleLineLayoutBlockList(); });
-        registerNotifyCallback("com.apple.WebKit.toggleSimpleLineLayout", [] { toggleSimpleLineLayout(); });
+        registerNotifyCallback("com.apple.WebKit.showSimpleLineLayoutCoverage", printSimpleLineLayoutCoverage);
+        registerNotifyCallback("com.apple.WebKit.showSimpleLineLayoutReasons", printSimpleLineLayoutBlockList);
+        registerNotifyCallback("com.apple.WebKit.toggleSimpleLineLayout", toggleSimpleLineLayout);
     });
 #endif
     AvoidanceReasonFlags reasons = { };

Modified: trunk/Source/WebCore/workers/Worker.cpp (218659 => 218660)


--- trunk/Source/WebCore/workers/Worker.cpp	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Source/WebCore/workers/Worker.cpp	2017-06-21 22:53:14 UTC (rev 218660)
@@ -59,7 +59,7 @@
 {
     if (!allWorkers) {
         allWorkers = new HashSet<Worker*>;
-        networkStateNotifier().addNetworkStateChangeListener([] (bool isOnLine) { networkStateChanged(isOnLine); });
+        networkStateNotifier().addNetworkStateChangeListener(WTF::Function<void (bool)> { networkStateChanged });
     }
 
     auto addResult = allWorkers->add(this);

Modified: trunk/Tools/ChangeLog (218659 => 218660)


--- trunk/Tools/ChangeLog	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Tools/ChangeLog	2017-06-21 22:53:14 UTC (rev 218660)
@@ -1,3 +1,18 @@
+2017-06-21  Chris Dumez  <[email protected]>
+
+        Allow constructing a WTF:Function from a function pointer
+        https://bugs.webkit.org/show_bug.cgi?id=173660
+
+        Reviewed by Alex Christensen.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WTF/Function.cpp:
+        (TestWebKitAPI::returnThree):
+        (TestWebKitAPI::returnFour):
+        (TestWebKitAPI::returnPassedValue):
+        (TestWebKitAPI::TEST):
+
 2017-06-21  Antoine Quint  <[email protected]>
 
         Add logging to identify when the Page suspends scripted animations

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/Function.cpp (218659 => 218660)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/Function.cpp	2017-06-21 22:46:46 UTC (rev 218659)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/Function.cpp	2017-06-21 22:53:14 UTC (rev 218660)
@@ -240,4 +240,41 @@
     FunctionDestructionChecker::functionResult = std::nullopt;
 }
 
+static int returnThree()
+{
+    return 3;
+}
+
+static int returnFour()
+{
+    return 4;
+}
+
+static int returnPassedValue(int value)
+{
+    return value;
+}
+
+TEST(WTF_Function, AssignFunctionPointer)
+{
+    Function<int()> f1 = returnThree;
+    EXPECT_TRUE(static_cast<bool>(f1));
+    EXPECT_EQ(3, f1());
+
+    f1 = returnFour;
+    EXPECT_TRUE(static_cast<bool>(f1));
+    EXPECT_EQ(4, f1());
+
+    f1 = nullptr;
+    EXPECT_FALSE(static_cast<bool>(f1));
+
+    Function<int(int)> f2 = returnPassedValue;
+    EXPECT_TRUE(static_cast<bool>(f2));
+    EXPECT_EQ(3, f2(3));
+    EXPECT_EQ(-3, f2(-3));
+
+    f2 = nullptr;
+    EXPECT_FALSE(static_cast<bool>(f2));
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to