From dc10c38a3f1c70d7dc237db8c754c5151c6dd458 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 12 Feb 2021 03:25:57 +0100 Subject: [PATCH] Add reset() method --- include/wrenpp/vm.hpp | 1 + src/vm.cpp | 74 ++++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/include/wrenpp/vm.hpp b/include/wrenpp/vm.hpp index 8be8d7b..9d16769 100644 --- a/include/wrenpp/vm.hpp +++ b/include/wrenpp/vm.hpp @@ -105,6 +105,7 @@ namespace wren { bool has_user_data() const; std::size_t dynafunc_byte_size() const; + void reset (Configuration* conf); private: struct LocalData; diff --git a/src/vm.cpp b/src/vm.cpp index 00ed56c..546d8d0 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -137,39 +137,7 @@ namespace wren { VM::VM (Configuration* conf, const detail::Callbacks& cb, void* user_data, std::uint32_t user_data_type) : m_local(std::make_unique(cb)) { - WrenConfiguration wconf; - wrenInitConfiguration(&wconf); - wconf.userData = static_cast(&m_local->callbacks); - - wconf.initialHeapSize = conf->initial_heap_size(); - wconf.minHeapSize = conf->min_heap_size(); - wconf.heapGrowthPercent = conf->heap_growth_percent(); - - if (cb.write_fn) - wconf.writeFn = &write_fn; - - if (cb.error_fn) - wconf.errorFn = &error_fn; - - if (cb.reallocate_fn) - wconf.reallocateFn = cb.reallocate_fn; - - if (cb.resolve_module_fn) - wconf.resolveModuleFn = &resolve_module_fn; - - if (cb.load_module_fn) - wconf.loadModuleFn = &load_module_fn; - - if (cb.foreign_method_fn) - wconf.bindForeignMethodFn = &foreign_method_fn; - - if (cb.foreign_class_fn) - wconf.bindForeignClassFn = &foreign_class_fn; - - m_local->wvm = wrenNewVM(&wconf); - if (not m_local->wvm) - throw std::runtime_error("Failed to initialize Wren VM"); - + this->reset(conf); m_local->user_data = user_data; m_local->user_data_type = user_data_type; } @@ -275,6 +243,46 @@ namespace wren { return m_local->dynafunc.total_memory(); } + void VM::reset (Configuration* conf) { + WrenConfiguration wconf; + wrenInitConfiguration(&wconf); + wconf.userData = static_cast(&m_local->callbacks); + + wconf.initialHeapSize = conf->initial_heap_size(); + wconf.minHeapSize = conf->min_heap_size(); + wconf.heapGrowthPercent = conf->heap_growth_percent(); + + auto& cb = m_local->callbacks; + if (cb.write_fn) + wconf.writeFn = &write_fn; + + if (cb.error_fn) + wconf.errorFn = &error_fn; + + if (cb.reallocate_fn) + wconf.reallocateFn = cb.reallocate_fn; + + if (cb.resolve_module_fn) + wconf.resolveModuleFn = &resolve_module_fn; + + if (cb.load_module_fn) + wconf.loadModuleFn = &load_module_fn; + + if (cb.foreign_method_fn) + wconf.bindForeignMethodFn = &foreign_method_fn; + + if (cb.foreign_class_fn) + wconf.bindForeignClassFn = &foreign_class_fn; + + if (m_local->wvm) + wrenFreeVM(m_local->wvm); + m_local->wvm = wrenNewVM(&wconf); + if (not m_local->wvm) + throw std::runtime_error("Failed to initialize Wren VM"); + + m_local->dynafunc.clear(); + } + void VM::ensure_slots (int num_slots) { wrenEnsureSlots(m_local->wvm, num_slots); }