Skip to content

Commit bfad24c

Browse files
authored
Support for Lua 5.4+ & C++17 (#111)
* Update checkout action on GHA CI * Show output on test failure * Fix test compat with C++17 * Add missing header * Disable warning in GCC 13 * Fix compatibility with Lua 5.4 The couroutine result index is always 1, not dependent on the number of results which is not available in < 5.4. Errors during GC are shown as warnings in 5.4+ * Add Lua 5.4.7 to the test matrix * Fix compatibility with Lua 5.4.4+ A change in Lua 5.4.4 resets the Lua status to OK before calling the panic handler. This affects a test checking for an out-of-memory exception. Check the message string instead for something memory related which is "good enough".
1 parent ef96070 commit bfad24c

File tree

7 files changed

+53
-10
lines changed

7 files changed

+53
-10
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
os: [ubuntu-latest]
18-
luaversion: [lua-5.1.5,lua-5.2.4,lua-5.3.6,lua-5.4.3]
18+
luaversion: [lua-5.1.5,lua-5.2.4,lua-5.3.6,lua-5.4.3,lua-5.4.7]
1919
fail-fast: false
2020
# The CMake configure and build commands are platform agnostic and should work equally
2121
# well on Windows or Mac. You can convert this to a matrix build if you need
@@ -24,7 +24,7 @@ jobs:
2424
runs-on: ${{ matrix.os }}
2525

2626
steps:
27-
- uses: actions/checkout@v2
27+
- uses: actions/checkout@v4
2828
- name: Download Lua
2929
run: wget https://www.lua.org/ftp/${{ matrix.luaversion }}.tar.gz && tar zxf ${{ matrix.luaversion }}.tar.gz
3030
- name: Configure CMake
@@ -40,5 +40,5 @@ jobs:
4040
working-directory: ${{github.workspace}}/build
4141
# Execute tests defined by the CMake configuration.
4242
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
43-
run: ctest -C ${{env.BUILD_TYPE}}
43+
run: ctest --output-on-failure -C ${{env.BUILD_TYPE}}
4444

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ if(NOT MSVC)#-Wall nonsense on MSVC
4545
add_definitions(-pedantic)
4646
add_definitions(-Wno-variadic-macros)
4747
add_definitions ("-Wno-unused-local-typedefs")
48+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13)
49+
add_definitions(-Wno-dangling-reference)
50+
endif()
4851
add_definitions ("-Wno-unknown-warning-option")
4952
#add_definitions("-std=c++11")
5053
endif(NOT MSVC)

include/kaguya/detail/lua_function_def.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ template <typename Derived> class LuaThreadImpl {
165165
int nresult = 1;
166166
int result = lua_resume(thread, state, argnum, &nresult);
167167
except::checkErrorAndThrow(result, thread);
168-
return detail::FunctionResultProxy::ReturnValue(thread, result, nresult - (lua_gettop(state) + 1),
169-
types::typetag<Result>());
168+
assert(nresult == lua_gettop(thread)); // All results are on top of the thread stack
169+
return detail::FunctionResultProxy::ReturnValue(thread, result, 1, types::typetag<Result>());
170170
}
171171
template <class... Args> FunctionResults operator()(Args &&... args);
172172
#else
@@ -197,8 +197,7 @@ template <typename Derived> class LuaThreadImpl {
197197
int nresult = 1; \
198198
int result = lua_resume(thread, state, argnum, &nresult); \
199199
except::checkErrorAndThrow(result, thread); \
200-
return detail::FunctionResultProxy::ReturnValue(thread, result, nresult - (lua_gettop(state) + 1), \
201-
types::typetag<Result>()); \
200+
return detail::FunctionResultProxy::ReturnValue(thread, result, 1, types::typetag<Result>()); \
202201
}
203202

204203
KAGUYA_RESUME_DEF(0)

test/test_01_primitive.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "kaguya/kaguya.hpp"
22
#include <limits>
33
#include "test_util.hpp"
4+
#include <limits>
45

56
KAGUYA_TEST_GROUP_START(test_01_primitive)
67

test/test_04_lua_function.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ KAGUYA_TEST_FUNCTION_DEF(coroutine)(kaguya::State &state) {
121121
cor2.resume<void>(corfun, 3);
122122
}
123123
{
124-
125124
state["cor2"] = kaguya::NewThread();
126125
kaguya::LuaRef cor2 = state["cor2"];
127126
TEST_CHECK(state("corfun = function(arg)"
@@ -137,6 +136,40 @@ KAGUYA_TEST_FUNCTION_DEF(coroutine)(kaguya::State &state) {
137136
yieldnum++;
138137
}
139138

139+
TEST_EQUAL(yieldnum, 10);
140+
}
141+
{
142+
// Mix yielding and receiving params
143+
state["cor3"] = kaguya::NewThread();
144+
kaguya::LuaRef cor2 = state["cor3"];
145+
TEST_CHECK(state("corfun = function(arg)"
146+
"for i = 1,arg do "
147+
"j, j1 = coroutine.yield() "
148+
"k = coroutine.yield(j) "
149+
"coroutine.yield(k, j1) "
150+
"end "
151+
"end"));
152+
kaguya::LuaRef corfun = state["corfun"];
153+
kaguya::FunctionResults res0 = cor2(corfun, 10);
154+
TEST_EQUAL(res0.size(), 0u); // 1st yield
155+
int yieldnum = 0;
156+
while (cor2.threadStatus() == LUA_YIELD) {
157+
const int j = yieldnum * 2 + 3;
158+
const int j1 = j + 7;
159+
int res_j = cor2(j, j1);
160+
TEST_EQUAL(res_j, j)
161+
const int k = j + 5;
162+
const kaguya::FunctionResults res_k_j1 = cor2(k);
163+
TEST_EQUAL(res_k_j1.result_size(), 2);
164+
const int res_k = res_k_j1.result_at(0);
165+
const int res_j1 = res_k_j1.result_at(1);
166+
TEST_EQUAL(res_k, k);
167+
TEST_EQUAL(res_j1, j1);
168+
kaguya::FunctionResults res_empty = cor2(); // First in loop or end
169+
TEST_EQUAL(res_empty.size(), 0u);
170+
yieldnum++;
171+
}
172+
140173
TEST_EQUAL(yieldnum, 10);
141174
}
142175
}

test/test_06_state.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,13 @@ KAGUYA_TEST_FUNCTION_DEF(allocation_error_test)(kaguya::State &) {
366366
} catch (const kaguya::LuaMemoryError &) {
367367
continue;
368368
}
369+
#if LUA_VERSION_RELEASE_NUM >= 50404
370+
// In Lua 5.4.4+ we don't get a specific error status for Lua panics
371+
catch (const kaguya::LuaUnknownError & e) {
372+
if (std::string(e.what()).find("memory") != std::string::npos)
373+
continue;
374+
}
375+
#endif
369376
TEST_CHECK(false);
370377
}
371378
}
@@ -472,7 +479,7 @@ KAGUYA_TEST_FUNCTION_DEF(this_typemismatch_error_test)(kaguya::State &state) {
472479
}
473480
}
474481

475-
#if LUA_VERSION_NUM >= 502
482+
#if LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM < 504
476483
KAGUYA_TEST_FUNCTION_DEF(gc_error_throw_test)(kaguya::State &) {
477484
bool catch_except = false;
478485
try {

test/test_util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ struct TestClass {
216216
};
217217
}
218218

219-
#if KAGUYA_USE_CPP11
219+
#if KAGUYA_USE_CPP11 && (__cplusplus < 201703L)
220220
inline std::ostream &operator<<(std::ostream &os, std::nullptr_t) {
221221
return os << "nullptr";
222222
}

0 commit comments

Comments
 (0)