Showing the changes to src/vm/module_resolver.cpp (1 of 17 files). Show all
Unified · Split
| @@ -0,0 +1,72 @@ | |||
| 1 | // | ||
| 2 | // Dis VM | ||
| 3 | // File: module_resolver.cpp | ||
| 4 | // Author: arr | ||
| 5 | // | ||
| 6 | |||
| 7 | #include <cassert> | ||
| 8 | #include <fstream> | ||
| 9 | #include <sstream> | ||
| 10 | #include <disvm.h> | ||
| 11 | #include <debug.h> | ||
| 12 | #include <exceptions.h> | ||
| 13 | #include "module_resolver.h" | ||
| 14 | |||
| 15 | using namespace disvm::runtime; | ||
| 16 | |||
| 17 | // Empty destructor for vm module resolver 'interface' | ||
| 18 | vm_module_resolver_t::~vm_module_resolver_t() | ||
| 19 | { } | ||
| 20 | |||
| 21 | default_resolver_t::default_resolver_t(disvm::vm_t &vm) | ||
| 22 | : _vm{ vm } | ||
| 23 | { } | ||
| 24 | |||
| 25 | default_resolver_t::default_resolver_t(disvm::vm_t &vm, std::vector<std::string> probing_paths) | ||
| 26 | : _probing_paths{ std::move(probing_paths) } | ||
| 27 | , _vm{ vm } | ||
| 28 | { } | ||
| 29 | |||
| 30 | default_resolver_t::~default_resolver_t() | ||
| 31 | { } | ||
| 32 | |||
| 33 | bool default_resolver_t::try_resolve_module(const char *path, std::unique_ptr<vm_module_t> &new_module) | ||
| 34 | { | ||
| 35 | assert(path != nullptr); | ||
| 36 | |||
| 37 | auto log = debug::is_component_tracing_enabled<debug::component_trace_t::module>(); | ||
| 38 | if (log) | ||
| 39 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "resolve: try module path: >>%s<<", path); | ||
| 40 | |||
| 41 | auto module_file = std::ifstream{ path, std::ifstream::binary }; | ||
| 42 | if (!module_file.is_open()) | ||
| 43 | { | ||
| 44 | // The raw path isn't valid, try using probing paths | ||
| 45 | for (auto p : _probing_paths) | ||
| 46 | { | ||
| 47 | p.append(path); | ||
| 48 | if (log) | ||
| 49 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "resolve: try module path: >>%s<<", p.c_str()); | ||
| 50 | |||
| 51 | module_file = std::ifstream{ p, std::ifstream::binary }; | ||
| 52 | if (module_file.is_open()) | ||
| 53 | { | ||
| 54 | if (log) | ||
| 55 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "resolve: successful modified module path: >>%s<< >>%s<<", path, p.c_str()); | ||
| 56 | |||
| 57 | break; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | if (!module_file.is_open()) | ||
| 62 | { | ||
| 63 | push_syscall_error_message(_vm, "Unable to resolve path"); | ||
| 64 | return false; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | new_module = disvm::read_module(module_file); | ||
| 69 | assert(new_module != nullptr); | ||
| 70 | |||
| 71 | return true; | ||
| 72 | } | ||