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

Function call_lua

example/lua_callback_result.cpp:94–119  ·  view source on GitHub ↗

If do_work succeeds, we return the resulting int answer. If it fails, we'll communicate that failure to our caller.

Source from the content-addressed store, hash-verified

92// If do_work succeeds, we return the resulting int answer. If it fails, we'll
93// communicate that failure to our caller.
94leaf::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
121int main()
122{

Callers 1

mainFunction · 0.70

Calls 2

loadMethod · 0.80
assigned_error_idMethod · 0.80

Tested by

no test coverage detected