Changeset 205313 in webkit
- Timestamp:
- Sep 1, 2016 1:40:23 PM (8 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r205275 r205313 1 2016-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 1 13 2016-08-31 Keith Rollin <krollin@apple.com> 2 14 -
trunk/Source/WTF/wtf/BlockPtr.h
r196071 r205313 24 24 */ 25 25 26 #ifndef BlockPtr_h 27 #define BlockPtr_h 26 #pragma once 28 27 29 28 #include <Block.h> … … 32 31 namespace WTF { 33 32 33 extern "C" void* _NSConcreteMallocBlock[32]; 34 34 35 template<typename> class BlockPtr; 35 36 … … 37 38 class BlockPtr<R (Args...)> { 38 39 public: 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 39 98 BlockPtr() 40 99 : m_block(nullptr) … … 42 101 } 43 102 44 BlockPtr( R (^block)(Args...))103 BlockPtr(BlockType block) 45 104 : m_block(Block_copy(block)) 46 105 { … … 82 141 } 83 142 143 BlockType get() const { return m_block; } 144 84 145 explicit operator bool() const { return m_block; } 85 146 bool operator!() const { return !m_block; } … … 91 152 return m_block(std::forward<Args>(arguments)...); 92 153 } 93 154 94 155 private: 95 R (^m_block)(Args...);156 BlockType m_block; 96 157 }; 97 158 … … 107 168 using WTF::makeBlockPtr; 108 169 109 #endif // BlockPtr_h -
trunk/Tools/ChangeLog
r205312 r205313 1 2016-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 1 14 2016-09-01 Alex Christensen <achristensen@webkit.org> 2 15 -
trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
r205212 r205313 34 34 1A7E8B3618120B2F00AEB74A /* FragmentNavigation.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1A7E8B351812093600AEB74A /* FragmentNavigation.html */; }; 35 35 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 */; }; 36 37 1ADBEFE3130C6AA100D61D19 /* simple-accelerated-compositing.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 1ADBEFBC130C6A0100D61D19 /* simple-accelerated-compositing.html */; }; 37 38 1AEDE22613E5E7E700E62FE8 /* InjectedBundleControllerMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AEDE22413E5E7A000E62FE8 /* InjectedBundleControllerMac.mm */; }; … … 677 678 1AAD19F51C7CE20300831E47 /* Coding.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = Coding.mm; sourceTree = "<group>"; }; 678 679 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>"; }; 679 681 1ADBEFAD130C689C00D61D19 /* ForceRepaint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ForceRepaint.cpp; sourceTree = "<group>"; }; 680 682 1ADBEFBC130C6A0100D61D19 /* simple-accelerated-compositing.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "simple-accelerated-compositing.html"; sourceTree = "<group>"; }; … … 1452 1454 isa = PBXGroup; 1453 1455 children = ( 1456 1ADAD14E1D77A9F600212586 /* BlockPtr.mm */, 1454 1457 BC029B1B1486B25900817DA9 /* RetainPtr.mm */, 1455 1458 ); … … 2123 2126 7C83DED41D0A590C00FEBCF3 /* HashSet.cpp in Sources */, 2124 2127 7C83DEE01D0A590C00FEBCF3 /* IntegerToStringConversion.cpp in Sources */, 2128 1ADAD1501D77A9F600212586 /* BlockPtr.mm in Sources */, 2125 2129 7C83DEE81D0A590C00FEBCF3 /* ListHashSet.cpp in Sources */, 2126 2130 7C83DF1D1D0A590C00FEBCF3 /* Lock.cpp in Sources */,
Note: See TracChangeset
for help on using the changeset viewer.