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) {
36
std::shared_ptr<variable::Object> object = produceObject(module_name);
37
array->values().push_back(object);
45
std::shared_ptr<variable::Object> ModuleCollector::produceObject(const std::string & module_name,
46
interpret::Interpret_interface * interpret)
50
auto it = _modules.find(module_name);
51
if (it != _modules.end())
52
return std::make_shared<variable::Object>(it->second->instantiate(it->second));
54
std::shared_ptr<variable::Module_interface> module_object = HardModuleLoader(_module_places).load(module_name,interpret);
56
_last_error = inout::fmt("Unable to create module '%1'").arg(module_name);
60
version_t v = module_object->version();
61
if (v.major() != LibVersion_Major || v.minor() > LibVersion_Minor) {
62
_last_error = inout::fmt("Incorrect version of module '%1'").arg(module_name);
66
_modules.insert({module_name, module_object});
67
return std::make_shared<variable::Object>(module_object->instantiate(module_object));
70
variable::Value ModuleCollector::invoke(variable::Object & object, const std::string & method_name, const variable::VariableSet_t & arguments)
74
const variable::Variable & function = object.getVariableByName(inout::toU16(method_name));
75
if (function.type() == variable::ValueType::Null) {
76
_last_error = inout::fmt("Member '%1' not found").arg(method_name);
80
if (function.type() != variable::ValueType::Function) {
81
_last_error = inout::fmt("Member '%1' is not a function").arg(method_name);
85
return object.invoke(inout::toU16(method_name), arguments);