Skip to content

Commit b61dcfa

Browse files
authored
C++20 Coroutines support (#218)
1 parent b551fc4 commit b61dcfa

18 files changed

Lines changed: 2274 additions & 90 deletions

Benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmake_minimum_required(VERSION 3.10)
2+
cmake_policy (SET CMP0169 OLD)
23

34
include(FetchContent)
45

Benchmarks/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
matplotlib
2+
numpy

CHANGES.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
## Master
2-
31
## Version 3.0
42

53
* Moved to C++17 as minimum supported standard C++ version.
@@ -17,6 +15,11 @@
1715
* Allow specifying a non virtual base class method when declaring class members (functions or variables) not exposed in the inherited class.
1816
* Allow using capturing lambdas in `Namespace::addFunction`, `Namespace::addProperty`, `Class<T>::addFunction`, `Class<T>::addStaticFunction`, `Class<T>::addProperty` and `Class<T>::addStaticProperty`.
1917
* Added multiple inheritance support: `deriveClass` now accepts more than one registered base class (e.g. `deriveClass<D, A, B>`).
18+
* Added C++20 coroutine integration: `CppCoroutine<R>` type that can be registered via `Namespace::addCoroutine()` to expose C++ generators to Lua using `co_yield` and `co_return`.
19+
* Added `LuaCoroutine` awaitable to resume a child Lua thread synchronously from inside a `CppCoroutine` body using `co_await`.
20+
* Added `lua_resume_x` and `lua_isyieldable_x` portable helpers in `LuaBridge/detail/LuaHelpers.h`.
21+
* Added `LUABRIDGE_HAS_CXX20_COROUTINES` feature-detection macro; opt out with `LUABRIDGE_DISABLE_CXX20_COROUTINES`.
22+
* Added `ErrorCode::CoroutineYieldFromNonCoroutine` and `ErrorCode::CoroutineAlreadyDone` error codes.
2023
* Added `Namespace::addVariable` to allow adding a modifiable value by copy into the namespace without incurring in function calls or metatables generation.
2124
* Added `luabridge::callWithHandler` free function and `LuaRef::callWithHandler` member to provide a custom Lua message handler during `lua_pcall`.
2225
* Added `luabridge::newFunction` free function and `LuaRef::newFunction` static method to wrap any C++ callable into a Lua function exposed as a `LuaRef`.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project (LuaBridge)
44

55
include (CMakeDependentOption)
66

7-
set (CMAKE_CXX_STANDARD 17)
7+
set (CMAKE_CXX_STANDARD 20)
88
set (CMAKE_CXX_STANDARD_REQUIRED ON)
99
set (CMAKE_CXX_EXTENSIONS OFF)
1010

Images/benchmarks.png

-8.6 KB
Loading

Manual.md

Lines changed: 256 additions & 6 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
[LuaBridge3](https://github.com/kunitoki/LuaBridge3) is a lightweight and dependency-free library for mapping data,
3636
functions, and classes back and forth between C++ and [Lua](http://wwww.lua.org) (a powerful,
3737
fast, lightweight, embeddable scripting language). LuaBridge has been tested
38-
and works with Lua 5.1.5, 5.2.4, 5.3.6, 5.4.8 and 5.5.0 as well as [LuaJit](https://luajit.org/) 2.1 onwards
39-
and for the first time also with [Luau](https://luau-lang.org/) 0.713 onwards and [Ravi](https://github.com/dibyendumajumdar/ravi) 1.0-beta11.
38+
and works with:
39+
* [PUC-Lua](https://lua.org) 5.1.5, 5.2.4, 5.3.6, 5.4.8 and 5.5.0
40+
* [LuaJit](https://luajit.org/) 2.1
41+
* [Luau](https://luau-lang.org/) 0.713
42+
* [Ravi](https://github.com/dibyendumajumdar/ravi) 1.0-beta11
4043

4144
## Features
4245

@@ -87,6 +90,7 @@ LuaBridge3 offers a set of improvements compared to vanilla LuaBridge:
8790
* Consistent numeric handling and conversions (signed, unsigned and floats) across all lua versions.
8891
* NaN and Inf values pass through floating-point stack conversions without error.
8992
* Simplified registration of enum types via the `luabridge::Enum` stack wrapper.
93+
* C++20 coroutine integration via `addCoroutine()` and `CppCoroutine<R>`; await Lua threads from C++ with `LuaCoroutine`.
9094
* Opt-out handling of safe stack space checks (automatically avoids exhausting lua stack space when pushing values!).
9195
* Optional strict stack conversions via `LUABRIDGE_STRICT_STACK_CONVERSIONS` (e.g. `bool` requires an actual boolean, not any truthy value).
9296
* Error handler support in Lua calls via `LuaRef::callWithHandler` and `luabridge::callWithHandler`.

Source/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set (LUABRIDGE_DETAIL_HEADERS
1515
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/CFunctions.h
1616
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/ClassInfo.h
1717
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Config.h
18+
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Coroutine.h
1819
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Enum.h
1920
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Errors.h
2021
${CMAKE_CURRENT_SOURCE_DIR}/LuaBridge/detail/Expected.h

Source/LuaBridge/LuaBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "detail/CFunctions.h"
2020
#include "detail/ClassInfo.h"
21+
#include "detail/Coroutine.h"
2122
#include "detail/Enum.h"
2223
#include "detail/Errors.h"
2324
#include "detail/Expected.h"

Source/LuaBridge/detail/Config.h

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@
1212
#error LuaBridge 3 requires a compliant C++17 compiler, or C++17 has not been enabled !
1313
#endif
1414

15+
#if defined(LUAU_FASTMATH_BEGIN)
16+
#define LUABRIDGE_ON_LUAU 1
17+
#elif defined(LUAJIT_VERSION)
18+
#define LUABRIDGE_ON_LUAJIT 1
19+
#elif defined(RAVI_OPTION_STRING2)
20+
#define LUABRIDGE_ON_RAVI 1
21+
#elif defined(LUA_VERSION_NUM)
22+
#define LUABRIDGE_ON_LUA 1
23+
#else
24+
#error "Lua headers must be included prior to LuaBridge ones"
25+
#endif
26+
27+
/**
28+
* @brief Enable C++20 coroutine integration with Lua coroutines.
29+
*
30+
* Requires C++20 and Lua 5.2+ (lua_yieldk). Not supported on Lua 5.1, LuaJIT, or Luau.
31+
* Define LUABRIDGE_DISABLE_CXX20_COROUTINES to force-disable even when C++20 is available.
32+
*/
33+
#if !defined(LUABRIDGE_HAS_CXX20_COROUTINES)
34+
#if !defined(LUABRIDGE_DISABLE_CXX20_COROUTINES) && (__cplusplus >= 202002L || (defined(_MSC_VER) && _HAS_CXX20)) && !(LUABRIDGE_ON_LUAU || LUABRIDGE_ON_LUAJIT || LUABRIDGE_ON_RAVI || LUA_VERSION_NUM < 502)
35+
#define LUABRIDGE_HAS_CXX20_COROUTINES 1
36+
#else
37+
#define LUABRIDGE_HAS_CXX20_COROUTINES 0
38+
#endif
39+
#endif
40+
1541
#if !defined(LUABRIDGE_HAS_EXCEPTIONS)
1642
#if defined(_MSC_VER)
1743
#if _CPPUNWIND || _HAS_EXCEPTIONS
@@ -48,18 +74,6 @@
4874
#define LUABRIDGE_NO_SANITIZE(x)
4975
#endif
5076

51-
#if defined(LUAU_FASTMATH_BEGIN)
52-
#define LUABRIDGE_ON_LUAU 1
53-
#elif defined(LUAJIT_VERSION)
54-
#define LUABRIDGE_ON_LUAJIT 1
55-
#elif defined(RAVI_OPTION_STRING2)
56-
#define LUABRIDGE_ON_RAVI 1
57-
#elif defined(LUA_VERSION_NUM)
58-
#define LUABRIDGE_ON_LUA 1
59-
#else
60-
#error "Lua headers must be included prior to LuaBridge ones"
61-
#endif
62-
6377
#if defined(__OBJC__)
6478
#define LUABRIDGE_ON_OBJECTIVE_C 1
6579
#endif

0 commit comments

Comments
 (0)