Changeset 205313 in webkit


Ignore:
Timestamp:
Sep 1, 2016 1:40:23 PM (8 years ago)
Author:
andersca@apple.com
Message:

add BlockPtr::fromCallable
https://bugs.webkit.org/show_bug.cgi?id=161504

Reviewed by Tim Horton.

Source/WTF:

BlockPtr::fromCallable lets you create an Objective-C block from any callable object - including lambdas that contain move-only types.
The block will be allocated on the heap so it doesn't ever need to be copied (which is how it can work with move-only types).

  • wtf/BlockPtr.h:

Tools:

Add a test.

  • TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
  • TestWebKitAPI/Tests/WTF/BlockPtr.mm: Added.

(TestWebKitAPI::TEST):

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r205275 r205313  
     12016-09-01  Anders Carlsson  <andersca@apple.com>
     2
     3        add BlockPtr::fromCallable
     4        https://bugs.webkit.org/show_bug.cgi?id=161504
     5
     6        Reviewed by Tim Horton.
     7
     8        BlockPtr::fromCallable lets you create an Objective-C block from any callable object - including lambdas that contain move-only types.
     9        The block will be allocated on the heap so it doesn't ever need to be copied (which is how it can work with move-only types).
     10
     11        * wtf/BlockPtr.h:
     12
    1132016-08-31  Keith Rollin  <krollin@apple.com>
    214
  • trunk/Source/WTF/wtf/BlockPtr.h

    r196071 r205313  
    2424 */
    2525
    26 #ifndef BlockPtr_h
    27 #define BlockPtr_h
     26#pragma once
    2827
    2928#include <Block.h>
     
    3231namespace WTF {
    3332
     33extern "C" void* _NSConcreteMallocBlock[32];
     34
    3435template<typename> class BlockPtr;
    3536
     
    3738class BlockPtr<R (Args...)> {
    3839public:
     40    using BlockType = R (^)(Args...);
     41
     42    template<typename F>
     43    static BlockPtr fromCallable(F function)
     44    {
     45        struct Descriptor {
     46            uintptr_t reserved;
     47            uintptr_t size;
     48            void (*copy)(void *dst, const void *src);
     49            void (*dispose)(const void *);
     50        };
     51
     52        struct Block {
     53            void* isa;
     54            int32_t flags;
     55            int32_t reserved;
     56            R (*invoke)(void *, Args...);
     57            const struct Descriptor* descriptor;
     58            F f;
     59        };
     60
     61        static const Descriptor descriptor {
     62            0,
     63            sizeof(Block),
     64
     65            // We keep the copy function null - the block is already on the heap
     66            // so it should never be copied.
     67            nullptr,
     68
     69            [](const void* ptr) {
     70                static_cast<Block*>(const_cast<void*>(ptr))->f.~F();
     71            }
     72        };
     73
     74        Block* block = static_cast<Block*>(malloc(sizeof(Block)));
     75        block->isa = _NSConcreteMallocBlock;
     76
     77        enum {
     78            BLOCK_NEEDS_FREE = (1 << 24),
     79            BLOCK_HAS_COPY_DISPOSE = (1 << 25),
     80        };
     81        const unsigned retainCount = 1;
     82
     83        block->flags = BLOCK_HAS_COPY_DISPOSE | BLOCK_NEEDS_FREE | (retainCount << 1);
     84        block->reserved = 0;
     85        block->invoke = [](void *ptr, Args... args) -> R {
     86            return static_cast<Block*>(ptr)->f(std::forward<Args>(args)...);
     87        };
     88        block->descriptor = &descriptor;
     89
     90        new (&block->f) F { std::move(function) };
     91
     92        BlockPtr blockPtr;
     93        blockPtr.m_block = static_cast<BlockType>(block);
     94
     95        return blockPtr;
     96    }
     97
    3998    BlockPtr()
    4099        : m_block(nullptr)
     
    42101    }
    43102
    44     BlockPtr(R (^block)(Args...))
     103    BlockPtr(BlockType block)
    45104        : m_block(Block_copy(block))
    46105    {
     
    82141    }
    83142
     143    BlockType get() const { return m_block; }
     144
    84145    explicit operator bool() const { return m_block; }
    85146    bool operator!() const { return !m_block; }
     
    91152        return m_block(std::forward<Args>(arguments)...);
    92153    }
    93    
     154
    94155private:
    95     R (^m_block)(Args...);
     156    BlockType m_block;
    96157};
    97158
     
    107168using WTF::makeBlockPtr;
    108169
    109 #endif // BlockPtr_h
  • trunk/Tools/ChangeLog

    r205312 r205313  
     12016-09-01  Anders Carlsson  <andersca@apple.com>
     2
     3        add BlockPtr::fromCallable
     4        https://bugs.webkit.org/show_bug.cgi?id=161504
     5
     6        Reviewed by Tim Horton.
     7
     8        Add a test.
     9
     10        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
     11        * TestWebKitAPI/Tests/WTF/BlockPtr.mm: Added.
     12        (TestWebKitAPI::TEST):
     13
    1142016-09-01  Alex Christensen  <achristensen@webkit.org>
    215
  • trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj

    r205212 r205313  
    3434                1A7E8B3618120B2F00AEB74A /* FragmentNavigation.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1A7E8B351812093600AEB74A /* FragmentNavigation.html */; };
    3535                1A9E52C913E65EF4006917F5 /* 18-characters.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = C045F9461385C2F800C0F3CD /* 18-characters.html */; };
     36                1ADAD1501D77A9F600212586 /* BlockPtr.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1ADAD14E1D77A9F600212586 /* BlockPtr.mm */; };
    3637                1ADBEFE3130C6AA100D61D19 /* simple-accelerated-compositing.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1ADBEFBC130C6A0100D61D19 /* simple-accelerated-compositing.html */; };
    3738                1AEDE22613E5E7E700E62FE8 /* InjectedBundleControllerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */; };
     
    677678                1AAD19F51C7CE20300831E47 /* Coding.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Coding.mm; sourceTree = "<group>"; };
    678679                1ABC3DED1899BE6D004F0626 /* Navigation.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Navigation.mm; sourceTree = "<group>"; };
     680                1ADAD14E1D77A9F600212586 /* BlockPtr.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = BlockPtr.mm; sourceTree = "<group>"; };
    679681                1ADBEFAD130C689C00D61D19 /* ForceRepaint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ForceRepaint.cpp; sourceTree = "<group>"; };
    680682                1ADBEFBC130C6A0100D61D19 /* simple-accelerated-compositing.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "simple-accelerated-compositing.html"; sourceTree = "<group>"; };
     
    14521454                        isa = PBXGroup;
    14531455                        children = (
     1456                                1ADAD14E1D77A9F600212586 /* BlockPtr.mm */,
    14541457                                BC029B1B1486B25900817DA9 /* RetainPtr.mm */,
    14551458                        );
     
    21232126                                7C83DED41D0A590C00FEBCF3 /* HashSet.cpp in Sources */,
    21242127                                7C83DEE01D0A590C00FEBCF3 /* IntegerToStringConversion.cpp in Sources */,
     2128                                1ADAD1501D77A9F600212586 /* BlockPtr.mm in Sources */,
    21252129                                7C83DEE81D0A590C00FEBCF3 /* ListHashSet.cpp in Sources */,
    21262130                                7C83DF1D1D0A590C00FEBCF3 /* Lock.cpp in Sources */,
Note: See TracChangeset for help on using the changeset viewer.