24 if (not fileName.empty())
29 for (
int i = 1; i <= argc; i++)
32 lua_pushstring(L, argv[i - 1]);
35 lua_setglobal(L,
"chiArgs");
52 int number_of_processes)
const
54 lua_State* L = this->console_state_;
56 lua_pushinteger(L, location_id);
57 lua_setglobal(L,
"chi_location_id");
59 lua_pushinteger(L, number_of_processes);
60 lua_setglobal(L,
"chi_number_of_processes");
67 lua_CFunction function_ptr)
69 auto& console = GetInstance();
72 if (console.lua_function_registry_.count(name_in_lua) > 0)
74 const auto& current_entry = console.lua_function_registry_.at(name_in_lua);
76 throw std::logic_error(std::string(__PRETTY_FUNCTION__) +
78 "to register lua function \"" +
80 "\" but the function "
81 "is already taken by " +
82 current_entry.function_raw_name);
85 console.lua_function_registry_.insert(std::make_pair(
96 const std::string& raw_name_in_lua, lua_CFunction function_ptr)
99 const std::string name_in_lua =
102 AddFunctionToRegistry(name_in_lua, function_ptr);
113 lua_CFunction function_ptr,
114 const std::string& namespace_name,
115 const std::string& function_name)
117 const std::string name_in_lua = namespace_name +
"::" + function_name;
119 AddFunctionToRegistry(name_in_lua, function_ptr);
128 const std::string& namespace_name,
129 const std::string& constant_name,
132 const std::string name_in_lua = namespace_name +
"::" + constant_name;
136 if (console.lua_constants_registry_.count(name_in_lua) > 0)
138 throw std::logic_error(std::string(__PRETTY_FUNCTION__) +
140 "to register lua const \"" +
143 "is already taken.");
146 console.lua_constants_registry_.insert(std::make_pair(name_in_lua, value));
160 const std::string& namespace_name,
161 const std::string& name_in_lua,
164 bool ignore_null_call_func )
166 const std::string name = (namespace_name.empty())
168 : namespace_name +
"::" + name_in_lua;
170 auto& console = GetInstance();
171 auto& registry = console.function_wrapper_registry_;
174 registry.count(name) > 0,
175 std::string(
"Attempted to register lua-function wrapper \"") + name +
176 "\" but a wrapper with the same name already exists");
178 if (not syntax_function) syntax_function = DefaultGetInParamsFunc;
180 if (not ignore_null_call_func)
187 registry.insert(std::make_pair(name, reg_entry));
195 const std::string& full_lua_name, lua_CFunction function_ptr)
197 auto L = GetInstance().console_state_;
200 if (lua_name_split.size() == 1)
202 lua_pushcfunction(L, function_ptr);
203 lua_setglobal(L, lua_name_split.back().c_str());
207 const std::vector<std::string> table_names(lua_name_split.begin(),
208 lua_name_split.end() - 1);
210 FleshOutLuaTableStructure(table_names);
212 lua_pushstring(L, lua_name_split.back().c_str());
213 lua_pushcfunction(L, function_ptr);
216 lua_pop(L, lua_gettop(L));
226 const std::string& full_lua_name)
228 auto L = GetInstance().console_state_;
231 auto MakeChunk = [&L, &full_lua_name]()
233 std::string chunk_code =
"local params = ...; ";
235 "return chi_console.LuaWrapperCall(\"" + full_lua_name +
"\", ...)";
237 luaL_loadstring(L, chunk_code.c_str());
241 std::vector<std::string> namespace_names;
242 for (
const auto& table_name : table_names)
243 if (table_name != table_names.back()) namespace_names.push_back(table_name);
245 const auto& function_name = table_names.back();
247 if (not namespace_names.empty())
249 FleshOutLuaTableStructure(namespace_names);
250 lua_pushstring(L, function_name.c_str());
257 lua_setglobal(L, function_name.c_str());
260 lua_pop(L, lua_gettop(L));
269 const std::string& full_lua_name)
271 auto L = GetInstance().console_state_;
274 auto RegisterObjectItems = [&L](
const std::string& full_name)
276 lua_pushstring(L,
"type");
277 lua_pushstring(L, full_name.c_str());
280 lua_pushstring(L,
"Create");
281 std::string chunk_code =
"local params = ...; ";
282 chunk_code +=
"return chiMakeObjectType(\"" + full_name +
"\", ...)";
284 luaL_loadstring(L, chunk_code.c_str());
290 FleshOutLuaTableStructure(table_names);
292 RegisterObjectItems(full_lua_name);
294 lua_pop(L, lua_gettop(L));
306 const std::vector<std::string>& table_names)
308 auto L = GetInstance().console_state_;
310 for (
const auto& table_name : table_names)
314 if (table_name == table_names.front())
316 lua_getglobal(L, table_name.c_str());
317 if (not lua_istable(L, -1))
321 lua_setglobal(L, table_name.c_str());
322 lua_getglobal(L, table_name.c_str());
327 lua_getfield(L, -1, table_name.c_str());
328 if (not lua_istable(L, -1))
331 lua_pushstring(L, table_name.c_str());
334 lua_getfield(L, -1, table_name.c_str());
345 auto& console = GetInstance();
346 auto L = console.console_state_;
352 lua_pushboolean(L, var_value.BoolValue());
354 lua_pushstring(L, var_value.StringValue().c_str());
356 lua_pushinteger(L,
static_cast<lua_Integer
>(var_value.IntegerValue()));
358 lua_pushnumber(L, var_value.FloatValue());
361 "double is supported");
364 if (path_names.size() == 1)
366 PushVaryingValue(value);
367 lua_setglobal(L, path_names.front().c_str());
371 std::vector<std::string> namespace_names;
372 for (
const auto& table_name : path_names)
373 if (table_name != path_names.back())
375 namespace_names.push_back(table_name);
378 FleshOutLuaTableStructure(namespace_names);
379 lua_pushstring(L, path_names.back().c_str());
380 PushVaryingValue(value);
384 lua_pop(L, lua_gettop(L));
393 for (
const auto& [key, entry] : function_wrapper_registry_)
403 if (not entry.call_func)
Chi::log.
Log() <<
"SYNTAX_BLOCK";
405 const auto in_params = entry.get_in_params_func();
406 in_params.DumpParameters();
420 [](
const std::vector<std::string>& list,
const std::string& value)
421 {
return std::find(list.begin(), list.end(), value) != list.end(); };
424 for (
const auto& [key, _] : object_factory.Registry())
426 SetObjectNamespaceTableStructure(key);
428 for (
const auto& [key, entry] : lua_function_registry_)
430 SetLuaFuncNamespaceTableStructure(key, entry.function_ptr);
432 for (
const auto& [key, entry] : function_wrapper_registry_)
434 if (entry.call_func) SetLuaFuncWrapperNamespaceTableStructure(key);
#define ChiLogicalErrorIf(condition, message)
#define ChiInvalidArgument(message)
static ChiObjectFactory & GetInstance() noexcept
LogStream Log(LOG_LVL level=LOG_0)
static void AddFunctionToRegistry(const std::string &name_in_lua, lua_CFunction function_ptr)
chi::ParameterBlock(*)(const chi::InputParameters &) WrapperCallFunc
void UpdateConsoleBindings(const chi::RegistryStatuses &old_statuses)
static void SetLuaFuncWrapperNamespaceTableStructure(const std::string &full_lua_name)
Formats a namespace structure as a table, but the last entry is a function call.
int ExecuteFile(const std::string &fileName, int argc, char **argv) const
chi::InputParameters(*)() WrapperGetInParamsFunc
static char AddLuaConstantToRegistry(const std::string &namespace_name, const std::string &constant_name, const chi_data_types::Varying &value)
Adds a constant to the lua state.
static char AddFunctionToRegistryInNamespaceWithName(lua_CFunction function_ptr, const std::string &namespace_name, const std::string &function_name)
Adds a lua_CFunction to the registry. With namespace-table analogy.
static void SetLuaConstant(const std::string &constant_name, const chi_data_types::Varying &value)
void PostMPIInfo(int location_id, int number_of_processes) const
static Console & GetInstance() noexcept
lua_State * console_state_
Pointer to lua console state.
static void SetObjectNamespaceTableStructure(const std::string &full_lua_name)
Formats a namespace structure as a table, but the last entry contains a "Create" function and a type.
static InputParameters DefaultGetInParamsFunc()
A default function for returning empty input parameters.
void DumpRegister() const
Dumps the object registry to stdout.
static void FleshOutLuaTableStructure(const std::vector< std::string > &table_names)
Makes sure a table structure exists for the list of table names.
static char AddFunctionToRegistryGlobalNamespace(const std::string &raw_name_in_lua, lua_CFunction function_ptr)
Adds a lua_CFunction to the registry.
static char AddWrapperToRegistryInNamespaceWithName(const std::string &namespace_name, const std::string &name_in_lua, WrapperGetInParamsFunc syntax_function, WrapperCallFunc actual_function, bool ignore_null_call_func=false)
Adds a function wrapper to the lua registry.
static void SetLuaFuncNamespaceTableStructure(const std::string &full_lua_name, lua_CFunction function_ptr)
Formats a namespace structure as table.
int chiMakeObject(lua_State *L)
@ INTEGER
Datatype mapping to int64_t.
@ STRING
Datatype mapping to std::string.
@ BOOL
Datatype mapping to bool.
@ FLOAT
Datatype mapping to double.
std::string StringUpToFirstReverse(const std::string &input, const std::string &search_string)
std::vector< std::string > StringSplit(const std::string &input, const std::string &delim)
WrapperGetInParamsFunc get_in_params_func
WrapperCallFunc call_func
std::vector< std::string > objfactory_keys_