Taskolib  1.3.3
lua_details.h
Go to the documentation of this file.
1 
23 // SPDX-License-Identifier: LGPL-2.1-or-later
24 
25 #ifndef TASKOLIB_LUA_DETAILS_H_
26 #define TASKOLIB_LUA_DETAILS_H_
27 
28 #include <chrono>
29 #include <functional>
30 #include <string>
31 #include <variant>
32 
33 #include "sol/sol.hpp"
34 #include "taskolib/CommChannel.h"
35 #include "taskolib/Context.h"
37 
38 namespace task {
39 
40 // Check that the lua lib has been build with the expected types
41 static_assert(std::is_same<LuaFloat, double>::value, "Unexpected Lua-internal floating point type");
42 static_assert(std::is_same<LuaInteger, long long>::value, "Unexpected Lua-internal integer type");
43 
44 // Abort the execution of the script by raising a Lua error with the given error message.
45 void abort_script_with_error(lua_State* lua_state, const std::string& msg);
46 
47 // Check if immediate termination has been requested via the CommChannel. If so, raise a
48 // Lua error.
49 void check_immediate_termination_request(lua_State* lua_state);
50 
51 // Check if the step timeout has expired and raise a Lua error if that is the case.
52 void check_script_timeout(lua_State* lua_state);
53 
60 CommChannel* get_comm_channel_ptr_from_registry(lua_State* lua_state);
61 
68 const Context& get_context_from_registry(lua_State* lua_state);
69 
81 OptionalStepIndex get_step_idx_from_registry(lua_State* lua_state);
82 
83 // Return a time point in milliseconds since the epoch, calculated from a time point t0
84 // plus a duration dt. In case of overflow, the maximum representable time point is
85 // returned.
86 LuaInteger get_ms_since_epoch(TimePoint t0, std::chrono::milliseconds dt);
87 
88 // A Lua hook that stops the execution of the script by raising a Lua error.
89 // This hook reinstalls itself so that it is called immediately if the execution should
90 // resume. This helps to break out of pcalls.
91 void hook_abort_with_error(lua_State* lua_state, lua_Debug*);
92 
93 // Check if the step timeout has expired or if immediate termination has been requested
94 // via the comm channel. If so, raise a Lua error.
95 void hook_check_timeout_and_termination_request(lua_State* lua_state, lua_Debug*);
96 
105 void install_custom_commands(sol::state& lua);
106 
107 // Install hooks that check for timeouts and immediate termination requests while a Lua
108 // script is being executed. If one of both occurs, the script terminates with an error
109 // message that contains the abort marker.
111  std::chrono::milliseconds timeout, OptionalStepIndex step_idx, const Context& context,
112  CommChannel* comm_channel, TimeoutTrigger* sequence_timeout);
113 
114 // Open a safe subset of the Lua standard libraries in the given Lua state.
115 //
116 // This opens the math, string, table, and UTF8 libraries. The base library is also
117 // opened, but the following potentially dangerous commands are removed:
118 // \code
119 // collectgarbage, debug, dofile, load, loadfile, print, require
120 // \endcode
121 void open_safe_library_subset(sol::state& lua);
122 
123 // An equivalent to Lua's print() function that stringifies and concatenates its arguments
124 // and finally sends a message of type Message::Type::output with the result.
125 void print_fct(sol::this_state, sol::variadic_args);
126 
127 // Pause execution for the specified time, observing timeouts and termination requests.
128 void sleep_fct(double seconds, sol::this_state sol);
129 
130 } // namespace task
131 
132 #endif
Declaration of the CommChannel struct.
Declaration of the Context and VariableValue types.
Logic to check if a timeout elapsed.
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
void open_safe_library_subset(sol::state &lua)
Definition: lua_details.cc:239
OptionalStepIndex get_step_idx_from_registry(lua_State *lua_state)
Get the index of the currently executed Step from the Lua registry.
Definition: lua_details.cc:160
void install_custom_commands(sol::state &lua)
Install implementations for some custom functions in the given Lua state.
Definition: lua_details.cc:215
void sleep_fct(double seconds, sol::this_state sol)
Definition: lua_details.cc:282
void abort_script_with_error(lua_State *lua_state, const std::string &msg)
Definition: lua_details.cc:59
LuaInteger get_ms_since_epoch(TimePoint t0, std::chrono::milliseconds dt)
Definition: lua_details.cc:176
void hook_abort_with_error(lua_State *lua_state, lua_Debug *)
Definition: lua_details.cc:205
void print_fct(sol::this_state sol, sol::variadic_args va)
Definition: lua_details.cc:258
LUA_INTEGER LuaInteger
The integer type used by the Lua interpreter.
Definition: Context.h:47
void check_script_timeout(lua_State *lua_state)
Definition: lua_details.cc:92
void install_timeout_and_termination_request_hook(sol::state &lua, TimePoint now, std::chrono::milliseconds timeout, OptionalStepIndex step_idx, const Context &context, CommChannel *comm_channel, TimeoutTrigger *sequence_timeout)
Definition: lua_details.cc:223
gul14::optional< StepIndex > OptionalStepIndex
An optional step index (gul14::optional<StepIndex>).
Definition: StepIndex.h:37
CommChannel * get_comm_channel_ptr_from_registry(lua_State *lua_state)
Retrieve a pointer to the used CommChannel from the Lua registry.
Definition: lua_details.cc:134
void check_immediate_termination_request(lua_State *lua_state)
Definition: lua_details.cc:74
const Context & get_context_from_registry(lua_State *lua_state)
Retrieve a reference to the used Context from the Lua registry.
Definition: lua_details.cc:146
std::chrono::time_point< Clock > TimePoint
Definition: time_types.h:34
void hook_check_timeout_and_termination_request(lua_State *lua_state, lua_Debug *)
Definition: lua_details.cc:196