4
Copyright (c) 2024 МГТУ им. Н.Э. Баумана, кафедра ИУ-6, Михаил Фетисов,
6
https://bmstu.codes/lsx/simodo/loom
9
#include "simodo/engine/FrameBody.h"
10
#include "simodo/variable/Module_interface.h"
11
#include "simodo/inout/format/fmt.h"
15
#if __cplusplus >= __cpp_2017
17
namespace fs = std::filesystem;
19
#include <experimental/filesystem>
20
namespace fs = std::filesystem::experimental;
23
namespace simodo::engine
25
FrameBody::FrameBody(inout::Reporter_abstract & m,
26
interpret::ModuleManagement_interface & mm,
27
const std::vector<std::string> & preload_module_names,
28
const std::string & initial_contracts_file,
29
const std::string & source_file)
32
, _preload_module_names(preload_module_names)
33
, _initial_contracts_file(initial_contracts_file)
34
, _source_file(source_file)
38
FrameBody::~FrameBody()
44
const interpret::Interpret_interface & FrameBody::interpret() const
49
bool FrameBody::prepare()
51
if (_stage >= Stage::Initialized)
54
_inter = _mm.interpret_factory().create();
59
_inter->stack().startBoundary(interpret::BoundScope::Global, interpret::BOUNDARY_MARK_MODULE_FRAME);
61
for(const std::string & module_name : _preload_module_names) {
62
std::shared_ptr<variable::Module_interface> module = _mm.loadHardModule(module_name, _inter);
66
_inter->stack().push({ inout::toU16(module_name), module->instantiate(module), inout::null_token_location,
67
{{ {variable::SPEC_ORIGIN, variable::SPEC_ORIGIN_MODULE},
68
{variable::SPEC_BUILTIN, true},
71
_inter->stack().addGlobal(_inter->stack().top());
74
if (!_initial_contracts_file.empty()) {
75
_inter->addFile(_initial_contracts_file);
76
_init_code = _mm.getCode(_initial_contracts_file, _inter->files());
82
_inter->addFile(_source_file);
83
_source_code = _mm.getCode(_source_file, _inter->files());
95
_stage = Stage::Prepared;
100
loom::FiberStatus FrameBody::run(interpret::NextStepBehaviour mode)
102
if (_stage < Stage::Prepared || _stage == Stage::Completed || !_code)
103
return loom::FiberStatus::Complete;
105
/// \todo Переделать без goto
107
if (_code == _source_code) {
108
if (_stage != Stage::Running) {
109
_stage = Stage::Running;
110
_inter->stack().startBoundary(interpret::BoundScope::Global, interpret::BOUNDARY_MARK_MODULE_FRAME);
111
_inter->logistics().setNextStepBehaviour(mode);
112
_inter->addFlowLayer(*_code);
113
_inter->loom().stretch(_inter->fiber());
116
_inter->logistics().setNextStepBehaviour(mode);
119
else if (_stage == Stage::Prepared) {
120
_stage = Stage::Initialized;
121
_inter->addFlowLayer(*_code);
122
_inter->loom().stretch(_inter->fiber());
125
_inter->logistics().setNextStepBehaviour(mode);
128
if (_inter->loom().paused())
129
_inter->loom().resume();
132
loom::FiberStatus fiber_status = _inter->loom().waitFiber(_inter->fiber());
135
switch (fiber_status)
137
case loom::FiberStatus::Complete:
138
if (_code == _init_code) {
139
_code = _source_code;
142
_stage = Stage::Completed;
152
void FrameBody::cancel()
154
_inter->loom().stop();
157
void FrameBody::pause()
159
_inter->loom().pause();
162
inout::Reporter_abstract & FrameBody::reporter() const
167
std::vector<loom::FiberStructure> FrameBody::fibers() const
169
return _inter->loom().fibers();
172
const loom::Fiber_interface * FrameBody::causer() const
174
return _inter->loom().causer();
177
inout::uri_set_t FrameBody::files() const
179
return _inter->files();
182
// ===============================================================================================
183
// static ========================================================================================
184
// ===============================================================================================
186
std::string makeInitialContractFile(const std::string & loom_dir)
188
fs::path initial_contracts_path = fs::path(loom_dir)
189
/ fs::path(engine::INITIAL_CONTRACTS_DIR)
190
/ fs::path(engine::INITIAL_CONTRACTS_FILE);
192
return initial_contracts_path.lexically_normal().string();