Skip to content

Commit

Permalink
[MOD] Use unique userdata pointer as mark for lua-cpp-coroutine first…
Browse files Browse the repository at this point in the history
…-run state.
  • Loading branch information
paintdream committed May 20, 2023
1 parent 7e5b3e5 commit c88d6e9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
44 changes: 25 additions & 19 deletions src/optional/iris_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,27 @@ namespace iris {
};

template <typename... args_t>
auto refguard(args_t&... args) noexcept {
auto ref_guard(args_t&... args) noexcept {
return refguard_t<sizeof...(args_t)>(state, args...);
}

// a guard for checking stack balance
struct stack_guard_t {
#ifdef _DEBUG
stack_guard_t(lua_State* state, int offset = 0) noexcept : L(state) { top = lua_gettop(L) + offset; }
~stack_guard_t() noexcept { assert(top == lua_gettop(L)); }

lua_State* L;
int top;
#else
stack_guard_t(lua_State* state, int offset = 0) noexcept {}
#endif
};

auto stack_guard(int offset = 0) noexcept {
return stack_guard_t(state, offset);
}

template <typename type_t>
struct has_registar {
template <typename> static std::false_type test(...);
Expand Down Expand Up @@ -430,19 +447,6 @@ namespace iris {
}
}

// a guard for checking stack balance
struct stack_guard_t {
#ifdef _DEBUG
stack_guard_t(lua_State* state, int offset = 0) noexcept : L(state) { top = lua_gettop(L) + offset; }
~stack_guard_t() noexcept { assert(top == lua_gettop(L)); }

lua_State* L;
int top;
#else
stack_guard_t(lua_State* state, int offset = 0) noexcept {}
#endif
};

static void push_arguments(lua_State* L) {}
template <typename first_t, typename... args_t>
static void push_arguments(lua_State* L, first_t&& first, args_t&&... args) {
Expand Down Expand Up @@ -745,24 +749,25 @@ namespace iris {
} else {
// mark state
using return_t = typename coroutine_t::return_type_t;
int top = lua_gettop(L);
auto coroutine = function(std::forward<params_t>(params)...);

if constexpr (!std::is_void_v<return_t>) {
coroutine.complete([L](return_t&& value) {
coroutine.complete([L, p = static_cast<void*>(&coroutine)](return_t&& value) {
push_variable(L, std::move(value));
push_variable(L, p);
coroutine_continuation(L);
}).run();
} else {
coroutine.complete([L]() {
coroutine.complete([L, p = static_cast<void*>(&coroutine)]() {
lua_pushnil(L);
push_variable(L, p);
coroutine_continuation(L);
}).run();
}

// already completed?
if (lua_gettop(L) != top) {
assert(lua_gettop(L) == top + 1);
if (lua_touserdata(L, -1) == &coroutine) {
lua_pop(L, 1);
return true;
} else {
return false;
Expand All @@ -772,6 +777,7 @@ namespace iris {

static void coroutine_continuation(lua_State* L) {
if (lua_status(L) == LUA_YIELD) {
lua_pop(L, 1);
#if LUA_VERSION_NUM <= 501
int ret = lua_resume(L, 1);
#elif LUA_VERSION_NUM <= 503
Expand Down
2 changes: 1 addition & 1 deletion test/iris_lua_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct example_t {
}

void join_value_refptr(iris_lua_t&& lua, iris_lua_t::refptr_t<example_t>&& rhs) noexcept {
auto guard = lua.refguard(rhs);
auto guard = lua.ref_guard(rhs);
if (rhs != nullptr) {
value += rhs->value;
}
Expand Down

0 comments on commit c88d6e9

Please sign in to comment.