Showing the changes to src/vm/vm.cpp (1 of 4 files). Show all
Unified · Split
| @@ -20,25 +20,6 @@ · 19 unmodified lines | |||
| 20 | using namespace disvm; | 20 | using namespace disvm; |
| 21 | using namespace disvm::runtime; | 21 | using namespace disvm::runtime; |
| 22 | 22 | ||
| 23 | vm_t::loaded_module_t::loaded_module_t(std::unique_ptr<runtime::vm_string_t> origin, std::shared_ptr<runtime::vm_module_t> module) | ||
| 24 | : origin{ std::move(origin) } | ||
| 25 | , module{ module } | ||
| 26 | { | ||
| 27 | assert(this->origin != nullptr); | ||
| 28 | } | ||
| 29 | |||
| 30 | vm_t::loaded_module_t::loaded_module_t(loaded_module_t &&other) | ||
| 31 | : origin{ std::move(other.origin) } | ||
| 32 | , module{ std::move(other.module) } | ||
| 33 | { | ||
| 34 | } | ||
| 35 | |||
| 36 | vm_t::loaded_module_t::~loaded_module_t() | ||
| 37 | { | ||
| 38 | if (origin != nullptr) | ||
| 39 | origin->release(); | ||
| 40 | } | ||
| 41 | |||
| 42 | vm_t::vm_t( | 23 | vm_t::vm_t( |
| 43 | create_vm_interface_callback_t<runtime::vm_scheduler_t> create_scheduler, | 24 | create_vm_interface_callback_t<runtime::vm_scheduler_t> create_scheduler, |
| 44 | create_vm_interface_callback_t<runtime::vm_garbage_collector_t> create_gc) | 25 | create_vm_interface_callback_t<runtime::vm_garbage_collector_t> create_gc) |
| @@ -80,6 +61,13 @@ vm_t::~vm_t() · 35 unmodified lines | |||
| 80 | // Release the scheduler first since it could be using the gc instance. | 61 | // Release the scheduler first since it could be using the gc instance. |
| 81 | _scheduler.reset(); | 62 | _scheduler.reset(); |
| 82 | _gc.reset(); | 63 | _gc.reset(); |
| 64 | |||
| 65 | std::lock_guard<std::mutex> lock{ _modules_lock }; | ||
| 66 | for (auto &m : _modules) | ||
| 67 | { | ||
| 68 | if (m.origin != nullptr) | ||
| 69 | m.origin->release(); | ||
| 70 | } | ||
| 83 | } | 71 | } |
| 84 | 72 | ||
| 85 | vm_version_t vm_t::get_version() const | 73 | vm_version_t vm_t::get_version() const |
| @@ -139,7 +127,7 @@ std::shared_ptr<runtime::vm_module_t> vm_t::load_module(const char *path) · 53 unmodified lines | |||
| 139 | throw vm_user_exception{ "Invalid module path" }; | 127 | throw vm_user_exception{ "Invalid module path" }; |
| 140 | 128 | ||
| 141 | std::lock_guard<std::mutex> lock{ _modules_lock }; | 129 | std::lock_guard<std::mutex> lock{ _modules_lock }; |
| 142 | 130 | ||
| 143 | // Check if the module is already loaded. | 131 | // Check if the module is already loaded. |
| 144 | auto iter = std::begin(_modules); | 132 | auto iter = std::begin(_modules); |
| 145 | while (iter != std::end(_modules)) | 133 | while (iter != std::end(_modules)) |
| @@ -182,26 +170,21 @@ std::shared_ptr<runtime::vm_module_t> vm_t::load_module(const char *path) · 36 unmodified lines | |||
| 182 | _tool_dispatch->on_module_load(new_module, *path_local); | 170 | _tool_dispatch->on_module_load(new_module, *path_local); |
| 183 | } | 171 | } |
| 184 | 172 | ||
| 185 | _modules.push_front(std::move(loaded_module_t{ std::move(path_local), new_module })); | 173 | _modules.push_front(std::move(loaded_vm_module_t{ std::move(path_local), new_module })); |
| 186 | 174 | ||
| 187 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "load: vm module: >>%s<<\n", path); | 175 | debug::log_msg(debug::component_trace_t::module, debug::log_level_t::debug, "load: vm module: >>%s<<\n", path); |
| 188 | 176 | ||
| 189 | return new_module; | 177 | return new_module; |
| 190 | } | 178 | } |
| 191 | 179 | ||
| 192 | std::vector<std::shared_ptr<runtime::vm_module_t>> vm_t::get_loaded_modules() const | 180 | void vm_t::enum_loaded_modules(loaded_vm_module_callback_t callback) const |
| 193 | { | 181 | { |
| 194 | auto result = std::vector<std::shared_ptr<runtime::vm_module_t>>{}; | 182 | if (callback == nullptr) |
| 183 | throw vm_system_exception{ "Callback should not be null" }; | ||
| 195 | 184 | ||
| 196 | std::lock_guard<std::mutex> lock{ _modules_lock }; | 185 | std::lock_guard<std::mutex> lock{ _modules_lock }; |
| 197 | for (auto &m : _modules) | 186 | for (auto &m : _modules) |
| 198 | { | 187 | callback(m); |
| 199 | auto loaded_module_maybe = m.module.lock(); | ||
| 200 | if (loaded_module_maybe != nullptr) | ||
| 201 | result.push_back(loaded_module_maybe); | ||
| 202 | } | ||
| 203 | |||
| 204 | return result; | ||
| 205 | } | 188 | } |
| 206 | 189 | ||
| 207 | runtime::vm_scheduler_control_t &vm_t::get_scheduler_control() const | 190 | runtime::vm_scheduler_control_t &vm_t::get_scheduler_control() const |