From 143f6c4508dc71e24ac0645952f97734e7f29d52 Mon Sep 17 00:00:00 2001 From: Vas Crabb Date: Fri, 17 Nov 2023 02:56:56 +1100 Subject: luaengine.cpp: Restored ability for wait functions to yield values from coroutines. Made the documentation on what it's supposed to do a bit clearer. --- docs/source/luascript/ref-common.rst | 20 +++++++++++--------- src/frontend/mame/luaengine.cpp | 10 +++++++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/docs/source/luascript/ref-common.rst b/docs/source/luascript/ref-common.rst index 85bd580a144..c08d3676621 100644 --- a/docs/source/luascript/ref-common.rst +++ b/docs/source/luascript/ref-common.rst @@ -58,21 +58,23 @@ classes are also available as properties of the emulator interface. Methods ~~~~~~~ -emu.wait(duration) +emu.wait(duration, …) Yields for the specified duration in terms of emulated time. The duration may be specified as an :ref:`attotime ` or a number - in seconds. Returns a Boolean indicating whether the duration expired - normally. + in seconds. Any additional arguments are returned from the coroutine. + Returns a Boolean indicating whether the duration expired normally. All outstanding calls to ``emu.wait`` will return ``false`` immediately if a saved state is loaded or the emulation session ends. Calling this function from callbacks that are not run as coroutines will raise an error. -emu.wait_next_update() - Yields until the next video/UI update. Calling this function from callbacks - that are not run as coroutines will raise an error. -emu.wait_next_frame() - Yields until the next emulated frame completes. Calling this function from - callbacks that are not run as coroutines will raise an error. +emu.wait_next_update(…) + Yields until the next video/UI update. Any arguments are returned from the + coroutine. Calling this function from callbacks that are not run as coroutines + will raise an error. +emu.wait_next_frame(…) + Yields until the next emulated frame completes. Any arguments are returned + from the coroutine. Calling this function from callbacks that are not run as + coroutines will raise an error. emu.add_machine_reset_notifier(callback) Add a callback to receive notifications when the emulated system is reset. Returns a :ref:`notifier subscription `. diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index 9a570b1803a..022a85b2456 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -815,7 +815,7 @@ void lua_engine::initialize() sol::table emu = sol().create_named_table("emu"); emu["wait"] = sol::yielding( - [this] (sol::this_state s, sol::object duration) + [this] (sol::this_state s, sol::object duration, sol::variadic_args args) { attotime delay; if (!duration) @@ -848,22 +848,26 @@ void lua_engine::initialize() if (m_waiting_tasks.begin() == pos) m_timer->reset(delay); m_waiting_tasks.emplace(pos, expiry, ref); + + return sol::variadic_results(args.begin(), args.end()); }); emu["wait_next_update"] = sol::yielding( - [this] (sol::this_state s) + [this] (sol::this_state s, sol::variadic_args args) { int const ret = lua_pushthread(s); if (ret == 1) luaL_error(s, "cannot wait from outside coroutine"); m_update_tasks.emplace_back(luaL_ref(s, LUA_REGISTRYINDEX)); + return sol::variadic_results(args.begin(), args.end()); }); emu["wait_next_frame"] = sol::yielding( - [this] (sol::this_state s) + [this] (sol::this_state s, sol::variadic_args args) { int const ret = lua_pushthread(s); if (ret == 1) luaL_error(s, "cannot wait from outside coroutine"); m_frame_tasks.emplace_back(luaL_ref(s, LUA_REGISTRYINDEX)); + return sol::variadic_results(args.begin(), args.end()); }); emu.set_function("add_machine_reset_notifier", make_notifier_adder(m_notifiers->on_reset, "machine reset")); emu.set_function("add_machine_stop_notifier", make_notifier_adder(m_notifiers->on_stop, "machine stop")); -- cgit v1.2.3