4
Copyright (c) 2021 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/stars
9
#include "simodo/module/ModuleCollector.h"
10
#include "simodo/module/HardModuleLoader.h"
11
#include "simodo/inout/convert/functions.h"
12
#include "simodo/inout/format/fmt.h"
16
using namespace simodo;
17
using namespace simodo::module;
19
namespace fs = std::filesystem;
21
ModuleCollector::ModuleCollector(std::vector<std::string> module_places)
22
: _module_places(module_places)
25
std::shared_ptr<variable::Array> ModuleCollector::produceObjectsByMask(const std::string & module_mask)
27
std::shared_ptr<variable::Array> array = std::make_shared<variable::Array>();
29
for(fs::path path : _module_places) {
30
if (!fs::exists(path))
32
for (auto const & dir_entry : fs::directory_iterator{path})
33
if (dir_entry.path().extension() == ".simodo-module") {
34
std::string module_name = dir_entry.path().stem().string();
35
if (module_name.substr(0,module_mask.size()) == module_mask) // -V1051
37
std::shared_ptr<variable::Object> object = produceObject(module_name);
46
std::shared_ptr<variable::Object> ModuleCollector::produceObject(const std::string & module_name,
47
interpret::Interpret_interface * interpret)
51
auto it = _modules.find(module_name);
52
if (it != _modules.end())
53
return it->second->instantiate(it->second).getObject();
55
std::shared_ptr<variable::Module_interface> module_object = HardModuleLoader(_module_places).load(module_name,interpret);
57
_last_error = inout::fmt("Unable to create module '%1'").arg(module_name);
61
version_t v = module_object->version();
62
if (v.major() != LibVersion_Major || v.minor() > LibVersion_Minor) {
63
_last_error = inout::fmt("Incorrect version of module '%1'").arg(module_name);
67
_modules.insert({module_name, module_object});
68
return module_object->instantiate(module_object).getObject();
71
variable::Value ModuleCollector::invoke(variable::Object & object, const std::string & method_name, const variable::VariableSet_t & arguments)
75
const variable::Variable & function = object.getVariableByName(inout::toU16(method_name));
76
if (function.type() == variable::ValueType::Null) {
77
_last_error = inout::fmt("Member '%1' not found").arg(method_name);
81
if (function.type() != variable::ValueType::Function) {
82
_last_error = inout::fmt("Member '%1' is not a function").arg(method_name);
86
return object.invoke(inout::toU16(method_name), arguments);