mirror of
https://github.com/boostorg/leaf.git
synced 2026-01-29 07:32:33 +00:00
Uniform wrapping for multi-line comments
This commit is contained in:
@@ -28,10 +28,11 @@ struct e_lua_pcall_error { int value; };
|
||||
struct e_lua_error_message { std::string value; };
|
||||
|
||||
|
||||
// This is a C callback with a specific signature, callable from programs written in Lua.
|
||||
// If it succeeds, it returns an int answer, by pushing it onto the Lua stack. But "sometimes"
|
||||
// it fails, in which case it throws an exception. This causes the Lua interpreter to abort and
|
||||
// pop back into the C++ code which called it (see call_lua below).
|
||||
// This is a C callback with a specific signature, callable from programs
|
||||
// written in Lua. If it succeeds, it returns an int answer, by pushing it onto
|
||||
// the Lua stack. But "sometimes" it fails, in which case it throws an
|
||||
// exception. This causes the Lua interpreter to abort and pop back into the C++
|
||||
// code which called it (see call_lua below).
|
||||
int do_work( lua_State * L )
|
||||
{
|
||||
bool success = rand()%2; // "Sometimes" do_work fails.
|
||||
@@ -53,12 +54,13 @@ std::shared_ptr<lua_State> init_lua_state()
|
||||
std::shared_ptr<lua_State> L(lua_open(), &lua_close);
|
||||
|
||||
// Register the do_work function (above) as a C callback, under the global
|
||||
// Lua name "do_work". With this, calls from Lua programs to do_work
|
||||
// will land in the do_work C function we've registered.
|
||||
// Lua name "do_work". With this, calls from Lua programs to do_work will
|
||||
// land in the do_work C function we've registered.
|
||||
lua_register( &*L, "do_work", &do_work );
|
||||
|
||||
// Pass some Lua code as a C string literal to Lua. This creates a global Lua
|
||||
// function called "call_do_work", which we will later ask Lua to execute.
|
||||
// Pass some Lua code as a C string literal to Lua. This creates a global
|
||||
// Lua function called "call_do_work", which we will later ask Lua to
|
||||
// execute.
|
||||
luaL_dostring( &*L, "\
|
||||
\n function call_do_work()\
|
||||
\n return do_work()\
|
||||
@@ -72,8 +74,8 @@ std::shared_ptr<lua_State> init_lua_state()
|
||||
// in Lua, and returns the value from do_work, which is written in C++ and
|
||||
// registered with the Lua interpreter as a C callback.
|
||||
|
||||
// If do_work succeeds, we return the resulting int answer.
|
||||
// If it fails, we'll communicate that failure to our caller.
|
||||
// If do_work succeeds, we return the resulting int answer. If it fails, we'll
|
||||
// communicate that failure to our caller.
|
||||
int call_lua( lua_State * L )
|
||||
{
|
||||
// Ask the Lua interpreter to call the global Lua function call_do_work.
|
||||
@@ -83,9 +85,9 @@ int call_lua( lua_State * L )
|
||||
auto load = leaf::on_error(e_lua_error_message{lua_tostring(L, 1)});
|
||||
lua_pop(L,1);
|
||||
|
||||
// We got a Lua error that is definitely not the error we're throwing in do_work.
|
||||
// (if it did throw an exception, we won't be here).
|
||||
// Throw a new exception to indicate that lua_pcall returned an error.
|
||||
// We got a Lua error that is definitely not the error we're throwing in
|
||||
// do_work. (if it did throw an exception, we won't be here). Throw a
|
||||
// new exception to indicate that lua_pcall returned an error.
|
||||
throw leaf::exception(e_lua_pcall_error{err});
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user