MCPcopy Create free account
hub / github.com/boostorg/leaf / do_work

Function do_work

example/lua_callback_result.cpp:50–63  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

48// This causes the Lua interpreter to abort and pop back into the C++ code which
49// called it (see call_lua below).
50int 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
66std::shared_ptr<lua_State> init_lua_state()

Callers

nothing calls this directly

Calls 1

new_errorFunction · 0.85

Tested by

no test coverage detected