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 calls luaL_error. This causes the Lua interpreter to abort and pop back into the C++ code which called it (see call_lua below).
| 48 | // This causes the Lua interpreter to abort and pop back into the C++ code which |
| 49 | // called it (see call_lua below). |
| 50 | int do_work( lua_State * L ) |
| 51 | { |
| 52 | bool success = rand() % 2; // "Sometimes" do_work fails. |
| 53 | if( success ) |
| 54 | { |
| 55 | lua_pushnumber(L, 42); // Success, push the result on the Lua stack, return to Lua. |
| 56 | return 1; |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | (void) leaf::new_error(ec1); |
| 61 | return luaL_error(L,"do_work_error"); // luaL_error does not return (longjmp). |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | std::shared_ptr<lua_State> init_lua_state() |
nothing calls this directly
no test coverage detected