AK: Support storing blocks in AK::Function

This has two slightly different implementations for ARC and non-ARC
compiler modes. The main idea is to store a block pointer as our
closure and use either ARC magic or BlockRuntime methods to manage
the memory for the block. Things are complicated by the fact that
we don't yet force-enable swift, so we can't count on the swift.org
llvm fork being our compiler toolchain. The patch adds some CMake
checks and ifdefs to still support environments without support
for blocks or ARC.
This commit is contained in:
Andrew Kaster 2025-03-16 17:35:38 -06:00 committed by Andrew Kaster
commit 01ac48b36f
Notes: github-actions[bot] 2025-03-18 23:16:13 +00:00
5 changed files with 287 additions and 11 deletions

View file

@ -45,3 +45,22 @@ serenity_option(ENABLE_STD_STACKTRACE OFF CACHE BOOL "Force use of std::stacktra
if (ENABLE_SWIFT)
include(${CMAKE_CURRENT_LIST_DIR}/Swift/swift-settings.cmake)
endif()
include(CheckCXXSourceCompiles)
set(BLOCKS_REQUIRED_LIBRARIES "")
if (NOT APPLE)
find_package(BlocksRuntime)
if (BlocksRuntime_FOUND)
set(BLOCKS_REQUIRED_LIBRARIES BlocksRuntime::BlocksRuntime)
set(CMAKE_REQUIRED_LIBRARIES BlocksRuntime::BlocksRuntime)
endif()
endif()
check_cxx_source_compiles([=[
int main() { __block int x = 0; auto b = ^{++x;}; b(); }
]=] CXX_COMPILER_SUPPORTS_BLOCKS)
set(CMAKE_REQUIRED_FLAGS "-fobjc-arc")
check_cxx_source_compiles([=[
int main() { auto b = ^{}; auto __weak w = b; w(); }
]=] CXX_COMPILER_SUPPORTS_OBJC_ARC)
unset(CMAKE_REQUIRED_FLAGS)