Lua: loopwarapper

This commit is contained in:
Ilari Liusvaara 2012-09-17 22:36:10 +03:00
parent 91f436344d
commit d6a17bde89
4 changed files with 92 additions and 0 deletions

View file

@ -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

View file

@ -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.

View file

@ -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);
}

18
src/lua/sysrc.cpp Normal file
View file

@ -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";