If do_work succeeds, we return the resulting int answer. If it fails, we'll communicate that failure to our caller.
| 92 | // If do_work succeeds, we return the resulting int answer. If it fails, we'll |
| 93 | // communicate that failure to our caller. |
| 94 | leaf::result<int> call_lua( lua_State * L ) |
| 95 | { |
| 96 | leaf::error_monitor cur_err; |
| 97 | |
| 98 | // Ask the Lua interpreter to call the global Lua function call_do_work. |
| 99 | lua_getfield( L, LUA_GLOBALSINDEX, "call_do_work" ); |
| 100 | if( int err = lua_pcall(L, 0, 1, 0) ) // Ask Lua to call the global function call_do_work. |
| 101 | { |
| 102 | std::string msg = lua_tostring(L, 1); |
| 103 | lua_pop(L, 1); |
| 104 | |
| 105 | // We got a Lua error which may be the error we're reporting from |
| 106 | // do_work, or some other error. If it is another error, |
| 107 | // cur_err.assigned_error_id() will return a new leaf::error_id, |
| 108 | // otherwise we'll be working with the original value returned by |
| 109 | // leaf::new_error in do_work. |
| 110 | return cur_err.assigned_error_id().load( e_lua_pcall_error{err}, e_lua_error_message{std::move(msg)} ); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | // Success! Just return the int answer. |
| 115 | int answer = lua_tonumber(L, -1); |
| 116 | lua_pop(L, 1); |
| 117 | return answer; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | int main() |
| 122 | { |
no test coverage detected