diff --git a/manual.lyx b/manual.lyx index fea684c0..7c1ae758 100644 --- a/manual.lyx +++ b/manual.lyx @@ -1817,6 +1817,53 @@ bus_address(number snesaddr) Returns virtual address corresponding to specified address on SNES bus. \end_layout +\begin_layout Subsubsection +loopwrapper(function fun, ...) +\end_layout + +\begin_layout Standard +Calls function fun with function and specified arguments. + The function passed suspends execution until the function returned is called. + Handy for linear flow control among multiple invocations of a hook. + Example code: +\end_layout + +\begin_layout LyX-Code +on_paint = loopwrapper(function(wait) +\end_layout + +\begin_deeper +\begin_layout LyX-Code +while true do +\end_layout + +\begin_deeper +\begin_layout LyX-Code +gui.text(0, 0, +\begin_inset Quotes eld +\end_inset + +Test! +\begin_inset Quotes erd +\end_inset + +); +\end_layout + +\begin_layout LyX-Code +wait(); +\end_layout + +\end_deeper +\begin_layout LyX-Code +end +\end_layout + +\end_deeper +\begin_layout LyX-Code +end); +\end_layout + \begin_layout Subsection Table bit: \end_layout diff --git a/manual.txt b/manual.txt index 443c85fa..cc472202 100644 --- a/manual.txt +++ b/manual.txt @@ -886,6 +886,25 @@ has expired, on_timer() will be called once. Returns virtual address corresponding to specified address on SNES bus. +8.1.8 loopwrapper(function fun, ...) + +Calls function fun with function and specified arguments. The +function passed suspends execution until the function returned is +called. Handy for linear flow control among multiple invocations +of a hook. Example code: + +on_paint = loopwrapper(function(wait) + + while true do + + gui.text(0, 0, “Test!”); + + wait(); + + end + +end); + 8.2 Table bit: Bitwise logical functions and related. diff --git a/src/lua/lua.cpp b/src/lua/lua.cpp index f9b0c597..2db257ca 100644 --- a/src/lua/lua.cpp +++ b/src/lua/lua.cpp @@ -19,6 +19,7 @@ extern "C" { uint64_t lua_idle_hook_time = 0x7EFFFFFFFFFFFFFFULL; uint64_t lua_timer_hook_time = 0x7EFFFFFFFFFFFFFFULL; +extern const char* lua_sysrc_script; namespace { @@ -338,6 +339,12 @@ namespace lua_setmetatable(L, -2); lua_setglobal(L, "_SYSTEM"); } + + void run_sysrc_lua(lua_State* L) + { + do_eval_lua(lua_sysrc_script); + } + } void lua_callback_do_paint(struct lua_render_context* ctx, bool non_synthetic) throw() @@ -552,6 +559,7 @@ void init_lua(bool soft) throw() luaL_openlibs(L); register_lua_functions(L); + run_sysrc_lua(L); copy_system_tables(L); } diff --git a/src/lua/sysrc.cpp b/src/lua/sysrc.cpp new file mode 100644 index 00000000..4e68ec9a --- /dev/null +++ b/src/lua/sysrc.cpp @@ -0,0 +1,18 @@ +const char* lua_sysrc_script = +"loopwrapper = function(fn, ...)\n" +" local routine = coroutine.create(fn);\n" +" local resume = function(...)\n" +" if coroutine.status(routine) ~= \"dead\" then\n" +" local x, y;\n" +" x, y = coroutine.resume(routine, ...);\n" +" if not x then\n" +" error(y);\n" +" end\n" +" end\n" +" end\n" +" local yield = function()\n" +" return coroutine.yield(routine);\n" +" end\n" +" resume(yield, ...);\n" +" return resume;\n" +"end\n"; \ No newline at end of file